示例#1
0
文件: lca.py 项目: Oneplus/cnccgbank
    def insert_quotes(self, deriv, attachment_node, higher):
        open_quote_leaf = make_open_quote_leaf(None)
        closed_quote_leaf = make_closed_quote_leaf(None)

        old_subtree = attachment_node.clone()
        
        if higher == "left":
            right_child = Node(attachment_node.cat, 0, 2, None,
                               old_subtree,
                               closed_quote_leaf)
            new_node    = Node(attachment_node.cat, 0, 2, None,
                               open_quote_leaf,
                               right_child)
        elif higher == "right":
            left_child  = Node(attachment_node.cat, 0, 2, None,
                               open_quote_leaf,
                               old_subtree)
            new_node    = Node(attachment_node.cat, 0, 2, None,
                               left_child,
                               closed_quote_leaf)
                            
                            
        prev_parent = attachment_node.parent
        was_left_child = (prev_parent is not None) and (prev_parent.lch is attachment_node)
        
        if prev_parent:
            if was_left_child:
                prev_parent.lch = new_node
            else:
                prev_parent.rch = new_node
                
        else: # Replace old root
            return new_node
            
        return deriv
示例#2
0
    def insert_quotes(self, deriv, attachment_node, higher):
        open_quote_leaf = make_open_quote_leaf(None)
        closed_quote_leaf = make_closed_quote_leaf(None)

        old_subtree = attachment_node.clone()

        if higher == "left":
            right_child = Node(attachment_node.cat, 0, 2, None, old_subtree,
                               closed_quote_leaf)
            new_node = Node(attachment_node.cat, 0, 2, None, open_quote_leaf,
                            right_child)
        elif higher == "right":
            left_child = Node(attachment_node.cat, 0, 2, None, open_quote_leaf,
                              old_subtree)
            new_node = Node(attachment_node.cat, 0, 2, None, left_child,
                            closed_quote_leaf)

        prev_parent = attachment_node.parent
        was_left_child = (prev_parent
                          is not None) and (prev_parent.lch is attachment_node)

        if prev_parent:
            if was_left_child:
                prev_parent.lch = new_node
            else:
                prev_parent.rch = new_node

        else:  # Replace old root
            return new_node

        return deriv
示例#3
0
    def insert_quote(self, deriv, tokens, at, quote, quote_type):
        '''Performs the actual quote insertion. Returns the root of the newly quoted derivation (which may differ
from the root of the input derivation).'''

        if quote == "begin": direction = "forwards"
        elif quote == "end": direction = "backwards"
        
        double = (quote_type == "``")
        
        node = get_leaf(deriv, at, direction)
        
        if (at is not None) and node:
            if quote == "end": # Process absorbed punctuation
                if self.punct_class:
                    node = self.punct_class.process_punct(deriv, node, at)
            
            if node and is_sublist(smaller=text(node), larger=tokens):
                attachment_node = node
                
                while (attachment_node.parent and is_sublist(smaller=text(attachment_node.parent),
                                                             larger=tokens)):
                    attachment_node = attachment_node.parent
                    
                prev_parent = attachment_node.parent
                was_left_child = (attachment_node.parent) and (attachment_node.parent.lch is attachment_node)
                
                if quote == "begin":
                    new_node = Node(attachment_node.cat, 0, 2, 
                                    parent=None, lch=make_open_quote_leaf(None, double),
                                    rch=attachment_node)
                elif quote == "end":
                    new_node = Node(attachment_node.cat, 0, 2,
                                    parent=None, lch=attachment_node,
                                    rch=make_closed_quote_leaf(None, double))
                                    
                if prev_parent:
                    if was_left_child:
                        prev_parent.lch = new_node
                    else:
                        prev_parent.rch = new_node
                else:
                    return new_node # Replace the old root

        return deriv