def _learn(self): """ Begins the learning process. Asks the user what part of speech the word is, and keeps track of the remaining words which we still need to learn about """ name = self.name unknown_all = list(self.memory.pop("topic"))[1] if len(unknown_all) > 0: unknown = unknown_all[0] del unknown_all[0] pos = ["Noun", \ "Plural Noun", \ "Adjective", \ "Adverb", \ "Transitive Verb", \ "Intransitive Verb", \ "Preposition", \ "Other"] self.bot.send("Which of the following parts of speech is \'" + \ unknown + "\'?\n" + print_list(pos), name) self.memory.push("topic", unknown_all) self.memory.push("topic", unknown) self.memory.pop("status") self.memory.push("status", "pos_first") return None else: self.memory.pop("status") return "reset"
def _topic(self, top, d=None, k=None, ques_word=None): if d == None: d = self.topics subtop = None nouns = set() n = find_noun(top) while n: noun = " ".join(n.leaves()) nouns.add(noun) n = find_noun(top, nouns) nouns = list(nouns) if "me" in nouns: nouns.remove("me") if "you" in nouns: nouns.remove("you") print "Nouns: " + str(nouns) ans = None for topic in nouns: for subtopic in nouns: # check to see if topic is a key in the knowledge # dictionary, and that the the entry corresponding # to topic is also a dictionary if d.has_key(topic) and isinstance(d[topic], dict): # if subtopic is a key in the entry corresponding # to topic, then set the subtopic entry as the answer. if d[topic].has_key(subtopic): print "TOPIC:", topic print "SUBTOPIC:", subtopic ans = d[topic][subtopic] # check to see if the current topic stored in memory is # the same as the topic we found. elif topic == k: # is the subtopic we found a key in the dictionary? # if so, set it's entry as the anser. if d.has_key(subtopic): print "TOPIC:", topic print "SUBTOPIC:", subtopic ans = d[subtopic] if not ans: for topic in nouns: # if the topic is a key in the dictionary if d.has_key(topic): # if the entry matching topic is a dictionary, then # we should ask what subtopic they want to know about if isinstance(d[topic], dict): print "TOPIC:", topic ans = d[topic]['default'] + "\n" + \ "Multiple keywords match your query. " + \ "What did you mean to ask about?\n\n" + \ print_list(d[topic].keys()) self.memory.push("topic", topic) self.memory.push("data", d[topic]) # otherwise, just give them the entry that corresponds # to topic else: print "TOPIC:", topic ans = d[topic] # if the topic we found is the same as the topic in # memory, then ask (again) which subtopic they # want to ask about elif topic == k: print "TOPIC:", topic ans = d['default'] + "\n" + \ "Multiple keywords match your query. " + \ "What did you mean to ask about?\n\n" + \ print_list(d.keys()) if not ans: if nouns == [] and ques_word == "what": print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) elif "what you know" in nouns or \ "knowledge" in nouns: print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) else: l = nouns.pop(0) if len(nouns) > 0: for n in xrange(len(nouns)-1): l += ", " + nouns[n] l += ", or " + nouns[-1] ans = "Sorry, I don't know about " + l + "." return ans
def _AI(self, mess, d = None, k = None): """ Parses the message, and attempts to locate a topic. If it is able to find a topic, it tells the user about the topic, otherwise it prints a message saying that it can't parse the sentence, or it doesn't know about the topic. """ # make sure the dictionary is set to something if d == None: d = self.topics # parse the sentence, and print the parse parse = self.parser.parse_sent(mess) print "PARSE:\n", parse ans = None # if the parse is returned as a tuple, then we know # that the parse failed. if isinstance(parse, tuple): # if the second value in the tuple is valid, then there were # words that were not in the grammar. Tell the user about the # words, and then enter a function to learn the foreign words. if parse[1]: self.bot.send("Sorry, I don't understand the following words: " + \ ", ".join(parse[1]) + ".", self.name) self.memory.push("topic", list(parse[1])) return self._learn() # otherwise, we just couldn't parse the sentence else: parse = self.parser.parse_NP(mess) print "NP PARSE:\n", parse if parse: ans = self._topic(parse, d=d, k=k) else: ans = "Sorry, I couldn't parse what you just said." # otherwise, the parse succeeded else: # find the sentence type: STATEMENT, QUESTION, or COMMAND type = get_sentence_type(parse) print "TYPE: " + str(type) # based on the sentence type, find the topic of the sentence. # we don't yet know what the subtopic is, so just set it to # None. top = find_topic(parse, type) print top # if the sentence is a question and find_topic() found a # topic, then top is a tuple, and we need to store the # parts separately. ques_word = None if type == QUESTION and top: ques_word = top[1] top = top[0] # if a topic was found, then we want to look for a # prepositional phrase. For example, we want to be able # to get TOPIC=emacs, SUBTOPIC=keys from "keys in emacs" if top: ans = self._topic(top, d=d, k=k, ques_word=ques_word) # otherwise, we couldn't find a topic from the sentence, so # tell them so else: if type == QUESTION and ques_word == "what": print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) else: print "TOPIC: None found" ans = "Sorry, I couldn't determine the topic of what you are asking me." # print the answer out to the terminal, and send the answer # to the user. print ans self.bot.send(ans, self.name)
queue[r - 1] = cursor cursor = cursor.next if r == 0: for i in range(1, k): queue[k - i].next = queue[k - i - 1] else: if tail: tail.next = queue[k - 1] tail = queue[0] queue[r] = None else: if tail: tail.next = queue[0] return new_head from helper import array_2_list from helper import print_list s = Solution() raw = [1, 2, 3, 4, 5] case = array_2_list(raw) r = s.reverseKGroup(case, 3) print_list(r)
def _topic(self, top, d=None, k=None, ques_word=None): if d == None: d = self.topics subtop = None nouns = set() n = find_noun(top) while n: noun = " ".join(n.leaves()) nouns.add(noun) n = find_noun(top, nouns) nouns = list(nouns) if "me" in nouns: nouns.remove("me") if "you" in nouns: nouns.remove("you") print "Nouns: " + str(nouns) ans = None for topic in nouns: for subtopic in nouns: # check to see if topic is a key in the knowledge # dictionary, and that the the entry corresponding # to topic is also a dictionary if d.has_key(topic) and isinstance(d[topic], dict): # if subtopic is a key in the entry corresponding # to topic, then set the subtopic entry as the answer. if d[topic].has_key(subtopic): print "TOPIC:", topic print "SUBTOPIC:", subtopic ans = d[topic][subtopic] # check to see if the current topic stored in memory is # the same as the topic we found. elif topic == k: # is the subtopic we found a key in the dictionary? # if so, set it's entry as the anser. if d.has_key(subtopic): print "TOPIC:", topic print "SUBTOPIC:", subtopic ans = d[subtopic] if not ans: for topic in nouns: # if the topic is a key in the dictionary if d.has_key(topic): # if the entry matching topic is a dictionary, then # we should ask what subtopic they want to know about if isinstance(d[topic], dict): print "TOPIC:", topic ans = d[topic]['default'] + "\n" + \ "Multiple keywords match your query. " + \ "What did you mean to ask about?\n\n" + \ print_list(d[topic].keys()) self.memory.push("topic", topic) self.memory.push("data", d[topic]) # otherwise, just give them the entry that corresponds # to topic else: print "TOPIC:", topic ans = d[topic] # if the topic we found is the same as the topic in # memory, then ask (again) which subtopic they # want to ask about elif topic == k: print "TOPIC:", topic ans = d['default'] + "\n" + \ "Multiple keywords match your query. " + \ "What did you mean to ask about?\n\n" + \ print_list(d.keys()) if not ans: if nouns == [] and ques_word == "what": print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) elif "what you know" in nouns or \ "knowledge" in nouns: print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) else: l = nouns.pop(0) if len(nouns) > 0: for n in xrange(len(nouns) - 1): l += ", " + nouns[n] l += ", or " + nouns[-1] ans = "Sorry, I don't know about " + l + "." return ans
def _AI(self, mess, d=None, k=None): """ Parses the message, and attempts to locate a topic. If it is able to find a topic, it tells the user about the topic, otherwise it prints a message saying that it can't parse the sentence, or it doesn't know about the topic. """ # make sure the dictionary is set to something if d == None: d = self.topics # parse the sentence, and print the parse parse = self.parser.parse_sent(mess) print "PARSE:\n", parse ans = None # if the parse is returned as a tuple, then we know # that the parse failed. if isinstance(parse, tuple): # if the second value in the tuple is valid, then there were # words that were not in the grammar. Tell the user about the # words, and then enter a function to learn the foreign words. if parse[1]: self.bot.send("Sorry, I don't understand the following words: " + \ ", ".join(parse[1]) + ".", self.name) self.memory.push("topic", list(parse[1])) return self._learn() # otherwise, we just couldn't parse the sentence else: parse = self.parser.parse_NP(mess) print "NP PARSE:\n", parse if parse: ans = self._topic(parse, d=d, k=k) else: ans = "Sorry, I couldn't parse what you just said." # otherwise, the parse succeeded else: # find the sentence type: STATEMENT, QUESTION, or COMMAND type = get_sentence_type(parse) print "TYPE: " + str(type) # based on the sentence type, find the topic of the sentence. # we don't yet know what the subtopic is, so just set it to # None. top = find_topic(parse, type) print top # if the sentence is a question and find_topic() found a # topic, then top is a tuple, and we need to store the # parts separately. ques_word = None if type == QUESTION and top: ques_word = top[1] top = top[0] # if a topic was found, then we want to look for a # prepositional phrase. For example, we want to be able # to get TOPIC=emacs, SUBTOPIC=keys from "keys in emacs" if top: ans = self._topic(top, d=d, k=k, ques_word=ques_word) # otherwise, we couldn't find a topic from the sentence, so # tell them so else: if type == QUESTION and ques_word == "what": print "TOPIC: knowledge" ans = "I know about:\n" + print_list(self.topics.keys()) else: print "TOPIC: None found" ans = "Sorry, I couldn't determine the topic of what you are asking me." # print the answer out to the terminal, and send the answer # to the user. print ans self.bot.send(ans, self.name)