Class: Gonzui::PhraseFinder
- Inherits:
-
Object
- Object
- Gonzui::PhraseFinder
- Includes:
- Util
- Defined in:
- lib/gonzui/searcher.rb
Overview
FIXME: It’s not efficient. It’s better to use a data structure like a priority queue to handle the list-of-list to achieve better performance.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(dbm, path_id, words) ⇒ PhraseFinder
constructor
A new instance of PhraseFinder.
- #match?(info_list, i) ⇒ Boolean
Methods included from Util
assert, assert_equal, assert_equal_all, assert_non_nil, assert_not_reached, benchmark, command_exist?, commify, eprintf, format_bytes, program_name, protect_from_signals, require_command, set_verbosity, shell_escape, unix?, vprintf, windows?, wprintf
Constructor Details
#initialize(dbm, path_id, words) ⇒ PhraseFinder
Returns a new instance of PhraseFinder.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gonzui/searcher.rb', line 19 def initialize(dbm, path_id, words) @word_ids = [] @list_of_list = [] words.each {|word| word_id = dbm.get_word_id(word) assert_non_nil(word_id) info_list = dbm.get_all_word_info(path_id, word_id) @word_ids.push(word_id) @list_of_list.push(info_list) } @last_word = words.last end |
Instance Method Details
#each ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gonzui/searcher.rb', line 48 def each prev = nil info_list = @list_of_list.flatten.sort_by {|info| info.seqno }.find_all {|info| v = info.seqno != prev prev = info.seqno v } info_list.length.times {|i| if match?(info_list, i) first = info_list[i] last = info_list[i + @word_ids.length - 1] length = last.byteno + @last_word.length - first.byteno occ = Occurrence.new(first.byteno, first.lineno, length) yield(occ) end } end |
#match?(info_list, i) ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gonzui/searcher.rb', line 33 def match?(info_list, i) j = 0 prev_seqno = nil @word_ids.each {|word_id| return false unless i + j < info_list.length info = info_list[i + j] return false unless word_id == info.word_id return false unless prev_seqno.nil? or (prev_seqno + 1) == info.seqno prev_seqno = info.seqno j += 1 } return true end |