Class: RMMSeg::Dictionary
- Inherits:
-
Object
- Object
- RMMSeg::Dictionary
- Includes:
- Singleton
- Defined in:
- lib/rmmseg/dictionary.rb
Overview
The dictionary is a singleton object which is lazily initialized. NOTE dictionary data should use the UNIX line-break ‘n’ instead of DOS ‘rn’.
Instance Method Summary collapse
-
#get_word(value) ⇒ Object
Get an instance of Word corresponding to
value
. -
#has_word?(value) ⇒ Boolean
Determin whether
value
is a word in the dictionary. -
#initialize ⇒ Dictionary
constructor
Initialize and load dictionaries from files specified by
Config.dictionaries
. -
#reload ⇒ Object
Reload all dictionary files.
-
#store_word(key, w = true) ⇒ Object
Store a new word to dictionary.
Constructor Details
#initialize ⇒ Dictionary
Initialize and load dictionaries from files specified by Config.dictionaries
.
12 13 14 |
# File 'lib/rmmseg/dictionary.rb', line 12 def initialize load_dictionaries end |
Instance Method Details
#get_word(value) ⇒ Object
Get an instance of Word corresponding to value
.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rmmseg/dictionary.rb', line 32 def get_word(value) word = @dic[value] # Construct a Word lazily if word == true word = Word.new(value.dup, Word::TYPES[:cjk_word]) @dic[value] = word elsif String === word word = Word.new(value.dup, Word::TYPES[:cjk_word], word.to_i) @dic[value] = word end word end |
#has_word?(value) ⇒ Boolean
Determin whether value
is a word in the dictionary.
17 18 19 |
# File 'lib/rmmseg/dictionary.rb', line 17 def has_word?(value) @dic.has_key?(value) end |
#reload ⇒ Object
Reload all dictionary files.
46 47 48 49 |
# File 'lib/rmmseg/dictionary.rb', line 46 def reload @dic = nil load_dictionaries end |
#store_word(key, w = true) ⇒ Object
Store a new word to dictionary. w
may be:
-
an instance of Word.
-
true
, then this is a normal world. -
a String(which can be converted to a Number) or Number. The number is the frequency of the word.
27 28 29 |
# File 'lib/rmmseg/dictionary.rb', line 27 def store_word(key, w=true) @dic[key] = w end |