def enableTwitter(userName): print( 'IMDBot: I can use Twitter to enhance your search experience by reading the latest tweets by actors, directors, etc., but first I need to ask for your permission. ' ) while True: # loop to ask if user wants to enable Twitter print( 'IMDBot: Please confirm (yes/no) if you would like to grant me read and write permission. If you say yes, the web page that opens will explain the permission in detail before confirming the authorization.' ) check = input(f'{userName}: ') checkFirst = check[:1].lower( ) # Save first letter (might only need y or n) checkArr = sy.getArray( check, []) # Turn user input into array for synonym checking if (checkFirst == 'y' or sy.findSyns(checkArr, 'yes') == 0): # If user said yes print('IMDBot: Great! ', end='') api = authenticateTwitter() # call method to authenticate return api elif (checkFirst == 'n' or sy.findSyns(checkArr, 'no') == 0): # If user said no print( 'IMDBot: Ok, I will not use Twitter. If you change your mind, please ask me to \"Enable Twitter\" ' ) # acknowledge user choice return '' else: print(f'IMDBot: I\'m sorry, I don\'t understand. ')
def findMovieForCompany(userName, query_movie_name): movie = "" movieFound = False while movieFound == False: search = '' if(query_movie_name == ''): print('IMDBot: Which movie do you have in mind?') search = input(f'{userName}: ') else: search = query_movie_name searchID = 0 #an index to go through the list to confirm the movie the user is talking about print(f'IMDBot: First, I need to find and confirm the movie you\'re talking about...') # Serves to provide a buffer while the query is sent movieNameCorrect = False while movieNameCorrect == False: try: movie = ia.get_movie(ia.search_movie(search.lower())[searchID].movieID) # gets the movieID through a search, then creates an object for the movie from the IMDbPy classes title = movie['title'] year = movie['year'] print(f'IMDBot: I found {title} ({year}). Is this the movie you\'re asking about?') movieCheck = input(f'{userName}: ') movieCheckFirst = movieCheck[:1].lower() movieCheckArr = sy.getArray(movieCheck, []) if (movieCheckFirst == 'y' or sy.findSyns(movieCheckArr, "yes") == 0): # If first letter is y or word is synonym of yes movieNameCorrect = True movieFound = True elif (movieCheckFirst == 'n' or sy.findSyns(movieCheckArr, "no") == 0): # If first letter is n or word is synonym of no print(f'IMDBot: Hmm. Ok, let me try again...') searchID += 1 #to continue through the list of movies found else: #if the user didn't enter an answer starting with y or n print(f'IMDBot: I\'m sorry. I don\'t understand.') except: print(f'IMDBot: Uh oh. I can\'t find any movies that match {search}. Let\'s try again.') break #starts from the beginning so the user can search for a new title return movie # returns movie object
def searchForMovie(userName, search): searchID = 0 #an index to go through the list to confirm the movie the user is talking about print('IMDBot: Ok! Let me see if I can find it...' ) # Serves to provide a buffer while the query is sent while True: try: if (searchID > 0 and searchID % 3 == 0 and askToContinue(userName) == False): return '' else: movie = ia.get_movie( ia.search_movie(search.lower())[searchID].movieID ) # gets the movieID through a search, then creates an object for the movie from the IMDbPy classes title = movie['title'] year = movie['year'] print( f'IMDBot: I found {title} ({year}). Is this the movie you\'re asking about?' ) movieCheck = input(f'{userName}: ') movieCheckFirst = movieCheck[:1].lower() movieCheckArr = sy.getArray(movieCheck, []) if (movieCheckFirst == 'y' or sy.findSyns(movieCheckArr, 'yes') == 0): return movie elif (movieCheck == 'n' or sy.findSyns(movieCheckArr, 'no') == 0): print(f'IMDBot: Hmm. Ok, let me try again...') searchID += 1 #to continue through the list of movies found else: #if the user didn't enter an answer starting with y or n print(f'IMDBot: I\'m sorry. I don\'t understand.') except: print( f'IMDBot: Uh oh. I can\'t find any movies that match {search}. Let\'s try again.' ) return findMovie(userName)
def whoPlayed(twAPI, userName, oldMovie, charName, newMovie_name): try: movie = '' # Variable to hold movie id of the one to look for the character in if(oldMovie == ''): # If there was no movie in locals(), then need to search for the new movie print(f'IMDBot: Before I can see who played {charName} in {newMovie_name}, I need to confirm the movie.') movie = searchForMovie(userName, newMovie_name) # will return movie object if successfully found. Otherwise, '' if (movie == ''): # will only be '' if user said to stop searching return 'User cancel' else: # If there was a movie in locals, it was passed in oldMovie oldMovie_name = oldMovie['title'] if (newMovie_name == '' or (newMovie_name == oldMovie_name)): movie = oldMovie # check using the old movie because we already have the movie ID else: print(f'IMDBot: Before I can see who played {charName} in {newMovie_name}, I need to confirm the movie') while True: print(f'IMDBot: We were talking about {oldMovie_name}. Want me to search for {newMovie_name}?') searchCheck = input(f'{userName}: ') sCheckFirst = searchCheck[:1].lower() # Save first letter (might only need y or n) sCheckArr = sy.getArray(searchCheck, []) # Turn user input into array for synonym checking if (sCheckFirst == 'y' or sy.findSyns(sCheckArr, 'yes') == 0): # If the user does want to search for the new movie name movie = searchForMovie(userName, newMovie_name) if movie == '': # movie will only return blank if the user said to stop searching return 'User cancel' break elif (sCheckFirst == 'n' or sy.findSyns(sCheckArr, 'no') == 0): # If the user does not want to search for the new movie name print('IMDBot: Ok. I\'ll see who played {charName} in {oldMovie_name}.') movie = oldMovie break else: print(f'IMDBot: I\'m sorry, I don\'t understand. ') title = movie['title'] print(f'IMDBot: Ok. Let me see...') #buffer while the bot searches castID = 0 while (castID < len(movie['cast'])): character = movie['cast'][castID].currentRole #need to look through each cast member's role actor = movie['cast'][castID] if (str(character).lower().find(charName.lower()) != -1): #compares the role and the string in lowercase to compare print(f'IMDBot: {character} is played by {actor}') if(twAPI != ''): tw.printLatestTweet(twAPI, actor, userName) # Twitter API - search for last tweet by that actor about this movie else: print(f'IMDBot: What else would you like to know?') return actor else: castID += 1 print(f'IMDBot: It looks like I can\'t find {charName} in {title}.') print(f'IMDBot: What else would you like to know about {title}?') return 'Couldn\'t find the character' except: print(f'IMDBot: Uh oh. Something went wrong.') print(f'IMDBot: What else would you like to know about {title}?') return 'Error handled'
def askToContinue(userName): while True: print('IMDBot: Wait. Would you like me to keep looking?') check = input(f'{userName}: ') checkFirst = check[:1].lower() checkArr = sy.getArray(check, []) if (checkFirst == 'y' or sy.findSyns(checkArr, 'yes') == 0): print(f'IMDBot: Ok. Let me see...') return True elif (check == 'n' or sy.findSyns(checkArr, 'no') == 0): print(f'IMDBot: Ok. What else can I help you with?') return False else: #if the user didn't enter an answer starting with y or n print(f'IMDBot: I\'m sorry. I don\'t understand.')
def askToReadAnother(userName, articleList, query): while True: print('IMDBot: Would you like to read one of the other articles?') check = input(f'{userName}: ') checkFirst = check[:1].lower() # Save first letter (might only need y or n) checkArr = sy.getArray(check, []) # Turn user input into array for synonym checking if (checkFirst == 'y' or sy.findSyns(checkArr, 'yes') == 0): # If user said yes print('IMDBot: Great! I\'ll list them again.') # acknowledge the yes displayResults(articleList, query) # display results again askToRead(userName, articleList, query) # ask which they want to read return elif (checkFirst == 'n' or sy.findSyns(checkArr, 'no') == 0): # If user said no return else: print(f'IMDBot: I\'m sorry, I don\'t understand. ') continue
def askToLikeTweet(api, tweet_obj_id, userName): try: if api.get_status( tweet_obj_id ).favorited: # Check if the tweet was already liked. If the user has already liked the tweet, then acknowledge and continue to next iteration of user input for converation print('IMDBot: Oh! It looks like you already liked this tweet!') print('IMDBot: What else would you like to know?') return else: while True: # Loop for yes or no question in case the user inputs something other than yes or no print(f'IMDBot: Do you want to \'like\' this tweet?' ) # Prompt to like tweet check = input(f'{userName}: ') # Receive input checkFirst = check[:1].lower( ) # Save first letter (only need y or n) checkArr = sy.getArray( check, []) # Turn user input into array for synonym checking if (checkFirst == 'y' or sy.findSyns(checkArr, 'yes') == 0): # if user said yes api.create_favorite( tweet_obj_id) # Tweepy's method to like a Tweet if api.get_status( tweet_obj_id ).favorited: # Check using the tweet ID if it is now liked print('IMDBot: Cool! You liked the tweet!' ) # Acknowledge if it is else: print( 'IMDBot: Woops! Something went wrong, so I could not like the tweet.' ) break elif (checkFirst == 'n' or sy.findSyns(checkArr, 'no') == 0): # if user said no print(f'IMDBot: Ok, you did not like the tweet.') break else: # if user said something other than yes or no print(f'IMDBot: I\'m sorry, I don\'t understand. ') continue print('IMDBot: What else can I do for you today?') return except: print( 'IMDBot: Uh oh. Something went wrong and I cannot continue this action. What else can I help you with today?' ) return
def checkName(name): #can be used if the user wants to change their username nameCorrect = False while nameCorrect == False: #using nameCorrect as a flag print(f'IMDBot: Is your name {name}?') nameCheck = input(f'You: ') # Receive input from user nameCheckFirst = nameCheck[:1].lower( ) # Save first letter (might only need y or n) nameCheckArr = sy.getArray( nameCheck, []) # Turn user input into array for synonym checking if (nameCheckFirst == 'y' or sy.findSyns(nameCheckArr, 'yes') == 0): print(f'IMDBot: That\'s a cool name, {name}! ', end="") nameCorrect = True elif (nameCheckFirst == 'n' or sy.findSyns(nameCheckArr, 'no') == 0): print('IMDBot: Sorry I got it wrong. ', end="") name = askForName() else: print(f'IMDBot: I\'m sorry, I don\'t understand. ') return name
def checker(userName, person_name, oldMovie, newMovie_name): try: # Finds the person in IMDB, gets the id code for person pers = ia.search_person(person_name.title()) p = ia.get_person(pers[0].personID) pName = p['name'] checkMovie = '' # Variable to hold the movie id of the movie that will be checked if (oldMovie == ''): # If no movie was being talked about previously print( f'IMDBot: Before I can check if {pName} was in {newMovie_name}, I need to confirm the movie.' ) checkMovie = f.searchForMovie( userName, newMovie_name) # Search for the new movie being talked about if ( checkMovie == '' ): # checkMovie will only return blank if the user said to stop searching return 'User cancelled' else: oldMovie_name = oldMovie['title'] if oldMovie_name != newMovie_name: # If the old movie and new movie names don't match print( f'IMDBot: Before I can check if {pName} was in {newMovie_name}, I need to confirm the movie.' ) while True: print( f'IMDBot: We were talking about {oldMovie_name}. Want me to search for {newMovie_name}?' ) searchCheck = input(userName + ': ') sCheckFirst = searchCheck[:1].lower( ) # Save first letter (might only need y or n) sCheckArr = sy.getArray( searchCheck, []) # Turn user input into array for synonym checking if ( sCheckFirst == 'y' or sy.findSyns(sCheckArr, 'yes') == 0 ): # If the user does want to search for the new movie name checkMovie = f.searchForMovie(userName, newMovie_name) if ( checkMovie == '' ): # checkMovie will only return blank if the user said to stop searching return 'User Cancelled' break elif ( sCheckFirst == 'n' or sy.findSyns(sCheckArr, 'no') == 0 ): # If the user does not want to search for the new movie name print( 'IMDBot: Ok. I\'ll check if {pName} was in {oldMovie_name}.' ) checkMovie = oldMovie break else: print(f'IMDBot: I\'m sorry, I don\'t understand. ') else: # if the old movie name and new movie name match checkMovie = oldMovie # check using the old movie because we already have the movie ID checkMovie_name = checkMovie['title'] if (p in checkMovie): print(f'IMDBot: Yes, {pName} was in {checkMovie_name}!') return True else: print(f'IMDBot: No, {pName} was not in {checkMovie_name}.') return False except: print( "IMDBot: Woops! Something went wrong. What else can I help you with?" ) return 'Error Found'
try: raw_user_input = input( f'{userName}: ') #collect user input for this iteration. entities = ner.listEntities(raw_user_input) movie_name = ner.getMovieName(raw_user_input) person_name = ner.getPersonName(raw_user_input) company_name = ner.getOrgName(raw_user_input) user_input = sp.fixSentence(raw_user_input, entities) user_input = sy.getArray( user_input, entities ) # User input is now an array. To look for keywords: if 'keyword' in user_input for i in range(len(user_input)): user_input[i] = user_input[i].lower() if ('bye' in user_input or sy.findSyns(user_input, 'goodbye') == 0): #end conversation if user says bye break elif ('nevermind' in user_input): print(f'IMDBot: Ok. How can I help?') elif ((sy.findSyns(user_input, 'find') == 0 or sy.findSyns(user_input, 'search') == 0) and ('another' in user_input or sy.findSyns(user_input, 'movie') == 0)): # pick the movie to talk about movie = f.findMovie(userName) elif ( sy.findSyns(user_input, 'director') == 0 or sy.findSyns(user_input, 'directed') == 0
def askToRead(userName, articleList, query): # Ask user which article they want to read (0 to cancel, 1-5 for articles, otherwise ask again) while True: print('IMDBot: Which article you would like to read?') user_input = input(f'{userName}: ') user_input = user_input.split(" ") user_input = [word.lower() for word in user_input] if('nevermind' in user_input or sy.findSyns(user_input, 'cancel') == 0 or sy.findSyns(user_input, 'stop') == 0 or sy.findSyns(user_input, 'none') == 0): return elif(2 in user_input or sy.findSyns(user_input, 'second') == 0 or sy.findSyns(user_input, 'two') == 0): printArticle(articleList[1]) break elif(3 in user_input or sy.findSyns(user_input, 'third') == 0 or sy.findSyns(user_input, 'three') == 0): printArticle(articleList[2]) break elif(4 in user_input or sy.findSyns(user_input, 'fourth') == 0 or sy.findSyns(user_input, 'four') == 0): printArticle(articleList[3]) break elif(5 in user_input or sy.findSyns(user_input, 'fifth') == 0 or sy.findSyns(user_input, 'five') == 0): printArticle(articleList[4]) break elif(1 in user_input or sy.findSyns(user_input, 'first') == 0 or sy.findSyns(user_input, 'one') == 0): # moved to bottom in case someone writes "second one" printArticle(articleList[0]) break else: print('IMDBot: I\'m sorry, I don\'t understand.') continue askToReadAnother(userName, articleList, query)