recommend_it 1.0.0 → 1.0.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 +4 -4
- data/lib/item_based_recommendation.rb +23 -0
- data/lib/models/menu.rb +9 -0
- data/lib/recommend.rb +4 -3
- data/lib/user_based_recommendation.rb +1 -2
- data/lib/utils/item_based_mock.rb +35 -0
- data/lib/utils/similars.rb +6 -6
- data/lib/utils/{mock.rb → user_based_mock.rb} +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6bd007a6750f5f64fa9337dd3f649280c536e0836e9cc02ee40177871d9fa975
|
|
4
|
+
data.tar.gz: 2c5f656a202541520ad3c83076117ee32f230eca508fbd6cbbd1e8543ecd3b58
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a41234b4c247f53ffd754dcb2bf3a21839683e41df8f7d0320cb6785fa06a32bc2354e37196038fd8bfcc7ae0d42ed4821428e2ebe76411f094b1a281890d941
|
|
7
|
+
data.tar.gz: 8a8cdfae16218d636f6c558f5b68e387f267ec4d667cf02be8c53057f2f5c309acdfe6f9aa761547fdf5ed7d2651ba631181a2608128df93f28c12651e00a816
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'utils/similars'
|
|
2
|
+
class ItemBasedRecommendation
|
|
3
|
+
include Similars
|
|
4
|
+
|
|
5
|
+
# Get recommendation for item
|
|
6
|
+
# item => menu item to get recommendation for
|
|
7
|
+
# data => matrix collection of item's views and orders
|
|
8
|
+
def recommendation(context)
|
|
9
|
+
item = context.item
|
|
10
|
+
data = context.data
|
|
11
|
+
similar_items = Similars.get_similars(item,data)
|
|
12
|
+
result = Hash.new
|
|
13
|
+
if similar_items.length > 0
|
|
14
|
+
recommend = Array.new
|
|
15
|
+
similar_items.each do |key, sim_index|
|
|
16
|
+
recommend.push(key)
|
|
17
|
+
end
|
|
18
|
+
result[:recommendation] = recommend
|
|
19
|
+
end
|
|
20
|
+
return result
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
data/lib/models/menu.rb
ADDED
data/lib/recommend.rb
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
require 'user_based_recommendation'
|
|
2
|
+
require 'item_based_recommendation'
|
|
2
3
|
|
|
3
4
|
class Recommend
|
|
4
5
|
attr_reader :item, :order_details, :data
|
|
5
6
|
attr_accessor :type
|
|
6
7
|
|
|
7
|
-
# @param type = This is the type of recommendation to be performed e.g UserBasedRecommendation.new
|
|
8
|
+
# @param type = This is the type of recommendation to be performed e.g UserBasedRecommendation.new, ItemBasedRecommendation
|
|
8
9
|
# @param item = This is the object the recommendation will be performed for e.g user
|
|
9
10
|
# @param order_details = Hash containing array of users ordered menu items
|
|
10
|
-
# @param data = Matrix data of
|
|
11
|
+
# @param data = Matrix data of implicit ratings of users/items
|
|
11
12
|
def initialize(type, item, order_details = [], data)
|
|
12
13
|
@type = type
|
|
13
14
|
@item = item
|
|
@@ -15,7 +16,7 @@ class Recommend
|
|
|
15
16
|
@data = data
|
|
16
17
|
end
|
|
17
18
|
|
|
18
|
-
# Returns hash of
|
|
19
|
+
# Returns hash of recommendations
|
|
19
20
|
def recommendation()
|
|
20
21
|
@type.recommendation(self)
|
|
21
22
|
end
|
|
@@ -10,7 +10,7 @@ class UserBasedRecommendation
|
|
|
10
10
|
user = context.item
|
|
11
11
|
data = context.data
|
|
12
12
|
order_details = context.order_details
|
|
13
|
-
similar_users = Similars.
|
|
13
|
+
similar_users = Similars.get_similars(user,data)
|
|
14
14
|
result = Hash.new
|
|
15
15
|
if similar_users.length > 0
|
|
16
16
|
user_menus = Array.new
|
|
@@ -30,7 +30,6 @@ class UserBasedRecommendation
|
|
|
30
30
|
|
|
31
31
|
# Returns all similars menu items
|
|
32
32
|
def get_similars_menus(similar_users,order_details)
|
|
33
|
-
number_of_users = similar_users.length
|
|
34
33
|
similars_menus = Array.new
|
|
35
34
|
similar_users.each do |user_id, sim_index|
|
|
36
35
|
get_all_similar_users_menu_items(order_details,user_id,similars_menus)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module ItemBasedMock
|
|
2
|
+
|
|
3
|
+
# Matrix data for items
|
|
4
|
+
# index 0 => number of views
|
|
5
|
+
# index 1 => number of orders placed
|
|
6
|
+
def self.matrix_data
|
|
7
|
+
return {
|
|
8
|
+
1 => [7,5],
|
|
9
|
+
2 => [1,1],
|
|
10
|
+
3 => [9,2],
|
|
11
|
+
4 => [30,17],
|
|
12
|
+
5 => [13,13],
|
|
13
|
+
6 => [50,1]
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.matrix_data_different
|
|
18
|
+
return {
|
|
19
|
+
1 => [7,5],
|
|
20
|
+
2 => [0,0]
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Returns an array of items with similarity indexes
|
|
25
|
+
def self.similarity_indexes
|
|
26
|
+
return {
|
|
27
|
+
4 => 0.9945239101364354,
|
|
28
|
+
5 => 0.9863939238321439,
|
|
29
|
+
2 => 0.9863939238321436,
|
|
30
|
+
3 => 0.9204443524957583,
|
|
31
|
+
6 => 0.8251932129390934
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
data/lib/utils/similars.rb
CHANGED
|
@@ -36,15 +36,15 @@ module Similars
|
|
|
36
36
|
return Math.sqrt(sum).to_f
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
# Returns an array of similarity
|
|
40
|
-
# Sort the similarity indexes in descending order
|
|
41
|
-
def self.
|
|
39
|
+
# Returns an array of similarity indexes
|
|
40
|
+
# Sort the similarity indexes in descending order
|
|
41
|
+
def self.get_similars(target,data)
|
|
42
42
|
sim = Hash.new
|
|
43
|
-
d1 = data[
|
|
43
|
+
d1 = data[target.id]
|
|
44
44
|
data.each do |key, d2|
|
|
45
|
-
if key !=
|
|
45
|
+
if key != target.id
|
|
46
46
|
index = similarity_index(d1, d2)
|
|
47
|
-
if index > 0 && !index.nan? # Only store
|
|
47
|
+
if index > 0 && !index.nan? # Only store records that have similarity index greater than 0 and is not NaN (NaN values are derived when document vector is filled with only zero values)
|
|
48
48
|
sim[key] = index
|
|
49
49
|
end
|
|
50
50
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: recommend_it
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Weje Emmanuel Okechukwu
|
|
@@ -16,11 +16,14 @@ executables: []
|
|
|
16
16
|
extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
|
+
- lib/item_based_recommendation.rb
|
|
20
|
+
- lib/models/menu.rb
|
|
19
21
|
- lib/models/user.rb
|
|
20
22
|
- lib/recommend.rb
|
|
21
23
|
- lib/user_based_recommendation.rb
|
|
22
|
-
- lib/utils/
|
|
24
|
+
- lib/utils/item_based_mock.rb
|
|
23
25
|
- lib/utils/similars.rb
|
|
26
|
+
- lib/utils/user_based_mock.rb
|
|
24
27
|
homepage: http://rubygems.org/gems/recommend_it
|
|
25
28
|
licenses: []
|
|
26
29
|
metadata: {}
|