Java Code Examples for edu.stanford.nlp.semgraph.SemanticGraph#getOutEdgesSorted()

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraph#getOutEdgesSorted() . 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: Simplifier.java    From tint with GNU General Public License v3.0 6 votes vote down vote up
protected static void addChildren(HashMultimap<Integer, Integer> children, Set<Integer> stack, IndexedWord current,
            SemanticGraph semanticGraph, Set<IndexedWord> used) {
        List<SemanticGraphEdge> edges = semanticGraph.getOutEdgesSorted(current);
        used.add(current);
        int index = current.index();

        for (Integer integer : stack) {
            children.put(integer, index);
        }

        Set<Integer> newStack = new HashSet<>(stack);
        newStack.add(index);

        for (SemanticGraphEdge edge : edges) {
            IndexedWord target = edge.getTarget();
//            String relation = edge.getRelation().toString();
//            if (relation.equals("punct")) {
//                continue;
//            }
            if (!used.contains(target)) {
                addChildren(children, newStack, target, semanticGraph, used);
            }
        }
    }
 
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: 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 5
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Finds the first occurrence of a grammatical relation or its descendants for a relative pronoun */
public static SemanticGraphEdge findDescendantRelativeRelation(SemanticGraph semanticGraph, IndexedWord root, 
        GrammaticalRelation rel) {
    List<SemanticGraphEdge> outedges = semanticGraph.getOutEdgesSorted(root);
    for (SemanticGraphEdge e : outedges) {
        if (e.getDependent().tag().charAt(0) == 'W' && rel.isAncestor(e.getRelation())) {
            return e;
        } else
            return findDescendantRelativeRelation(semanticGraph, e.getDependent(), rel);
    }
    return null;
}
 
Example 6
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 7
Source File: Simplifier.java    From tint with GNU General Public License v3.0 5 votes vote down vote up
public static Set<IndexedWord> getChildren(SemanticGraph semanticGraph, IndexedWord node) {
    Set<IndexedWord> ret = new HashSet<>();
    List<SemanticGraphEdge> outEdgesSorted = semanticGraph.getOutEdgesSorted(node);
    for (SemanticGraphEdge semanticGraphEdge : outEdgesSorted) {
        ret.add(semanticGraphEdge.getDependent());
    }
    return ret;
}
 
Example 8
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 9
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Creates a constituent for the relative clause implied by rel 
 * @param semanticGraph The semantic graph
 * @param root The root of the constituent
 * @param type The type of the constituent
 */
private static IndexedConstituent createRelConstituent(SemanticGraph semanticGraph, IndexedWord root, Type type) {
    List<SemanticGraphEdge> outrcmod = semanticGraph.getOutEdgesSorted(root);
    SemanticGraphEdge rccop = DpUtils.findFirstOfRelation(outrcmod, EnglishGrammaticalRelations.COPULA);
    if (rccop != null) {
        Set<IndexedWord> excludercmod = DpUtils.exclude(semanticGraph, EXCLUDE_RELATIONS_COMPLEMENT, root);
        return new IndexedConstituent(semanticGraph, root, Collections.<IndexedWord> emptySet(), excludercmod, type);
    } else
        return new IndexedConstituent(semanticGraph, root, type);
}
 
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: 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 12
Source File: ClauseDetector.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Generates a clause from a possessive relation
 * @param subject The subject of the clause
 * @param object  The object of the clause */
