From 64dd24e1698e11ce461048f30e3367fe33f626fa Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 12 Feb 2014 18:11:33 +0100 Subject: SparseVector: from_a --- Makefile | 1 + lib/nlp_ruby/SparseVector.rb | 4 ++++ lib/nlp_ruby/Vector.rb | 43 ------------------------------------------- 3 files changed, 5 insertions(+), 43 deletions(-) delete mode 100644 lib/nlp_ruby/Vector.rb diff --git a/Makefile b/Makefile index c2e2021..9eb47c2 100644 --- a/Makefile +++ b/Makefile @@ -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 - -- cgit v1.2.3