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 """ assert_type(child, Papfunc_SemanticNode) SemanticNode.add_child(self, child)
def _syntactic_node_2_semantic_node(syntactic_node, vector_space, composition_model, normed=True): """Create a SemanticNode from a SyntacticNode recursively Args: syntactic_node: the input syntatic_node 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 node """ # if the node is a terminal node: # - retrieve the lexical vector # if the node is non-terminal # - recursively apply this function to the child nodes to get the vector # representations of the child nodes # - use the composition model, and the vectors of the children to compute # the vector of the current node # if syntactic_node.is_terminal(): new_node = SemanticNode.create_semantic_node(syntactic_node, None) try: row_vector = vector_space.get_row(syntactic_node._word) if normed: new_node._vector = RowNormalization().apply(row_vector) #print "shouldn't be here" else: new_node._vector = row_vector # print new_node._vector except KeyError: #print "missing word:", syntactic_node._word matrix_type = type(vector_space.cooccurrence_matrix) vector_shape = (1,vector_space.cooccurrence_matrix.shape[1]) if isinstance(composition_model, Multiplicative): new_node._vector = matrix_type(np.ones(vector_shape, dtype=np.float)) else: new_node._vector = matrix_type(np.zeros(vector_shape, dtype=np.float)) else: new_node = SemanticNode(syntactic_node.label, None) for child in syntactic_node._children: new_child = _syntactic_node_2_semantic_node(child, vector_space, composition_model, normed) new_node.add_child(new_child) new_vector = new_node.get_child(0).vector # print new_node for i in range(1,len(new_node._children)): new_vector = composition_model._compose(new_vector, new_node.get_child(i).vector) new_node.vector = new_vector #print syntactic_node.get_surface_string() #print new_node.vector[0,0] return new_node