diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | lib/nlp_ruby/SparseVector.rb | 4 | ||||
-rw-r--r-- | lib/nlp_ruby/Vector.rb | 43 |
3 files changed, 5 insertions, 43 deletions
@@ -2,6 +2,7 @@ version := $$(grep s.version nlp_ruby.gemspec | awk '{print $$3}' | sed "s|'||g" all: + rm nlp_ruby-$(version).gem gem build nlp_ruby.gemspec install: diff --git a/lib/nlp_ruby/SparseVector.rb b/lib/nlp_ruby/SparseVector.rb index 964ef4e..a4e2bce 100644 --- a/lib/nlp_ruby/SparseVector.rb +++ b/lib/nlp_ruby/SparseVector.rb @@ -5,6 +5,10 @@ class SparseVector < Hash 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 diff --git a/lib/nlp_ruby/Vector.rb b/lib/nlp_ruby/Vector.rb deleted file mode 100644 index c749ed9..0000000 --- a/lib/nlp_ruby/Vector.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Vector < Array - - def sum - self.inject(:+) - end - - def average - self.sum/self.size.to_f - end - - def variance - avg = self.average - var = 0.0 - self.each { |i| var += (avg - i)**2 } - var - end - - def stddev list_of_values, decimal_places=-1 - Math.sqrt self.variance - end - - def dot other - return nil if self.size!=other.size - sum = 0.0 - self.each_with_index { |i,j| sum += i*other[j] } - return sum - end - - def magnitude - Math.sqrt self.inject { |sum,i| sum+i**2 } - end - - def cosinus_sim other - return self.dot(other)/(self.mag*other.mag) - end - - def euclidian_dist other - sum = 0.0 - self.each_with_index { |i,j| sum += (i - other[j])**2 } - return Math.sqrt(sum) - end -end - |