Class: Predictable::Recommender
- Inherits:
-
Object
- Object
- Predictable::Recommender
- Defined in:
- lib/predictable/recommender.rb
Overview
Recommender
Defines the Recommender interface and implements it using the Prediction.io API.
recommender = Recommender.new(client)
recommender.create_item(item)
recommender.create_user(user)
recommender.record_action(user, "conversion", item)
recommender.recommended_items(user, 10)
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#recommendation_engine ⇒ Object
Returns the value of attribute recommendation_engine.
-
#similarity_engine ⇒ Object
Returns the value of attribute similarity_engine.
Instance Method Summary collapse
- #create_item(item, attrs = {}) ⇒ Object
- #create_user(user, attrs = {}) ⇒ Object
- #delete_item(item) ⇒ Object
- #delete_user(user) ⇒ Object
-
#initialize(client, engines = {}) ⇒ Recommender
constructor
A new instance of Recommender.
- #recommended_items(user, n, opts = {}) ⇒ Object
- #record_action(user, action, item, opts = {}) ⇒ Object
- #similar_items(item, n, opts = {}) ⇒ Object
Constructor Details
#initialize(client, engines = {}) ⇒ Recommender
Returns a new instance of Recommender.
20 21 22 23 24 |
# File 'lib/predictable/recommender.rb', line 20 def initialize(client, engines={}) @client = client @recommendation_engine = engines[:recommendation_engine] @similarity_engine = engines[:similarity_engine] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
17 18 19 |
# File 'lib/predictable/recommender.rb', line 17 def client @client end |
#recommendation_engine ⇒ Object
Returns the value of attribute recommendation_engine.
18 19 20 |
# File 'lib/predictable/recommender.rb', line 18 def recommendation_engine @recommendation_engine end |
#similarity_engine ⇒ Object
Returns the value of attribute similarity_engine.
18 19 20 |
# File 'lib/predictable/recommender.rb', line 18 def similarity_engine @similarity_engine end |
Instance Method Details
#create_item(item, attrs = {}) ⇒ Object
26 27 28 |
# File 'lib/predictable/recommender.rb', line 26 def create_item(item, attrs = {}) client.acreate_item(item.pio_iid, item.pio_itypes, attrs) end |
#create_user(user, attrs = {}) ⇒ Object
34 35 36 |
# File 'lib/predictable/recommender.rb', line 34 def create_user(user, attrs = {}) client.acreate_user(user.pio_uid, attrs) end |
#delete_item(item) ⇒ Object
30 31 32 |
# File 'lib/predictable/recommender.rb', line 30 def delete_item(item) client.adelete_item(item.pio_iid) end |
#delete_user(user) ⇒ Object
38 39 40 |
# File 'lib/predictable/recommender.rb', line 38 def delete_user(user) client.adelete_user(user.pio_uid) end |
#recommended_items(user, n, opts = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/predictable/recommender.rb', line 47 def recommended_items(user, n, opts = {}) = opts.stringify_keys client.identify(user.pio_uid) begin client.get_itemrec_top_n(recommendation_engine, n, opts) rescue PredictionIO::Client::ItemRecNotFoundError => e [] end end |
#record_action(user, action, item, opts = {}) ⇒ Object
42 43 44 45 |
# File 'lib/predictable/recommender.rb', line 42 def record_action(user, action, item, opts = {}) client.identify(user.pio_uid) client.arecord_action_on_item(action.to_s, item.pio_iid, opts) end |
#similar_items(item, n, opts = {}) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/predictable/recommender.rb', line 57 def similar_items(item, n, opts = {}) begin client.get_itemsim_top_n(similarity_engine, item.pio_iid, n, opts) rescue PredictionIO::Client::ItemSimNotFoundError [] end end |