Class: SClust::KMean::ClusterPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/sclust/kmean/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sparse_vector, source_object = nil) ⇒ ClusterPoint

Initialize the ClusterPoint with a SparseVector or SparseLabeledVector.



59
60
61
62
63
# File 'lib/sclust/kmean/cluster.rb', line 59

def initialize(sparse_vector, source_object = nil)
    @values  = sparse_vector
    @cluster = nil
    @source_object = source_object
end

Instance Attribute Details

#clusterObject

Returns the value of attribute cluster.



55
56
57
# File 'lib/sclust/kmean/cluster.rb', line 55

def cluster
  @cluster
end

#source_objectObject

Returns the value of attribute source_object.



55
56
57
# File 'lib/sclust/kmean/cluster.rb', line 55

def source_object
  @source_object
end

#valuesObject (readonly)

Returns the value of attribute values.



55
56
57
# File 'lib/sclust/kmean/cluster.rb', line 55

def values
  @values
end

Instance Method Details

#add(clusterPoint, weight) ⇒ Object

Add each item in the cluster point to this cluster point adjusting the values per the given weight. Weght is a value from 0.0 - 1.0, inclusive. A value of 1 means that this clusterPoint is 100% assigned to this cluster point while a weight value of 0 will have no effect.



72
73
74
# File 'lib/sclust/kmean/cluster.rb', line 72

def add(clusterPoint, weight)
    @values.merge(clusterPoint.values).keys.each { |i| @values[i] = ( @values[i] * (1-weight) ) + (clusterPoint.values[i] * weight)}
end

#distance(clusterPoint) ⇒ Object



65
66
67
# File 'lib/sclust/kmean/cluster.rb', line 65

def distance(clusterPoint)
    CosineDistance.distance(@values, clusterPoint.values)
end

#get_max_terms(n = 3) ⇒ Object

Return the top n words. Return all the terms sorted if n is 0.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sclust/kmean/cluster.rb', line 83

def get_max_terms(n=3)
    
    values_to_terms = {}
        
    @values.each do |t, v|
        values_to_terms[v] ||= []
        values_to_terms[v] << SClust::Util::Word.new(t, v, {:stemmed_word => t})
    end
    
    sorted_values = values_to_terms.keys.sort { |x,y|  y <=> x }

    result = []
    
    #n = @values.length if ( n > @values.length || n == 0)
    
    catch(:haveEnough) do
        
        sorted_values.each do |value|

            result += values_to_terms[value]

            throw :haveEnough if result.length >= n
            
        end
        
    end
    
    # Trim our results to exactly the requested size.
    result[0...n]
    
end

#get_term_value(term) ⇒ Object



115
116
117
# File 'lib/sclust/kmean/cluster.rb', line 115

def get_term_value(term)
    @values[term]
end

#sub(clusterPoint, weight) ⇒ Object

Similar to add, but subtract.



78
79
80
# File 'lib/sclust/kmean/cluster.rb', line 78

def sub(clusterPoint, weight)
    @values.merge(clusterPoint.values).keys.each { |i| @values[i] = ( @values[i] - (clusterPoint.values[i] * weight) ) / ( 1 - weight ) }
end