Class: ThatLanguage::Result

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language_code:) ⇒ Result

Returns a new instance of Result.



8
9
10
11
12
13
# File 'lib/that_language/result.rb', line 8

def initialize(language_code:)
  @language_code = language_code
  @value = 0.0
  @hit_count = 0
  @words_count = 0
end

Instance Attribute Details

#hit_countObject (readonly)

Returns the value of attribute hit_count.



5
6
7
# File 'lib/that_language/result.rb', line 5

def hit_count
  @hit_count
end

#language_codeObject (readonly)

Returns the value of attribute language_code.



5
6
7
# File 'lib/that_language/result.rb', line 5

def language_code
  @language_code
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/that_language/result.rb', line 5

def value
  @value
end

#words_countObject

Returns the value of attribute words_count.



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

def words_count
  @words_count
end

Instance Method Details

#<(other) ⇒ Object



38
39
40
# File 'lib/that_language/result.rb', line 38

def <(other)
  value < other.value
end

#<=>(other) ⇒ Object



34
35
36
# File 'lib/that_language/result.rb', line 34

def <=>(other)
  value <=> other.value
end

#>(other) ⇒ Object



42
43
44
# File 'lib/that_language/result.rb', line 42

def >(other)
  value > other.value
end

#add(value) ⇒ Object



15
16
17
18
19
20
# File 'lib/that_language/result.rb', line 15

def add(value)
  return @value unless value > 0

  @hit_count += 1
  @value += value
end

#confidenceObject



22
23
24
25
26
# File 'lib/that_language/result.rb', line 22

def confidence
  return 0 unless words_count > 0

  value / words_count
end

#hit_ratioObject



28
29
30
31
32
# File 'lib/that_language/result.rb', line 28

def hit_ratio
  return 0.0 if @words_count == 0

  @hit_count.to_f / @words_count
end

#languageObject



46
47
48
# File 'lib/that_language/result.rb', line 46

def language
  Iso639[language_code] if language_code
end

#to_hObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/that_language/result.rb', line 50

def to_h
  {
    language: language,
    language_code: language_code,
    confidence: confidence,
    value: value,
    hit_ratio: hit_ratio,
    hit_count: hit_count,
    words_count: words_count
  }
end

#to_jsonObject



62
63
64
# File 'lib/that_language/result.rb', line 62

def to_json
  to_h.to_json
end