2

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'},...} 
2
  • Are you given the text files in the given formats or do you make them in that format? Commented Mar 12, 2018 at 1:06
  • @BlooB I am given in this format that’s the problem. Commented Mar 12, 2018 at 1:51

1 Answer 1

1

My understanding is that you have ratings file (let's call it ratings.txt) that looks like this:

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 

And you have books.txt file that looks like this:

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 

First, this is how you can read all the ratings the way you want:

# Reading all the ratings from a file ratings = {} name = None with open("ratings.txt") as fp: for line in fp: line = line.strip() if name is None: name = line else: ratings[name] = map(int,line.split()) name = None print (ratings) 

Output:

{'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], '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]} 

Then your code for reading books list a bit simplified:

# Reading the entire file into a list with open("books.txt", "r") as books_file: books = [line.rstrip('\n') for line in books_file] print (books) 

Then you can merge those together in this way:

for name in ratings: scores = ratings[name] ratings[name] = {'ratings': dict(zip(books,scores))} print (ratings) 

Output:

{'Moose': {'ratings': {'Laurie Halse Anderson,Speak ': 0, "Douglas Adams,The Hitchhiker's Guide To The Galaxy ": 5, 'Mitch Albom,The Five People You Meet in Heaven ': 0, 'Richard Adams,Watership Down': 5, 'Maya Angelou,I Know Why the Caged Bird Sings': 0}}, 'Ben': {'ratings': {'Laurie Halse Anderson,Speak ': 0, "Douglas Adams,The Hitchhiker's Guide To The Galaxy ": 5, 'Mitch Albom,The Five People You Meet in Heaven ': 0, 'Richard Adams,Watership Down': 0, 'Maya Angelou,I Know Why the Caged Bird Sings': 0}}} 

Note that some scores are lost in the output because my file books.txt contains only five books, so function zip returns array of pairs of length 5.

Sign up to request clarification or add additional context in comments.

3 Comments

Mankukha Thank you for your help. I have not yet checked if it works as don’t have access to a machine. But could you please advise me on the following: with this data structure i was planning to calculate the likeliness (both have reviewed and liked or disliked similar books) of 2 users by calculating a vector dot product and then recommending a number of books that user 1 have not read. So would this data structure be a good way to do this or there is a better solution.
@tacticalslipper, for a small dataset like that it doesn't matter much what exact data structure you have. I would just work with lists of ratings, like: {'Moose': [5, 5, 0, 0, 0, ...], 'Ben': [5, 0, 0, 0, 0, ...]}. No need to combine.
Thanks I tried the the code and it works the output is exactly what I wanted. Thanks for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.