1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| -- sjaccardstore( 'j:sim:u:1', 'sim:u:1', 'sim:u:2', 'u:2' )
local function sjaccardstore( destination1, set1, set2, member1 )
local sinter_count = 0
local sunion_count = 0
do
-- minimize scope for tables
local sinter = redis.call( 'sinter', set1, set2 )
sinter_count = #sinter
end
do
local sunion = redis.call( 'sunion', set1, set2 )
sunion_count = #sunion
end
local jaccard = 0
if sunion_count ~= 0 then
jaccard = sinter_count / sunion_count
end
if jaccard ~= 0 then
redis.call( 'zadd', destination1, jaccard, member1 )
else
redis.call( 'zrem', destination1, member1 )
end
-- lua number converts to redis integers
-- we want float, so return string
return jaccard
end
return tostring( sjaccardstore( ARGV[1], ARGV[2], ARGV[3], ARGV[4] ) )
-- sjaccardstorebothways( 'j:sim:u:1', 'j:sim:u:2', 'sim:u:1', 'sim:u:2', 'u:2', 'u:1' )
local function sjaccardstorebothways( destination1, destination2, set1, set2, member1, member2 )
local jaccard = sjaccardstore( destination1, set1, set2, member1 )
if jaccard ~= 0 then
redis.call( 'zadd', destination2, jaccard, member2 )
else
redis.call( 'zrem', destination2, member2 )
end
return jaccard
end
return tostring( sjaccardstorebothways( ARGV[1], ARGV[2], ARGV[3], ARGV[4], ARGV[5], ARGV[6] ) )
|