Class: NLP::Dictionary

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category_file = :rid, restore = true) ⇒ Dictionary

Returns a new instance of Dictionary.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dictionaries/dictionary.rb', line 6

def initialize(category_file=:rid,restore = true)
  state_file = File.expand_path(DICTIONARY_CACHE_DIR+".#{category_file.to_s}")
  if restore and File.exist?(state_file)
    @tree = Dictionary.restore(state_file) 
  else
    @tree = PlTrie.new
    load_categories(File.dirname(__FILE__)+"/../../dict/#{category_file.to_s}", category_file )
    store(state_file)
  end

end

Instance Attribute Details

#treeObject

Returns the value of attribute tree.



4
5
6
# File 'lib/dictionaries/dictionary.rb', line 4

def tree
  @tree
end

Class Method Details

.restore(state_file) ⇒ Object



25
26
27
28
29
# File 'lib/dictionaries/dictionary.rb', line 25

def self.restore( state_file )
  File.open( File.expand_path( state_file ) ) do |file|
    Marshal.restore( file )
  end
end

Instance Method Details

#find(word) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/dictionaries/dictionary.rb', line 31

def find(word)
  begin 
    @tree.find(word)
  rescue
    nil
  end
end

#load_categories(category_file, type) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dictionaries/dictionary.rb', line 39

def load_categories(category_file,type)
  category = nil
  primary = nil
  secondary = nil
  tertiary = nil

  if type == :rid
    cat_class = NLP.const_get("RIDCategory")
  else
    cat_class = NLP.const_get("LIWCCategory")
  end

  File.open(category_file) do |file|
    while line = file.gets
      line.chomp!
      begin
        lead, rest = line.scan(/(\t*)(.*)/).first
        if lead.size == 0
          category = primary = cat_class.new(rest)
          secondary, tertiary = nil
        elsif lead.size == 1
          category = secondary = cat_class.new(rest, primary)
          tertiary = nil
        elsif lead.size == 2 && ( cat = line.strip.index(/^[A-ZĄŚĘĆŃŹŻŁÓ_]+$/)) && cat >= 0 
          category = tertiary = cat_class.new( rest, secondary )
        else
          word = rest.downcase.gsub( /\s*\(1\)$/, '' )
          @tree.insert(word, category)
        end
      rescue
        raise
      end
    end
  end
end

#store(state_file) ⇒ Object



18
19
20
21
22
23
# File 'lib/dictionaries/dictionary.rb', line 18

def store( state_file )
  File.open( File.expand_path( state_file ), "w" ) do |file|
    Marshal.dump( self.tree, file )
  end
  self
end