edu.stanford.nlp.semgraph.SemanticGraphEdge Java Examples

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraphEdge. 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: IndexedConstituent.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/** Checks whether this constituent is a prepositional phrase (i.e., starts with a preposition). */
public boolean isPrepositionalPhrase() { //This is a mess, find other way of fixing. This is purelly heuristic. It needs to know the semantic graph for the sentence after this is fixed the member variable sentSemanticGraph can be removed
  List<IndexedWord> parents = semanticGraph.getParentList(
      root); //This is not the cleanest way semantics messed up. specially with the rel we cannot just check if the head is a preposition (return root.tag().equals("IN")) because the parser some times includes a preposition in the verbal phrase "He is about to win"
  for (IndexedWord parent : parents) {
    SemanticGraphEdge edge = semanticGraph.getEdge(parent, root);
    if (DpUtils.isRel(edge)) return true;
    if (DpUtils.isAnyPrep(edge)) {
      List<IndexedWord> ancestors = semanticGraph.getParentList(parent);
      for (IndexedWord ancestor : ancestors) {
        SemanticGraphEdge ed = semanticGraph.getEdge(ancestor, parent);
        if (DpUtils.isRcmod(ed)) return true;
      }
    }
  }
  return false;
  //return root.tag().equals("IN");
}
 
Example #3
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 #4
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 #5
Source File: StanfordRNNDParser.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
  mappingProvider.configure(jCas.getCas());
  DKPro2CoreNlp converter = new DKPro2CoreNlp();
  Annotation annotatios = converter.convert(jCas, new Annotation());
  List<CoreMap> sentences = annotatios.get(CoreAnnotations.SentencesAnnotation.class);
  for (CoreMap sentence : sentences) {
    GrammaticalStructure gs = parser.predict(sentence);
    SemanticGraph semanticGraph = SemanticGraphFactory.makeFromTree(gs, SemanticGraphFactory.Mode.CCPROCESSED, GrammaticalStructure.Extras.MAXIMAL, null);;
    semanticGraph.prettyPrint();
    semanticGraph = semanticGraphUniversalEnglishToEnglish(semanticGraph);
    sentence.set(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class, semanticGraph);
    for(SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
      System.out.println(edge);
    }
  }
  convertDependencies(jCas, annotatios, true);
}
 
