summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/SparseVector.rb
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-02-14 18:56:47 +0100
committerPatrick Simianer <p@simianer.de>2014-02-14 18:56:47 +0100
commit062eda911830c779aa685885b8e15ecceabfc085 (patch)
tree18f02b6e327d211dd717173161024203d9630f57 /lib/nlp_ruby/SparseVector.rb
parent0347cbe4157bb4721e58342243272e0515d286ba (diff)
some class methods; Translation scores dict; DAG edges->outgoing; TFIDF module
Diffstat (limited to 'lib/nlp_ruby/SparseVector.rb')
-rw-r--r--lib/nlp_ruby/SparseVector.rb57
1 files changed, 40 insertions, 17 deletions
diff --git a/lib/nlp_ruby/SparseVector.rb b/lib/nlp_ruby/SparseVector.rb
index b80373c..3096412 100644
--- a/lib/nlp_ruby/SparseVector.rb
+++ b/lib/nlp_ruby/SparseVector.rb
@@ -12,14 +12,32 @@ class SparseVector < Hash
a.each_with_index { |i,j| self[j] = i }
end
+ def self.from_a a
+ v = SparseVector.new
+ v.from_a a
+ return v
+ end
+
def from_h h
h.each_pair { |k,v| self[k] = v }
end
+ def self.from_h h
+ v = SparseVector.new
+ v.from_h h
+ return v
+ end
+
def from_s s
from_h eval(s)
end
+ def self.from_s s
+ v = SparseVector.new
+ v.from_s s
+ return v
+ end
+
def to_kv sep='=', join=' '
a = []
self.each_pair { |k,v|
@@ -35,6 +53,12 @@ class SparseVector < Hash
}
end
+ def self.from_kv s
+ v = SparseVector.new
+ v.from_kv s
+ return v
+ end
+
def from_file fn, sep='='
f = ReadFile.new(fn)
while line = f.gets
@@ -44,6 +68,12 @@ class SparseVector < Hash
end
end
+ def self.from_file fn, sep='='
+ v = SparseVector.new
+ v.from_file fn, sep
+ return v
+ end
+
def join_keys other
self.keys + other.keys
end
@@ -126,24 +156,17 @@ class SparseVector < Hash
}
return new
end
-end
-
-
-module SparseVector
-
-def SparseVector::mean a
- mean = SparseVector.new
- a.each { |i|
- i.each_pair { |k,v|
- mean[k] += v
+ def self.mean a
+ mean = SparseVector.new
+ a.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
-
-
+ n = a.size.to_f
+ mean.each_pair { |k,v| mean[k] = v/n }
+ return mean
+ end
end