Class: Kaerukeyword::DoubleArray

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

Overview

ダブル配列

Instance Method Summary collapse

Constructor Details

#initialize(key_array = nil) ⇒ DoubleArray

Returns a new instance of DoubleArray.



87
88
89
90
91
92
93
94
95
# File 'lib/kaerukeyword.rb', line 87

def initialize(key_array = nil)
  hash_tree = HashTree.new(key_array)
  @base = [1]
  @check = [0]
  @code = {"end" => 1}
  construct_array(hash_tree.tree, 0) if key_array.is_a?(Array)
  hash_tree = nil
  GC.start
end

Instance Method Details

#add(key) ⇒ Object

未サポート

Raises:



100
101
102
# File 'lib/kaerukeyword.rb', line 100

def add(key)
  raise NotSupportError, "you don't add keyword after initilize."
end

#construct_array(hash_tree, now_x) ⇒ Object

ハッシュ木からBC配列を構築



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kaerukeyword.rb', line 137

def construct_array(hash_tree, now_x)
  x_hash = {}
  chars = hash_tree.keys.sort_by{|k| k.to_s}
  code_set(chars)
  @base[now_x] = base = get_base(chars, now_x)

  chars.each do |c|
    @check[base + @code[c]] = now_x
    if c == "end"
      @base[base + @code[c]] = -base
    else
      @base[base + @code[c]] = base
      x_hash[c] = base + @code[c]
    end
  end

  x_hash.each {|c, x| construct_array(hash_tree[c], x)}
end

#search(text) ⇒ Object

検索



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
# File 'lib/kaerukeyword.rb', line 107

def search(text)
  list = []
  word = ""
  buffer = ""
  now_x = 0

  text.scan(/./).each do |c|
    code = @code[c]
    base = @base[now_x]
    if code && @base[base + code] && @check[base + code] == now_x
      now_x = base + code
      buffer << c
      if @base[@base[now_x] + @code["end"]].to_i < 0 && @check[@base[now_x] + @code["end"]] == now_x
        word = buffer.dup
      end
    else
      list << word unless word.empty?
      word = ""
      buffer = ""
      now_x = 0
    end
  end

  list << word unless word.empty?
  list
end