Class: Igo::Unknown

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

Overview

未知語の検索を行うクラス

Instance Method Summary collapse

Constructor Details

#initialize(data_dir) ⇒ Unknown

コンストラクタ

data_dir

辞書ファイルのディレクトリパス



93
94
95
96
97
98
99
# File 'lib/igo/dictionary.rb', line 93

def initialize(data_dir)
  # 文字カテゴリ管理クラス
  @category = CharCategory.new(data_dir)

  # 文字カテゴリが空白の文字のID
  @space_id = @category.category(' '.unpack("U*")[0]).id
end

Instance Method Details

#search(text, start, wdic, result) ⇒ Object

検索

text
start
wdic
result


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/igo/dictionary.rb', line 106

def search(text, start, wdic, result)
  txt = text.unpack("U*")
  length = txt.size
  ch = txt[start]
  ct = @category.category(ch)

  if !result.empty? and !ct.invoke
    return
  end

  is_space = (ct.id == @space_id)
  limit = [length, ct.length + start].min

  for i in start..(limit - 1)
    wdic.search_from_trie_id(ct.id, start, (i - start) + 1, is_space, result)
  
    if((i + 1) != limit and !(@category.compatible?(ch, text[i + 1])))
      return
    end
  end

  if ct.group and limit < length
    for i in limit..(length - 1)
      if not @category.compatible?(ch, txt[i])
        wdic.search_from_trie_id(ct.id, start, i - start, is_space, result)
        return
      end
    end
    wdic.search_from_trie_id(ct.id, start, length - start, is_space, result)
  end
end