Method: Evoc::SVD#clusters

Defined in:
lib/evoc/svd.rb

#clusters(query, threshold = 0) ⇒ Object

Find the clusters in the current svd given a change-vector/query

threshold: the minimum value of an element in the U matrix,

to be considered as part of an cluster


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/evoc/svd.rb', line 43

def clusters(query,threshold = 0)
  clusters = Hash.new
  perfect_match = []
  query_indexes = query.map {|q_item| item2index(q_item)}.compact #remove nil values
  col_index = 0
  self.u.each_column do |col|
    #initiate cluster
    clusters[col_index] = {pos: {query_match: [], clustered: []},
                            neg: {query_match: [], clustered: []}}
    # get the column of the item
    col.each_with_index do |row_item,row_index|
      # check that the row item is part of cluster
      if row_item.abs > threshold 
        sign = row_item > 0 ? :pos : :neg
        # check if its another item from the query
        if query_indexes.include? row_index
          clusters[col_index][sign][:query_match] << index2item(row_index)
          # check if all items in the cluster was in the query (perfect match)
          if clusters[col_index][sign][:query_match].size == query.size
            perfect_match << [col_index,sign]
          end
        else
          clusters[col_index][sign][:clustered] << [index2item(row_index),row_item]
        end
      end
    end
    col_index += 1
  end
  [perfect_match,clusters]
end