Class: Fuzzzy::Ngram::Searcher
- Inherits:
-
Base
show all
- Defined in:
- lib/fuzzzy/methods/ngram/searcher.rb
Constant Summary
collapse
- SEGMENT_POINTS =
Ruby implementation: def segment_points index
right = distance + index
left = index > distance ? (index - distance) : 0
i = left
while i <= right do
yield i
i += 1
end
end
<<-EOC
VALUE
_segment_points(VALUE self, VALUE _index)
{
int index, distance, left, right, i;
index = NUM2INT(_index);
distance = NUM2INT(rb_funcall(self, rb_intern("distance"), 0));
right = index + distance;
if(index > distance) {
left = index - distance;
} else {
left = 0;
}
for(i = left; i <= right; i++) {
rb_yield(INT2NUM(i));
}
return Qnil;
}
EOC
Constants included
from Redis
Redis::INDEX_KEY
Instance Attribute Summary
Attributes inherited from MethodBase
#context
Instance Method Summary
collapse
Methods inherited from Base
#index_type, #ngrams
Methods inherited from MethodBase
#index_name, #prepare_string, #stopwords, #with_context
Methods included from Redis
counter_key, #counter_key, #dictionary_key, #redis, #shared_key
Instance Method Details
#distance ⇒ Object
74
75
76
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 74
def distance
context[:distance] ||= 0
end
|
#index_keys ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 64
def index_keys
keys = []
ngrams.each_with_index do |ngram, index|
segment_points(index) do |i|
keys << index_key(ngram, i)
end
end
keys
end
|
#query_index_string ⇒ Object
90
91
92
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 90
def query_index_string
context[:prepared_query] ||= prepare_string(context[:query])
end
|
#result ⇒ Object
78
79
80
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 78
def result
context[:result] ||= []
end
|
#search(cntx) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 42
def search cntx
with_context(cntx) do
return [] if query_index_string.empty?
if ids = redis.sunion(*index_keys)
ids.each do |id|
string = redis.get(dictionary_key(id))
dist = Levenshtein.distance(query_index_string, string)
result << {
:id => id,
:distance => dist,
:alpha => string
} if dist <= distance
end
result.sort_by!{|item|item[sort_by]} if sort_by
result.map!{|item|item[:id]} unless with_cache?
result
else
[]
end
end
end
|
#sort_by ⇒ Object
86
87
88
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 86
def sort_by
context[:sort_by]
end
|
#with_cache? ⇒ Boolean
82
83
84
|
# File 'lib/fuzzzy/methods/ngram/searcher.rb', line 82
def with_cache?
context[:with_cache] ||= false
end
|