Class Foo::Acts::Indexed::SearchAtom
In: lib/search_atom.rb
Parent: Object

Methods

Public Class methods

Contains a hash of records. { ‘record_id’ => [pos1, pos2, pos] }

[Source]

    # File lib/search_atom.rb, line 19
19:         def initialize
20:           @records = {}
21:         end

Public Instance methods

Adds pos to the array of positions for record_id.

[Source]

    # File lib/search_atom.rb, line 34
34:         def add_position(record_id, pos)
35:           add_record(record_id)
36:           @records[record_id] << pos
37:         end

Adds record_id to the stored records.

[Source]

    # 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.

[Source]

    # File lib/search_atom.rb, line 24
24:         def include_record?(record_id)
25:           @records.include?(record_id)
26:         end

Returns an array of positions for record_id stored in this Atom.

[Source]

    # File lib/search_atom.rb, line 45
45:         def positions(record_id)
46:           return @records[records] if include_record?(record_id)
47:           nil
48:         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.

[Source]

    # 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.

[Source]

    # File lib/search_atom.rb, line 40
40:         def record_ids
41:           @records.keys
42:         end

Removes record_id from this Atom.

[Source]

    # 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.

[Source]

    # 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

Protected Instance methods

[Source]

     # File lib/search_atom.rb, line 100
100:         def include_position?(record_id,pos)
101:           @records[record_id].include?(pos)
102:         end

[Validate]