private static void addPossessiveClause(ClausIE clausIE, IndexedWord subject, IndexedWord object,
    SemanticGraph semanticGraph) {
  Clause clause = new Clause(semanticGraph);
  SemanticGraph newSemanticGraph = new SemanticGraph(semanticGraph);
  clause.setSubject(0);
  clause.setVerb(1);
  clause.getDobjects().add(2);
  Set<IndexedWord> excludesub = new TreeSet<IndexedWord>();
  Set<IndexedWord> excludeobj = new TreeSet<IndexedWord>();

  excludeobj.add(subject);
  List<SemanticGraphEdge> outedobj = newSemanticGraph.getOutEdgesSorted(object);
  excludeVertexPoss(outedobj, excludeobj, clausIE);

  SemanticGraphEdge rcmod = null;
  if (subject.tag().charAt(0) == 'W') {
    IndexedWord root = newSemanticGraph.getParent(object);
    if (root.tag().equals("IN")) root = newSemanticGraph.getParent(root); // "I saw the man in whose wife I trust"
    List<SemanticGraphEdge> inedges = newSemanticGraph.getIncomingEdgesSorted(root);
    rcmod = DpUtils.findFirstOfRelation(inedges, EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER);
  } else {
    List<SemanticGraphEdge> outedges = newSemanticGraph.getOutEdgesSorted(subject);
    SemanticGraphEdge ps = DpUtils.findFirstOfRelation(outedges, EnglishGrammaticalRelations.POSSESSIVE_MODIFIER);
    if (ps != null) excludesub.add(ps.getDependent());
  }

  if (rcmod != null) {
    clause.getConstituents().add(createRelConstituent(newSemanticGraph, rcmod.getGovernor(), Type.SUBJECT));
    ((IndexedConstituent) clause.getConstituents().get(0)).getExcludedVertexes()
        .addAll(excludesub); // to avoid the s in  "Bill's clothes are great".
  } else {
    clause.getConstituents().add(new IndexedConstituent(newSemanticGraph, subject, Collections.<IndexedWord>emptySet(), excludesub, Type.SUBJECT));
  }
  clause.getConstituents().add(new TextConstituent(clausIE.options.possessiveVerb, Constituent.Type.VERB));
  clause.getConstituents()
      .add(new IndexedConstituent(newSemanticGraph, object, Collections.<IndexedWord>emptySet(), excludeobj, Constituent.Type.DOBJ));
  clause.setType(Clause.Type.SVO);
  clausIE.clauses.add(clause);
}
 
Example 13
Source File: ClauseDetector.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Creates a constituent for the relative clause implied by rel
 * @param semanticGraph The semantic graph
 * @param root The root of the constituent
 * @param type The type of the constituent*/
private static IndexedConstituent createRelConstituent(SemanticGraph semanticGraph, IndexedWord root, Type type) {

  List<SemanticGraphEdge> outrcmod = semanticGraph.getOutEdgesSorted(root);
  SemanticGraphEdge rccop = DpUtils.findFirstOfRelation(outrcmod, EnglishGrammaticalRelations.COPULA);
  if (rccop != null) {
    Set<IndexedWord> excludercmod = DpUtils.exclude(semanticGraph, EXCLUDE_RELATIONS_COMPLEMENT, root);
    return new IndexedConstituent(semanticGraph, root, Collections.<IndexedWord>emptySet(), excludercmod, type);
  } else return new IndexedConstituent(semanticGraph, root, type);
}
 
Example 14
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 15
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Finds the first occurrence of a grammatical relation or its descendants for a relative pronoun */
public static SemanticGraphEdge findDescendantRelativeRelation(SemanticGraph semanticGraph, IndexedWord root, GrammaticalRelation rel) {
  List<SemanticGraphEdge> outedges = semanticGraph.getOutEdgesSorted(root);
  for (SemanticGraphEdge e : outedges) {
    if (e.getDependent().tag().charAt(0) == 'W' && rel.isAncestor(e.getRelation())) {
      return e;
    } else return findDescendantRelativeRelation(semanticGraph, e.getDependent(), rel);
  }
  return null;
}
 
Example 16
Source File: Clause.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private void collectEdges(IndexedWord root, SemanticGraph semanticGraph, Set<SemanticGraphEdge> edges) {
  for(SemanticGraphEdge edge: semanticGraph.getOutEdgesSorted(root)) {
    if(edges.contains(edge)) {
      break;
    }
    edges.add(edge);
    collectEdges(edge.getDependent(), semanticGraph, edges);
  }
}
 
