diff --git a/Searchmoviewithtitles.py b/Searchmoviewithtitles.py new file mode 100644 index 0000000..0ba3ae4 --- /dev/null +++ b/Searchmoviewithtitles.py @@ -0,0 +1,57 @@ +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 +