summaryrefslogtreecommitdiff
path: root/lib/zipf/SparseVector.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/zipf/SparseVector.rb')
-rw-r--r--lib/zipf/SparseVector.rb16
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb
index e9af799..837403a 100644
--- a/lib/zipf/SparseVector.rb
+++ b/lib/zipf/SparseVector.rb
@@ -37,7 +37,7 @@ class SparseVector < Hash
end
def from_s s
- from_h eval(s)
+ from_h eval(s.strip)
end
def self.from_s s
@@ -55,12 +55,18 @@ class SparseVector < Hash
end
def from_kv s, sep='=', join=/\s/
- s.split(join).each { |i|
+ s.strip.split(join).each { |i|
k,v = i.split(sep)
self[k] = v.to_f
}
end
+ def self.from_kv s, sep='=', join=/\s/
+ v = SparseVector.new
+ v.from_kv s, sep, join
+ return v
+ end
+
def to_json
JSON.dump self.to_h
end
@@ -75,12 +81,6 @@ class SparseVector < Hash
return v
end
- def self.from_kv s, sep='=', join=/\s/
- v = SparseVector.new
- v.from_kv s, sep, join
- return v
- end
-
def from_file fn, sep='='
f = ReadFile.new(fn)
while line = f.gets