Class: ThatLanguage::Detect

Inherits:
Object
  • Object
show all
Defined in:
lib/that_language/detect.rb

Constant Summary collapse

WORD_REGEX =
/(\p{Word}+)/
START_WITH_DIGIT_REGEX =
/^\d.*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup_context) ⇒ Detect

Returns a new instance of Detect.



8
9
10
# File 'lib/that_language/detect.rb', line 8

def initialize(lookup_context)
  @lookup_context = lookup_context
end

Instance Attribute Details

#lookup_contextObject (readonly)

Returns the value of attribute lookup_context.



6
7
8
# File 'lib/that_language/detect.rb', line 6

def lookup_context
  @lookup_context
end

Instance Method Details

#details(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/that_language/detect.rb', line 24

def details(text)
  words = words_for(text)

  result_set = ResultSet.new(words.length)

  words.each do |word|
    word_result = lookup_context.normalized(word)
    word_result.each do |language_code, value|
      next unless value > 0

      result_set.for(language_code).add(value)
    end
  end

  result_set
end

#detect(text) ⇒ Object



20
21
22
# File 'lib/that_language/detect.rb', line 20

def detect(text)
  details(text).winner
end

#language(text) ⇒ Object



12
13
14
# File 'lib/that_language/detect.rb', line 12

def language(text)
  detect(text).language
end

#language_code(text) ⇒ Object



16
17
18
# File 'lib/that_language/detect.rb', line 16

def language_code(text)
  detect(text).language_code
end