Class: EncodingEstimator::Distribution

Inherits:
Object
  • Object
show all
Defined in:
lib/encoding_estimator/distribution.rb

Constant Summary collapse

@@distributions =
{}

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ Distribution

Create a new distribution object for a given language

Parameters:



10
11
12
13
14
# File 'lib/encoding_estimator/distribution.rb', line 10

def initialize( language )

  @@distributions[ language.path ] ||= load_language language
  @distribution                      = @@distributions[ language.path ]
end

Instance Method Details

#evaluate(str, penalty) ⇒ Float

Calculate the likelihood of a string for the given language

Parameters:

  • str (String)

    Data to calculate the likelihood for

  • penalty (Float)

    Threshold which defines when chars are weighted negative (-> calc score - thresh)

Returns:

  • (Float)

    Total likelihood



21
22
23
24
25
26
# File 'lib/encoding_estimator/distribution.rb', line 21

def evaluate( str, penalty )
  dist = @distribution
  sum = 0.0
  str.each_char { |c| sum += dist.fetch( c, 0.0 ) - penalty }
  sum
end