예제 #1
0
파일: process.py 프로젝트: arne-cl/fosay
def find_maxposs(caseframe):
    res = 0
    for c in caseframe.keys():
        if not is_prop(c):
            if type(caseframe[c]) == type([]):
                res += len(caseframe[c])
            else:
                res += 1
    return res
예제 #2
0
파일: process.py 프로젝트: arne-cl/fosay
def unparse(lang, label, state, caseframe, parent = None):
    "Uses generator to yield all possible sentences"
    if lang.to_type_code(label) == const.type["epsilon"]: ############
        yield None ##########################
    if caseframe == None:
        return
    
    if is_terminal(label):
        w = Token()
        w.set_ghost(is_empty(label))
        w.type = lang.to_type_code(label)
        for c in transferred_attributes[lang.to_type_code(label)]:
            if c == concept["real-number"] and not c in caseframe:
                continue
            w.attr(c, caseframe[c])
        yield w
    elif state == STATE_END:
        if all(is_prop(key) for key in caseframe.keys()):
            yield parent
    else:
        net = lang.atn[label]
        if not parent:
            parent = FlowerLingUnit(lang.to_type_code(label), 0, 0)

            for key in caseframe.keys():
                if is_prop(key):
                    parent.attr(key, caseframe.get(key, None))

        if lang.to_type_code(label) in caseframe and \
            type(caseframe[lang.to_type_code(label)]) == list and \
            concept["conj-function"] in caseframe and \
            not caseframe[concept["conj-function"]] is None:

            if not STATE_CONJ_A1 in net: return ###it is needed when we go on a way of an empty alternative. Like NP:EMPTY
            objs = caseframe[lang.to_type_code(label)]
            conj = caseframe[concept["conj-function"]], caseframe[concept["conj-type"]], caseframe[concept["conj-str"]]
            for x in gen_conj(lang, label, deepcopy(parent), net[STATE_CONJ_A1][0][1], net[STATE_CONJ_A4][1][1], deepcopy(objs), conj):
                yield x
        else:
            p = net[state]
            q = {}
            mp = find_maxposs(caseframe)

            for labl, f, standard, nextState, maxel, minel in p:
                if maxel != INDEFINITE and not maxel >= mp >= minel:
                    continue
                if standard in q.keys():
                    q[standard] += [(labl, f, nextState)]
                else:
                    q[standard] = [(labl, f, nextState)]

            q = [value for key, value in sorted(q.items(), key = lambda x: -1*x[0])]

            i = 0
            good_speed = False
            while not good_speed and i < len(q):
                for labl, f, nextState in q[i]:
                    if nextState == STATE_CONJ_A1: continue

                    cl = lang.to_type_code(labl)
                    if cl in modificators and req(cl, caseframe):
                        tmp = deepcopy(caseframe)
                        words = [(Token(cl), tmp)]
                    elif type(caseframe.get(cl)) == type([]):
                        words = []
                        for i in range(len(caseframe.get(cl))):
                            tmp = deepcopy(caseframe)
                            if len(tmp[cl]) == 1:
                                cf = tmp[cl][0]
                                del tmp[cl]
                            else:
                                cf = tmp[cl].pop(i)
                            words += [(word, deepcopy(tmp)) for word in unparse(lang, labl, STATE_START, cf, None)]
                    else:
                        tmp = deepcopy(caseframe)
                        if cl in tmp:
                            del tmp[cl]
                        words = [(word, deepcopy(tmp)) for word in unparse(lang, labl, STATE_START, caseframe.get(cl), None)]

                    for word, cf in words:
                        par = deepcopy(parent)
                        for x in f(lang, par, word) if f else [par]:
                            for rest in unparse(lang, label, nextState, deepcopy(cf), x):
                                good_speed = True
                                yield rest
                i += 1