In [1]:
try:
import OpenMRS as om
except:
# At this point, you probably haven't installed OpenMRS. You can install it by:
# sudo pip install git+https://github.com/BerryAI/Acai
# Now we are going to import OpenMRS from the source.
# Note: This assumes you are currently in the 'examples/' folder running this notebook.
import os
import sys
CWD = os.getcwd()
print 'current working directory:', CWD
sys.path.append(os.path.join(CWD, '..'))
import OpenMRS as om
In [2]:
example_ratings = om.data.get_example_ratings()
example_tracks = om.data.get_example_tracks()
print 'These are user ids:'
print example_ratings.keys()[:5]
print 'These are track ids and rating scores for the first user:'
print example_ratings.values()[0].items()[:5]
In [3]:
engine = om.RecommendationEngine() # or equivalently, use the following line
# engine = om.RecommendationEngine(catalog=SimpleCatalog(example_tracks))
# Feed the user ratings into the recommendation engine and train it.
engine.train(ratings=example_ratings)
Now the recommendation engine
knows about users and their ratings. We can retrieve and print them out.
In [4]:
one_user = engine.get_user_ids()[0]
ratings = engine.get_ratings_by_user(user_id=one_user)
print ('Ratings by user %s (5 being most favorable and 1 least favorable):' %
one_user)
for track_id, rating in ratings.items()[:5]:
print ' User rates %s on track %s' % (rating,
engine.catalog.get_track_by_id(track_id))
In [5]:
# Recommend tracks for a user.
recommended_tracks = engine.recommend(user_id=one_user, num=10)
print '\nRecommended tracks for user %s:' % one_user
for t in recommended_tracks:
print t
You can start streaming the recommended tracks to the user. Note that some tracks will be the same as what this user has listened to, so you if you want to encourage diversity, make sure to exclude the recently played tracks.
And after your users rate these recommended tracks, you can keep feeding the data back to the recommendation engine and get new recommendations.