summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-17 15:24:07 +0200
committerPatrick Simianer <p@simianer.de>2014-06-17 15:24:07 +0200
commit773f13eab521db5dadd993c00dc4c780970d5d2e (patch)
treef756a83fb37bcc19cf25b06f555b700ce3d3d29e
parent96d4323b720b858ba8445ca8332006c374500a70 (diff)
bump to 1.0.2, to/from_json methods for SparseVector
-rw-r--r--lib/zipf/SparseVector.rb23
-rw-r--r--zipf.gemspec4
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb
index 3096412..33bb396 100644
--- a/lib/zipf/SparseVector.rb
+++ b/lib/zipf/SparseVector.rb
@@ -1,3 +1,6 @@
+require 'json'
+
+
class SparseVector < Hash
def initialize arg=nil
@@ -18,6 +21,12 @@ class SparseVector < Hash
return v
end
+ def to_h
+ h = {}
+ self.each_pair { |k,v| h[k] = v }
+ return h
+ end
+
def from_h h
h.each_pair { |k,v| self[k] = v }
end
@@ -53,6 +62,20 @@ class SparseVector < Hash
}
end
+ def to_json
+ JSON.dump self.to_h
+ end
+
+ def from_json s
+ from_h JSON.load(s)
+ end
+
+ def self.from_json s
+ v = SparseVector.new
+ v.from_json s
+ return v
+ end
+
def self.from_kv s
v = SparseVector.new
v.from_kv s
diff --git a/zipf.gemspec b/zipf.gemspec
index 4853b2f..5e6fcd1 100644
--- a/zipf.gemspec
+++ b/zipf.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = 'zipf'
- s.version = '1.0'
- s.date = '2014-06-16'
+ s.version = '1.0.2'
+ s.date = '2014-06-17'
s.summary = 'zipf'
s.description = 'NLP related tools and classes'
s.authors = ['Patrick Simianer']