Class: Yanbi::Dictionary

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, klass) ⇒ Dictionary

Returns a new instance of Dictionary.



16
17
18
19
20
21
# File 'lib/dictionary.rb', line 16

def initialize(w, klass)
  @index = {}
  @klass = klass
  i = (0..w.size).to_a
  w.zip(i).each { |x| @index[x.first] = x.last }
end

Instance Attribute Details

#bag_classObject

Returns the value of attribute bag_class.



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

def bag_class
  @bag_class
end

Class Method Details

.load(fname) ⇒ Object

Raises:

  • (LoadError)


28
29
30
31
32
# File 'lib/dictionary.rb', line 28

def self.load(fname)
  c = YAML.load(File.read(fname + '.yml'))
  raise LoadError unless c.is_a? self
  c
end

Instance Method Details

#save(name) ⇒ Object



34
35
36
37
38
# File 'lib/dictionary.rb', line 34

def save(name)
  File.open(name + '.yml', 'w') do |out|
    YAML.dump(self, out)
  end
end

#to_idx(doc) ⇒ Object



23
24
25
26
# File 'lib/dictionary.rb', line 23

def to_idx(doc)
  bag = @klass.new(doc)
  bag.words.map { |w| @index[w] }
end