def antonym(inp): simlified_inp = read.simplify(inp) simlified_word_list = simlified_inp.split(" ") word = simlified_word_list[0] keywords = ["of", "antonym", "antonyms", "word"] for i in range(len(keywords)): if keywords[i] in simlified_word_list: index = simlified_word_list.index(keywords[i]) + 1 try: word = simlified_word_list[index] break except: break try: definition_dict = dictionary.antonym(word) response = "Antonyms of " + word + " are " response += ", ".join(definition_dict) except: response = "I couldn't find antonyms for " + word + "." return (response)
def define(inp): simlified_inp = read.simplify(inp) simlified_word_list = simlified_inp.split(" ") word = simlified_word_list[0] keywords = ["def", "define", "of", "word", "definition"] for i in range(len(keywords)): if keywords[i] in simlified_word_list: index = simlified_word_list.index(keywords[i]) + 1 try: word = simlified_word_list[index] break except: break try: definition_dict = dictionary.meaning(word) response = "The word " + word + " means " for key in definition_dict: definition_list = definition_dict[key] response += "as a " + key + " " response += ", ".join(definition_list) response += "; " except: response = "I couldn't find " + word + "." return (response)
def rank_words(self, lines): updated_list = [] for word in self.word_list: #simlify the word word = read.simplify(word) #occurrences in this string occur_here = self.simplified_word_list.count(word) #number of total lines total_lines = len(lines) #total number of lines with this word lines_with_word = 0 #placeholder #update placeholder value for key in lines: line = lines[key] if word in line.simplified_word_list: lines_with_word += 1 #put it all together updated_list.append(occur_here * 10 * math.log(total_lines / lines_with_word)) self.word_rank_list = updated_list
def rank_lines(inp): simlified_input = read.simplify(inp) input_simlified_word_list = simlified_input.split(" ") #rank all responses for key in lines: line = lines[key] #the words in the input and in the line intersection_strings = [] intersection = [] #reset current rank line.current_rank = 0 for i in range(len(line.simplified_word_list)): word = line.simplified_word_list[i] rank = line.word_rank_list[i] if (word in input_simlified_word_list): intersection_strings.append(word) intersection.append(rank) #combine rank_to_be = 1 for i in intersection: rank_to_be = rank_to_be * i #extra weight towards percentage of overlap dilute = 1 #take away certain power from the overlap function sum_len = (len(line.simplified_word_list) + len(input_simlified_word_list)) rank_to_be = rank_to_be * (len(intersection) / sum_len) line.current_rank = rank_to_be
def respond_to(inp): simplified_inp = read.simplify(inp) rank_lines(inp) #pick highest ranking response highest = 0 highest_obj = [] for key in lines: line = lines[key] if line.current_rank > highest: highest = line.current_rank highest_obj = [line] elif line.current_rank == highest: highest_obj.append(line) #follow lines to choose from followline_choice = [] for obj in highest_obj: followline_choice += obj.follow_lines response_obj = random.choice(followline_choice) response_string = response_obj["string"] #check for special responses if response_obj["indicator"] == "///": response_string = special_responses.functions[response_string](inp) userdata["conversation"].append(inp) userdata["conversation"].append(response_string) #write context back to json read.write_json(context, context_file_name) return (response_string)
def area(inp): simlified = read.simplify(inp) two_dim = [ "square", "triangle", "circle", "rectangle", "rect", "tri", "radius", "diameter" ] word_list = two_dim numbers = find_nums(inp) key_words = [] for i in word_list: if i in simlified: key_words.append(i) try: if "square" in key_words or "rectangle" in key_words or "rect" in key_words: area = numbers[0] * numbers[1] return (str(area) + ": area of rectangle b/h " + str(numbers[0]) + "/" + str(numbers[1])) if "triangle" in key_words or "tri" in key_words: area = numbers[0] * numbers[1] * 0.5 return (str(area) + ": area of triangle b/h " + str(numbers[0]) + "/" + str(numbers[1])) if "circle" in key_words: radius = numbers[0] pi = math.pi if "diameter" in key_words: #account for radius and diameter options radius = radius / 2 if 3.14 in numbers: #account for more standard version of pi pi = 3.14 unrounded_area_string = str(radius**2) + " * pi" rounded_area_string = str(pi * radius * radius) return (rounded_area_string + ", " + unrounded_area_string + ": area of circle " + str(pi) + " as pi and radius " + str(radius)) except: return ( "Did you want to find area? Specify the shape and provide two numbers." )