Class: Rlid::LanguageProbabilities
- Inherits:
-
Object
- Object
- Rlid::LanguageProbabilities
- Defined in:
- lib/rlid/web.rb,
lib/rlid/probabilities/language_probabilities.rb
Direct Known Subclasses
Constant Summary collapse
- CLASSNAME =
"languageresult"
- MAX_OUTPUT =
3
Instance Method Summary collapse
- #*(other) ⇒ Object
- #[](lang) ⇒ Object
- #first ⇒ Object
-
#initialize(args = {}) ⇒ LanguageProbabilities
constructor
A new instance of LanguageProbabilities.
- #random_language ⇒ Object
- #to_a ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ LanguageProbabilities
Returns a new instance of LanguageProbabilities.
46 47 48 49 50 51 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 46 def initialize(args={}) @percentage = Hash.new(0) args.each do |languages, percentage| add(languages, percentage) end end |
Instance Method Details
#*(other) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 85 def *(other) if not other.is_a? LanguageProbabilities p other.inspect raise InvalidArgument.new(other) end res = LanguageProbabilities.new() @percentage.each_key do |lang| res.percentage[lang] = percentage[lang] * other.percentage[lang] end res.normalize res end |
#[](lang) ⇒ Object
73 74 75 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 73 def [](lang) @percentage[lang] end |
#first ⇒ Object
81 82 83 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 81 def first sorted.first[LANG] end |
#random_language ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 53 def random_language r = rand sum = 0 @percentage.each do |language, perc| sum += perc return language if sum > r #puts "#{sum}(#{r})" end warn "rounding error!! (sum is not 1!!)" @percentage.keys.first end |
#to_a ⇒ Object
77 78 79 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 77 def to_a sorted.map{|x| {language: x[LANG], confidence: x[PERC]}} end |
#to_html ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/rlid/web.rb', line 6 def to_html sorted[0...MAX_OUTPUT].map do |x| perc = Percentage.new(x[PERC]).to_s lang = Language.name_of(x[LANG]) "<div class='#{CLASSNAME}'>" + "#{lang} (#{perc}% chance)</div>" end.join("\n") end |
#to_s ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/rlid/probabilities/language_probabilities.rb', line 65 def to_s sorted[0...MAX_OUTPUT].map do |x| # calculate the digits formatted_perc = Percentage.new(x[PERC]).to_s "#{x[LANG]}(#{formatted_perc})" end.join(" : ") end |