Problem
I have 2 text files (ratings.txt & books.txt). Ratings file contains all the ratings for the books in Books file (there are 55 ratings for each user for the 55 books in books.txt ). The code I have makes a dictionary with user as KEY and ratings as VALUE and I also created a list for books. But I want to make separate values so I can create a dictionary for each of the value for each user assign to corresponding book.
And after that i need an algorithm which would recommend the User A books that both user A and User B have reviewed (both user dislike a certain book or both like a certain book).
Would I be able to use the method to output A personalised recommendation or my method is not efficient. Is this possible to do this problem and if it is am I making it too complicated and is there any easy way to approach this.
Text file:
1.ratings
Ben 5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 Moose 5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0
2.books
Douglas Adams,The Hitchhiker's Guide To The Galaxy Richard Adams,Watership Down Mitch Albom,The Five People You Meet in Heaven Laurie Halse Anderson,Speak Maya Angelou,I Know Why the Caged Bird Sings
Code:
1.dict
filename = input("") ratings = [] with open(filename) as fp: for line in fp: ratings.extend(line.strip().split(',')) d = {ratings[i]:ratings[i+1] for i in range(0,len(ratings),2)} print(d) 2.list
print ("\nReading the entire file into a list.") text_file = open("books.txt", "r") lines = text_file.readlines() lines[:] = [line.rstrip('\n') for line in lines] print (lines) Output:
1.ratings
{'Ben': '5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5', 'Moose': '5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 -3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0'...} 2.books
["Douglas Adams,The Hitchhiker's Guide To The Galaxy", 'Richard Adams,Watership Down', 'Mitch Albom,The Five People You Meet in Heaven', 'Laurie Halse Anderson,Speak', 'Maya Angelou,I Know Why the Caged Bird Sings', 'Jay Asher,Thirteen Reasons Why', 'Isaac Asimov,Foundation Series', 'Ann Brashares,The Sisterhood of the Travelling Pants', 'Libba Bray,A Great and Terrible Beauty', 'Dan Brown,The Da Vinci Code'...] What I want the output to be is:
1.ratings
{'Ben': '5','0','0','0','0','0','0','1',...} 1.and combining list and dict
{'Ben':{'ratings':{"Douglas Adams,The Hitchhiker's Guide To The Galaxy":'5'},{"Richard Adams,Watership Down":'0'},{'Mitch Albom,The Five People You Meet in Heaven':'0'},{'Laurie Halse Anderson,Speak':'0'},...}