def builtin_say_eoa(g, pe): """ say_eoa ( +Lang, +Str, [+Score] ) """ pe._trace('CALLED BUILTIN say_eoa', g) pred = g.terms[g.inx] args = pred.args if len(args) < 2: raise PrologRuntimeError('say_eoa: at least 2 args expected.') if len(args) > 3: raise PrologRuntimeError('say_eoa: max 3 args expected.') arg_L = pe.prolog_eval(args[0], g.env).name arg_S = pe.prolog_get_string(args[1], g.env) _queue_action(g, [Predicate('say'), arg_L, arg_S]) score = 0.0 if len(args) > 2: score = pe.prolog_get_float(args[2], g.env) _eoa(g, pe, score) return True
def builtin_tokenize(g, pe): """ tokenize (+Lang, +Str, -Tokens) """ pe._trace('CALLED BUILTIN tokenize', g) pred = g.terms[g.inx] args = pred.args if len(args) != 3: raise PrologRuntimeError('tokenize: 3 args expected.', g.location) arg_lang = pe.prolog_eval(args[0], g.env, g.location) if not isinstance(arg_lang, Predicate) or len(arg_lang.args) > 0: raise PrologRuntimeError( 'tokenize: first argument: constant expected, %s found instead.' % repr(args[0]), g.location) arg_str = pe.prolog_get_string(args[1], g.env, g.location) arg_tokens = pe.prolog_get_variable(args[2], g.env, g.location) tokens = list( map(lambda s: StringLiteral(s), tokenize(arg_str, lang=arg_lang.name))) g.env[arg_tokens] = ListLiteral(tokens) return True
def _eoa(g, pe, score): if not (ACTION_VARNAME in g.env): raise PrologRuntimeError('eoa: no action defined.') pe.end_action(g.env[ACTION_VARNAME], score) del g.env[ACTION_VARNAME]
def _prolog_from_json(o): if o['pt'] == 'Constant': return Predicate(o['name']) if o['pt'] == 'StringLiteral': return StringLiteral(o['s']) if o['pt'] == 'NumberLiteral': return NumberLiteral(o['f']) if o['pt'] == 'ListLiteral': return ListLiteral(o['l']) raise PrologRuntimeError('cannot convert from json: %s .' % repr(o))
def builtin_r_sayv(g, pe): """" r_sayv (+Context, +Var, +Fmt) """ pe._trace('CALLED BUILTIN r_sayv', g) pred = g.terms[g.inx] args = pred.args if len(args) != 3: raise PrologRuntimeError( 'r_sayv: 3 args (+Context, +Var, +Fmt) expected.', g.location) arg_context = pe.prolog_eval(args[0], g.env, g.location) arg_var = pe.prolog_eval(args[1], g.env, g.location) arg_fmt = pe.prolog_get_constant(args[2], g.env, g.location) if not isinstance(arg_var, Literal): raise PrologRuntimeError( u'r_sayv: failed to eval "%s"' % unicode(args[1]), g.location) # import pdb; pdb.set_trace() res = {} if isinstance(arg_var, StringLiteral): v = arg_var.s else: v = unicode(arg_var) if arg_fmt == 'd': v = unicode(int(float(v))) elif arg_fmt == 'f': v = unicode(float(v)) res = do_assertz(g.env, Clause(Predicate( 'c_say', [arg_context, StringLiteral(v)]), location=g.location), res=res) return [res]
def builtin_action_context_push(pe, args): """ context_push(+Name, +Value) """ logging.debug('CALLED BUILTIN ACTION context_push %s' % repr(args)) if len(args) != 2: raise PrologRuntimeError('context_push: 2 args expected.') key = args[0].name value = args[1] # print u"builtin_set_context: %s -> %s" % (key, unicode(value)) pe.push_context(key, value)
def builtin_uriref(g, pe): pe._trace('CALLED BUILTIN uriref', g) pred = g.terms[g.inx] args = pred.args if len(args) != 2: raise PrologRuntimeError('uriref: 2 args expected.') if not isinstance(args[0], Predicate): raise PrologRuntimeError( 'uriref: first argument: predicate expected, %s found instead.' % repr(args[0])) if not isinstance(args[1], Variable): raise PrologRuntimeError( 'uriref: second argument: variable expected, %s found instead.' % repr(args[1])) g.env[args[1].name] = StringLiteral( pe.kb.resolve_aliases_prefixes(args[0].name)) return True
def builtin_context_get_fn(pred, env, rt): """ context_get(+Name) """ rt._trace_fn('CALLED FUNCTION context_get', g) args = pred.args if len(args) != 1: raise PrologRuntimeError('context_get: 1 arg expected.') key = args[0].name v = pe.read_context(key) if not v: return ListLiteral([]) return v
def builtin_say(g, pe): """ say ( +Lang, +Str ) """ pe._trace('CALLED BUILTIN say', g) pred = g.terms[g.inx] args = pred.args if len(args) != 2: raise PrologRuntimeError('say: 2 args expected.') arg_L = pe.prolog_eval(args[0], g.env).name arg_S = pe.prolog_get_string(args[1], g.env) _queue_action(g, [Predicate('say'), arg_L, arg_S]) return True
def builtin_edit_distance(g, pe): """" edit_distance (+Tokens1, +Tokens2, -Distance) """ pe._trace('CALLED BUILTIN edit_distance', g) pred = g.terms[g.inx] args = pred.args if len(args) != 3: raise PrologRuntimeError('edit_distance: 3 args expected.', g.location) arg_tok1 = pe.prolog_get_list(args[0], g.env, g.location) arg_tok2 = pe.prolog_get_list(args[1], g.env, g.location) arg_dist = pe.prolog_get_variable(args[2], g.env, g.location) g.env[arg_dist] = NumberLiteral(edit_distance(arg_tok1.l, arg_tok2.l)) return True
def builtin_eoa(g, pe): """ eoa ( [+Score] ) """ pe._trace('CALLED BUILTIN eoa', g) pred = g.terms[g.inx] args = pred.args if len(args) > 1: raise PrologRuntimeError('eoa: max 1 arg expected.') score = 0.0 if len(args) > 0: score = pe.prolog_get_float(args[0], g.env) _eoa(g, pe, score) return True
def builtin_action_rdf_assert(pe, args): """ rdf_assert (+S, +P, +O) """ logging.debug('CALLED BUILTIN ACTION rdf_assert %s' % repr(args)) if len(args) != 3: raise PrologRuntimeError('rdf_assert: 3 args expected, got %d args' % len(args)) arg_s = args[0] arg_p = args[1] arg_o = args[2] quads = [(pl_to_rdf(arg_s, {}, pe, {}, pe.kb), pl_to_rdf(arg_p, {}, pe, {}, pe.kb), pl_to_rdf(arg_o, {}, pe, {}, pe.kb), pe.context_gn)] pe.kb.addN(quads)
def builtin_context_get(g, pe): """ context_get(+Name, -Value) """ pe._trace('CALLED BUILTIN context_get', g) pred = g.terms[g.inx] args = pred.args if len(args) != 2: raise PrologRuntimeError('context_get: 2 args expected.') key = args[0].name arg_v = pe.prolog_get_variable(args[1], g.env) v = pe.read_context(key) if not v: # import pdb; pdb.set_trace() return False g.env[arg_v] = v return True
def rdf_to_pl(l): value = unicode(l) if isinstance(l, rdflib.Literal): if l.datatype: datatype = str(l.datatype) if datatype == 'http://www.w3.org/2001/XMLSchema#decimal': value = NumberLiteral(float(value)) elif datatype == 'http://www.w3.org/2001/XMLSchema#float': value = NumberLiteral(float(value)) elif datatype == 'http://www.w3.org/2001/XMLSchema#integer': value = NumberLiteral(float(value)) elif datatype == 'http://www.w3.org/2001/XMLSchema#dateTime': dt = dateutil.parser.parse(value) value = NumberLiteral(time.mktime(dt.timetuple())) elif datatype == 'http://www.w3.org/2001/XMLSchema#date': dt = dateutil.parser.parse(value) value = NumberLiteral(time.mktime(dt.timetuple())) elif datatype == DT_LIST: value = json.JSONDecoder( object_hook=_prolog_from_json).decode(value) elif datatype == DT_CONSTANT: value = Predicate(value) else: raise PrologRuntimeError( 'sparql_query: unknown datatype %s .' % datatype) else: if l.value is None: value = ListLiteral([]) else: value = StringLiteral(value) else: value = StringLiteral(value) return value
def builtin_r_say(g, pe): """" r_say (+Context, +Token) """ pe._trace('CALLED BUILTIN r_say', g) pred = g.terms[g.inx] args = pred.args if len(args) != 2: raise PrologRuntimeError('r_say: 2 args (+Context, +Token) expected.', g.location) arg_context = pe.prolog_eval(args[0], g.env, g.location) arg_token = pe.prolog_eval(args[1], g.env, g.location) # import pdb; pdb.set_trace() res = {} res = do_assertz(g.env, Clause(Predicate('c_say', [arg_context, arg_token]), location=g.location), res=res) return [res]
def _rdf_exec(g, pe, generate_lists=False): # rdflib.plugins.sparql.parserutils.CompValue # # class CompValue(OrderedDict): # def __init__(self, name, **values): # # SelectQuery( # p = # Project( # p = # LeftJoin( # p2 = # BGP( # triples = [(rdflib.term.Variable(u'leaderobj'), rdflib.term.URIRef(u'http://dbpedia.org/ontology/leader'), rdflib.term.Variable(u'leader'))] # _vars = set([rdflib.term.Variable(u'leaderobj'), rdflib.term.Variable(u'leader')]) # ) # expr = # TrueFilter( # _vars = set([]) # ) # p1 = # BGP( # triples = [(rdflib.term.Variable(u'leader'), rdflib.term.URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef(u'http://schema.org/Person')), (rdflib.term.Variable(u'leader'), rdflib.term.URIRef(u'http://www.w3.org/2000/01/rdf-schema#label'), rdflib.term.Variable(u'label'))] # _vars = set([rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leader')]) # ) # _vars = set([rdflib.term.Variable(u'leaderobj'), rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leader')]) # ) # PV = [rdflib.term.Variable(u'leader'), rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leaderobj')] # _vars = set([rdflib.term.Variable(u'leaderobj'), rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leader')]) # ) # datasetClause = None # PV = [rdflib.term.Variable(u'leader'), rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leaderobj')] # _vars = set([rdflib.term.Variable(u'leaderobj'), rdflib.term.Variable(u'label'), rdflib.term.Variable(u'leader')]) # ) pred = g.terms[g.inx] args = pred.args # if len(args) == 0 or len(args) % 3 != 0: # raise PrologRuntimeError('rdf: one or more argument triple(s) expected, got %d args' % len(args)) distinct = False triples = [] optional_triples = [] filters = [] limit = 0 offset = 0 arg_idx = 0 var_map = {} # string -> rdflib.term.Variable while arg_idx < len(args): arg_s = args[arg_idx] # check for optional structure if isinstance(arg_s, Predicate) and arg_s.name == 'optional': s_args = arg_s.args if len(s_args) != 3: raise PrologRuntimeError('rdf: optional: triple arg expected') arg_s = s_args[0] arg_p = s_args[1] arg_o = s_args[2] logging.debug('rdf: optional arg triple: %s' % repr( (arg_s, arg_p, arg_o))) optional_triples.append( (pl_to_rdf(arg_s, g.env, pe, var_map, pe.kb), pl_to_rdf(arg_p, g.env, pe, var_map, pe.kb), pl_to_rdf(arg_o, g.env, pe, var_map, pe.kb))) arg_idx += 1 # check for filter structure elif isinstance(arg_s, Predicate) and arg_s.name == 'filter': logging.debug('rdf: filter structure detected: %s' % repr(arg_s.args)) s_args = arg_s.args # transform multiple arguments into explicit and-tree pl_expr = s_args[0] for a in s_args[1:]: pl_expr = Predicate('and', [pl_expr, a]) filters.append( prolog_to_filter_expression(pl_expr, g.env, pe, var_map, pe.kb)) arg_idx += 1 # check for distinct elif isinstance(arg_s, Predicate) and arg_s.name == 'distinct': s_args = arg_s.args if len(s_args) != 0: raise PrologRuntimeError( 'rdf: distinct: unexpected arguments.') distinct = True arg_idx += 1 # check for limit/offset elif isinstance(arg_s, Predicate) and arg_s.name == 'limit': s_args = arg_s.args if len(s_args) != 1: raise PrologRuntimeError('rdf: limit: one argument expected.') limit = pe.prolog_get_int(s_args[0], g.env) arg_idx += 1 elif isinstance(arg_s, Predicate) and arg_s.name == 'offset': s_args = arg_s.args if len(s_args) != 1: raise PrologRuntimeError('rdf: offset: one argument expected.') offset = pe.prolog_get_int(s_args[0], g.env) arg_idx += 1 else: if arg_idx > len(args) - 3: raise PrologRuntimeError( 'rdf: not enough arguments for triple') arg_p = args[arg_idx + 1] arg_o = args[arg_idx + 2] logging.debug('rdf: arg triple: %s' % repr((arg_s, arg_p, arg_o))) triples.append( (pl_to_rdf(arg_s, g.env, pe, var_map, pe.kb), pl_to_rdf(arg_p, g.env, pe, var_map, pe.kb), pl_to_rdf(arg_o, g.env, pe, var_map, pe.kb))) arg_idx += 3 logging.debug('rdf: triples: %s' % repr(triples)) logging.debug('rdf: optional_triples: %s' % repr(optional_triples)) logging.debug('rdf: filters: %s' % repr(filters)) if len(triples) == 0: raise PrologRuntimeError( 'rdf: at least one non-optional triple expected') var_list = var_map.values() var_set = set(var_list) p = CompValue('BGP', triples=triples, _vars=var_set) for t in optional_triples: p = CompValue('LeftJoin', p1=p, p2=CompValue('BGP', triples=[t], _vars=var_set), expr=CompValue('TrueFilter', _vars=set([]))) for f in filters: p = CompValue('Filter', p=p, expr=f, _vars=var_set) if limit > 0: p = CompValue('Slice', start=offset, length=limit, p=p, _vars=var_set) if distinct: p = CompValue('Distinct', p=p, _vars=var_set) algebra = CompValue('SelectQuery', p=p, datasetClause=None, PV=var_list, _vars=var_set) result = pe.kb.query_algebra(algebra) logging.debug('rdf: result (len: %d): %s' % (len(result), repr(result))) if len(result) == 0: return False if generate_lists: # bind each variable to list of values for binding in result: for v in binding.labels: l = binding[v] value = rdf_to_pl(l) if not v in g.env: g.env[v] = ListLiteral([]) g.env[v].l.append(value) return True else: # turn result into list of bindings res_bindings = [] for binding in result: res_binding = {} for v in binding.labels: l = binding[v] value = rdf_to_pl(l) res_binding[v] = value res_bindings.append(res_binding) if len(res_bindings) == 0 and len(result) > 0: res_bindings.append({}) # signal success logging.debug('rdf: res_bindings: %s' % repr(res_bindings)) return res_bindings
def builtin_weather_data (g, pe): """ weather_data (PLACE, TSTART, TEND, CODE, PREC, TEMP_MIN, TEMP_MAX, CLOUDS) """ pe._trace ('CALLED BUILTIN weather_data', g) # import pdb; pdb.set_trace() pred = g.terms[g.inx] args = pred.args if len(args) != 8: raise PrologRuntimeError('weather_data: expected 8 args, %d args found.' % len(args), g.location) arg_Place = pe.prolog_eval (args[0], g.env, g.location) arg_TStart = pe.prolog_get_string (args[1], g.env, g.location) arg_TEnd = pe.prolog_get_string (args[2], g.env, g.location) tstart = dateutil.parser.parse(arg_TStart) tend = dateutil.parser.parse(arg_TEnd) arg_code = pe.prolog_get_variable (args[3], g.env, g.location) arg_prec = pe.prolog_get_variable (args[4], g.env, g.location) arg_temp_min = pe.prolog_get_variable (args[5], g.env, g.location) arg_temp_max = pe.prolog_get_variable (args[6], g.env, g.location) arg_clouds = pe.prolog_get_variable (args[7], g.env, g.location) wevs = pe.search_predicate('weather_events', [arg_Place, '_1', '_2', '_3', '_4', '_5', '_6', '_7']) cnt = 0 code = '' prec = 0.0 temp_min = 10000.0 temp_max = -10000.0 clouds = 0.0 for wev in wevs: logging.debug(repr(wev)) unbound_values = False for k in ['_1', '_2', '_3', '_4', '_5', '_6', '_7']: if not k in wev: unbound_values = True break if unbound_values: logging.debug ("skipping: unbound values found.") continue wev_tstart = dateutil.parser.parse(wev['_1'].s) wev_tend = dateutil.parser.parse(wev['_2'].s) if (wev_tstart > tend) or (wev_tend < tstart): # logging.info ('ignoring wev %s' % repr(wev)) # import pdb; pdb.set_trace() continue wev_code = wev['_3'].s[:2] wev_prec = wev['_4'].f wev_temp_min = wev['_5'].f wev_temp_max = wev['_6'].f wev_clouds = wev['_7'].f if wev_temp_min < temp_min: temp_min = wev_temp_min if wev_temp_max > temp_max: temp_max = wev_temp_max if wev_code > code: code = wev_code prec += wev_prec clouds += wev_clouds cnt += 1 if cnt == 0: raise PrologRuntimeError('weather_data: no data found.', g.location) prec /= float(cnt) clouds /= float(cnt) g.env[arg_code] = StringLiteral(code) g.env[arg_prec] = NumberLiteral(prec) g.env[arg_temp_min] = NumberLiteral(temp_min) g.env[arg_temp_max] = NumberLiteral(temp_max) g.env[arg_clouds] = NumberLiteral(clouds) return True
def builtin_context_score(g, pe): """ context_score(+Name, ?Value, +Points, ?Score [, +MinPoints]) """ pe._trace('CALLED BUILTIN context_score', g) pred = g.terms[g.inx] args = pred.args if len(args) < 4: raise PrologRuntimeError('context_score: at least 4 args expected.') if len(args) > 5: raise PrologRuntimeError('context_score: max 5 args expected.') key = args[0].name value = pe.prolog_eval(args[1], g.env) points = pe.prolog_get_float(args[2], g.env) scorev = pe.prolog_get_variable(args[3], g.env) if len(args) == 5: min_score = pe.prolog_get_float(args[4], g.env) else: min_score = 0.0 score = g.env[scorev].f if scorev in g.env else 0.0 if value: stack = pe.read_context(key) if stack: i = 1 for v in stack.l: if v == value: score += points / float(i) break i += 1 if score < min_score: return False g.env[scorev] = NumberLiteral(score) return True if not isinstance(args[1], Variable): raise PrologRuntimeError( u'score_context: arg 2 literal or variable expected, %s found instead.' % unicode(args[1])) res = [] stack = pe.read_context(key) if stack: i = 1 for v in stack.l: s = score + points / float(i) if s >= min_score: res.append({ args[1].name: v, scorev: NumberLiteral(score + points / float(i)) }) i += 1 else: if score >= min_score: res.append({ args[1].name: ListLiteral([]), scorev: NumberLiteral(score) }) return res
def builtin_ner(g, pe): global ner_dict """ ner (+Lang, +Cat, +TS, +TE, +Tokens, -Entity, -Score) """ pe._trace('CALLED BUILTIN ner', g) pred = g.terms[g.inx] args = pred.args if len(args) != 7: raise PrologRuntimeError( 'ner: 7 args ( +Lang, +Cat, +TS, +TE, +Tokens, -Entity, -Score ) expected.', g.location) # # extract args, tokens # arg_Lang = pe.prolog_get_constant(args[0], g.env, g.location) arg_Class = pe.prolog_eval(args[1], g.env, g.location) arg_TStart = pe.prolog_get_int(args[2], g.env, g.location) arg_TEnd = pe.prolog_get_int(args[3], g.env, g.location) arg_Tokens = pe.prolog_get_list(args[4], g.env, g.location) arg_Entity = pe.prolog_get_variable(args[5], g.env, g.location) arg_Score = pe.prolog_get_variable(args[6], g.env, g.location) # import pdb; pdb.set_trace() if not ner_dict: _build_ner_dict(pe) if not arg_Lang in ner_dict: raise PrologRuntimeError('ner: lang %s unknown.' % arg_Lang, g.location) tokens = list(map(lambda x: x.s, arg_Tokens.l)) res = [] if isinstance(arg_Class, Variable): for c in ner_dict[arg_Lang]: for entity, score in ner(arg_Lang, c, arg_TStart, arg_TEnd, tokens): r = { arg_Class.name: Predicate(c), arg_Entity: Predicate(entity), arg_Score: NumberLiteral(score) } res.append(r) else: if not arg_Class.name in ner_dict[arg_Lang]: raise PrologRuntimeError('ner: class %s unknown.' % arg_Class.name, g.location) for entity, score in _ner(arg_Lang, arg_Class.name, arg_TStart, arg_TEnd, tokens): r = { arg_Entity: Predicate(entity), arg_Score: NumberLiteral(score) } res.append(r) return res
def test_module(self, module_name, run_trace=False, test_name=None): self.rt.set_trace(run_trace) m = self.modules[module_name] logging.info('running tests of module %s ...' % (module_name)) num_tests = 0 num_fails = 0 for tc in self.session.query( model.TestCase).filter(model.TestCase.module == module_name): if test_name: if tc.name != test_name: logging.info('skipping test %s' % tc.name) continue num_tests += 1 rounds = json.loads(tc.rounds) prep = json_to_prolog(tc.prep) round_num = 0 prev_context = None res = {} for t_in, t_out, test_actions in rounds: test_in = u' '.join(t_in) test_out = u' '.join(t_out) logging.info("nlp_test: %s round %d test_in : %s" % (tc.name, round_num, repr(test_in))) logging.info("nlp_test: %s round %d test_out : %s" % (tc.name, round_num, repr(test_out))) logging.info("nlp_test: %s round %d test_actions: %s" % (tc.name, round_num, repr(test_actions))) #if round_num>0: # import pdb; pdb.set_trace() res, cur_context = self._setup_context( user=TEST_USER, lang=tc.lang, inp=t_in, prev_context=prev_context, prev_res=res) # prep if prep: # import pdb; pdb.set_trace() # self.rt.set_trace(True) for p in prep: solutions = self.rt.search(Clause( None, p, location=self.dummyloc), env=res) if len(solutions) != 1: raise (PrologRuntimeError( 'Expected exactly one solution from preparation code for test "%s", got %d.' % (tc.name, len(solutions)))) res = solutions[0] # inp / resp inp = self._compute_net_input(res, cur_context) # look up code in DB acode = None matching_resp = False for tdr in self.session.query(model.TrainingData).filter( model.TrainingData.lang == tc.lang, model.TrainingData.inp == json.dumps(inp)): if acode: logging.warn( u'%s: more than one acode for test_in "%s" found in DB!' % (tc.name, test_in)) acode = json.loads(tdr.resp) pcode = self._reconstruct_prolog_code(acode) clause = Clause(None, pcode, location=self.dummyloc) solutions = self.rt.search(clause, env=res) # import pdb; pdb.set_trace() for solution in solutions: actual_out, actual_actions, score = self._extract_response( cur_context, solution) # logging.info("nlp_test: %s round %d %s" % (clause.location, round_num, repr(abuf)) ) if len(test_out) > 0: if len(actual_out) > 0: actual_out = u' '.join( tokenize(u' '.join(actual_out), tc.lang)) logging.info( "nlp_test: %s round %d actual_out : %s (score: %f)" % (tc.name, round_num, actual_out, score)) if actual_out != test_out: logging.info( "nlp_test: %s round %d UTTERANCE MISMATCH." % (tc.name, round_num)) continue # no match logging.info( "nlp_test: %s round %d UTTERANCE MATCHED!" % (tc.name, round_num)) # check actions if len(test_actions) > 0: logging.info( "nlp_test: %s round %d actual acts : %s" % (tc.name, round_num, repr(actual_actions))) # print repr(test_actions) actions_matched = True act = None for action in test_actions: for act in actual_actions: # print " check action match: %s vs %s" % (repr(action), repr(act)) if action == act: break if action != act: actions_matched = False break if not actions_matched: logging.info( "nlp_test: %s round %d ACTIONS MISMATCH." % (tc.name, round_num)) continue logging.info( "nlp_test: %s round %d ACTIONS MATCHED!" % (tc.name, round_num)) matching_resp = True res = solution break if matching_resp: break if acode is None: logging.error('failed to find db entry for %s' % json.dumps(inp)) logging.error( u'Error: %s: no training data for test_in "%s" found in DB!' % (tc.name, test_in)) num_fails += 1 break if not matching_resp: logging.error( u'nlp_test: %s round %d no matching response found.' % (tc.name, round_num)) num_fails += 1 break prev_context = cur_context round_num += 1 self.rt.set_trace(False) return num_tests, num_fails
def builtin_sparql_query(g, pe): pe._trace('CALLED BUILTIN sparql_query', g) pred = g.terms[g.inx] args = pred.args if len(args) < 1: raise PrologRuntimeError('sparql_query: at least 1 argument expected.') query = pe.prolog_get_string(args[0], g.env) # logging.debug("builtin_sparql_query called, query: '%s'" % query) # run query result = pe.kb.query(query) # logging.debug("builtin_sparql_query result: '%s'" % repr(result)) if len(result) == 0: return False # turn result into lists of literals we can then bind to prolog variables res_map = {} res_vars = {} # variable idx -> variable name for binding in result: for v in binding.labels: l = binding[v] value = rdf_to_pl(l) if not v in res_map: res_map[v] = [] res_vars[binding.labels[v]] = v res_map[v].append(value) # logging.debug("builtin_sparql_query res_map : '%s'" % repr(res_map)) # logging.debug("builtin_sparql_query res_vars: '%s'" % repr(res_vars)) # apply bindings to environment vars v_idx = 0 for arg in args[1:]: sparql_var = res_vars[v_idx] prolog_var = pe.prolog_get_variable(arg, g.env) value = res_map[sparql_var] # logging.debug("builtin_sparql_query mapping %s -> %s: '%s'" % (sparql_var, prolog_var, value)) g.env[prolog_var] = ListLiteral(value) v_idx += 1 return True