summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-02-12 18:11:33 +0100
committerPatrick Simianer <p@simianer.de>2014-02-12 18:11:33 +0100
commit64dd24e1698e11ce461048f30e3367fe33f626fa (patch)
tree3afd3233aa9a02c25ab7559dee6f62c2627fec87
parent27bc315543a4e3002e5d4ec0e37be3dcc2e3114e (diff)
SparseVector: from_a
-rw-r--r--Makefile1
-rw-r--r--lib/nlp_ruby/SparseVector.rb4
-rw-r--r--lib/nlp_ruby/Vector.rb43
3 files changed, 5 insertions, 43 deletions
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
-