Example #6
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 #7
Source File: ProcessConjunctions.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Checks if two nodes are conjoined by a given conjunction */
private static boolean nextToVerb(IndexedWord firstVerb, IndexedWord secondVerb, IndexedWord conj, 
        SemanticGraph semGraph, List<SemanticGraphEdge> sortedOutEdges){
	// The index of the 'conj' relation in 'sortedOutEdges'. Initially, set to -1.
	int conjIndex = -1;
	
	// Go to the "conj" relation linking the first and the second conjoin.
	// Store the relation index in 'conjIndex'
	for (int i=0; i<=sortedOutEdges.size(); i++){
		SemanticGraphEdge edge = sortedOutEdges.get(i);

		if (edge.getSource().equals(firstVerb) && edge.getTarget().equals(secondVerb) && 
				edge.getRelation().getShortName().equals("conj")){
			conjIndex = i;
			break;
		}
	}
	
	// Move to the first relation to the left. If it is is a "cc" relation, this is the relation corresponding to 
	// the coordinating conjunction (return true), else -> return false
	if (conjIndex > 0){
		SemanticGraphEdge leftConjEdge = sortedOutEdges.get(conjIndex-1);
		if (leftConjEdge.getRelation().getShortName().equals("cc")){
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
 
Example #8
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 #9
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Removes some edges from the given semantic graph.
 * 
 * This method traverses the semantic graph starting from the given root. An edge is removed if
 * (1) its child appears in <code>excludeVertexes</code>, (2) its relation appears in
 * <code>excludeRelations</code>, or (3) the edge has the root as parent and its relation
 * appears in <code>excludeRelationsTop</code>. */
public static void removeEdges(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
        Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop) {
    if (!excludeVertexes.contains(root)) {
        Set<SemanticGraphEdge> edgesToRemove = new HashSet<SemanticGraphEdge>();
        subgraph(graph, root, excludeVertexes, excludeRelations, excludeRelationsTop, edgesToRemove, 0);
        for (SemanticGraphEdge edge : edgesToRemove) {
            graph.removeEdge(edge);
        }
    }
}
 
Example #10
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 #11
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 #12
Source File: StanfordRNNDParser.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static SemanticGraph semanticGraphUniversalEnglishToEnglish(SemanticGraph semanticGraph) {
  for (SemanticGraphEdge edge: semanticGraph.edgeListSorted()) {
    GrammaticalRelation oldRel = edge.getRelation();
    edge.setRelation(EnglishGrammaticalRelations.shortNameToGRel.get(oldRel.getShortName()));
  }
  return semanticGraph;
}
 
Example #13
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Remove the 'punct' edges
 * @param g -> Semantic graph gotten from the dependency parse 
 */
public static void removePunctEdges(SemanticGraph semanticGraph){
    for (SemanticGraphEdge edge : new ArrayList<>(semanticGraph.edgeListSorted())){
        if (EnglishGrammaticalRelations.PUNCTUATION.equals(edge.getRelation())){
            semanticGraph.removeEdge(edge);
        }
    }
}
 
Example #14
Source File: Modality.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Default constructor. Assumes 'certainty' modality type, creates empty lists of poss/cert words and edges **/
public Modality(){
    this.modalityType = Modality.Type.CERTAINTY;
    this.possibilityEdges = new ObjectOpenHashSet<SemanticGraphEdge>();
    this.possibilityWords = new ObjectOpenHashSet<IndexedWord>();
    this.certaintyWords = new ObjectOpenHashSet<IndexedWord>();
    this.certaintyEdges = new ObjectOpenHashSet<SemanticGraphEdge>();
}
 
Example #15
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 #16
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 in a set of edges */
public static SemanticGraphEdge findFirstOfRelation(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
    for (SemanticGraphEdge e : edges) {
        if (rel.equals(e.getRelation())) {
            return e;
        }
    }
    return null;
}
 
Example #17
Source File: Modality.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor with given the modality type, list of possibility words and possibility edges. The certainty 
 * lists of words and edges are empty. 
 * @param t: modality type
 * @param possWords: possibility words
 * @param possEdges: possibility edges
 */
public Modality(Modality.Type t, ObjectOpenHashSet<IndexedWord> possWords, 
        ObjectOpenHashSet<SemanticGraphEdge> possEdges){
    this.modalityType = t;
    this.possibilityWords = possWords;
    this.possibilityEdges = possEdges;
    this.certaintyWords = new ObjectOpenHashSet<IndexedWord>();
    this.certaintyEdges = new ObjectOpenHashSet<SemanticGraphEdge>();
}
 
Example #18
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Implementation for
 * {@link #removeEdges(SemanticGraph, IndexedWord, Collection, Collection, Collection)} */
private static void subgraph(SemanticGraph graph, IndexedWord root, Collection<IndexedWord> excludeVertexes,
    Collection<GrammaticalRelation> excludeRelations, Collection<GrammaticalRelation> excludeRelationsTop,
    Collection<SemanticGraphEdge> edgesToRemove) {
  subgraph(graph, root, excludeVertexes,
      excludeRelations, excludeRelationsTop,
      edgesToRemove, new HashSet<>());
}
 
Example #19
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 #20
Source File: MinIE.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given an annotated proposition, check if it contains a clause modifier as an object. If so, return 'true', else
 * return 'false'
 * @param proposition: annotated proposition
 * @return: 'true' if the object is a clause modifier; 'false' otherwise
 */
public boolean detectClauseModifier(ObjectArrayList<AnnotatedPhrase> proposition){
    /*for (IndexedWord word: proposition.get(1).getWordList()){
        if (word.index() == -2)
            continue;
        if (this.sentenceSemGraph.getParent(word) != null){
            SemanticGraphEdge edge = this.sentenceSemGraph.getEdge(this.sentenceSemGraph.getParent(word), word);
            if ((edge.getRelation() == EnglishGrammaticalRelations.SUBJECT) || 
                (edge.getRelation() == EnglishGrammaticalRelations.NOMINAL_SUBJECT) ||
                (edge.getRelation() == EnglishGrammaticalRelations.CLAUSAL_SUBJECT) ||
                (edge.getRelation() == EnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT)){
                return true;
            }
        }
    }*/
    
    if (CoreNLPUtils.verbInList(proposition.get(2).getWordList())){
        for (IndexedWord word: proposition.get(2).getWordList()){
            if (this.sentenceSemGraph.getParent(word) != null){
                SemanticGraphEdge edge = this.sentenceSemGraph.getEdge(this.sentenceSemGraph.getParent(word), word);
                if ((edge.getRelation() == EnglishGrammaticalRelations.SUBJECT) || 
                    (edge.getRelation() == EnglishGrammaticalRelations.NOMINAL_SUBJECT) ||
                    (edge.getRelation() == EnglishGrammaticalRelations.CLAUSAL_SUBJECT) ||
                    (edge.getRelation() == EnglishGrammaticalRelations.NOMINAL_PASSIVE_SUBJECT)){
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example #21
Source File: ClauseDetector.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** TODO */
private static int ancestorOf(SemanticGraph semanticGraph, IndexedWord node, List<IndexedWord> ancestors) {
  for (SemanticGraphEdge e : semanticGraph.getIncomingEdgesSorted(node)) {
    int index = ancestors.indexOf(node);
    if (index >= 0) return index;
    index = ancestorOf(semanticGraph, e.getGovernor(), ancestors);
    if (index >= 0) return index;
  }
  return -1;
}
 
Example #22
Source File: ClauseDetector.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Creates a constituent for a possessive relative clause
 * @param semanticGraph The semantic graph
 * @param poss The edge referring to the possessive relation
 * @param rcmod The relative clause modifier of the relation
 * @param constGovernor The root of the constituent
 * @param type The type of the constituent
*/
private static Constituent createPossConstituent(SemanticGraph semanticGraph,
        SemanticGraphEdge poss, SemanticGraphEdge rcmod, IndexedWord constGovernor, Type type) {
    SemanticGraph newSemanticGraph = new SemanticGraph(semanticGraph);
    double weight = poss.getWeight();
    newSemanticGraph.addEdge(poss.getGovernor(), rcmod.getGovernor(), EnglishGrammaticalRelations.POSSESSION_MODIFIER, 
                             weight, false);
    Set<IndexedWord> exclude = DpUtils.exclude(newSemanticGraph, EXCLUDE_RELATIONS_COMPLEMENT, rcmod.getGovernor());
    newSemanticGraph.removeEdge(poss);
    newSemanticGraph.removeEdge(rcmod);
    return new IndexedConstituent(newSemanticGraph, constGovernor, Collections.<IndexedWord> emptySet(), exclude, type);
}
 
Example #23
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 #24
Source File: ProcessConjunctions.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Checks if two conjoints verbs share all dependents */
private static boolean shareAll(List<SemanticGraphEdge> outedges, IndexedWord root, IndexedWord conj, 
        SemanticGraph semGraph, GrammaticalRelation rel) {
    for (SemanticGraphEdge edge : outedges) {
        if (DpUtils.isAnySubj(edge) || edge.getDependent().equals(conj))
            continue;
        else if (!isDescendant(conj, root, edge.getDependent(), semGraph))
            return false;
    }

    return true;
}
 
Example #25
Source File: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static void processCC(List<SemanticGraph> semanticGraphs, Options options) {
  boolean exit = false;
  for(SemanticGraph semanticGraph: semanticGraphs) {
    Iterator<SemanticGraphEdge> edges = semanticGraph.edgeIterable().iterator();
    while(edges.hasNext()) {
      SemanticGraphEdge edge = edges.next();
      if(DpUtils.isAnyConj(edge)) {
        boolean addDependenciesToGovernor = false;
        boolean addDependenciesToDependent = false;
        if(edge.getGovernor().get(CoreAnnotations.PartOfSpeechAnnotation.class).startsWith("V")) {
          if(countRelevantOutgoing(edge, semanticGraph, true)) {
            addDependenciesToGovernor = true;
          } else if (countRelevantOutgoing(edge, semanticGraph, false)){
            addDependenciesToDependent = true;
          }
        }
        SemanticGraph nSemanticGraph = createSemanticGraph(semanticGraph, edge,
            addDependenciesToGovernor, addDependenciesToDependent);
        semanticGraphs.add(nSemanticGraph);
        exit = true;
        break;
      }
    }
    if(exit) {
      break;
    }
  }
}
 
Example #26
Source File: Simplifier.java    From tint with GNU General Public License v3.0 5 votes vote down vote up
public static Set<IndexedWord> getParents(SemanticGraph semanticGraph, IndexedWord node) {
    Set<IndexedWord> ret = new HashSet<>();
    List<SemanticGraphEdge> incomingEdgesSorted = semanticGraph.getIncomingEdgesSorted(node);
    if (incomingEdgesSorted.size() > 0) {
        for (SemanticGraphEdge semanticGraphEdge : incomingEdgesSorted) {
            ret.add(semanticGraphEdge.getGovernor());
        }
    }
    return ret;
}
 
Example #27
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/** Checks if a given grammatical relation is contained in a set of edges */
public static boolean containsRelation(List<SemanticGraphEdge> edges, GrammaticalRelation rel) {
  return findFirstOfRelation(edges, rel) != null;
}
 
Example #28
Source File: DpUtils.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** Checks if a given edge holds an object relation */
public static boolean isAnyObj(SemanticGraphEdge edge) {
    return EnglishGrammaticalRelations.OBJECT.isAncestor(edge.getRelation());
}
 
Example #29
Source File: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/** Retrieves the heads of the clauses according to the CCs processing options. The result contains
 * verbs conjoined and a complement if it is conjoined with a verb.*/
public static List<IndexedWord> getIndexedWordsConj(SemanticGraph semanticGraph, Tree depTree, IndexedWord root, GrammaticalRelation rel,
    List<SemanticGraphEdge> toRemove, Options option, boolean firstLevel) {
  List<IndexedWord> ccs = new ArrayList<IndexedWord>(); // to store the conjoints
  if (firstLevel) ccs.add(root);
  List<SemanticGraphEdge> outedges = semanticGraph.outgoingEdgeList(root);
  for (SemanticGraphEdge edge : outedges) {
    if (edge.getRelation().equals(rel)) {
      List<SemanticGraphEdge> outed = semanticGraph.outgoingEdgeList(edge.getDependent());
      // first condition tests if verbs are involved in the conjoints. Conjunctions between complements are treated elsewhere.
      boolean ccVerbs = edge.getDependent().tag().charAt(0) == 'V' || edge.getGovernor().tag().charAt(0) == 'V';
      //This condition will check if there is a cop conjoined with a verb
      boolean ccCop = DpUtils.findFirstOfRelationOrDescendent(outed, EnglishGrammaticalRelations.COPULA) != null;
      // this condition checks if there are two main clauses conjoined by the CC
      boolean ccMainClauses = DpUtils.findFirstOfRelationOrDescendent(outed, EnglishGrammaticalRelations.SUBJECT) != null
          || DpUtils.findFirstOfRelationOrDescendent(outed, EnglishGrammaticalRelations.EXPLETIVE) != null;

      // This flag will check if the cc should be processed according to the flag and the
      // shared elements.
      boolean notProcess = !option.processCcAllVerbs && outed.isEmpty() && shareAll(outedges, depTree, semanticGraph, root, edge.getDependent());

      if ((ccVerbs || ccCop) && !ccMainClauses && !notProcess) {
        ccs.add(edge.getDependent());
      }

      // Disconnects the conjoints. Independent clauses are always disconnected.
      if (((ccVerbs || ccCop) && !notProcess) || ccMainClauses) {
        toRemove.add(edge);

        //To remove the coordination
        if (option.processCcAllVerbs || !notProcess) {
          List<SemanticGraphEdge> conjunct = DpUtils.getEdges(outedges, EnglishGrammaticalRelations.COORDINATION);
          for (SemanticGraphEdge e : conjunct) {
            if (e.getDependent().index() > edge.getDependent().index()) continue;
            if (nextToVerb(depTree, semanticGraph, root, edge.getDependent(), e.getDependent())) {
              toRemove.add(e);
              break;
            }
          }
        }
      }
      ccs.addAll(getIndexedWordsConj(semanticGraph, depTree, edge.getDependent(), rel, toRemove, option, false));
    }
  }
  if (ccs.size() > 1) rewriteGraph(semanticGraph, depTree, ccs);
  return ccs;
}
 
Example #30
Source File: Polarity.java    From minie with GNU General Public License v3.0 4 votes vote down vote up
/** Adding elements to lists **/
public void addNegativeEdge(SemanticGraphEdge e){
    this.negativeEdges.add(e);
}