예제 #1
0
def treeToDeriv(tree):
    if type(tree[1]) is not tuple:
        r = Rule()
        r.setup(tree[0], [tree[1],], 1.0)
        return [r,]
    else:
        r = Rule()
        rhs = [x[0] for x in tree[1:]]
        assert(len(rhs) <= 2), "Non-binary rule: %s" % str(rhs)
        r.setup(tree[0], rhs, 1.0)
        res = [r,]
        for subt in tree[1:]:
            res += treeToDeriv(subt)
        return res
예제 #2
0
    def parseFail(self, sentence):
        top = self.top
        topBar = "@%s" % top
        lhs = top
        ana = self.makeAnalysis(top=top, initial=self.grammar.depth())

        for word in sentence:
            posInsRule = Rule()
            posInsRule.setup(lhs, ["FW", topBar], 1.0)
            lhs = topBar
            posInsRule.terminal = False
            ana = ana.extend(posInsRule, None, None, self, doFOM=False)
            wordInsRule = Rule()
            wordInsRule.setup("FW", [word,], 1.0)
            wordInsRule.terminal = True
            ana = ana.extend(wordInsRule, None, None, self, doFOM=False)

        endRule = Rule()
        endRule.setup(topBar, [], 1.0)
        endRule.terminal = True
        ana = ana.extend(endRule, None, None, self, doFOM=False)

        return ana