def add_response(input_str): call = process_text.stripgrammar(input_str.split('"')[1]) story_split = input_str.split('"')[3] story_split = re.split('<|>', story_split) for curr_string in range(len(story_split)): if '|' in story_split[curr_string]: story_split[curr_string] = story_split[curr_string].split('|') return call, story_split
def get_response(self, message, server): """ Query the response index and return the appropriate response. If there is none, return an empty string. Params: message - the message, a string server - a string identifying the server the message is from """ if server not in self.index.keys(): return "" split_msg = process_text.stripgrammar(message).split( " ") # ignore grammar # final: custom graham modules for module in self.modules: for call, response in self.index[server][module.__name__].items(): if set(process_text.stripgrammar(call).split(" ")).issubset( set(split_msg)): self.decay_response(server, module.__name__, call) return module.get_response(message.lower(), call, response) return ""
def splice_word(message, call): vowels = ["a", "e", "i", "o", "u"] # for a given message, return a list of indices of vowels def get_vowel_index(message): vowel_indices = [] for letter_i in range(len(message)): if message[letter_i] in vowels: vowel_indices.append(letter_i) return vowel_indices # check if word is in message word_index = -1 message_stripped = process_text.stripgrammar(message).split(" ") for word in range(len(message_stripped)): if message_stripped[word] == call: word_index = word # return an empty string if not if (word_index == len(message_stripped)-1) or word_index == -1: return "" # cludge words together first_word = message_stripped[word_index] second_word = message_stripped[word_index+1] first_vowel_indices = get_vowel_index(first_word) second_vowel_indices = get_vowel_index(second_word) # if there are no vowels, do nothing if len(first_vowel_indices) == 0 or len(second_vowel_indices) == 0: return "" first_word_altered = first_word[:first_vowel_indices[0]] second_word_altered = second_word[second_vowel_indices[0]:] replacement = first_word_altered+second_word_altered return replacement
def add_response(input_str): call = process_text.stripgrammar(input_str.split('"')[1]) response = input_str.split('"')[3] return call, response
def add_response(self, message, server): ''' Append a response to the index. The syntax is as follows: ~respond "call" "response" ~permute "original" "corrected" Saves the updated index to the file "call_response_index.json", "w" as well as updating self.index. Params: message - the message, a string server - a string identifying the server the message is from ''' # note: normally a generic try\except is bad but it's better for this # to respond with the error than to just crash or whatever return_msg = "did nothing" if message.split(" ")[0] == "~graham-help": return 'Curent server name: "' + server + '"\n\n' + self.help_msg # handle errors num_delimiters = 4 if server == "": server = message.split('"')[5] num_delimiters += 2 if message.count('"') != num_delimiters and not message.startswith( "~graham-help"): return 'Error: got too many " marks or " in the wrong place. Note: as " are part of the graham syntax, they are forbidden in messages.' if len(process_text.stripgrammar(message.split('"')[1])) < 3: return "Error: call phrase is less than three characters long. Make it longer!" #formulate response try: if server not in self.index.keys(): # create new server self.index[server] = { "respond": {}, "permute": {}, "generate": {} } for module in self.modules: self.index[server][module.add_response_syntax.split("-") [1]] = {} for module in self.modules: if message.split(" ")[0] == "~" + module.add_response_syntax: call, response, = module.add_response('"'.join( message.split('"')[:-1])) if module.__name__ not in self.index[server]: self.index[server][module.__name__] = {} self.index[server][module.__name__][call] = response return_msg = module.return_msg except Exception as e: return "could not parse request. errror: " + repr((e)) #update index on hdd with open(self.profile_path, "w") as write_file: json.dump(self.index, write_file) return return_msg