예제 #1
0
    def tree(self):
        '''alternatively: simulate actions'''

        (children, action, _) = self.backptrs[0]
        if children is None:
            if FLAGS.pretag and action[0] == -2:
                return DepTree(self.i, action[2])
            elif FLAGS.shifttag and action[0] == 0:
                return DepTree(self.i, action[2])
            else:
                return DepTree(
                    self.i,
                    action[1])  # yang: SHIFT tag (changes tag in deptree)
        else:
            left, right = children  # REDUCE
            return DepTree.combine(left.tree(), right.tree(), action)
예제 #2
0
    def tree(self):
        '''alternatively: simulate actions'''

        (children, action, _) = self.backptrs[0]
        if children is None:
            return DepTree(self.i) # SHIFT
        else:
            left, right = children  # REDUCE            
            return DepTree.combine(left.tree(), right.tree(), action)
예제 #3
0
 def __init__(self, sent_id, parse_tree, dep_tree, words):
     self.leaves = []
     self.id = sent_id
     self.tree = Tree(parse_tree, sent_id)
     self.get_leaves()
     self.words = words
     self.begin_offset = words[0][1]['CharacterOffsetBegin']
     self.end_offset = words[-1][1]['CharacterOffsetEnd']
     self.word_ids = []
     self.true_connectives = []
     self.checked_connectives = []
     self.depTree = DepTree(self, dep_tree)
     self.clauses = []
     self.break_clauses()
예제 #4
0
    def take(self, action, action_gold=False):
        '''returns a list (iterator) of resulting states.'''

        if self.i == self.j == 0:  ## don't count start
            actioncost = 0
        else:
            ## applying the model weights
            actioncost = self.feats(action).dot(
                self.model.weights) if self.model is not None else 0

        if action == 0:  # SHIFT
            new = State(self.j, self.j+1, action, \
                        self.stack + [DepTree(self.j)])
            new.inside = 0
            self.shiftcost = actioncost  # N.B.: self!
            new.score = self.score + actioncost  # forward cost
            new.step = self.step + 1
            new.leftptrs = [self]
            new.backptrs = [(None, action)]  # shift has no children

            new.gold = self.gold and action_gold  # gold is sequentially incremented

            yield new  # shift always returns one unique offspring

        else:  # REDUCE
            for leftstate in self.leftptrs:  # i'm combining with it
                newtree = leftstate.stack[-1].combine(
                    self.stack[-1],
                    action)  # N.B.:theory! NOT self.stack[-2] with -1
                ## N.B.: theory! NOT self.stack[:-2]
                new = State(leftstate.i, self.j, action, \
                            leftstate.stack[:-1] + [newtree])

                new.inside = leftstate.inside + self.inside + \
                             leftstate.shiftcost + actioncost # N.B.

                new.score = leftstate.score + self.inside + leftstate.shiftcost + actioncost  #n.b.
                ## WRONG: new.score = self.score + actioncost # forward cost, only true for non-DP
                new.step = self.step + 1
                new.leftptrs = leftstate.leftptrs
                new.backptrs = [((leftstate, self), action)]

                # meaning of x.gold: first of all, viterbi-inside derivation is gold
                # and also, there is a gold path predicting x (same as earley item: bottom-up + top-down filtering)
                new.gold = leftstate.gold and self.gold and action_gold  # gold is binary

                yield new
예제 #5
0
 def this_tree(self):
     ## very careful: sym=False! do not symbolize again
     ##      return Tree(self.label, self.span, wrd=self.word, sym=False)
     t = DepTree(int(self.label))
     t.canonical = True
     return t