class RMMSeg::Dictionary
The dictionary is a singleton object which is lazily initialized. NOTE dictionary data should use the UNIX line-break ‘n’ instead of DOS ‘rn’.
Public Class Methods
new()
click to toggle source
Initialize and load dictionaries from files specified by Config.dictionaries
.
# File lib/rmmseg/dictionary.rb, line 12 def initialize load_dictionaries end
Public Instance Methods
get_word(value)
click to toggle source
Get an instance of Word
corresponding to value
.
# 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)
click to toggle source
Determin whether value
is a word in the dictionary.
# File lib/rmmseg/dictionary.rb, line 17 def has_word?(value) @dic.has_key?(value) end
reload()
click to toggle source
Reload all dictionary files.
# File lib/rmmseg/dictionary.rb, line 46 def reload @dic = nil load_dictionaries end
store_word(key, w=true)
click to toggle source
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.
# File lib/rmmseg/dictionary.rb, line 27 def store_word(key, w=true) @dic[key] = w end
Private Instance Methods
load_dictionaries()
click to toggle source
# File lib/rmmseg/dictionary.rb, line 52 def load_dictionaries @dic = Hash.new Config.dictionaries.each { |file, has_freq| if has_freq load_dictionary_with_freq(file) else load_dictionary(file) end } end
load_dictionary(file)
click to toggle source
# File lib/rmmseg/dictionary.rb, line 71 def load_dictionary(file) File.open(file, "r") { |f| f.each_line { |line| line.slice!(-1) # chop! @dic[line] = true } } end
load_dictionary_with_freq(file)
click to toggle source
# File lib/rmmseg/dictionary.rb, line 63 def load_dictionary_with_freq(file) File.open(file, "r") { |f| f.each_line { |line| pair = line.split(" ") @dic[pair[0]] = pair[1] } } end