Class: Rlid::NaiveBayesProbabilityGuesser

Inherits:
NaiveBayesGuesser show all
Defined in:
lib/rlid/language_guesser/naive_bayes_guesser.rb

Direct Known Subclasses

NaiveBayesPriorGuesser

Instance Attribute Summary

Attributes inherited from LanguageGuesser

#name

Instance Method Summary collapse

Methods inherited from NaiveBayesGuesser

#initialize

Methods inherited from LanguageGuesser

#initialize

Constructor Details

This class inherits a constructor from Rlid::NaiveBayesGuesser

Instance Method Details

#guess_language(string) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rlid/language_guesser/naive_bayes_guesser.rb', line 30

def guess_language(string)
  results = {}
  tot = 0.0 # for normalization
  @models.probabilities(string) do |lang, p|
    size = string.preprocess(3).size
    long = Math.log(1 + size)
    # higher means lower
    short = 1
    exp = short/long
    prob = p**exp
    results[lang] = prob
    tot += prob
  end
  # normalize
  results.each_key do |k|
    results[k] /= tot if tot != 0
  end

  LanguageProbabilities.new(results)
end