| Class | Foo::Acts::Indexed::SearchAtom |
| In: |
lib/search_atom.rb
|
| Parent: | Object |
Contains a hash of records. { ‘record_id’ => [pos1, pos2, pos] }
# File lib/search_atom.rb, line 19
19: def initialize
20: @records = {}
21: end
Adds record_id to the stored records.
# File lib/search_atom.rb, line 29
29: def add_record(record_id)
30: @records[record_id] = [] if !include_record?(record_id)
31: end
Returns true if the given record is present.
# File lib/search_atom.rb, line 24
24: def include_record?(record_id)
25: @records.include?(record_id)
26: end
Returns at atom containing the records and positions of self preceded by former "former latter" or "big dog" where "big" is the former and "dog" is the latter.
# File lib/search_atom.rb, line 57
57: def preceded_by(former)
58: matches = SearchAtom.new
59: latter = {}
60: former.record_ids.each do |rid|
61: latter[rid] = @records[rid] if @records[rid]
62: end
63: # Iterate over each record in latter.
64: latter.each do |record_id,pos|
65:
66: # Iterate over each position.
67: pos.each do |p|
68: # Check if previous position is in former.
69: if former.include_position?(record_id,p-1)
70: matches.add_record(record_id) if !matches.include_record?(record_id)
71: matches.add_position(record_id,p)
72: end
73: end
74:
75: end
76: return matches
77: end
Returns all record IDs stored in this Atom.
# File lib/search_atom.rb, line 40
40: def record_ids
41: @records.keys
42: end
Removes record_id from this Atom.
# File lib/search_atom.rb, line 51
51: def remove_record(record_id)
52: @records.delete(record_id)
53: end
Returns a hash of record_ids and weightings for each record in the atom.
# File lib/search_atom.rb, line 81
81: def weightings(records_size)
82: out = {}
83: @records.each do |r_id, pos|
84:
85: # Fixes a bug when the records_size is zero. i.e. The only record
86: # contaning the word has been deleted.
87: if records_size < 1
88: out[r_id] = 0.0
89: next
90: end
91:
92: # weighting = frequency * log (records.size / records_with_atom)
93: out[r_id] = pos.size * Math.log(records_size / @records.size)
94: end
95: out
96: end