def getTopDerivation(sentence, languageProcessor, grammar): # Return (action, formula) # - action is either |tell| or |ask| # - formula is a logical form print print '>>>', sentence utterance = nlparser.Utterance(sentence, languageProcessor) print 'Utterance:', utterance derivations = nlparser.parseUtterance(utterance, grammar, verbose=0) if not derivations: raise Exception('Error: Parsing failed. (0 derivations)') return derivations[0].form
def process(self, line): line = line.strip() if line.startswith('#') or line == '': return utterance = nlparser.Utterance(line, self.processor) if self.verbose >= 1: print(('Shallow analysis:', utterance)) # Handle special commands if utterance.tokens[0] == 'help': print("Commands:") print(" help: print this message") print(" verbose <int>: set verbosity level") print(" forget: clear the knowledge base") print(" dump: display contents of knowledge base") print(" <any natural language utterance>") return if utterance.tokens[0] == 'verbose': self.verbose = int(utterance.tokens[1]) return if utterance.tokens[0] == 'forget': print("Starting from a blank slate.") self.initKB() return if utterance.tokens[0] == 'dump': self.kb.dump() return # Semantic parsing derivations = nlparser.parseUtterance(utterance, self.rules, verbose=self.verbose) if len(derivations) == 0: print("Sorry, I could not parse that.") return deriv = derivations[0] mode, form = deriv.form if self.verbose >= 1: print(('Parsed logical formula: %s[%s]' % (mode, form))) # Query the knowledge base self.kb.verbose = opts.verbose if mode == 'ask': response = self.kb.ask(form) if mode == 'tell': response = self.kb.tell(form) print() logic.showKBResponse(response, self.verbose)