Class: EncodingEstimator::LanguageModel

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

Overview

Class representing a language model. This can either be an internal model (provided by the gem) or an external one (user defined model).

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ LanguageModel

Initialize a new object from a language file or an internal language profile

Parameters:

  • language (String|Symbol)

    If symbol given, interpreted as internal language identifier, otherwise interpreted as a path to a language model file



14
15
16
# File 'lib/encoding_estimator/language_model.rb', line 14

def initialize( language )
  @language = language
end

Instance Method Details

#distributionEncodingEstimator::Distribution

Load the distribution file into a distribution object

Returns:



53
54
55
# File 'lib/encoding_estimator/language_model.rb', line 53

def distribution
  @distribution ||= EncodingEstimator::Distribution.new( self )
end

#external?Boolean

Check if this instance represents an external model file

Returns:

  • (Boolean)

    true, if the model referenced is an external file



39
40
41
# File 'lib/encoding_estimator/language_model.rb', line 39

def external?
  !internal?
end

#internal?Boolean

Check if this instance represents an internal model file

Returns:

  • (Boolean)

    true, if the model referenced is an internal model



32
33
34
# File 'lib/encoding_estimator/language_model.rb', line 32

def internal?
  @language.is_a? Symbol
end

#pathString

Get the full (absolute) path to the language model file

Returns:

  • (String)

    Path to the language model



46
47
48
# File 'lib/encoding_estimator/language_model.rb', line 46

def path
  @path ||= ( internal? ? internal_path : external_path )
end

#valid?Boolean

Check if the instance is a valid language model representation

Returns:

  • (Boolean)

    true, if the referenced model file exists



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

def valid?
  if external?
    File.file? external_path
  else
    @language.to_s.size == 2 and File.file? internal_path
  end
end