class EncodingEstimator::LanguageModel

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

Public Class Methods

new( language ) click to toggle source

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

@param [String|Symbol] language If symbol given, interpreted as internal language identifier,

otherwise interpreted as a path to a language model file
# File lib/encoding_estimator/language_model.rb, line 14
def initialize( language )
  @language = language
end

Public Instance Methods

distribution() click to toggle source

Load the distribution file into a distribution object

@return [EncodingEstimator::Distribution] Object representing the language model

# File lib/encoding_estimator/language_model.rb, line 53
def distribution
  @distribution ||= EncodingEstimator::Distribution.new( self )
end
external?() click to toggle source

Check if this instance represents an external model file

@return [Boolean] true, if the model referenced is an external file

# File lib/encoding_estimator/language_model.rb, line 39
def external?
  !internal?
end
internal?() click to toggle source

Check if this instance represents an internal model file

@return [Boolean] true, if the model referenced is an internal model

# File lib/encoding_estimator/language_model.rb, line 32
def internal?
  @language.is_a? Symbol
end
path() click to toggle source

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

@return [String] Path to the language model

# File lib/encoding_estimator/language_model.rb, line 46
def path
  @path ||= ( internal? ? internal_path : external_path )
end
valid?() click to toggle source

Check if the instance is a valid language model representation

@return [Boolean] true, if the referenced model file exists

# 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

Private Instance Methods

external_path() click to toggle source

Return the language file path

@return [String] File path

# File lib/encoding_estimator/language_model.rb, line 69
def external_path
  File.expand_path( @language )
end
internal_path() click to toggle source

Create the file path to the language statistics file

@return [String] File path

# File lib/encoding_estimator/language_model.rb, line 62
def internal_path
  File.expand_path( File.dirname( __FILE__ ) + "/lang/#{@language.to_s}.json" )
end