def confirm_film_response(bot, message, User): """Stage 2 Response. Confirming the extracted film information was correct. @param {Bot} bot @param {String} message @param {Person} User""" skipFlag = False db_query = db.getQueryInfo(User.id, 1) film_names = set() if db_query: for result in db_query: FilmName = db.getFilmByID(result['Information']) if FilmName: film_names.add(FilmName['Title']) next_question_message = "Sorry I'm not sure I understand." if film_names: film_names = format_query_info(list(film_names)) next_question_message = next_question_message + " Did you want a film similar to {}".format( film_names) next_stage = 'ConfirmFilm' if check_for_expected_input(message, skip): skipFlag = True if check_for_expected_input(message, positives) or skipFlag: #TODO CHECK FOR ADDING OF FILMS/REMOVING OF FILMS genres_query_info = db.getQueryInfo(User.id, 2) if genres_query_info: genres = format_query_info( [genre['Information'] for genre in genres_query_info]) if skipFlag: next_question_message = "Ok let's skip choosing a similar film. I just want to confirm that you did want a film that was a {}".format( genres) else: next_question_message = "Alright that sounds good! I just want to confirm that you did want a film that was a {}".format( genres) next_stage = 'ConfirmGenre' else: if skipFlag: next_question_message = "Ok let's skip choosing a similar film. Did you want the film to have any specific genres?" else: next_question_message = "Alright that sounds good! Did you want the film to have any specific genres?" next_stage = 'AskGenre' elif check_for_expected_input(message, negatives): next_stage = 'AskFilm' next_question_message = "Ok, so do you want the film I suggest to be similar to another film?" db.removeQueryInfo(User.id, 1) bot.send_message(User.id, next_question_message) setUserContextAndStage(User.id, contexts['FilmSuggestion'], stages['filmSuggestion'][next_stage])
def confirm_crew_response(bot, message, User): """Stage 6 Response. Confirming the extracted crew information was correct. @param {Bot} bot @param {String} message @param {Person} User""" skipFlag = False if check_for_expected_input(message, skip): skipFlag = True if skipFlag or check_for_expected_input(message, positives): generate_film(bot, User, 0) db.updateSuggestedFilmIndex(User.id, 0) elif check_for_expected_input(message, negatives): next_question_message = "Ok so did you want any specific people to have been involved with the film?" bot.send_message(User.id, next_question_message) setUserContextAndStage(User.id, contexts['FilmSuggestion'], stages['filmSuggestion']['AskCrew']) db.removeQueryInfo(User.id, 3) else: db_query = db.getQueryInfo(User.id, 3) crew_names = [] if db_query: for result in db_query: crew_names.append( db.getCrewByID(result['Information'])['Name']) crew_names = format_query_info(crew_names) next_question_message = "Sorry I don't understand." if crew_names: next_question_message = next_question_message + " Did you want a film with {}?".format( crew_names) bot.send_message(User.id, next_question_message)
def confirm_genre_response(bot, message, User): """Stage 4 Response. Confirming the extracted genre information was correct. @param {Bot} bot @param {String} message @param {Person} User""" db_query = db.getQueryInfo(User.id, 2) genre = '' if db_query: genre = [query['Information'] for query in db_query] next_question_message = "Sorry I'm not sure I understand." if genre: next_question_message = next_question_message + " Did you want the film to be a {}".format( format_query_info(genre)) next_stage = 'ConfirmGenre' skipFlag = False if check_for_expected_input(message, skip): skipFlag = True if skipFlag or check_for_expected_input(message, positives): crew_query_info = db.getQueryInfo(User.id, 3) if crew_query_info: crewIDs = [crew['Information'] for crew in crew_query_info] crew_names = [] for crewID in crewIDs: crew_names.append(db.getCrewByID(crewID)['Name']) crew = format_query_info(crew_names) if skipFlag: next_question_message = "Ok, let's skip choosing a genre. From what you said earlier, you wanted {} to have been involved with the film?".format( crew) else: next_question_message = "Right, I'll note that down. From what you said earlier, you wanted {} to have been involved with the film?".format( crew) next_stage = 'ConfirmCrew' else: if skipFlag: next_question_message = "Ok let's skip choosing a genre. Are there any people you want to have been involved with the film?" else: next_question_message = "Right, I'll note that down. Are there any people you want to have been involved with the film?" next_stage = "AskCrew" elif check_for_expected_input(message, negatives): next_stage = 'AskGenre' next_question_message = "Ok, so are there any genres that you want the film to be of?" db.removeQueryInfo(User.id, 2) bot.send_message(User.id, next_question_message) setUserContextAndStage(User.id, contexts['FilmSuggestion'], stages['filmSuggestion'][next_stage])
def ask_film_response(bot, message, User): """Stage 3 Response. Asking if the user wants the suggested film to be similar to another film. @param {Bot} bot @param {String} message @param {Person} User""" skipFlag = False next_stage = 'AskFilm' next_question_message = "Sorry I don't understand. Do you want the film to be similar to another film you've seen?" if check_for_expected_input(message, skip): skipFlag = True if skipFlag or check_for_expected_input(message, negatives): genres_query_info = db.getQueryInfo(User.id, 2) if genres_query_info: genres = format_query_info( [genre['Information'] for genre in genres_query_info]) next_question_message = "Ok. So you want a film that is a {}?".format( genres) next_stage = 'ConfirmGenre' else: next_question_message = "Ok, Did you want the film to have any specific genres?" next_stage = 'AskGenre' elif check_for_expected_input(message, positives): next_question_message = "Ok, what film do you want it to be similar to?" else: films = extract_film(User.id, message) #In case the user just sends the film title and nothing else preprocessed_message = preprocess_text(message) db_query = search_for_film_in_db(preprocessed_message) if db_query: film = (db_query['FilmID'], db_query['Title']) if film not in films: films.append(film) db.insertQueryInformation(User.id, film[0], 1) if films: filmTitles = format_query_info([film[1] for film in films]) next_question_message = "So you want a film similar to {}?".format( filmTitles) next_stage = 'ConfirmFilm' bot.send_message(User.id, next_question_message) setUserContextAndStage(User.id, contexts['FilmSuggestion'], stages['filmSuggestion'][next_stage])
def ask_genre_response(bot, message, User): """Stage 5 Response. Asking the user if they want the suggested film to be of a certain genre. @param {Bot} bot @param {String} message @param {Person} User""" skipFlag = False next_stage = 'AskGenre' next_question_message = "Sorry I'm not sure what you meant, were there any genres you had in mind for the film I will suggest?" if check_for_expected_input(message, skip): skipFlag = True if skipFlag or check_for_expected_input(message, negatives): crew_query_info = db.getQueryInfo(User.id, 3) if crew_query_info: crewIDs = [crew['Information'] for crew in crew_query_info] crew_names = [] for crewID in crewIDs: crew_names.append(db.getCrewByID(crewID)['Name']) crew = format_query_info(crew_names) next_question_message = "ok so you want a film where {} was involved?".format( crew) next_stage = 'ConfirmCrew' else: next_question_message = "Ok so do you want a film with specific actors, directors or writers?" next_stage = 'AskCrew' elif check_for_expected_input(message, positives): next_question_message = "Ok, what genres did you have in mind?" else: genres = extract_genres(User.id, message) if genres: genres = format_query_info(genres) next_question_message = "So you want a film which is a {}?".format( genres) next_stage = 'ConfirmGenre' bot.send_message(User.id, next_question_message) setUserContextAndStage(User.id, contexts['FilmSuggestion'], stages['filmSuggestion'][next_stage])
def build_tailored_films_table(User): """Build a films table tailored to the user's requests @param {Person} User @returns {DataFrame} filmsTable @returns {String} requested_filmIDs @returns {List} requested_film_indices - List of Ints. Indicies of original requested films (for when there are multiple films requested)""" filmsTable = getFilmTable() query_info = getQueryInfo(User.id) requested_filmIDs = [] requested_genres = [] requested_crew = [] for result in query_info: if result['Type'] == 1: requested_filmIDs.append(result['Information']) elif result['Type'] == 2: requested_genres.append(result['Information']) elif result['Type'] == 3: requested_crew.append(result['Information']) # Removed any potential incorrect values requested_filmIDs = [ requested_filmID for requested_filmID in requested_filmIDs if requested_filmID.startswith("tt") ] # Store copy of requested Film IDs original_requested_filmIDs = requested_filmIDs # Combine requested films metadata if requested_filmIDs: if len(requested_filmIDs) > 1: requested_filmIDs, filmsTable = create_combined_film( requested_FilmIDs, filmsTable) else: requested_filmIDs = ''.join(requested_filmIDs) # No Film Selected if not requested_filmIDs: requested_filmIDs = get_generated_requested_filmID(User.id, filmsTable) original_requested_filmIDs = [requested_filmIDs] # No Genres Selected if not requested_genres: if User.favouriteGenre: requested_genres.append(User.favouriteGenre) if User.secondFavouriteGenre: requested_genres.append(User.secondFavouriteGenre) if User.thirdFavouriteGenre: requested_genres.append(User.thirdFavouriteGenre) #TODO (Future) - Check for exact film requirements # exactFilmsTable = filmsTable[( # ((filmsTable['Writers'].apply(lambda writers: find_films_with_user_query(requested_crew, writers) if requested_crew else True) | # filmsTable['Directors'].apply(lambda directors: find_films_with_user_query(requested_crew, directors) if requested_crew else True) | # filmsTable['CrewID'].apply(lambda crew: find_films_with_user_query(requested_crew, crew) if requested_crew else True)) & # filmsTable['Genres'].apply(lambda genre: find_films_with_all_genres(requested_genres, genre) if requested_genres else True)) & # (filmsTable['isAdult'].apply(lambda isAdult: False if (User.age < 18 and isAdult == 1) else True)) # )] # if exactFilmsTable.shape[0] != 0: # return (0, exactFilmsTable['Title']) # differentGenresFilmTable = filmsTable[( # ((filmsTable['Writers'].apply(lambda writers: find_films_with_user_query(requested_crew, writers) if requested_crew else True) | # filmsTable['Directors'].apply(lambda directors: find_films_with_user_query(requested_crew, directors) if requested_crew else True) | # filmsTable['CrewID'].apply(lambda crew: find_films_with_user_query(requested_crew, crew) if requested_crew else True)) & # filmsTable['Genres'].apply(lambda genre: find_films_with_user_query(requested_genres, genre) if requested_genres else True)) & # )] # (filmsTable['isAdult'].apply(lambda isAdult: False if (User.age < 18 and isAdult == 1) else True)) filmsTable = filmsTable[( (filmsTable['Writers'].apply( lambda writers: find_films_with_user_query(requested_crew, writers) if requested_crew else True) | filmsTable['Directors'].apply( lambda directors: find_films_with_user_query( requested_crew, directors) if requested_crew else True) | filmsTable['CrewID'].apply(lambda crew: find_films_with_user_query( requested_crew, crew) if requested_crew else True) | filmsTable['Genres'].apply(lambda genre: find_films_with_user_query( requested_genres, genre) if requested_genres else True)) & (filmsTable['isAdult'].apply(lambda isAdult: False if ( User.age < 18 and isAdult == 1) else True)) | (filmsTable['FilmID'] == requested_filmIDs))] # Store the requested film data requested_film = filmsTable[(filmsTable['FilmID'] == requested_filmIDs)] if filmsTable.shape[0] > 2000: filmsTable = sort_by_highest_imdb_score(filmsTable)[:2000] # Check whether we removed the requested film from the table when reducing the size if filmsTable[(filmsTable['FilmID'] == requested_filmIDs)].empty: filmsTable = pd.concat([filmsTable, requested_film]) filmsTable = filmsTable.reset_index(drop=True) requested_film_indices = [ int(filmsTable[(filmsTable['FilmID'] == filmID)].index.values) for filmID in original_requested_filmIDs ] return filmsTable, requested_filmIDs, requested_film_indices