Class: FuzzBall::Searcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_indexObject (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

#filesObject (readonly)

Returns the value of attribute files.



4
5
6
# File 'lib/fuzz_ball/searcher.rb', line 4

def files
  @files
end

#files_arrayObject (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

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/fuzz_ball/searcher.rb', line 4

def options
  @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

#inspectObject



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