예제 #1
0
    def _reconstruct_prolog_code(self, acode):

        todo = [('and', [])]

        idx = 0
        while idx < len(acode):

            a = acode[idx]
            if a == 'or(':
                todo.append(('or', []))
            elif a == 'and(':
                todo.append(('and', []))
            elif a == ')':
                c = todo.pop()
                todo[len(todo) - 1][1].append(Predicate(c[0], c[1]))
            else:

                clause = self.aip_parser.parse_line_clause_body(a)

                todo[len(todo) - 1][1].append(clause.body)

            idx += 1

        if len(todo) != 1:
            logging.warn('unbalanced acode detected.')
            return None

        c = todo.pop()
        return Predicate(c[0], c[1])
예제 #2
0
def convert_nlp_test(pred):

    global test_cnt

    # print "% ", unicode(pred)

    lang = pred.args[0].name
    ivr_in = pred.args[1].args[0].args[0]
    ivr_out = pred.args[1].args[1].args[0]

    head = Predicate(name='nlp_test',
                     args=[
                         StringLiteral(MODULE_NAME),
                         Predicate(name=lang),
                         StringLiteral('t%04d' % test_cnt),
                         Predicate(name='FIXME'),
                         ListLiteral([ivr_in, ivr_out,
                                      ListLiteral([])])
                     ])

    test_cnt += 1

    clause = Clause(head=head)

    print unicode(clause)
예제 #3
0
    def _setup_context (self, user, lang, inp, prev_context, prev_res):

        cur_context = Predicate(do_gensym (self.rt, 'context'))
        res = { }
        if ASSERT_OVERLAY_VAR_NAME in prev_res:
            res[ASSERT_OVERLAY_VAR_NAME] = prev_res[ASSERT_OVERLAY_VAR_NAME].clone()

        res = do_assertz ({}, Clause ( Predicate('user',   [cur_context, Predicate(user)])  , location=self.dummyloc), res=res)
        res = do_assertz ({}, Clause ( Predicate('lang',   [cur_context, Predicate(lang)])  , location=self.dummyloc), res=res)

        token_literal = ListLiteral (list(map(lambda x: StringLiteral(x), inp)))
        res = do_assertz ({}, Clause ( Predicate('tokens', [cur_context, token_literal])    , location=self.dummyloc), res=res)

        currentTime = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC).isoformat()
        res = do_assertz ({}, Clause ( Predicate('time',   [cur_context, StringLiteral(currentTime)]) , location=self.dummyloc), res=res)

        if prev_context:

            res = do_assertz ({}, Clause ( Predicate('prev', [cur_context, prev_context]) , location=self.dummyloc), res=res)

            # copy over all previous context statements to the new one
            s1s = self.rt.search_predicate ('context', [prev_context, '_1', '_2'], env=res)
            for s1 in s1s:
                res = do_assertz ({}, Clause ( Predicate('context', [cur_context, s1['_1'], s1['_2']]) , location=self.dummyloc), res=res)
            # copy over all previous mem statements to the new one
            s1s = self.rt.search_predicate ('mem', [prev_context, '_1', '_2'], env=res)
            for s1 in s1s:
                res = do_assertz ({}, Clause ( Predicate('mem', [cur_context, s1['_1'], s1['_2']]) , location=self.dummyloc), res=res)
            # import pdb; pdb.set_trace()

        res['C'] = cur_context

        return res, cur_context
예제 #4
0
def convert_answerz(c):

    pred = c.head

    lang = pred.args[1].name
    n = pred.args[2].name

    pred = c.body

    s = pred.args[2].s

    head = Predicate(
        name='nlp_%s_r' % MODULE_NAME,
        args=[Predicate(name=lang),
              Predicate(name=n),
              Variable(name='R')])

    body = Predicate(
        name='says',
        args=[Predicate(name=lang),
              Variable(name='R'),
              StringLiteral(s)])

    clause = Clause(head=head, body=body)

    print unicode(clause)
예제 #5
0
def convert_nlp_gens(pred):

    # print "gens ", pred.args

    lang = pred.args[0].name
    ms = pred.args[1].s
    resp = pred.args[2]

    res = convert_macro_string(ms)

    res = Predicate(
        name='nlp_gens',
        args=[StringLiteral(MODULE_NAME),
              Predicate(name=lang), res, resp])

    print unicode(res) + u'.'
예제 #6
0
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
예제 #7
0
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))
예제 #8
0
    def _builtin_action_wrapper(self, name, g, pe):

        l = [Predicate(name)]
        for arg in g.terms[g.inx].args:
            # logging.debug ('_builtin_action_wrapper: %s arg=%s' % (name, repr(arg)))
            value = pe.prolog_eval(arg, g.env)
            l.append(value)

        if not ACTION_VARNAME in g.env:
            g.env[ACTION_VARNAME] = []

        g.env[ACTION_VARNAME].append(l)
        return True
예제 #9
0
def convert_nlp_gen(pred):

    print "% ", unicode(pred)

    lang = pred.args[0].name
    ms = pred.args[1].s

    res = convert_macro_string(ms)

    head = Predicate(name='nlp_%s_s' % MODULE_NAME,
                     args=[
                         Predicate(name=lang),
                         Predicate(name='fixme'),
                         Variable(name='S')
                     ])

    body = Predicate(name='hears',
                     args=[Predicate(name=lang),
                           Variable(name='S'), res])

    clause = Clause(head=head, body=body)

    print unicode(clause)
예제 #10
0
    def find_prev_context (self, user, env={}):

        pc    = None
        ctxid = 0

        # logging.debug ('find_prev_context: user=%s' % user)

        for s in self.rt.search_predicate('user', ['_1', Predicate(user)], env=env):

            cid = int (s['_1'].name[7:])
            if not pc or cid>ctxid:
                pc = s['_1']
            # logging.debug ('find_prev_context: s=%s, pc=%s' % (unicode(s), unicode(pc)))

        return pc
예제 #11
0
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
예제 #12
0
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]
예제 #13
0
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
예제 #14
0
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]
예제 #15
0
파일: ner.py 프로젝트: upsource/zamia-ai
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
예제 #16
0
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