Beispiel #1
0
def ask(*sentences):
    '''
    Retrieve objects from clips.
    sentences is a list of facts or things,
    and they can contain variables,
    whose scope is the set of sentences asked.
    return a list of dicts with the names of the variables
    used in the sentences asked as keys
    and the matched objects as values.
    If there are no variables in the
    question, but the asked sentences match,
    return True.
    if there is no match, return False
    '''
    clps, templs = get_instances(*sentences)
    resp = []
    if clps:
        names = [Namable.from_clips(ins) for ins in clps]
        while names:
            first = names[:len(templs)]
            names = names[len(templs):]
            rsp = {}
            for templ in templs:
                if utils.varpat.match(templ[0]) and not templ[0].startswith('Y'):
                    rsp[templ[0]] = str(first[templs.index(templ)])
            if rsp:
                resp.append(rsp)
        if not resp:
            resp = True
    else:
        resp = False
    return resp
Beispiel #2
0
def ask_obj(sentence):
    '''
    retrieve sentences in clips
    matching the given sentence.
    Can use variables.
    '''
    clps, templs = get_instances(sentence)
    sens = []
    if clps:
        if isinstance(sentence, Thing):
            for ins in clps:
                sens.append(Namable.from_clips(ins))
        elif isinstance(sentence, Fact):
            for ins in clps:
                i = clips.FindInstance(ins)
                if issubclass(utils.get_class(str(i.Class.Name)), Fact):
                    sens.append(Fact.from_clips(ins))
    return sens