Example #1
0
 def create_papfunc_node(cls, semantic_node, vecspace, matspace,multiply_matrices=False):
     '''
     Create a Papfunc semantic node from a semantic node, a vector space, and a space of (flattened) matrices. An optional Boolean argument, if set to True, makes matrices to be multiplied rather than summed when both subconstituents have arity greater than 0.
     '''
     label = semantic_node.label
     if semantic_node.is_terminal():#for terminal nodes perform lexical lookup
         word = semantic_node.word
         pos = semantic_node.pos
         if hasattr(semantic_node, "_lemma"):
             lemma = semantic_node.lemma
             papfunc_node = Papfunc_SemanticNode(label,None, word=word, pos=pos, lemma=lemma)
         else:
             papfunc_node = Papfunc_SemanticNode(label,None, word=word, pos=pos)
             
     else:# initialize a non-terminal node
         papfunc_node = Papfunc_SemanticNode(label,None)
         if semantic_node._children and not semantic_node.is_terminal():
             #initialize and add each child
             for child in semantic_node._children:
                 assert_type(child, SemanticNode, "argument needs to be of type SemanticNode")
                 papfunc_child=Papfunc_SemanticNode.create_papfunc_node(child, vecspace, matspace,multiply_matrices=multiply_matrices)
                 papfunc_node.add_child(papfunc_child)
     #compositionally obtain semantic representations
     papfunc_node.compute_matreps(vecspace,matspace,multiply_matrices=multiply_matrices)
     if len(papfunc_node._numrep)>0:
         papfunc_node.set_vector(papfunc_node._numrep[0].transpose())
     return papfunc_node
 def __init__(self, lambda_, lexical_space):
     '''
     Constructor
     '''
     super(SemanticSyntacticTreeKernel, self).__init__(lambda_)
     assert_type(lexical_space, Space, "lexical_space must be of type Space")
     self._lexical_space = lexical_space
 def create_papfunc_node(cls, semantic_node, vecspace, matspace,multiply_matrices=False):
     '''
     Create a Papfunc semantic node from a semantic node, a vector space, and a space of (flattened) matrices. An optional Boolean argument, if set to True, makes matrices to be multiplied rather than summed when both subconstituents have arity greater than 0.
     '''
     label = semantic_node.label
     if semantic_node.is_terminal():#create a terminal node
         word = semantic_node.word
         pos = semantic_node.pos
         if hasattr(semantic_node, "_lemma"):
             lemma = semantic_node.lemma
             papfunc_node = Papfunc_SemanticNode(label,None, word=word, pos=pos, lemma=lemma)
         else:
             papfunc_node = Papfunc_SemanticNode(label,None, word=word, pos=pos)
             
     else:#initialize a nonterminal node
         papfunc_node = Papfunc_SemanticNode(label,None)
         if semantic_node._children and not semantic_node.is_terminal():
             #print(semantic_node)
             for child in semantic_node._children:
                 #print(child)
                 assert_type(child, SemanticNode, "argument needs to be of type SemanticNode")
                 papfunc_child=Papfunc_SemanticNode.create_papfunc_node(child, vecspace, matspace,multiply_matrices=multiply_matrices)
                 #print(papfunc_child)
                 papfunc_node.add_child(papfunc_child)
     #compositionally obtain semantic representations
     papfunc_node.compute_matreps(vecspace,matspace,multiply_matrices=multiply_matrices)
     # set vector attribute for non-empty elements taking the first element of
     # the vector-matrix structure
     if len(papfunc_node._numrep)>0:
     # compositionally derived vectors are vertical, we need to transpose them
         papfunc_node.set_vector(papfunc_node._numrep[0].transpose())
     return papfunc_node
 def __init__(self,root):
     '''Constructor
     
     Args:
     root<SyntacticNode>: the root node of the tree
     '''
     type_utils.assert_type(root, SyntacticNode)
     self._root = root
 def __init__(self, label, vector, *args, **kwargs):
     super(SemanticNode, self).__init__(label, *args, **kwargs)
     if vector is not None:
         assert_type(vector, Matrix, "argument vector needs to be of type Matrix")
     #set default attributes
     self._vector = vector
     self._matrep = []
     self._numrep = []
 def dot_product(self, tree1, tree2):
     assert_type(tree1, SemanticTree)
     assert_type(tree2, SemanticTree)
     sentence_vector1 = tree1._root._vector
     sentence_vector2 = tree2._root._vector
     if sentence_vector1.norm() == 0.0 or sentence_vector2.norm() == 0.0:
         return 0.0
     else:
         return self._similarity.get_sim(sentence_vector1, sentence_vector2)
