lsh 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,10 @@ module LSH
31
31
  @@gsl_random.gaussian(1, dim)
32
32
  end
33
33
 
34
+ def self.zeros(dim)
35
+ GSL::Vector.alloc(dim)
36
+ end
37
+
34
38
  def self.random_gaussian_matrix(k, l)
35
39
  GSL::Matrix.randn(k, l)
36
40
  end
@@ -70,6 +74,17 @@ module GSL
70
74
  alloc(*o['data'])
71
75
  end
72
76
 
77
+ def hash
78
+ to_a.hash
79
+ end
80
+
81
+ def save(file)
82
+ fwrite(file)
83
+ end
84
+
85
+ def load(file)
86
+ fread(file)
87
+ end
73
88
  end
74
89
 
75
90
  end
@@ -32,6 +32,10 @@ module LSH
32
32
  JBLAS.randn(k, l)
33
33
  end
34
34
 
35
+ def self.zeros(dim)
36
+ JBLAS::DoubleMatrix.new(1, dim)
37
+ end
38
+
35
39
  def self.dot(v1, v2)
36
40
  (v1 * v2.t)[0,0]
37
41
  end
@@ -23,18 +23,25 @@ module LSH
23
23
 
24
24
  class RedisBackend
25
25
 
26
- attr_reader :redis
26
+ attr_reader :redis, :data_dir
27
27
 
28
- def initialize(params = { :redis => { :host => '127.0.0.1', :port => 6379 } })
28
+ def initialize(params = { :redis => { :host => '127.0.0.1', :port => 6379 }, :data_dir => 'data' })
29
29
  @redis = Redis.new(params[:redis])
30
+ @data_dir = params[:data_dir]
31
+ Dir.mkdir(@data_dir) unless File.exists?(@data_dir)
30
32
  end
31
33
 
32
34
  def reset!
33
35
  @redis.flushall
36
+ Dir.foreach(@data_dir) {|f| File.delete(File.join(@data_dir, f)) if f != '.' and f != '..' and f.end_with?('.dat')}
34
37
  end
35
38
 
36
39
  def has_index?
37
- projections and parameters and @redis.get("buckets") > 0
40
+ projections and parameters and number_of_buckets > 0
41
+ end
42
+
43
+ def number_of_buckets
44
+ @redis.get("buckets") || 0
38
45
  end
39
46
 
40
47
  def projections=(projections)
@@ -68,7 +75,8 @@ module LSH
68
75
  end
69
76
 
70
77
  def add_vector_to_bucket(bucket, hash, vector)
71
- @redis.sadd "#{bucket}:#{hash}", vector.to_json
78
+ vector.save(File.join(@data_dir, vector.hash.to_s+'.dat')) # Writing vector to disk
79
+ @redis.sadd "#{bucket}:#{hash}", vector.hash.to_s # Only storing vector's hash in Redis
72
80
  end
73
81
 
74
82
  def find_bucket(i)
@@ -76,7 +84,13 @@ module LSH
76
84
  end
77
85
 
78
86
  def query_bucket(bucket, hash)
79
- @redis.smembers("#{bucket}:#{hash}").map { |vector_json| JSON.parse(vector_json) }
87
+ results = []
88
+ @redis.smembers("#{bucket}:#{hash}").map do |vector_hash|
89
+ vector = MathUtil.zeros(parameters[:dim])
90
+ vector.load(File.join(@data_dir, vector_hash+'.dat'))
91
+ results << vector
92
+ end
93
+ results
80
94
  end
81
95
 
82
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-27 00:00:00.000000000 Z
12
+ date: 2012-12-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gsl