Example 17
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** Generates a clause from a possessive relation
* @param subject The subject of the clause
* @param object  The object of the clause 
*/
private static void addPossessiveClause(ClausIE clausIE, IndexedWord subject, IndexedWord object) {
    Clause clause = new Clause();
    SemanticGraph newSemanticGraph = new SemanticGraph(clausIE.getSemanticGraph());
    clause.setSubject(0);
    clause.verb = 1;
    clause.dobjects.add(2);
    Set<IndexedWord> excludesub = new TreeSet<IndexedWord>();
    Set<IndexedWord> excludeobj = new TreeSet<IndexedWord>();

    excludeobj.add(subject);
    List<SemanticGraphEdge> outedobj = newSemanticGraph.getOutEdgesSorted(object);
    excludeVertexPoss(outedobj, excludeobj, clausIE);

    SemanticGraphEdge rcmod = null;
    if (subject.tag().charAt(0) == 'W') {
        IndexedWord root = newSemanticGraph.getParent(object); 
        if (root != null){
            if (root.tag().equals(POS_TAG.IN)){
                root = newSemanticGraph.getParent(root); // "I saw the man in whose wife I trust"
            }
            List<SemanticGraphEdge> inedges = newSemanticGraph.getIncomingEdgesSorted(root);
            rcmod = DpUtils.findFirstOfRelation(inedges, EnglishGrammaticalRelations.RELATIVE_CLAUSE_MODIFIER);
        }
    } else {
        List<SemanticGraphEdge> outedges = newSemanticGraph.getOutEdgesSorted(subject);
        SemanticGraphEdge ps = DpUtils.findFirstOfRelation(outedges, EnglishGrammaticalRelations.POSSESSIVE_MODIFIER);
        if (ps != null){
            excludesub.add(ps.getDependent());
        }
    }

    if (rcmod != null) {
        clause.constituents.add(createRelConstituent(newSemanticGraph, rcmod.getGovernor(), Type.SUBJECT));
        // To avoid the s in  "Bill's clothes are great".
        ((IndexedConstituent) clause.constituents.get(0)).getExcludedVertexes().addAll(excludesub);
    } else {
        clause.constituents.add(new IndexedConstituent(newSemanticGraph, subject, Collections.<IndexedWord> emptySet(), 
                                                        excludesub, Type.SUBJECT));
    }
    
    // Create a relation phrase with the possessive verb 'has'
    Phrase possPhrase = new Phrase();
    IndexedWord possessiveVerb = new IndexedWord();
    possessiveVerb.setWord("has");
    possessiveVerb.setTag(POS_TAG.VBZ);
    possessiveVerb.setNER(NE_TYPE.NO_NER);
    possessiveVerb.setLemma("have");
    possessiveVerb.setValue("has");
    possessiveVerb.setIndex(-2);
    possPhrase.addWordToList(possessiveVerb);
    possPhrase.setRoot(possessiveVerb);
    
    clause.constituents.add(new PhraseConstituent(possPhrase, Constituent.Type.VERB));
    clause.constituents.add(new IndexedConstituent(newSemanticGraph, object, Collections.<IndexedWord> emptySet(), 
                                                    excludeobj, Constituent.Type.DOBJ));
    clause.setType(Clause.Type.SVO);
    clausIE.getClauses().add(clause);
}
 
Example 18
Source File: ReverbPropositionGeneration.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public int generateReverbRelation(Clause clause, IndexedWord root, SemanticGraph semanticGraph,
    Map<Integer, IndexedWord> included, Map<Integer, IndexedWord> definite,
    boolean top, List<IndexedWord> constituentHeads, List<Integer> allowOmitAlternative) {
  included.put(root.beginPosition(), root);
  if(!isPresent(included, definite, allowOmitAlternative)) {
    included.remove(root.beginPosition());
    return 0;
  } else {
    List<SemanticGraphEdge> outGoingEdges = semanticGraph.getOutEdgesSorted(root);
    int result = 0;
    for (SemanticGraphEdge edge : outGoingEdges) {

      if (DpUtils.isAux(edge) //avoid auxiliaries
          || DpUtils.isNeg(edge) //avoid negations
          || DpUtils.isAnySubj(edge) //avoid subject
          || edge.getDependent().beginPosition() < edge.getGovernor().beginPosition() //avoid left edges (anything before the root)
          || DpUtils.isPunctuation(edge)) { //avoid punctuation
        continue;
      }
      String pos = edge.getDependent().get(CoreAnnotations.PartOfSpeechAnnotation.class);
      if (EXCLUDE_POS_FROM_RELATIOM.contains(pos)) {
        continue;
      }
      if(DpUtils.isAuxPass(edge)) {
        allowOmitAlternative.add(edge.getDependent().beginPosition());
      }
      int success = generateReverbRelation(clause, edge.getDependent(), semanticGraph, included, definite, false, constituentHeads, allowOmitAlternative);
      if(success == 0
          && constituentHeads.contains(edge.getDependent())
          && root.beginPosition() < edge.getDependent().beginPosition()) {
        return 0;
      }
      result = Math.max(result, success);
      if (edge.getDependent().get(CoreAnnotations.PartOfSpeechAnnotation.class).equals("IN") && result < 2) {
        break;
      }
      boolean stop = false;
      if (top && success == 0) {
        for (Constituent c : clause.getConstituents()) {
          if(c instanceof TextConstituent) {
            continue;
          }
          IndexedConstituent ic = (IndexedConstituent) c;
          if (edge.getDependent().equals(ic.root) && (ic.type.equals(Constituent.Type.DOBJ)
              || ic.type.equals(Constituent.Type.ACOMP) || (ic.type.equals(Constituent.Type.COMPLEMENT) && clause.getCop()))) {
            definite.clear();
            stop = true;
          }
        }
      }
      if(stop) {
        break;
      }
    }
    return 1 + result;
  }
}