Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on Nov 13, 2017
- @author: Haodong Huang and Alden Radoncic
- I pledge my honor that I have abided by the Stevens Honor System
- -hhuang21
- -aradonci
- '''
- import sys
- def readInData():
- """Reads the data from the file musicrecplus.txt"""
- database = {}
- try:
- handle = open("musicrecplus.txt", "r")
- lines = handle.read().splitlines()
- handle.close()
- for line in lines:
- name, artists = line.split(":")
- database[name] = sorted(artists.split(","))
- except:
- return database
- return database
- def getUser():
- """Gets the username from the user"""
- print("Enter your name (put a $ symbol after your name if you wish your preferences to remain private):")
- username = input()
- return username
- def myMap(fn, iterable):
- """applies a function over a list"""
- ret = [0]*len(iterable)
- i = 0
- for thing in iterable:
- ret[i] = fn(thing)
- i += 1
- return ret
- def onePerLine(iterable):
- """Prints a list one iterable on each line"""
- for thing in iterable:
- print(thing)
- def saveUserPreferences(user, database):
- ''' Writes out the user's preferences to the file. '''
- def getPreferences():
- """Gets the user's preferences"""
- prefs = []
- print("Enter an artist that you like (Enter to finish)")
- newPref = input()
- while newPref != '':
- prefs.append(newPref)
- print("Enter an artist that you like (Enter to finish)")
- newPref = input()
- prefs.sort()
- return prefs
- database[user] = getPreferences()
- file = open("musicrecplus.txt", "w")
- for user in database:
- toSave = str(user) + ":" + ",".join(database[user]) + \
- "\n"
- file.write(toSave)
- file.close()
- menu(user, database)
- def getRecommendations(user, database):
- """Prints out the recommendations for the user"""
- def compare(userArtists, otherArtists, otherUName):
- if otherUName[-1] == "$":
- return -1
- iUser = 0
- iOther = 0
- score = 0
- while iUser < len(userArtists) and iOther < len(otherArtists):
- aUser = userArtists[iUser]
- aOther = otherArtists[iOther]
- if aUser == aOther:
- score += 1
- iUser += 1
- iOther += 1
- elif aUser < aOther:
- iUser += 1
- else:
- iOther += 1
- if len(userArtists) == score:
- return -1
- return score
- def diff(userArtists, otherArtists):
- iUser = 0
- iOther = 0
- diffs = []
- while iUser < len(userArtists) and iOther < len(otherArtists):
- aUser = userArtists[iUser]
- aOther = otherArtists[iOther]
- if aUser == aOther:
- iUser += 1
- iOther += 1
- elif aUser < aOther:
- iUser += 1
- else:
- diffs.append(aOther)
- iOther += 1
- if iOther < len(otherArtists):
- diffs.extend(otherArtists[iOther:])
- return diffs
- userArtists = database[user]
- ranked = sorted(myMap(lambda uname:(compare(userArtists, database[uname], uname), uname), database), reverse = True)
- if len(ranked) == 0 or ranked[0][0] == -1:
- print("No recommendations available at this time")
- menu(user, database)
- maxScore = ranked[0][0]
- iMax = 0
- for result in ranked:
- if maxScore != result[0]:
- break
- iMax += 1
- artists = []
- for i in range(iMax):
- artists.extend(diff(userArtists, database[ranked[i][1]]))
- artists = sorted(list(set(artists)))
- onePerLine(artists)
- menu(user, database)
- def artistLikes(database):
- """Returns a dictionary of the artists and the likes they have"""
- likes = {}
- databaseCopy = dict(database)
- for users, artists in database.items():
- if users[-1] == "$":
- del databaseCopy[users]
- for user, artists in databaseCopy.items():
- for artist in artists:
- if artist in likes:
- likes[artist] += 1
- else:
- likes[artist] = 1
- return likes
- def mostPopular(user, database):
- """Returns the most popular artists"""
- popularArtist = []
- likes = artistLikes(database)
- for artist in likes:
- if popularArtist == []:
- popularArtist = [artist]
- elif likes[artist] > likes[popularArtist[0]]:
- popularArtist = [artist]
- elif likes[artist] == likes[popularArtist[0]]:
- popularArtist.append(artist)
- if popularArtist == []:
- print("Sorry, no artists found")
- onePerLine(popularArtist)
- menu(user, database)
- def howPopular(user, database):
- """Returns the number of likes that the most popular artist has"""
- popularArtist = []
- mostLikes = 0
- likes = artistLikes(database)
- for artist, votes in likes.items():
- if popularArtist == []:
- popularArtist = [artist]
- mostLikes = votes
- elif likes[artist] > likes[popularArtist[0]]:
- popularArtist = [artist]
- mostLikes = votes
- elif likes[artist] == likes[popularArtist[0]]:
- popularArtist.append(artist)
- mostLikes = votes
- if mostLikes == 0:
- print("Sorry, no artists found")
- print(mostLikes)
- menu(user, database)
- def mostLikes(user, database):
- """Returns the users with the most amount of likes"""
- userLikes = {}
- for user, artists in database.items():
- likes = 0
- for artist in range(len(artists)):
- likes += 1
- userLikes[user] = likes
- userLikesCopy = dict(userLikes)
- for user, likes in userLikes.items():
- if user[-1] == "$":
- del userLikesCopy[user]
- maxLikes = 0
- maxLikesUser = []
- for user, likes in userLikesCopy.items():
- if likes > maxLikes:
- maxLikes = likes
- maxLikesUser = [user]
- elif likes == maxLikes:
- maxLikesUser.append(user)
- if maxLikes == 0:
- print("Sorry, no user found")
- else:
- onePerLine(maxLikesUser)
- menu(user, database)
- def quitSave():
- """Safely exits the program.
- Does not need to write to file because saveUserPreference already does that"""
- sys.exit(0)
- def menu(user, database):
- """Prints out the menu"""
- print("""Enter a letter to choose an option
- e - Enter preferences
- r - Get recommendations
- p - Show most popular artist
- h - How popular is the most popular
- m - Which user has the most likes
- q - Quit and save""")
- option = input()
- if option == 'e':
- saveUserPreferences(user, database)
- elif option == 'r':
- getRecommendations(user, database)
- elif option == 'p':
- mostPopular(user, database)
- elif option == 'h':
- howPopular(user, database)
- elif option == 'm':
- mostLikes(user, database)
- elif option == 'q':
- quitSave()
- else:
- menu(user, database)
- def main():
- """The main function that starts the program"""
- database = readInData()
- user = getUser()
- if user in database:
- menu(user, database)
- else:
- saveUserPreferences(user, database)
- if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement