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
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
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
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
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
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
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
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
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