lsh 0.1.0-java → 0.1.1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lsh/math_util_gsl.rb +15 -0
- data/lib/lsh/math_util_jblas.rb +4 -0
- data/lib/lsh/storage/redis_backend.rb +19 -5
- metadata +2 -2
data/lib/lsh/math_util_gsl.rb
CHANGED
@@ -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
|
data/lib/lsh/math_util_jblas.rb
CHANGED
@@ -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
|
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
|
-
@
|
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
|
-
|
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
@@ -2,14 +2,14 @@
|
|
2
2
|
name: lsh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- Yves Raimond
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jblas-ruby
|