class Classifier::ContentNode

This is an internal data structure class for the LSI node. Save for #raw_vector_with, it should be fairly straightforward to understand. You should never have to use it directly.

Attributes

categories[RW]
lsi_norm[RW]
lsi_vector[RW]
raw_norm[RW]
raw_vector[RW]
word_hash[R]

Public Class Methods

new( word_hash, *categories ) click to toggle source

If text_proc is not specified, the source will be duck-typed via source.to_s

# File lib/classifier/lsi/content_node.rb, line 18
def initialize( word_hash, *categories )
  @categories = categories || []
  @word_hash = word_hash
end

Public Instance Methods

raw_vector_with( word_list ) click to toggle source

Creates the raw vector out of #word_hash using word_list as the key for mapping the vector space.

# File lib/classifier/lsi/content_node.rb, line 35
def raw_vector_with( word_list )
  if $GSL
     vec = GSL::Vector.alloc(word_list.size)
  else
     vec = Array.new(word_list.size, 0)
  end

  @word_hash.each_key do |word|
    vec[word_list[word]] = @word_hash[word] if word_list[word]
  end
 
  # Perform the scaling transform
  total_words = vec.sum
  
  # Perform first-order association transform if this vector has more
  # than one word in it. 
  if total_words > 1.0 
    weighted_total = 0.0
    vec.each do |term|
      if ( term > 0 )
        weighted_total += (( term / total_words ) * Math.log( term / total_words ))
      end
    end 
    vec = vec.collect { |val| Math.log( val + 1 ) / -weighted_total }
  end
  
  if $GSL
     @raw_norm   = vec.normalize
     @raw_vector = vec
  else
     @raw_norm   = Vector[*vec].normalize
     @raw_vector = Vector[*vec]
  end
end
search_norm() click to toggle source

Use this to fetch the appropriate search vector in normalized form.

# File lib/classifier/lsi/content_node.rb, line 29
def search_norm
  @lsi_norm || @raw_norm
end
search_vector() click to toggle source

Use this to fetch the appropriate search vector.

# File lib/classifier/lsi/content_node.rb, line 24
def search_vector
  @lsi_vector || @raw_vector
end