<s> from mlcollection . datasets . movielens . movielensparser import MovieLensParser <EOL> from mlcollection . lib . collaborativefiltering import pearson <EOL> from operator import itemgetter <EOL> class MovieRecommender ( MovieLensParser ) : <EOL> def __init__ ( self ) : <EOL> MovieLensParser . __init__ ( self ) <EOL> def movie_recommendations ( self , individual_id ) : <EOL> ranked_dataset = { } <EOL> for user_id , ratingset in self . ratings_by_userid . iteritems ( ) : <EOL> ranked_dataset . setdefault ( user_id , { } ) <EOL> ranked_dataset [ user_id ] = dict ( [ ( r . movie_id , r . rating ) for r in ratingset ] ) <EOL> filter = pearson . PearsonCollaborativeFilter ( ranked_dataset ) <EOL> recommendations = filter . get_recommendations ( individual_id ) <EOL> return recommendations <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> user_id = int ( raw_input ( "<STR_LIT>" ) ) <EOL> rec = MovieRecommender ( ) <EOL> user = rec . user_by_id [ user_id ] <EOL> recommendations = rec . movie_recommendations ( user_id ) <EOL> print "<STR_LIT>" % ( user . id , user . age , user . gender , user . occupation ) <EOL> for i , ( movie_id , score ) in enumerate ( list ( reversed ( sorted ( recommendations . iteritems ( ) , key = itemgetter ( <NUM_LIT:1> ) ) ) ) [ : <NUM_LIT:20> ] ) : <EOL> movie = rec . movies_by_id [ movie_id ] <EOL> print "<STR_LIT>" % ( movie_id , score , movie . title ) </s>
