summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/SparseVector.rb
blob: cdf966c7027b90f0b7d9ad674d3e5b740ee63a34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class SparseVector < Hash

  def initialize
    super
    self.default = 0
  end

  def from_a a
    a.each_with_index { |i,j| self[j] = i }
  end

  def from_h h
    h.each_pair { |k,v| self[k] = v }
  end

  def from_s s
    from_h eval(s)
  end

  def sum
    self.values.inject(:+)
  end

  def average
    self.sum/self.size.to_f
  end

  def variance
    avg = self.average
    var = 0.0
    self.values.each { |i| var += (avg - i)**2 }
    return var
  end

  def stddev
    Math.sqrt self.variance
  end

  def dot other
    sum = 0.0
    self.each_pair { |k,v| sum += v * other[k] }
    return sum
  end

  def zeros n
    (0).upto(n-1) { |i| self[i] = 0.0 }
  end

  def magnitude
    Math.sqrt self.values.inject { |sum,i| sum+i**2 }
  end

  def cosinus_sim other
    self.dot(other)/(self.magnitude*other.magnitude)
  end

  def euclidian_dist other
    dims = [self.keys, other.keys].flatten.uniq
    sum = 0.0
    dims.each { |d| sum += (self[d] - other[d])**2 }
    return Math.sqrt(sum)
  end

  # FIXME
  def from_kv_file fn, sep=' '
    f = ReadFile.new(fn)
    while line = f.gets
      key, value = line.strip.split sep
      value = value.to_f
      self[key] = value
    end
  end
  
  # FIXME
  def to_kv sep='='
    a = []
    self.each_pair { |k,v|
      a << "#{k}#{sep}#{v}"
    }
    return a.join ' '
  end

  # FIXME
  def to_kv2 sep='='
    a = []
    self.each_pair { |k,v|
      a << "#{k}#{sep}#{v}"
    }
    return a.join "\n"
  end

  def join_keys other
    self.keys + other.keys
  end

  def + other
    new = SparseVector.new
    join_keys(other).each { |k|
      new[k] = self[k]+other[k]
    }
    return new
  end

  def - other
    new = SparseVector.new
    join_keys(other).each { |k|
      new[k] = self[k]-other[k]
    }
    return new
  end

  def * scalar
    raise ArgumentError, "Arg is not numeric #{scalar}" unless scalar.is_a? Numeric
    new = SparseVector.new
    self.keys.each { |k|
      new[k] = self[k] * scalar
    }
    return new
  end
end

def mean_sparse_vector array_of_vectors
  mean = SparseVector.new
  array_of_vectors.each { |i|
    i.each_pair { |k,v|
      mean[k] += v
    }
  }
  n = array_of_vectors.size.to_f
  mean.each_pair { |k,v| mean[k] = v/n }
  return mean
end