recommengine 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/.rspec +2 -0
- data/LICENSE +22 -0
- data/README.md +2 -0
- data/lib/calculator.rb +11 -0
- data/lib/euclidean_calculator.rb +19 -0
- data/lib/flipper.rb +19 -0
- data/lib/matcher.rb +43 -0
- data/lib/pearson_calculator.rb +52 -0
- data/lib/recommender.rb +62 -0
- data/lib/recommengine.rb +26 -0
- data/recommengine.gemspec +18 -0
- data/spec/lib/euclidean_calculator_spec.rb +10 -0
- data/spec/lib/flipper_spec.rb +53 -0
- data/spec/lib/matcher_spec.rb +62 -0
- data/spec/lib/pearson_calculator_spec.rb +10 -0
- data/spec/lib/recommender_spec.rb +29 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/support/test_data.rb +21 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 3576a6e695494260a695452d8da638d6a37e4774
|
|
4
|
+
data.tar.gz: ed27b1734ddbef8e9504cc2c8cbd64dba6409fd7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7b80f257d9f4931371e3c2130a606a323557d0acd0e570f43bb1585b29534dcf8691629d00a98abe148b6518419569d89644aa022af2fd93d6a34e94b748de6a
|
|
7
|
+
data.tar.gz: 66550d76441f76abca9249d6d557e785b4d98a5729b16a6d8fef05f6ea79f2842437a24c39d62919b470615e42f75c928610352e4f00af6485c090a29c390195
|
data/.gitignore
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/test/tmp/
|
|
9
|
+
/test/version_tmp/
|
|
10
|
+
/tmp/
|
|
11
|
+
|
|
12
|
+
.DS_Store
|
|
13
|
+
|
|
14
|
+
## Specific to RubyMotion:
|
|
15
|
+
.dat*
|
|
16
|
+
.repl_history
|
|
17
|
+
build/
|
|
18
|
+
|
|
19
|
+
## Documentation cache and generated files:
|
|
20
|
+
/.yardoc/
|
|
21
|
+
/_yardoc/
|
|
22
|
+
/doc/
|
|
23
|
+
/rdoc/
|
|
24
|
+
|
|
25
|
+
## Environment normalisation:
|
|
26
|
+
/.bundle/
|
|
27
|
+
/vendor/bundle
|
|
28
|
+
/lib/bundler/man/
|
|
29
|
+
|
|
30
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
31
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
32
|
+
# Gemfile.lock
|
|
33
|
+
# .ruby-version
|
|
34
|
+
# .ruby-gemset
|
|
35
|
+
|
|
36
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
37
|
+
.rvmrc
|
data/.rspec
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Cody Knauer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
data/lib/calculator.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module RecommEngine
|
|
2
|
+
class EuclideanCalculator < Calculator
|
|
3
|
+
attr_reader :data, :subject, :comparate
|
|
4
|
+
|
|
5
|
+
def initialize(data:, subject:, comparate:)
|
|
6
|
+
super
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def calc
|
|
10
|
+
1.0 / (Math.sqrt(1.0 + sum_simalarities))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def sum_simalarities
|
|
16
|
+
@data[@subject].map{ |product, score| ((score - data[comparate][product])**2) if data[comparate][product] }.compact.inject(:+)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/flipper.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module RecommEngine
|
|
2
|
+
class Flipper
|
|
3
|
+
def initialize(data)
|
|
4
|
+
@data = data
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def flip
|
|
8
|
+
result = {}
|
|
9
|
+
result.default = {}
|
|
10
|
+
@data.each_key do |k|
|
|
11
|
+
@data[k].each_key do |kk|
|
|
12
|
+
result[kk] = {} if result[kk].empty?
|
|
13
|
+
result[kk][k] = @data[k][kk]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
result
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/matcher.rb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module RecommEngine
|
|
2
|
+
class Matcher
|
|
3
|
+
attr_reader :data, :subject, :similarity, :num
|
|
4
|
+
|
|
5
|
+
def initialize(data:, subject:, similarity: RecommEngine::DEFAULT_ALGORITHM, num: RecommEngine::DEFAULT_MATCHES_NUMBER)
|
|
6
|
+
@data = data
|
|
7
|
+
@subject = subject
|
|
8
|
+
@similarity = similarity
|
|
9
|
+
@num = num
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def top_matches
|
|
13
|
+
all_matches.reverse[0..upper_limit]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def bottom_matches
|
|
17
|
+
all_matches[0..upper_limit]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def all_matches
|
|
23
|
+
scores.sort_by{|k, v| v}
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def scores
|
|
27
|
+
scores = {}
|
|
28
|
+
data.each_key do |k|
|
|
29
|
+
next if subject == k
|
|
30
|
+
scores[k] = similarity_calculator.new(data: @data, subject: @subject, comparate: k).calc
|
|
31
|
+
end
|
|
32
|
+
scores
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def similarity_calculator
|
|
36
|
+
Module.const_get("RecommEngine::#{@similarity}Calculator")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def upper_limit
|
|
40
|
+
num - 1
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module RecommEngine
|
|
2
|
+
class PearsonCalculator < Calculator
|
|
3
|
+
attr_accessor :sum1, :sum2, :sq_sum1, :sq_sum2, :product_sum
|
|
4
|
+
|
|
5
|
+
def initialize(data:, subject:, comparate:)
|
|
6
|
+
super
|
|
7
|
+
@sum1 = @sum2 = @sq_sum1 = @sq_sum2 = @product_sum = 0
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def calc
|
|
11
|
+
return 0 if similar_items.empty?
|
|
12
|
+
sum_all_similarities
|
|
13
|
+
perform_equation
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def similar_items
|
|
19
|
+
@similar_items ||= @data[@subject].map{ |k, v| k if @data[@comparate].keys.include?(k) }.compact
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def number_of_hits
|
|
23
|
+
@number_of_hits ||= similar_items.length
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def sum_all_similarities
|
|
27
|
+
similar_items.each{ |item| sum_simalarities(item) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def sum_simalarities(item)
|
|
31
|
+
subject_val = @data[@subject][item]
|
|
32
|
+
comparate_val = @data[@comparate][item]
|
|
33
|
+
@sum1 += subject_val
|
|
34
|
+
@sum2 += comparate_val
|
|
35
|
+
@sq_sum1 += subject_val**2
|
|
36
|
+
@sq_sum2 += comparate_val**2
|
|
37
|
+
@product_sum += subject_val * comparate_val
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def perform_equation
|
|
41
|
+
denominator.zero? ? 0 : numerator/denominator
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def numerator
|
|
45
|
+
@product_sum - (@sum1 * @sum2/number_of_hits)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def denominator
|
|
49
|
+
@denominator ||= Math.sqrt((@sq_sum1 - (@sum1**2)/number_of_hits)*(@sq_sum2 - (@sum2**2)/number_of_hits))
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
data/lib/recommender.rb
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module RecommEngine
|
|
2
|
+
class Recommender
|
|
3
|
+
attr_reader :data, :subject, :similarity, :similarity_scores, :totals, :similarity_sums
|
|
4
|
+
|
|
5
|
+
def initialize(data:, subject:, similarity: RecommEngine::DEFAULT_ALGORITHM)
|
|
6
|
+
@data = data
|
|
7
|
+
@subject = subject
|
|
8
|
+
@similarity = similarity
|
|
9
|
+
@similarity_scores = {}
|
|
10
|
+
@totals = {}
|
|
11
|
+
@similarity_sums = {}
|
|
12
|
+
@totals.default = 0
|
|
13
|
+
@similarity_sums.default = 0
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def recs
|
|
17
|
+
@data.each_key do |comperate|
|
|
18
|
+
next if is_subject?(comperate) || non_positive_similarity?(comperate)
|
|
19
|
+
@data[comperate].each_key do |product|
|
|
20
|
+
update_sums(comperate, product) unless scored_by_subject?(@subject, product)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
rankings
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def is_subject?(comperate)
|
|
29
|
+
comperate == @subject
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def score(comperate)
|
|
33
|
+
return @similarity_scores[comperate] if @similarity_scores && @similarity_scores[comperate]
|
|
34
|
+
@similarity_scores[comperate] = similarity_calculator.new(data: @data, subject: @subject, comparate: comperate).calc
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def similarity_calculator
|
|
38
|
+
Module.const_get("RecommEngine::#{@similarity}Calculator")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def non_positive_similarity?(comperate)
|
|
42
|
+
score(comperate) <= 0
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def scored_by_subject?(subject, product)
|
|
46
|
+
if @data[subject][product]
|
|
47
|
+
!@data[subject][product].zero? || @data[subject].include?(product)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def update_sums(comperate, product)
|
|
52
|
+
@totals[product] += @data[comperate][product] * score(comperate)
|
|
53
|
+
@similarity_sums[product] += score(comperate)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def rankings
|
|
57
|
+
rankings = {}
|
|
58
|
+
@totals.each { |subject, total| rankings[subject] = total / @similarity_sums[subject] }
|
|
59
|
+
rankings.sort_by{|k, v| v}.reverse
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/recommengine.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Dir['./lib/*.rb'].each { |f| require f }
|
|
2
|
+
|
|
3
|
+
module RecommEngine
|
|
4
|
+
DEFAULT_ALGORITHM = 'Pearson'
|
|
5
|
+
DEFAULT_MATCHES_NUMBER = 3
|
|
6
|
+
|
|
7
|
+
def recommendations(data:, subject:, similarity: RecommEngine::DEFAULT_ALGORITHM)
|
|
8
|
+
RecommEngine::Recommender.new(data: data, subject: subject, similarity: similarity).recs
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
alias_method :recs, :recommendations
|
|
12
|
+
|
|
13
|
+
def top_matches(data:, subject:, similarity: RecommEngine::DEFAULT_ALGORITHM, num: RecommEngine::DEFAULT_MATCHES_NUMBER)
|
|
14
|
+
RecommEngine::Matcher.new(data: data, subject: subject, similarity: similarity, num: num).top_matches
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def bottom_matches(data:, subject:, similarity: RecommEngine::DEFAULT_ALGORITHM, num: RecommEngine::DEFAULT_MATCHES_NUMBER)
|
|
18
|
+
RecommEngine::Matcher.new(data: data, subject: subject, similarity: similarity, num: num).bottom_matches
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def flip(data)
|
|
22
|
+
RecommEngine::Flipper.new(data).flip
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module_function :recommendations, :recs, :top_matches, :flip
|
|
26
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = 'recommengine'
|
|
5
|
+
s.version = '0.1.1'
|
|
6
|
+
s.date = '2015-08-27'
|
|
7
|
+
s.summary = "A flexible recommendation engine."
|
|
8
|
+
s.description = "A flexible recommendation engine supporting multiple similarity algorithms for use in ecommerce sites, marketplaces, social sharing apps, and more."
|
|
9
|
+
s.authors = ["Cody Knauer"]
|
|
10
|
+
s.email = 'codyknauer@gmail.com'
|
|
11
|
+
s.files = `git ls-files`.split("\n")
|
|
12
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
13
|
+
s.homepage = 'http://github.com/c0d3s/recommengine'
|
|
14
|
+
s.license = 'MIT'
|
|
15
|
+
s.require_paths = ["lib"]
|
|
16
|
+
|
|
17
|
+
s.add_development_dependency 'rspec', '~> 3.1'
|
|
18
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
describe '.EuclideanCalculator' do
|
|
2
|
+
include_context "test data"
|
|
3
|
+
let(:euclidean_calc) { RecommEngine::EuclideanCalculator.new(data: data, subject: 'ODB', comparate: 'Raekwon') }
|
|
4
|
+
|
|
5
|
+
describe '#calc' do
|
|
6
|
+
it 'calculates euclidean similarity' do
|
|
7
|
+
expect(euclidean_calc.calc).to eq(0.42640143271122083)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
describe '.recommender' do
|
|
2
|
+
include_context "test data"
|
|
3
|
+
let(:flipper) { RecommEngine::Flipper.new(data) }
|
|
4
|
+
|
|
5
|
+
describe '#flip' do
|
|
6
|
+
it 'flips products and users' do
|
|
7
|
+
expect(flipper.flip).to eq(
|
|
8
|
+
{"The Sixth Sense"=>
|
|
9
|
+
{"Ghostface Killah"=>2.5,
|
|
10
|
+
"RZA"=>3.0,
|
|
11
|
+
"Raekwon"=>2.5,
|
|
12
|
+
"Method Man"=>3.0,
|
|
13
|
+
"ODB"=>3.0},
|
|
14
|
+
"Snakes on a Plane"=>
|
|
15
|
+
{"Ghostface Killah"=>3.5,
|
|
16
|
+
"RZA"=>3.5,
|
|
17
|
+
"Raekwon"=>3.0,
|
|
18
|
+
"GZA"=>3.5,
|
|
19
|
+
"Method Man"=>4.0,
|
|
20
|
+
"ODB"=>4.0,
|
|
21
|
+
"Inspectah Deck"=>4.5},
|
|
22
|
+
"27 Dresses"=>
|
|
23
|
+
{"Ghostface Killah"=>3.0,
|
|
24
|
+
"RZA"=>1.5,
|
|
25
|
+
"GZA"=>3.0,
|
|
26
|
+
"Method Man"=>2.0},
|
|
27
|
+
"The Avengers"=>
|
|
28
|
+
{"Ghostface Killah"=>3.5,
|
|
29
|
+
"RZA"=>5.0,
|
|
30
|
+
"Raekwon"=>3.5,
|
|
31
|
+
"GZA"=>4.0,
|
|
32
|
+
"Method Man"=>3.0,
|
|
33
|
+
"ODB"=>5.0,
|
|
34
|
+
"Inspectah Deck"=>4.0},
|
|
35
|
+
"Pootie Tang"=>
|
|
36
|
+
{"Ghostface Killah"=>2.5,
|
|
37
|
+
"RZA"=>3.5,
|
|
38
|
+
"GZA"=>2.5,
|
|
39
|
+
"Method Man"=>2.0,
|
|
40
|
+
"ODB"=>3.5,
|
|
41
|
+
"Inspectah Deck"=>1.0},
|
|
42
|
+
"Titanic"=>
|
|
43
|
+
{"Ghostface Killah"=>3.0,
|
|
44
|
+
"RZA"=>3.0,
|
|
45
|
+
"Raekwon"=>4.0,
|
|
46
|
+
"GZA"=>4.5,
|
|
47
|
+
"Method Man"=>3.0,
|
|
48
|
+
"ODB"=>3.0}
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
describe '.matcher' do
|
|
2
|
+
include_context "test data"
|
|
3
|
+
let(:default_matcher) { RecommEngine::Matcher.new(data: data, subject: 'ODB') }
|
|
4
|
+
let(:euclidean_matcher) { RecommEngine::Matcher.new(data: data, subject: 'ODB', similarity: 'Euclidean') }
|
|
5
|
+
let(:default_2_matcher) { RecommEngine::Matcher.new(data: data, subject: 'ODB', num: 2) }
|
|
6
|
+
|
|
7
|
+
describe '#initialize' do
|
|
8
|
+
it 'defaults to Pearson similarity algorithm if none is specified' do
|
|
9
|
+
expect(default_matcher.similarity).to eq('Pearson')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'defaults to top 3 matches if num is not specified' do
|
|
13
|
+
expect(default_matcher.num).to eq(3)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context 'when default calculator' do
|
|
18
|
+
describe '#bottom_matches' do
|
|
19
|
+
it 'utilizes Pearson algorithm' do
|
|
20
|
+
expect(default_matcher.send('similarity_calculator')).to eq(Module.const_get('RecommEngine::PearsonCalculator'))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'returns bottom most matches' do
|
|
24
|
+
expect(default_matcher.bottom_matches).to eq([["GZA", 0.02857142857142857], ["Raekwon", 0.13483997249264842], ["Method Man", 0.21128856368212925]])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe '#top_matches' do
|
|
29
|
+
it 'returns bottom most matches' do
|
|
30
|
+
expect(default_matcher.top_matches).to eq([["RZA", 0.963795681875635], ["Ghostface Killah", 0.7470178808339965], ["Inspectah Deck", 0.66284898035987]])
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context 'when euclidean calculator' do
|
|
36
|
+
describe '#bottom_matches' do
|
|
37
|
+
it 'returns bottom most matches' do
|
|
38
|
+
expect(euclidean_matcher.bottom_matches).to eq([["Inspectah Deck", 0.34299717028501764], ["Method Man", 0.3713906763541037], ["Raekwon", 0.42640143271122083]])
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe '#top_matches' do
|
|
43
|
+
it 'returns bottom most matches' do
|
|
44
|
+
expect(euclidean_matcher.top_matches).to eq([["RZA", 0.8944271909999159], ["Ghostface Killah", 0.4588314677411235], ["GZA", 0.42640143271122083]])
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context 'when specified number' do
|
|
50
|
+
describe '#top_matches' do
|
|
51
|
+
it 'returns 2 records' do
|
|
52
|
+
expect(default_2_matcher.top_matches.length).to eq(2)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe '#bottom_matches' do
|
|
57
|
+
it 'returns 2 records' do
|
|
58
|
+
expect(default_2_matcher.bottom_matches.length).to eq(2)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
describe '.PearsonCalculator' do
|
|
2
|
+
include_context "test data"
|
|
3
|
+
let(:pearson_calc) { RecommEngine::PearsonCalculator.new(data: data, subject: 'ODB', comparate: 'Raekwon') }
|
|
4
|
+
|
|
5
|
+
describe '#calc' do
|
|
6
|
+
it 'calculates pearson similarity' do
|
|
7
|
+
expect(pearson_calc.calc).to eq(0.13483997249264842)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
describe '.recommender' do
|
|
2
|
+
include_context "test data"
|
|
3
|
+
let(:default_recommender) { RecommEngine::Recommender.new(data: data, subject: 'Raekwon') }
|
|
4
|
+
let(:euclidean_recommender) { RecommEngine::Recommender.new(data: data, subject: 'Raekwon', similarity: 'Euclidean') }
|
|
5
|
+
|
|
6
|
+
describe '#initialize' do
|
|
7
|
+
it 'defaults to Pearson similarity algorithm if none is specified' do
|
|
8
|
+
expect(default_recommender.similarity).to eq('Pearson')
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context 'when default calculator' do
|
|
13
|
+
describe '#recs' do
|
|
14
|
+
it 'utilizes Pearson algorithm' do
|
|
15
|
+
expect(default_recommender.send('similarity_calculator')).to eq(Module.const_get('RecommEngine::PearsonCalculator'))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'returns ranked recommendations' do
|
|
19
|
+
expect(default_recommender.recs).to eq([["27 Dresses", 2.8092760065251268], ["Pootie Tang", 2.694636703980363]])
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context 'when euclidean calculator' do
|
|
25
|
+
it 'returns ranked recommendations' do
|
|
26
|
+
expect(euclidean_recommender.recs).to eq([["27 Dresses", 2.493876153902421], ["Pootie Tang", 2.445567381116539]])
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
shared_context 'test data' do
|
|
2
|
+
let(:data) {
|
|
3
|
+
{'Ghostface Killah'=> {'The Sixth Sense'=> 2.5, 'Snakes on a Plane'=> 3.5,
|
|
4
|
+
'27 Dresses'=> 3.0, 'The Avengers'=> 3.5, 'Pootie Tang'=> 2.5,
|
|
5
|
+
'Titanic'=> 3.0},
|
|
6
|
+
'RZA'=> {'The Sixth Sense'=> 3.0, 'Snakes on a Plane'=> 3.5,
|
|
7
|
+
'27 Dresses'=> 1.5, 'The Avengers'=> 5.0, 'Titanic'=> 3.0,
|
|
8
|
+
'Pootie Tang'=> 3.5},
|
|
9
|
+
'Raekwon'=> {'The Sixth Sense'=> 2.5, 'Snakes on a Plane'=> 3.0,
|
|
10
|
+
'The Avengers'=> 3.5, 'Titanic'=> 4.0},
|
|
11
|
+
'GZA'=> {'Snakes on a Plane'=> 3.5, '27 Dresses'=> 3.0,
|
|
12
|
+
'Titanic'=> 4.5, 'The Avengers'=> 4.0,
|
|
13
|
+
'Pootie Tang'=> 2.5},
|
|
14
|
+
'Method Man'=> {'The Sixth Sense'=> 3.0, 'Snakes on a Plane'=> 4.0,
|
|
15
|
+
'27 Dresses'=> 2.0, 'The Avengers'=> 3.0, 'Titanic'=> 3.0,
|
|
16
|
+
'Pootie Tang'=> 2.0},
|
|
17
|
+
'ODB' => {'The Sixth Sense'=> 3.0, 'Snakes on a Plane'=> 4.0,
|
|
18
|
+
'Titanic'=> 3.0, 'The Avengers'=> 5.0, 'Pootie Tang'=> 3.5},
|
|
19
|
+
'Inspectah Deck'=> {'Snakes on a Plane'=>4.5, 'Pootie Tang'=>1.0, 'The Avengers'=>4.0}}
|
|
20
|
+
}
|
|
21
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: recommengine
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Cody Knauer
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-08-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.1'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.1'
|
|
27
|
+
description: A flexible recommendation engine supporting multiple similarity algorithms
|
|
28
|
+
for use in ecommerce sites, marketplaces, social sharing apps, and more.
|
|
29
|
+
email: codyknauer@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- ".gitignore"
|
|
35
|
+
- ".rspec"
|
|
36
|
+
- LICENSE
|
|
37
|
+
- README.md
|
|
38
|
+
- lib/calculator.rb
|
|
39
|
+
- lib/euclidean_calculator.rb
|
|
40
|
+
- lib/flipper.rb
|
|
41
|
+
- lib/matcher.rb
|
|
42
|
+
- lib/pearson_calculator.rb
|
|
43
|
+
- lib/recommender.rb
|
|
44
|
+
- lib/recommengine.rb
|
|
45
|
+
- recommengine.gemspec
|
|
46
|
+
- spec/lib/euclidean_calculator_spec.rb
|
|
47
|
+
- spec/lib/flipper_spec.rb
|
|
48
|
+
- spec/lib/matcher_spec.rb
|
|
49
|
+
- spec/lib/pearson_calculator_spec.rb
|
|
50
|
+
- spec/lib/recommender_spec.rb
|
|
51
|
+
- spec/spec_helper.rb
|
|
52
|
+
- spec/support/test_data.rb
|
|
53
|
+
homepage: http://github.com/c0d3s/recommengine
|
|
54
|
+
licenses:
|
|
55
|
+
- MIT
|
|
56
|
+
metadata: {}
|
|
57
|
+
post_install_message:
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
requirements: []
|
|
72
|
+
rubyforge_project:
|
|
73
|
+
rubygems_version: 2.4.5
|
|
74
|
+
signing_key:
|
|
75
|
+
specification_version: 4
|
|
76
|
+
summary: A flexible recommendation engine.
|
|
77
|
+
test_files:
|
|
78
|
+
- spec/lib/euclidean_calculator_spec.rb
|
|
79
|
+
- spec/lib/flipper_spec.rb
|
|
80
|
+
- spec/lib/matcher_spec.rb
|
|
81
|
+
- spec/lib/pearson_calculator_spec.rb
|
|
82
|
+
- spec/lib/recommender_spec.rb
|
|
83
|
+
- spec/spec_helper.rb
|
|
84
|
+
- spec/support/test_data.rb
|