Class: Langusta::DetectorFactory

Inherits:
Object
  • Object
show all
Includes:
Inspector
Defined in:
lib/langusta/detector_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inspector

#object_ptr

Constructor Details

#initializeDetectorFactory

Returns a new instance of DetectorFactory.



7
8
9
10
# File 'lib/langusta/detector_factory.rb', line 7

def initialize
  @word_lang_prob_map = {}
  @lang_list = []
end

Instance Attribute Details

#lang_listObject (readonly)

Returns the value of attribute lang_list.



5
6
7
# File 'lib/langusta/detector_factory.rb', line 5

def lang_list
  @lang_list
end

#word_lang_prob_mapObject (readonly)

Returns the value of attribute word_lang_prob_map.



5
6
7
# File 'lib/langusta/detector_factory.rb', line 5

def word_lang_prob_map
  @word_lang_prob_map
end

Instance Method Details

#add_profile(profile) ⇒ Object

Adds a new language profile to this factory.

Parameters:

  • language (LangProfile)

    profile to be added.

  • index (Fixnum)

    at which the language profile is to be added.

  • counts (Fixnum)

    how many language profiles are to be added to this factory in total.

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/langusta/detector_factory.rb', line 16

def add_profile(profile)
  raise DuplicateProfilesError.new(profile.name) if @lang_list.include?(profile.name)
  @lang_list << profile.name
  last_lang_index = @lang_list.size - 1

  profile.freq.keys.each do |word|
    @word_lang_prob_map[word] ||= []
    prob = 1.0 * profile.freq[word] / profile.n_words[word.length - 1]
    @word_lang_prob_map[word][last_lang_index] = prob
  end
end

#create(alpha = nil) ⇒ Detector

Creates a new detector object, based on a preconfigured set of language profiles.

Returns:



30
31
32
33
34
35
36
37
38
# File 'lib/langusta/detector_factory.rb', line 30

def create(alpha=nil)
  if alpha
    detector = create_detector()
    detector.alpha = alpha
    detector
  else
    create_detector()
  end
end

#inspectObject



40
41
42
# File 'lib/langusta/detector_factory.rb', line 40

def inspect
  "#<#{self.class.name}:0x#{object_ptr} (#{@lang_list.size} profile(s))"
end