Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Movie-ChatBot/Searchmoviewithtitles.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (48 sloc)
2.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import math | |
import re | |
import sys | |
from random import randint | |
def movie_title_str(self, search_str): | |
matches = [] | |
min_edit_length = float('inf') | |
for movie_index in xrange(0, len(self.titles)): | |
movie = self.titles[movie_index] | |
# rearrange title to put articles in front | |
title = str.lower(self.rearrange_articles(movie[0])) | |
if search_str == title: | |
return movie_index | |
else: | |
if search_str in title: # check if search_str is a substring of title | |
edit_length = self.edit_length(title, search_str) | |
if edit_length < min_edit_length: | |
matches = [movie_index] + matches | |
if len(matches) > 10: | |
matches = matches[0:10] | |
min_edit_length = edit_length | |
else: | |
if len(matches) < 10: | |
matches.append(movie_index) | |
if len(matches) > 1: | |
index = -1 | |
for i in xrange(0, len(matches)): | |
print ("str(i) + ': ' + self.titles[matches[i]][0] + '\n'") | |
while index < 0 or index > len(matches): | |
input_str = raw_input('Did you mean one of these movies ? Please select the index of the movie you meant + \ or enter "none" if none of them are what you are looking for: ') | |
if input_str == "none" | |
return -2 | |
try: | |
index = int(input_str) | |
if index < 0 or index > len(matches): | |
print ("Please enter \'none\' or a valid number from the above indices.") | |
except ValueError: | |
print ("Please enter \'none\' or a valid number from the above indices.") | |
except NameError: | |
print ("Please enter \'none\' or a valid number from the above indices.") | |
except SyntaxError: | |
print ("Please enter \'none\' or a valid number from the above indices.") | |
return matches[index] | |
elif len(matches) == 1: | |
return matches[0] | |
else: | |
return -1 |