Example #7
0
    def add_child(self,child):
        """Add a child Node to a Node.

        Args:
        child: the child Node
        """
        # check whether the child belong to the class Node
        type_utils.assert_type(child, Node)
        
        self._children.append(child)
    def add_child(self, child):
        """Add a child SyntacticNode to a SyntacticNode.

        Args:
        child: the child SyntacticNode
        """
        if self._type == SyntacticNode.TERMINAL:
            raise ValueError("Terminal node cannot have child node")
        type_utils.assert_type(child, SyntacticNode)
        Node.add_child(self, child)
    def add_child(self, child):
        """Add a child SemanticNode to a SemanticNode.
        This method overrides the add_child method from SyntacticNode to
        enforce the constraint that the child parameter must be a SemanticNode

        Args:
        child: the child SemanticNode
        """
        assert_type(child, SemanticNode)
        SyntacticNode.add_child(self, child)
    def add_child(self, child):
        """Add a child Papfunc_SemanticNode to a Papfunc_SemanticNode.
        This method overrides the add_child method from SemanticNode to
        enforce the constraint that the child parameter must be a Papfunc_SemanticNode

        Args:
        child: the child Papfunc_SemanticNode
        """
        #print ("adding child %s..." %child)
        assert_type(child, Papfunc_SemanticNode)
        SemanticNode.add_child(self, child)
 def __init__(self, label, vector, *args, **kwargs):
     '''
     Constructor
     
     Args:
         label<string>: the label (cat in CCG tree) of the node
         vector<composes.matrix.matrix.Matrix>: the vector represention of 
             the node
         kwargs: see the constructor of a syntactic node
     '''
     
     super(SemanticNode, self).__init__(label, *args, **kwargs)
     
     if vector is not None:
         assert_type(vector, Matrix, "argument vector needs to be of type Matrix")
     self._vector = vector
 def dot_product(self, tree1, tree2):
     assert_type(tree1, SyntacticTree)
     assert_type(tree2, SyntacticTree)
     all_nodes1 = tree1.get_nodes()
     all_nodes1.reverse()
     all_nodes2 = tree2.get_nodes()
     all_nodes2.reverse()
     node2id1 = list2dict(all_nodes1)
     node2id2 = list2dict(all_nodes2)
     delta_matrix = -np.ones((len(all_nodes1), len(all_nodes2)), np.float)
     # print "\n",delta_matrix
     for node1 in all_nodes1:
         for node2 in all_nodes2:
             self._delta(node1, node2, node2id1, node2id2, delta_matrix)
     # print delta_matrix
     return delta_matrix.sum()
Example #13
0
def syntactic_tree_2_semantic_tree(syntactic_tree, vector_space, 
                                   composition_model, normed=True):
    """Create a SemanticTree from a SyntacticTree
    
    Args:
        syntactic_tree: the input syntatic_tree
        vector_space: a vector space where the lexical vectors can be retrieved
        composition_model: the compositional model, with which the vector
            representations of phrases are computed (the compositional model
            should be either WeightedAdditive, Multiplicative or FullAdditive)
        normed: a boolean value indicating whether the lexical vectors should be
            normalized or not
        
    Returns:
        the semantic tree
    """
    
    assert_type(syntactic_tree, SyntacticTree)
    return SemanticTree(_syntactic_node_2_semantic_node(syntactic_tree._root,
                                                         vector_space,
                                                         composition_model, normed))
 def dot_product(self, tree1, tree2):
     assert_type(tree1, SemanticTree)
     assert_type(tree2, SemanticTree)
     return super(MixedCompositionalTreeKernel, self).dot_product(tree1, tree2)
Example #15
0
def lemma_tree_2_lemmapos_tree(syntactic_tree, excluded_poss = {}):
    assert_type(syntactic_tree, SyntacticTree)
    return SyntacticTree(_lemma_tree_2_lemmapos_tree(syntactic_tree._root,
                                                     excluded_poss))
 def __init__(self, root):
     '''
     Constructor
     '''
     type_utils.assert_type(root, SemanticNode)
     self._root = root
 def dot_product(self, tree1, tree2):
     assert_type(tree1, SemanticTree)
     assert_type(tree2, SemanticTree)
     return super(SemanticTreeKernel, self).dot_product(tree1,tree2)