def handle(self): prep = Preprocessor().parse(self.rfile.read()) preprocessed = prep.strip('\n') resp = nl.yacc.parse(preprocessed.strip('\n')) if preprocessed.endswith('.'): nl.extend() nl.now() self.wfile.write(resp)
def run_npl(fname): # open file, read lines # tell asserions # compare return of questions with provided output with open(fname) as f: prep = Preprocessor().parse(f.read()) resp, buff = None, '' for sen in prep.split('\n'): logger.info(sen) sen = sen.strip('\n ') if resp is not None: sen = sen.strip('.') logger.info('%s match %s' % (sen, resp)) assert re.compile(sen).match(resp) resp = None elif sen and not sen.startswith('#'): buff += ' ' + sen if buff.endswith('.'): logger.info(nl.yacc.parse(buff)) buff = '' elif buff.endswith('?'): resp = nl.yacc.parse(buff) buff = ''
def p_passtime(p): """order : PASSTIME DOT | NOW DOT""" response = nl.now() p[0] = str(response) def p_import(p): "order : IMPORT URI DOT" uri = p[2][1:-1] try: remote = urllib2.urlopen(uri) except Exception, e: raise ImportError("Could not import %s. " "Reason: %s" % (uri, str(e))) preprocessed = Preprocessor().parse(remote.read()) buff = "" for sen in preprocessed.readlines(): sen = sen.strip() if sen and not sen.startswith("#"): buff += " " + sen if buff.endswith("."): yacc.parse(buff) buff = "" p[0] = "Contents of %s imported" % uri def p_question(p): """question : fact QMARK | definition QMARK""" response = nl.kb.ask_obj(p[1])