Class: FuzzBall::Searcher
- Inherits:
-
Object
- Object
- FuzzBall::Searcher
- Defined in:
- lib/fuzz_ball/searcher.rb
Instance Attribute Summary collapse
-
#duple_index ⇒ Object
readonly
Returns the value of attribute duple_index.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#files_array ⇒ Object
readonly
Returns the value of attribute files_array.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add(str) ⇒ Object
-
#initialize(files, opts = {}) ⇒ Searcher
constructor
A new instance of Searcher.
- #inspect ⇒ Object
- #search(needle, opts = {}) ⇒ Object
Constructor Details
#initialize(files, opts = {}) ⇒ Searcher
Returns a new instance of Searcher.
6 7 8 9 10 11 12 |
# File 'lib/fuzz_ball/searcher.rb', line 6 def initialize(files, opts = {}) @options = opts @files = files @files_array = files.collect {|f| str2arr(f)} index_duples! end |
Instance Attribute Details
#duple_index ⇒ Object (readonly)
Returns the value of attribute duple_index.
4 5 6 |
# File 'lib/fuzz_ball/searcher.rb', line 4 def duple_index @duple_index end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
4 5 6 |
# File 'lib/fuzz_ball/searcher.rb', line 4 def files @files end |
#files_array ⇒ Object (readonly)
Returns the value of attribute files_array.
4 5 6 |
# File 'lib/fuzz_ball/searcher.rb', line 4 def files_array @files_array end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/fuzz_ball/searcher.rb', line 4 def @options end |
Instance Method Details
#add(str) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fuzz_ball/searcher.rb', line 14 def add( str ) str_arr = str2arr( str ) files << str files_array << str_arr duple_index.add( files_array.count - 1, str_arr ) true end |
#inspect ⇒ Object
53 54 55 |
# File 'lib/fuzz_ball/searcher.rb', line 53 def inspect %Q[<FuzzBall::Searcher n_files=#{files_array.count}>] end |
#search(needle, opts = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fuzz_ball/searcher.rb', line 25 def search(needle, opts = {}) needle_ary = str2arr(needle) results = [] return results if (needle.length < 2) decimate_strings!( needle_ary ).each do |candidate| smith = SmithWaterman.new(needle_ary, candidate) results << { :alignment => smith.alignment, :score => (smith.score / candidate.length), # normalize by string length; this favors shorter strings even if a longer string has a higher smith score :string => candidate.pack("U*") } end if (opts[:order] == :descending) results.sort! {|a,b| b[:score] <=> a[:score]} else results.sort! {|a,b| a[:score] <=> b[:score]} end results = results.first(opts[:limit]) if opts[:limit].is_a?(Fixnum) results end |