molten-trunk/molten5_withgrid/vendor/plugins/acts_as_ferret/lib/more_like_this.rb
def retrieve_terms(id, class_name, reader, options)
          raise "more_like_this atm only works on saved records" if id.nil?
          document_number = document_number(id, class_name) rescue nil
          field_names = options[:field_names]
          max_num_tokens = options[:max_num_tokens]
          term_freq_map = Hash.new(0)
          doc = nil
          record = nil
          field_names.each do |field|
            #puts "field: #{field}"
            term_freq_vector = reader.term_vector(document_number, field) if document_number
            #if false
            if term_freq_vector
              # use stored term vector
              # puts 'using stored term vector'
              term_freq_vector.terms.each do |term|
                term_freq_map[term.text] += term.positions.size unless noise_word?(term.text, options)
              end
            else
              # puts 'no stored term vector'
              # no term vector stored, but we have stored the contents in the index
              # -> extract terms from there
              content = nil
              if document_number
                doc = reader[document_number]
                content = doc[field]
              end
              unless content
                # no term vector, no stored content, so try content from this instance
                record ||= options[:base_class].constantize.find(id)
                content = record.content_for_field_name(field.to_s)
              end
              puts "have doc: #{doc[:id]} with #{field} == #{content}"
              token_count = 0
              
              ts = options[:analyzer].token_stream(field, content)
              while token = ts.next
                break if (token_count+=1) > max_num_tokens
                next if noise_word?(token.text, options)
                term_freq_map[token.text] += 1
              end
            end
          end
          term_freq_map
        end
# creates a term/term_frequency map for terms from the fields
# given in options[:field_names]
creates a term/term_frequency map for terms from the fields
given in options[:field_names]
creates a term / term_frequency map for terms from the fields given in options [ : field_names ]
def retrieve_terms(id, class_name, reader, options)
          raise "more_like_this atm only works on saved records" if id.nil?
          document_number = document_number(id, class_name) rescue nil
          field_names = options[:field_names]
          max_num_tokens = options[:max_num_tokens]
          term_freq_map = Hash.new(0)
          doc = nil
          record = nil
          field_names.each do |field|
            term_freq_vector = reader.term_vector(document_number, field) if document_number
            if term_freq_vector
              term_freq_vector.terms.each do |term|
                term_freq_map[term.text] += term.positions.size unless noise_word?(term.text, options)
              end
            else
              content = nil
              if document_number
                doc = reader[document_number]
                content = doc[field]
              end
              unless content
                record ||= options[:base_class].constantize.find(id)
                content = record.content_for_field_name(field.to_s)
              end
              puts "have doc: #{doc[:id]} with #{field} == #{content}"
              token_count = 0
              ts = options[:analyzer].token_stream(field, content)
              while token = ts.next
                break if (token_count+=1) > max_num_tokens
                next if noise_word?(token.text, options)
                term_freq_map[token.text] += 1
              end
            end
          end
          term_freq_map
        end
def retrieve_terms ( id , class_name , reader , options ) raise "more_like_this atm only works on saved records" if id . nil? document_number = document_number ( id , class_name ) rescue nil field_names = options [ :field_names ] max_num_tokens = options [ :max_num_tokens ] term_freq_map = Hash . new ( 0 ) doc = nil record = nil field_names . each do | field | term_freq_vector = reader . term_vector ( document_number , field ) if document_number if term_freq_vector term_freq_vector . terms . each do | term | term_freq_map [ term . text ] += term . positions . size unless noise_word? ( term . text , options ) end else content = nil if document_number doc = reader [ document_number ] content = doc [ field ] end unless content record ||= options [ :base_class ] . constantize . find ( id ) content = record . content_for_field_name ( field . to_s ) end puts "have doc: #{doc[:id]} with #{field} == #{content}" token_count = 0 ts = options [ :analyzer ] . token_stream ( field , content ) while token = ts . next break if ( token_count += 1 ) > max_num_tokens next if noise_word? ( token . text , options ) term_freq_map [ token . text ] += 1 end end end term_freq_map end
creates a term/term_frequency map for terms from the fields
given in options[:field_names]
creates a term / term_frequency map for terms from the fields given in options [ : field_names ]
#puts "field: #{field}" #if false # use stored term vector # puts 'using stored term vector' # puts 'no stored term vector' # no term vector stored, but we have stored the contents in the index # -> extract terms from there # no term vector, no stored content, so try content from this instance
id
class_name
reader
options