commendo 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/commendo/content_set.rb +11 -2
- data/lib/commendo/version.rb +1 -1
- data/test/content_set_test.rb +74 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5d32053e7810788416b8a9d1c42937bff79462b
|
4
|
+
data.tar.gz: aa65282bdfbe68311fdc140fcbbe59dabc029c92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 316b145f9abc4370d0e621e2e374ae9105fe61f9f647b150a1acca82b4818be46bd994eac011cba57d6560bf8c8e5430d0b6a7e09634c313ea09723cd2f35662
|
7
|
+
data.tar.gz: f9d134bf294c9abd189af50400948d4dc2fed55e5fb925e3f39cc714fa6090cc4c67bac3dd896e0c40bcde730549deebe4b3525d9ec520c6432882beec0e7afa
|
data/CHANGELOG.md
CHANGED
data/lib/commendo/content_set.rb
CHANGED
@@ -68,8 +68,17 @@ module Commendo
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def similar_to(resource)
|
71
|
-
|
72
|
-
|
71
|
+
if resource.kind_of? Array
|
72
|
+
keys = resource.map do |res|
|
73
|
+
similarity_key(res)
|
74
|
+
end
|
75
|
+
tmp_key = "#{key_base}:tmp:#{SecureRandom.uuid}"
|
76
|
+
redis.zunionstore(tmp_key, keys)
|
77
|
+
similar_resources = redis.zrevrange(tmp_key, 0, -1, with_scores: true)
|
78
|
+
redis.del(tmp_key)
|
79
|
+
else
|
80
|
+
similar_resources = redis.zrevrange(similarity_key(resource), 0, -1, with_scores: true)
|
81
|
+
end
|
73
82
|
similar_resources.map do |resource|
|
74
83
|
{resource: resource[0], similarity: resource[1].to_f}
|
75
84
|
end
|
data/lib/commendo/version.rb
CHANGED
data/test/content_set_test.rb
CHANGED
@@ -95,7 +95,21 @@ module Commendo
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def test_calculate_yields_after_each
|
98
|
-
|
98
|
+
redis = Redis.new(db: 15)
|
99
|
+
redis.flushdb
|
100
|
+
key_base = 'CommendoTests'
|
101
|
+
cs = ContentSet.new(redis, key_base)
|
102
|
+
(3..23).each do |group|
|
103
|
+
(3..23).each do |res|
|
104
|
+
cs.add_by_group(group, res) if res % group == 0
|
105
|
+
end
|
106
|
+
end
|
107
|
+
expected_keys = ['CommendoTests:resources:3', 'CommendoTests:resources:4', 'CommendoTests:resources:5', 'CommendoTests:resources:6', 'CommendoTests:resources:7', 'CommendoTests:resources:8', 'CommendoTests:resources:9', 'CommendoTests:resources:10', 'CommendoTests:resources:11', 'CommendoTests:resources:12', 'CommendoTests:resources:13', 'CommendoTests:resources:14', 'CommendoTests:resources:15', 'CommendoTests:resources:16', 'CommendoTests:resources:17', 'CommendoTests:resources:18', 'CommendoTests:resources:19', 'CommendoTests:resources:20', 'CommendoTests:resources:21', 'CommendoTests:resources:22', 'CommendoTests:resources:23']
|
108
|
+
actual_keys = []
|
109
|
+
cs.calculate_similarity { |key, index, total|
|
110
|
+
actual_keys << key
|
111
|
+
}
|
112
|
+
assert_equal expected_keys.sort, actual_keys.sort
|
99
113
|
end
|
100
114
|
|
101
115
|
def test_calculate_deletes_old_values_first
|
@@ -221,6 +235,65 @@ module Commendo
|
|
221
235
|
|
222
236
|
end
|
223
237
|
|
238
|
+
def test_recommends_for_many
|
239
|
+
redis = Redis.new(db: 15)
|
240
|
+
redis.flushdb
|
241
|
+
ts = TagSet.new(redis, 'CommendoTests:tags')
|
242
|
+
cs = ContentSet.new(redis, 'CommendoTests', ts)
|
243
|
+
(3..23).each do |group|
|
244
|
+
(3..23).each do |res|
|
245
|
+
cs.add(res, group) if res % group == 0
|
246
|
+
ts.add(res, 'mod3') if res.modulo(3).zero?
|
247
|
+
ts.add(res, 'mod4') if res.modulo(4).zero?
|
248
|
+
ts.add(res, 'mod5') if res.modulo(5).zero?
|
249
|
+
end
|
250
|
+
end
|
251
|
+
cs.calculate_similarity
|
252
|
+
expected = [
|
253
|
+
{resource: '18', similarity: 1.333},
|
254
|
+
{resource: '3', similarity: 1.25},
|
255
|
+
{resource: '6', similarity: 0.833},
|
256
|
+
{resource: '12', similarity: 0.7},
|
257
|
+
{resource: '21', similarity: 0.667},
|
258
|
+
{resource: '15', similarity: 0.667},
|
259
|
+
{resource: '9', similarity: 0.533},
|
260
|
+
{resource: '4', similarity: 0.25},
|
261
|
+
{resource: '8', similarity: 0.2},
|
262
|
+
{resource: '16', similarity: 0.167},
|
263
|
+
{resource: '20', similarity: 0.143}
|
264
|
+
]
|
265
|
+
actual = cs.similar_to([12, 6, 9])
|
266
|
+
assert_equal expected, actual
|
267
|
+
#, include: ['mod4'], exclude: ['mod3', 'mod5']
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_recommends_for_many_applies_filters
|
271
|
+
redis = Redis.new(db: 15)
|
272
|
+
redis.flushdb
|
273
|
+
ts = TagSet.new(redis, 'CommendoTests:tags')
|
274
|
+
cs = ContentSet.new(redis, 'CommendoTests', ts)
|
275
|
+
(3..23).each do |group|
|
276
|
+
(3..23).each do |res|
|
277
|
+
cs.add(res, group) if res % group == 0
|
278
|
+
ts.add(res, 'mod3') if res.modulo(3).zero?
|
279
|
+
ts.add(res, 'mod4') if res.modulo(4).zero?
|
280
|
+
ts.add(res, 'mod5') if res.modulo(5).zero?
|
281
|
+
end
|
282
|
+
end
|
283
|
+
cs.calculate_similarity
|
284
|
+
actual = cs.filtered_similar_to([12, 6, 9], include: ['mod4'], exclude: ['mod3', 'mod5'])
|
285
|
+
refute contains_resource('6', actual)
|
286
|
+
refute contains_resource('18', actual)
|
287
|
+
assert contains_resource('4', actual)
|
288
|
+
refute contains_resource('3', actual)
|
289
|
+
refute contains_resource('9', actual)
|
290
|
+
assert contains_resource('8', actual)
|
291
|
+
refute contains_resource('21', actual)
|
292
|
+
assert contains_resource('16', actual)
|
293
|
+
refute contains_resource('15', actual)
|
294
|
+
refute contains_resource('20', actual)
|
295
|
+
end
|
296
|
+
|
224
297
|
def similar_to(cs, resource, similar)
|
225
298
|
contains_resource(similar, cs.similar_to(resource))
|
226
299
|
end
|