Class: Dat::Dict

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

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Dict

Returns a new instance of Dict.



6
7
8
9
10
11
12
13
# File 'lib/dat/dict.rb', line 6

def initialize(opt={})
  @dict = {} # The internal hash which maps string words to Dat::Word objects
  file = opt[:file] || File.open(File.expand_path("../../../data/dict", __FILE__))
  bogus = opt[:bogus] || File.open(File.expand_path("../../../data/bogus", __FILE__))

  import file
  remove bogus
end

Instance Method Details

#[](word) ⇒ Object



15
16
17
# File 'lib/dat/dict.rb', line 15

def [](word)
  @dict[word]
end

#delete(word) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/dat/dict.rb', line 23

def delete(word)
  w = @dict[word]
  return nil if !w
  w.relatives.each do |r|
    r.relatives.delete(w)
  end
  @dict.delete(w.get)
end

#each(&block) ⇒ Object



19
20
21
# File 'lib/dat/dict.rb', line 19

def each(&block)
  @dict.each(&block)
end

#to_sObject



32
33
34
35
36
# File 'lib/dat/dict.rb', line 32

def to_s
  result = ""
  @dict.each { |k,v| result << v.to_dict_entry << "\n" }
  result
end