Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. '''
  2. Created on Nov 13, 2017
  3.  
  4. @author: Haodong Huang and Alden Radoncic
  5. I pledge my honor that I have abided by the Stevens Honor System
  6. -hhuang21
  7. -aradonci
  8. '''
  9. import sys
  10. def readInData():
  11. """Reads the data from the file musicrecplus.txt"""
  12. database = {}
  13. try:
  14. handle = open("musicrecplus.txt", "r")
  15. lines = handle.read().splitlines()
  16. handle.close()
  17. for line in lines:
  18. name, artists = line.split(":")
  19. database[name] = sorted(artists.split(","))
  20. except:
  21. return database
  22. return database
  23. def getUser():
  24. """Gets the username from the user"""
  25. print("Enter your name (put a $ symbol after your name if you wish your preferences to remain private):")
  26. username = input()
  27. return username
  28. def myMap(fn, iterable):
  29. """applies a function over a list"""
  30. ret = [0]*len(iterable)
  31. i = 0
  32. for thing in iterable:
  33. ret[i] = fn(thing)
  34. i += 1
  35. return ret
  36. def onePerLine(iterable):
  37. """Prints a list one iterable on each line"""
  38. for thing in iterable:
  39. print(thing)
  40. def saveUserPreferences(user, database):
  41. ''' Writes out the user's preferences to the file. '''
  42. def getPreferences():
  43. """Gets the user's preferences"""
  44. prefs = []
  45. print("Enter an artist that you like (Enter to finish)")
  46. newPref = input()
  47. while newPref != '':
  48. prefs.append(newPref)
  49. print("Enter an artist that you like (Enter to finish)")
  50. newPref = input()
  51. prefs.sort()
  52. return prefs
  53. database[user] = getPreferences()
  54. file = open("musicrecplus.txt", "w")
  55. for user in database:
  56. toSave = str(user) + ":" + ",".join(database[user]) + \
  57. "\n"
  58. file.write(toSave)
  59. file.close()
  60. menu(user, database)
  61. def getRecommendations(user, database):
  62. """Prints out the recommendations for the user"""
  63. def compare(userArtists, otherArtists, otherUName):
  64. if otherUName[-1] == "$":
  65. return -1
  66. iUser = 0
  67. iOther = 0
  68. score = 0
  69. while iUser < len(userArtists) and iOther < len(otherArtists):
  70. aUser = userArtists[iUser]
  71. aOther = otherArtists[iOther]
  72. if aUser == aOther:
  73. score += 1
  74. iUser += 1
  75. iOther += 1
  76. elif aUser < aOther:
  77. iUser += 1
  78. else:
  79. iOther += 1
  80. if len(userArtists) == score:
  81. return -1
  82. return score
  83. def diff(userArtists, otherArtists):
  84. iUser = 0
  85. iOther = 0
  86. diffs = []
  87. while iUser < len(userArtists) and iOther < len(otherArtists):
  88. aUser = userArtists[iUser]
  89. aOther = otherArtists[iOther]
  90. if aUser == aOther:
  91. iUser += 1
  92. iOther += 1
  93. elif aUser < aOther:
  94. iUser += 1
  95. else:
  96. diffs.append(aOther)
  97. iOther += 1
  98. if iOther < len(otherArtists):
  99. diffs.extend(otherArtists[iOther:])
  100. return diffs
  101. userArtists = database[user]
  102. ranked = sorted(myMap(lambda uname:(compare(userArtists, database[uname], uname), uname), database), reverse = True)
  103. if len(ranked) == 0 or ranked[0][0] == -1:
  104. print("No recommendations available at this time")
  105. menu(user, database)
  106.  
  107. maxScore = ranked[0][0]
  108. iMax = 0
  109. for result in ranked:
  110. if maxScore != result[0]:
  111. break
  112. iMax += 1
  113. artists = []
  114. for i in range(iMax):
  115. artists.extend(diff(userArtists, database[ranked[i][1]]))
  116. artists = sorted(list(set(artists)))
  117. onePerLine(artists)
  118. menu(user, database)
  119. def artistLikes(database):
  120. """Returns a dictionary of the artists and the likes they have"""
  121. likes = {}
  122. databaseCopy = dict(database)
  123. for users, artists in database.items():
  124. if users[-1] == "$":
  125. del databaseCopy[users]
  126. for user, artists in databaseCopy.items():
  127. for artist in artists:
  128. if artist in likes:
  129. likes[artist] += 1
  130. else:
  131. likes[artist] = 1
  132. return likes
  133. def mostPopular(user, database):
  134. """Returns the most popular artists"""
  135. popularArtist = []
  136. likes = artistLikes(database)
  137. for artist in likes:
  138. if popularArtist == []:
  139. popularArtist = [artist]
  140. elif likes[artist] > likes[popularArtist[0]]:
  141. popularArtist = [artist]
  142. elif likes[artist] == likes[popularArtist[0]]:
  143. popularArtist.append(artist)
  144. if popularArtist == []:
  145. print("Sorry, no artists found")
  146. onePerLine(popularArtist)
  147. menu(user, database)
  148.  
  149. def howPopular(user, database):
  150. """Returns the number of likes that the most popular artist has"""
  151. popularArtist = []
  152. mostLikes = 0
  153. likes = artistLikes(database)
  154. for artist, votes in likes.items():
  155. if popularArtist == []:
  156. popularArtist = [artist]
  157. mostLikes = votes
  158. elif likes[artist] > likes[popularArtist[0]]:
  159. popularArtist = [artist]
  160. mostLikes = votes
  161. elif likes[artist] == likes[popularArtist[0]]:
  162. popularArtist.append(artist)
  163. mostLikes = votes
  164. if mostLikes == 0:
  165. print("Sorry, no artists found")
  166. print(mostLikes)
  167.  
  168. menu(user, database)
  169. def mostLikes(user, database):
  170. """Returns the users with the most amount of likes"""
  171. userLikes = {}
  172. for user, artists in database.items():
  173. likes = 0
  174. for artist in range(len(artists)):
  175. likes += 1
  176. userLikes[user] = likes
  177. userLikesCopy = dict(userLikes)
  178. for user, likes in userLikes.items():
  179. if user[-1] == "$":
  180. del userLikesCopy[user]
  181. maxLikes = 0
  182. maxLikesUser = []
  183. for user, likes in userLikesCopy.items():
  184. if likes > maxLikes:
  185. maxLikes = likes
  186. maxLikesUser = [user]
  187. elif likes == maxLikes:
  188. maxLikesUser.append(user)
  189. if maxLikes == 0:
  190. print("Sorry, no user found")
  191. else:
  192. onePerLine(maxLikesUser)
  193. menu(user, database)
  194. def quitSave():
  195. """Safely exits the program.
  196. Does not need to write to file because saveUserPreference already does that"""
  197. sys.exit(0)
  198. def menu(user, database):
  199. """Prints out the menu"""
  200. print("""Enter a letter to choose an option
  201. e - Enter preferences
  202. r - Get recommendations
  203. p - Show most popular artist
  204. h - How popular is the most popular
  205. m - Which user has the most likes
  206. q - Quit and save""")
  207. option = input()
  208. if option == 'e':
  209. saveUserPreferences(user, database)
  210. elif option == 'r':
  211. getRecommendations(user, database)
  212. elif option == 'p':
  213. mostPopular(user, database)
  214. elif option == 'h':
  215. howPopular(user, database)
  216. elif option == 'm':
  217. mostLikes(user, database)
  218. elif option == 'q':
  219. quitSave()
  220. else:
  221. menu(user, database)
  222. def main():
  223. """The main function that starts the program"""
  224. database = readInData()
  225. user = getUser()
  226. if user in database:
  227. menu(user, database)
  228. else:
  229. saveUserPreferences(user, database)
  230. if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement