Java Code Examples for edu.stanford.nlp.semgraph.SemanticGraphEdge#getDependent()

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraphEdge#getDependent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReverbPropositionGeneration.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private IndexedWord getNewRoot(Map<Integer, IndexedWord> included,
    IndexedConstituent ic, Set<IndexedWord> heads, IndexedWord start, SemanticGraph semanticGraph) {
  List<SemanticGraphEdge> outEdges = semanticGraph.getOutEdgesSorted(start);
  IndexedWord nHead;
  for(SemanticGraphEdge edge: outEdges) {
    if(heads.contains(edge.getDependent())) {
      continue;
    }
    if(included.values().contains(edge.getDependent())
        || ic.excludedVertexes.contains(edge.getDependent())) {
      if((nHead = getNewRoot(included, ic, heads, edge.getDependent(), semanticGraph)) == null) {
        continue;
      } else {
        return nHead;
      }
    } else if(!included.values().contains(edge.getDependent())
        && edge.getDependent().get(CoreAnnotations.PartOfSpeechAnnotation.class).startsWith("N")) {
      return edge.getDependent();
    }
  }
  return null;
}
 
Example 2
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private static void subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
    Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
    Collection<SemanticGraphEdge> edgesToRemove, Set<SemanticGraphEdge> exploredEdges) {
  List<SemanticGraphEdge> edges = graph.getOutEdgesSorted(root);
  for (SemanticGraphEdge e : edges) {
    if(exploredEdges.contains(e)) {
      continue;
    }
    IndexedWord child = e.getDependent();
    exploredEdges.add(e);
    if (excludeVertexes.contains(child)
        || excludeRelations.contains(e.getRelation())
        || excludeRelationsTop.contains(e.getRelation())
        || containsRelationOrDescendant(excludeRelations, e.getRelation())
        || containsRelationOrDescendant(excludeRelationsTop, e.getRelation())) {
      edgesToRemove.add(graph.getEdge(root, child));
    } else {
      subgraph(graph, child, excludeVertexes, excludeRelations, Collections.<GrammaticalRelation>emptySet(), edgesToRemove, exploredEdges);
    }
  }
}
 
Example 3
Source File: ClauseDetector.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/** Creates a clause from a parataxis relation
 * @param root Head of the parataxis relation
 * @param parroot  Dependent of the parataxis relation
 * @param roots List of clause roots*/
private static void addParataxisClause(ClausIE clausIE, IndexedWord root, IndexedWord parroot, List<IndexedWord> roots, SemanticGraph semanticGraph) {
  Constituent verb = new IndexedConstituent(semanticGraph, parroot, Type.VERB);
  List<SemanticGraphEdge> outedges = semanticGraph.getOutEdgesSorted(parroot);
  SemanticGraphEdge subject = DpUtils.findFirstOfRelationOrDescendent(outedges, EnglishGrammaticalRelations.SUBJECT);
  if (subject != null) {
    Constituent subjectConst = new IndexedConstituent(semanticGraph, subject.getDependent(), Type.SUBJECT);
    Constituent object = new IndexedConstituent(semanticGraph, root, Type.DOBJ);
    ((IndexedConstituent) object).excludedVertexes.add(parroot);
    Clause clause = new Clause(semanticGraph);
    clause.setSubject(0);
    clause.setVerb(1);
    clause.getDobjects().add(2);
    clause.getConstituents().add(subjectConst);
    clause.getConstituents().add(verb);
    clause.getConstituents().add(object);
    clause.setType(Clause.Type.SVO);
    clausIE.clauses.add(clause);
    roots.add(null);

  }

}
 
Example 4
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
/** Creates a clause from a parataxis relation 
 * @param root Head of the parataxis relation
 * @param parroot  Dependent of the parataxis relation
 * @param roots List of clause roots
 */
private static void addParataxisClause(ClausIE clausIE, IndexedWord root, IndexedWord parroot, List<IndexedWord> roots) {
    Constituent verb = new IndexedConstituent(clausIE.getSemanticGraph(), parroot, Type.VERB);
    List<SemanticGraphEdge> outedges = clausIE.getSemanticGraph().getOutEdgesSorted(parroot);
    SemanticGraphEdge subject = DpUtils.findFirstOfRelationOrDescendent(outedges, EnglishGrammaticalRelations.SUBJECT);
    if (subject != null) {
        Constituent subjectConst = new IndexedConstituent(clausIE.getSemanticGraph(), subject.getDependent(), 
                                                            Type.SUBJECT);
        Constituent object = new IndexedConstituent(clausIE.getSemanticGraph(), root, Type.DOBJ);
        ((IndexedConstituent) object).excludedVertexes.add(parroot);
        Clause clause = new Clause();
        clause.setSubject(0);
        clause.verb = 1;
        clause.dobjects.add(2);
        clause.constituents.add(subjectConst);
        clause.constituents.add(verb);
        clause.constituents.add(object);
        clause.setType(Clause.Type.SVO);
        clausIE.getClauses().add(clause);
        roots.add(null);
    }
}
 
Example 5
Source File: AnswerPOS.java    From uncc2014watsonsim with GNU General Public License v2.0 6 votes vote down vote up
public double scoreAnswer(Question q, Answer a) {

for (SemanticGraph graph : a.getGraphs()) {

	if(!graph.getRoots().isEmpty())
	{
	if (graph.getFirstRoot().tag().contains("NN")) {
		for (SemanticGraphEdge edge : graph.edgeIterable()) {

			IndexedWord a1 = edge.getDependent();
			IndexedWord a2 = edge.getGovernor();

			if (a1.tag().contains("NN")) {
				return 1.0;
			}
			if (a2.tag().contains("NN")) {
				return 1.0;
			}

		}

	}
   }
  }
return 0.0;
}
 
Example 6
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static void replaceNodeFromSemanticGraph(IndexedWord original, IndexedWord replace, SemanticGraph semanticGraph, boolean onlyIncoming) {
  List<SemanticGraphEdge> edgesToAdd = new ArrayList<>();
  edgesToAdd.addAll(semanticGraph.getIncomingEdgesSorted(original));
  if(!onlyIncoming) {
    edgesToAdd.addAll(semanticGraph.getOutEdgesSorted(original));
  }
  edgesToAdd.addAll(DpUtils.findAllRelationsOrDescendant(semanticGraph.getOutEdgesSorted(original), EnglishGrammaticalRelations.SUBJECT));
  edgesToAdd.addAll(DpUtils.findAllRelationsOrDescendant(semanticGraph.getOutEdgesSorted(original), EnglishGrammaticalRelations.COPULA));
  Set<GrammaticalRelation> relations = DpUtils.getIncommingRelations(replace, semanticGraph);
  for(SemanticGraphEdge edge: edgesToAdd) {
    IndexedWord governor = replace;
    IndexedWord dependent = edge.getDependent();
    if(edge.getGovernor().equals(original)) {
      governor = replace;
      dependent = edge.getDependent();
    } else {
      if(relations.contains(edge.getRelation())) {
        continue;
      }
      governor = edge.getGovernor();
      dependent = replace;
    }
    semanticGraph.addEdge(governor, dependent, edge.getRelation(), edge.getWeight(), edge.isExtra());
  }
  if(semanticGraph.getRoots().contains(original)) {
    semanticGraph.resetRoots();
    semanticGraph.setRoot(replace);
  }
  disconectNodeFromSemanticGraph(original, semanticGraph);
}
 
Example 7
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Disconnects independent clauses by removing the edge representing the coordinating conjunction */
public static void disconectClauses(SemanticGraph graph, Constituent constituent) {
  List<SemanticGraphEdge> outedges = graph.getOutEdgesSorted(((IndexedConstituent) constituent).getRoot());
  for (int i = 0; i < outedges.size(); i++) {
    SemanticGraphEdge e = outedges.get(i);
    if (DpUtils.isAnyConj(e)) {
      IndexedWord child = e.getDependent();
      List<SemanticGraphEdge> outNewRoot = graph.getOutEdgesSorted(child);
      SemanticGraphEdge sub = DpUtils.findFirstOfRelationOrDescendent(outNewRoot, EnglishGrammaticalRelations.SUBJECT);
      if (sub != null) graph.removeEdge(e);
    }
  }
}
 
Example 8
Source File: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private static void addEdges(List<SemanticGraphEdge> outEdgesSorted, IndexedWord governor, SemanticGraph semanticGraph) {
  Set<GrammaticalRelation> relations = collectRelations(outEdgesSorted);
  Set<GrammaticalRelation> noRelations = collectRelations(semanticGraph.getOutEdgesSorted(governor));
  relations.removeAll(noRelations);
  for(SemanticGraphEdge edge: outEdgesSorted) {
    if(!relations.contains(edge.getRelation())) {
      continue;
    }
    SemanticGraphEdge nedge = new SemanticGraphEdge(governor, edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
    semanticGraph.addEdge(nedge);
  }
}
 
Example 9
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Implementation for
 * {@link #removeEdges(SemanticGraph, IndexedWord, Collection, Collection, Collection)} */
private static int subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
        Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
        Collection<SemanticGraphEdge> edgesToRemove, int counter) {
    
    /* TODO: In some sentences there is infinite recursion. Dirty fix to stop it. 
     
     Example sentence:
     "Policies on electronic tickets differ ''from airline to airline and airport to airport,'' said Ms. McInerney, 
     whose group is working with the airline industry on e-ticket policies and the matter of standardizing itineraries 
     and receipts, perhaps with a universal template to create more readily verifiable printouts that carry uniform 
     information like a ticket number that can be matched to an airline computer reservation."
 
     */
    counter++;
    if (counter > MAX_RECURSION_ITERATIONS){
        return counter;
    }

    List<SemanticGraphEdge> edges = graph.getOutEdgesSorted(root);
    for (SemanticGraphEdge e : edges) {
        IndexedWord child = e.getDependent();
        if (excludeVertexes.contains(child) || excludeRelations.contains(e.getRelation())
                || excludeRelationsTop.contains(e.getRelation())) {
            edgesToRemove.add(graph.getEdge(root, child));
        } else {
            counter = subgraph(graph, child, excludeVertexes, excludeRelations, 
                    Collections.<GrammaticalRelation> emptySet(), edgesToRemove, counter);
        }
    }
    
    return counter;
}
 
Example 10
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Disconnects independent clauses by removing the edge representing the coordinating conjunction */
public static void disconectClauses(SemanticGraph graph, Constituent constituent) {
    List<SemanticGraphEdge> outedges = graph.getOutEdgesSorted(((IndexedConstituent) constituent).getRoot());
    for (int i = 0; i < outedges.size(); i++) {
        SemanticGraphEdge e = outedges.get(i);
        if (DpUtils.isAnyConj(e)) {
            IndexedWord child = e.getDependent();
            List<SemanticGraphEdge> outNewRoot = graph.getOutEdgesSorted(child);
            SemanticGraphEdge sub = DpUtils.findFirstOfRelationOrDescendent(outNewRoot, 
                    EnglishGrammaticalRelations.SUBJECT);
            if (sub != null)
                graph.removeEdge(e);
        }
    }
}
 
Example 11
Source File: CoreNLPUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
private static void getSubTreeEdgesHelper(IndexedWord vertice, SemanticGraph sg, Set<SemanticGraphEdge> tabuEdges) {
    for (SemanticGraphEdge edge : sg.outgoingEdgeIterable(vertice)) {
        if (!tabuEdges.contains(edge)) {
            IndexedWord dep = edge.getDependent();
            tabuEdges.add(edge);
            getSubTreeEdgesHelper(dep, sg, tabuEdges);
        }
    }
}
 
Example 12
Source File: ReplaceSubordinateRule.java    From tint with GNU General Public License v3.0 5 votes vote down vote up
static TreeSet<String> getPersons(SemanticGraph semanticGraph, IndexedWord word, CoreMap sentence) {
    Stack<IndexedWord> wordsToCheck = new Stack<>();
    wordsToCheck.add(word);

    int index = word.index();

    while (!wordsToCheck.isEmpty()) {
        IndexedWord thisWord = wordsToCheck.pop();
        List<SemanticGraphEdge> outEdgesSorted = semanticGraph.getOutEdgesSorted(thisWord);
        for (SemanticGraphEdge semanticGraphEdge : outEdgesSorted) {
            IndexedWord dependent = semanticGraphEdge.getDependent();
            String pos = dependent.get(CoreAnnotations.PartOfSpeechAnnotation.class);
            if (pos.equals("VA")) {
                index = Math.min(index, dependent.index());
                wordsToCheck.push(dependent);
            }
        }
    }

    CoreLabel token = sentence.get(CoreAnnotations.TokensAnnotation.class).get(index - 1);
    String morpho = token.get(DigiMorphAnnotations.MorphoAnnotation.class);
    String[] parts = morpho.split("\\s+");
    TreeSet<String> persons = new TreeSet<>();
    for (int i = 1; i < parts.length; i++) {
        String[] vParts = parts[i].split("\\+");
        if (!vParts[1].equals("v")) {
            continue;
        }

        persons.add(vParts[5] + "+" + vParts[6]);
    }
    return persons;
}
 
Example 13
Source File: Simplifier.java    From tint with GNU General Public License v3.0 5 votes vote down vote up
public static void getChildrenRecursive(SemanticGraph semanticGraph, IndexedWord node, LinkedHashSet<IndexedWord> list, int iter)
        throws Exception {
    if (iter > MAX_ITER) {
        throw new Exception("Too many iterations");
    }
    List<SemanticGraphEdge> outEdgesSorted = semanticGraph.getOutEdgesSorted(node);
    for (SemanticGraphEdge semanticGraphEdge : outEdgesSorted) {
        IndexedWord child = semanticGraphEdge.getDependent();
        list.add(child);
        getChildrenRecursive(semanticGraph, child, list, iter + 1);
    }
}
 
Example 14
Source File: AnswerPOS.java    From uncc2014watsonsim with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
	Answer a = new Answer("For luck Kate will only knock on this wood");
	// System.err.println(a.graphs.size());
	// System.out.println("hello");
	double score = 0;
	for (SemanticGraph graph : a.getGraphs()) {

		if (graph.getFirstRoot().tag().contains("NN")) {
			for (SemanticGraphEdge edge : graph.edgeIterable()) {

				 GrammaticalRelation rel = edge.getRelation(); 
				IndexedWord a1 = edge.getDependent();
				IndexedWord a2 = edge.getGovernor();

				// System.out.println(a1.originalText()+"Tag: "+a1.tag());
				// System.out.println(a2.originalText()+" Tag: "+a2.tag()+" "+rel.getShortName()+" Relation to "+a1.originalText()+" Tag: "+a1.tag());
				if (a1.tag().contains("NN")) {
					score = 1.0;
					// return

				}
				if (a2.tag().contains("NN")) {
					score = 1.0;
					// return

				}

			}

		}
	}

}