edu.stanford.nlp.semgraph.SemanticGraph Java Examples

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraph. 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: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/** Checks if a node depending on one conjoint also depends to the other */
//"He buys and sells electronic products" "Is products depending on both sells and buys?"
private static boolean isDescendant(Tree parse, SemanticGraph semanticGraph, IndexedWord checkIW, IndexedWord pivotIW, IndexedWord elementIW) {

  Tree pivot = DpUtils.getNode(pivotIW, parse, semanticGraph);
  Tree check = DpUtils.getNode(checkIW, parse, semanticGraph);
  Tree element = DpUtils.getNode(elementIW, parse, semanticGraph);

  while ((!element.value().equals("ROOT"))) {// find a common parent between the head conjoint
    // and the constituent of the element
    if (element.pathNodeToNode(element, pivot) != null) // is this efficient enough?
      break;
    element = element.parent(parse);
  }

  return element.dominates(check);
}
 
Example #2
Source File: DpUtils.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
/** Correspondence between nodes in Tree and SemanticGraph */
public static Tree getNode(IndexedWord word, Tree depTree, SemanticGraph semanticGraph) {
  int indexSC = semanticGraph.vertexListSorted().indexOf(word);
  int indexDT = Integer.MAX_VALUE;
  Tree result = null;
  List<Tree> descTree = depTree.getLeaves();
  for (int i = descTree.size() - 1; i >= 0; i--) {
    if (descTree.get(i).toString().equals(word.word())) {
      if (i - indexSC < 0) break;
      else if ((i - indexSC) < indexDT) {
        result = descTree.get(i);
        indexDT = i - indexSC;
      }
    }
  }
  return result;
}
 
Example #3
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 #4
Source File: ProcessConjunctions.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
/** Checks if a node depending on one conjoint also depends to the other */
//"He buys and sells electronic products" "Is products depending on both sells and buys?"
private static boolean isDescendant(IndexedWord checkWord, IndexedWord pivotWord, IndexedWord elementWord, 
        SemanticGraph semGraph) {
    Collection <IndexedWord> roots = semGraph.getRoots();
    
    while (!roots.contains(elementWord)){
        if (!semGraph.getShortestUndirectedPathNodes(elementWord, pivotWord).isEmpty())
            break;
        elementWord = semGraph.getParent(elementWord);
    }
    List<SemanticGraphEdge> path = semGraph.getShortestDirectedPathEdges(elementWord, checkWord);
    if (path == null)
        return false;
    else
        return true;
}
 
Example #5
Source File: NumberOfToken.java    From NLIWOD with GNU Affero General Public License v3.0 6 votes vote down vote up
/***
 * Returns a list of all noun phrases of the question q.
 * @param q  a question
 * @return list of noun phrases
 */
private ArrayList<String> getNounPhrases(String q) {
		ArrayList<String> nounP = new ArrayList<String>();
    
		Annotation annotation = new Annotation(q);
       PIPELINE.annotate(annotation);
     
       List<CoreMap> question = annotation.get(CoreAnnotations.SentencesAnnotation.class);
       
       for (CoreMap sentence : question) {
           SemanticGraph basicDeps = sentence.get(BasicDependenciesAnnotation.class);
           Collection<TypedDependency> typedDeps = basicDeps.typedDependencies();
        
           Iterator<TypedDependency> dependencyIterator = typedDeps.iterator();
           while(dependencyIterator.hasNext()) {
           	TypedDependency dependency = dependencyIterator.next();
           	String depString = dependency.reln().toString();
           	if(depString.equals("compound") || depString.equals("amod")) {
           		String dep = dependency.dep().toString();
           		String gov = dependency.gov().toString();
           		nounP.add(dep.substring(0, dep.lastIndexOf("/")) + " " + gov.substring(0, gov.lastIndexOf("/")));
           	}
           }
       }    
       return nounP;
	}
 
Example #6
Source File: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private static void removeUnnecessary(List<Constituent> result) {
  for (Constituent c : result) {
    SemanticGraph semanticGraph = ((IndexedConstituent) c).getSemanticGraph();
    IndexedWord root = ((IndexedConstituent) c).getRoot();
    List<SemanticGraphEdge> edges = semanticGraph.edgeListSorted();
    Set<IndexedWord> descendants = semanticGraph.descendants(root);
    for (int i = 0; i < edges.size(); i++) {
      if (!descendants.contains(edges.get(i).getDependent())) {
        semanticGraph.removeEdge(edges.get(i));
        edges = semanticGraph.edgeListSorted();
        i--;
      }
    }
  }

}
 
Example #7
Source File: SubjDictionaryMinimization.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
public static void minimizeSubject(AnnotatedPhrase subject, SemanticGraph sg, ObjectOpenHashSet<String> collocations){
    // Do the safe minimization first
    SubjSafeMinimization.minimizeSubject(subject, sg);
    
    // If the subject is frequent, don't minimize anything
    if (collocations.contains(CoreNLPUtils.listOfWordsToLemmaString(subject.getWordList()).toLowerCase())){
        return;
    }
    
    // Minimization object
    Minimization simp = new Minimization(subject, sg, collocations);
    
    // remWords: list of words to be removed (reusable variable)
    // matchWords: list of matched words from the regex (reusable variable)
    List<CoreMap> remWords = new ArrayList<>();
    List<CoreMap> matchWords = new ArrayList<>(); 
    
    // Safe minimization on the noun phrases and named entities within the subj. phrase
    simp.nounPhraseDictMinimization(remWords, matchWords);
    simp.removeVerbsBeforeNouns(remWords, matchWords);
    simp.namedEntityDictionaryMinimization(remWords, matchWords);
}
 
Example #8
Source File: ProcessConjunctions.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private static SemanticGraph createSemanticGraph(SemanticGraph semanticGraph, SemanticGraphEdge r,
    boolean addDependenciesToGovernor, boolean addDependenciesToDependent) {
  SemanticGraph result = new SemanticGraph(semanticGraph);
  DpUtils.replaceNodeFromSemanticGraph(r.getGovernor(), r.getDependent(), result, true);
  DpUtils.disconectNodeFromSemanticGraph(r.getDependent(), semanticGraph);
  DpUtils.removeEdges(semanticGraph, DpUtils.findAllRelations(semanticGraph.getOutEdgesSorted(r.getGovernor()),
      EnglishGrammaticalRelations.COORDINATION));
  if(addDependenciesToGovernor) {
    addEdges(result.getOutEdgesSorted(r.getDependent()), r.getGovernor(), semanticGraph);
  }
  if(addDependenciesToDependent) {
    addEdges(semanticGraph.getOutEdgesSorted(r.getGovernor()), r.getDependent(), result);
  }
  result.prettyPrint();
  return result;
}
 
Example #9
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 #10
Source File: PropositionGenerator.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
/** Generates a textual representation of a given constituent plus a set of words*/
private Phrase generatePhrase(IndexedConstituent constituent, Collection<IndexedWord> words, SemanticGraph sGraph) {
    Phrase phrase = new Phrase();
    
    if (constituent.isPrepositionalPhrase(sGraph)) {
        // TODO: before, it was: constituent.getRoot().originalText(). For some reason, in the case for
        // "in Los Angeles", the word "in" returns empty string for originalText(), and the actual word for word().
        // Check if this compromises the code in some way
        // TODO: see if you could find a faster way to make this check (not to go through the list of all excluded
        // words, for instance: use a flag as an input parameter)
        if (!constituent.excludedVertexes.contains(constituent.getRoot())){
            phrase.addWordToList(constituent.getRoot());
        }
    }

    for (IndexedWord word : words) {
        if (DpUtils.filterTokens(word))
            continue;
        phrase.addWordToList(word);
    }

    return phrase;
}
 
Example #11
Source File: ObjDictionaryMinimization.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Minimize only the objects that are considered to have "non-frequent patterns"
 * @param obj: the object phrase
 * @param sg: semantic graph of the sentence
 * @param freqObjs: dictionary of multi-word expressions (frequent objects)
 */
public static void minimizeObject(AnnotatedPhrase obj, SemanticGraph sg, ObjectOpenHashSet<String> collocations){
    // Do the safe minimization first
    ObjSafeMinimization.minimizeObject(obj, sg);
    
    // If the object is frequent, don't minimize anything
    if (collocations.contains(CoreNLPUtils.listOfWordsToLemmaString(obj.getWordList()).toLowerCase())){
        return;
    }
    
    // Minimization object
    Minimization simp = new Minimization(obj, sg, collocations);
    
    // remWords: list of words to be removed (reusable variable)
    // matchWords: list of matched words from the regex (reusable variable)
    List<CoreMap> remWords = new ArrayList<>();
    List<CoreMap> matchWords = new ArrayList<>(); 
    
    // Safe minimization on the noun phrases and named entities within the subj. phrase
    simp.nounPhraseDictMinimization(remWords, matchWords);
    simp.namedEntityDictionaryMinimization(remWords, matchWords);
}
 
Example #12
Source File: Trees.java    From uncc2014watsonsim with GNU General Public License v2.0 6 votes vote down vote up
public static List<CoreMap> parse(String text) {
    
    // create an empty Annotation just with the given text
    Annotation document = new Annotation(text);
    
    // run all Annotators on this text
    pipeline.annotate(document);
    
    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    List<Tree> trees = new ArrayList<>();
    List<Tree> dependencies = new ArrayList<>();
    
    for(CoreMap sentence: sentences) {
      // this is the parse tree of the current sentence
    	Tree t = sentence.get(TreeAnnotation.class);
    	SemanticGraph graph = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
    	trees.add(t);
    }
    return sentences;
}
 
Example #13
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 #14
Source File: CoreNLPUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a CoreNLP pipeline and an input sentence, generate dependency parse for the sentence and return
 * the SemanticGraph object as a result
 * @param pipeline - CoreNLP pipeline
 * @param snt - input sentence
 * @return dependency parse in SemanticGraph object
 */
public static SemanticGraph parse(StanfordCoreNLP pipeline, String snt) {
    Annotation document = new Annotation(snt);
    pipeline.annotate(document);
    
    //A CoreMap is a sentence with annotations
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    SemanticGraph semanticGraph = null;
    for(CoreMap sentence: sentences) {
        semanticGraph = sentence.get(BasicDependenciesAnnotation.class);
    }
    
    return semanticGraphUniversalEnglishToEnglish(semanticGraph);
}
 
Example #15
Source File: Phrase.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a sentence semantic graph, set the typed dependencies list of the phrase. For this to work, the list of 
 * words (this.wordlist) must be already known. Otherwise, the tds list will be empty. Each typed dependency in the list
 * must contain both the parent and the child in the wordslist. 
 * @param sg: sentence semantic graph (the phrase must be derived from this graph, i.e. all the nodes and edges of the 
 *            phrase must be found in this graph. Otherwise, the TDs list will be empty)
 */
public void setTdsFromSentenceSemGraph(SemanticGraph sg){
    // If the semantic graph of the sentence or the list of words are empty, return
    if (sg.isEmpty() || this.wordList.isEmpty()){
        tds = new ObjectArrayList<TypedDependency>();
        return;
    }
        
    for (TypedDependency td: sg.typedDependencies()){
        if (this.wordList.contains(td.dep()) && this.wordList.contains(td.gov()))
            this.tds.add(td);
    }
}
 
Example #16
Source File: ObjSafeMinimization.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Minimize only the objects that are considered to have "safe patterns"
 * @param object: the objects phrase
 * @param sg: the semantic graph of the whole sentence
 */
public static void minimizeObject(AnnotatedPhrase object, SemanticGraph sg){
    Minimization simp = new Minimization(object, sg, new ObjectOpenHashSet<String>());
    
    // remWords: list of words to be removed (reusable variable)
    // matchWords: list of matched words from the regex (reusable variable)
    List<CoreMap> remWords = new ArrayList<>();
    List<CoreMap> matchWords = new ArrayList<>(); 
    
    // Safe minimization on the noun phrases and named entities
    simp.nounPhraseSafeMinimization(remWords, matchWords);
    simp.namedEntitySafeMinimization(remWords, matchWords);
}
 
Example #17
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 #18
Source File: Extract.java    From phrases with Apache License 2.0 5 votes vote down vote up
private HashSet<Pattern> ExtractSentencePatterns(CoreMap sentence) {
    SemanticGraph semanticGraph = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);

    List<Pattern> primary = ExtractPrimaryPatterns(semanticGraph.typedDependencies());

    List<Pattern> combined;
    combined = ExtractCombinedPatterns(primary, primary);
    combined.addAll(ExtractCombinedPatterns(combined, primary));
    combined.addAll(ExtractCombinedPatterns(combined, primary));

    return PruneCombinedPatterns(combined);
}
 
Example #19
Source File: ClauseDetector.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Generates a clause from an apposition
 * @param subject The subject of the clause (first argument of the appos relation)
 * @param object  The object of the clause (second argument of the appos relation)*/
private static void addApposClause(ClausIE clausIE, IndexedWord subject, IndexedWord object, SemanticGraph semanticGraph) {
  Clause clause = new Clause(semanticGraph);
  clause.setSubject(0);
  clause.setVerb(1);
  clause.setComplement(2);
  clause.getConstituents().add(new IndexedConstituent(semanticGraph, subject, Constituent.Type.SUBJECT));
  clause.getConstituents().add(new TextConstituent(clausIE.options.appositionVerb, Constituent.Type.VERB));
  clause.getConstituents().add(new IndexedConstituent(semanticGraph, object, Constituent.Type.COMPLEMENT));
  clause.setType(Clause.Type.SVC);
  clausIE.clauses.add(clause);
}
 
Example #20
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 #21
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 #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: 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 #24
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 #25
Source File: IndexedConstituent.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
/** Returns a copy of the semantic graph of this constituent in which all edges (from any
 * included vertex) to excluded vertexes have been removed. Useful for proposition generation. */
public SemanticGraph createReducedSemanticGraph() {
  SemanticGraph result = new SemanticGraph(semanticGraph);
  DpUtils.removeEdges(result, root, excludedVertexes);
  for (IndexedWord v : additionalVertexes) {
    DpUtils.removeEdges(result, v, excludedVertexes);
  }
  return result;
}
 
Example #26
Source File: DpUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
public static void removePunctFromSemGraph(SemanticGraph semanticGraph){
    for (IndexedWord vertex : new ArrayList<>(semanticGraph.vertexSet())){
        if (vertex.value().equals(",") || vertex.value().equals(".") || vertex.value().equals("\'\'")){
            semanticGraph.removeVertex(vertex);
        } 
    }
}
 
Example #27
Source File: CoreNLPTest.java    From Shour with MIT License 5 votes vote down vote up
@Ignore
public void testGetDependency(){
    System.out.println("dependency");
    System.out.println(nlp.getDependency("今天天气不错,我们出去走走,不出去的话对不起这大好春光啊!").toString(SemanticGraph.OutputFormat.LIST));
    System.out.println(nlp.getDependency("今天天气真晴朗").toString(SemanticGraph.OutputFormat.LIST));
    System.out.println(nlp.getDependency("你猜猜我是谁").toString(SemanticGraph.OutputFormat.LIST));
    System.out.println(nlp.getDependency("真好").toString(SemanticGraph.OutputFormat.LIST));
    System.out.println(nlp.getDependency("你被我爱着").toString(SemanticGraph.OutputFormat.LIST));
    System.out.println(nlp.getDependency("最近过的怎么样").toString(SemanticGraph.OutputFormat.LIST));
}
 
Example #28
Source File: CoreNLPUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a list of indexed words and a semantic graph, return the root word of the word list. We assume that
 * all the words from the list can be found in the semantic graph sg, and the words in wordList are connected
 * within the semantic graph of the sentence - sg, and that they all share a common root.
 * @param sg: semantic graph of the sentence
 * @param wordList: the phrase from the sentence, represented as a list of words
 * @return the root word from the phrase
 */
public static IndexedWord getRootFromWordList(SemanticGraph sg, ObjectArrayList<IndexedWord> wordList){
    // If the word list is consisted of one word - return that word
    if (wordList.size() == 1) return wordList.get(0);
    
    IndexedWord constituentRoot = null;
    
    // We only search as high as grandparents
    // constituentRoot = sg.getCommonAncestor(wordList.get(0), wordList.get(wordList.size()-1));

    // If the commonancestor is deeper in the tree, the constituent root is the word with shortest distance
    // to the root of the sentence
    int minPathToRoot = Integer.MAX_VALUE;
    int pathToRoot = -1;
    for (int i = 0; i < wordList.size(); i++){
        // The words with index -2 are the ones that cannot be found in the semantic graph (synthetic words)
        // This happens in the relations (see in clausie.ClauseDetector.java), and those words are the head words
        if (wordList.get(i).index() == -2){
            return wordList.get(i);
        }
        pathToRoot = sg.getShortestDirectedPathNodes(sg.getFirstRoot(), wordList.get(i)).size();
        if (pathToRoot < minPathToRoot){ //TODO: throws NPE sometimes
            minPathToRoot = pathToRoot;
            constituentRoot = wordList.get(i);
        }
    }

    return constituentRoot;
}
 
Example #29
Source File: MinIE.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/** Default constructor **/
public MinIE(){
    this.propositions = new ObjectArrayList<AnnotatedProposition>();
    this.sentenceSemGraph = new SemanticGraph();
    this.sentence = new ObjectArrayList<>();
    this.propsWithAttribution = new ObjectOpenHashSet<>();
}
 
Example #30
Source File: CoreNLPUtils.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given a list of indexed words and a semantic graph, return the root word of the word list. We assume that
 * all the words from the list can be found in the semantic graph sg, and the words in wordList are connected
 * within the semantic graph of the sentence - sg. If there are multiple words which have the shortest distance
 * to the sentence root, then choose the most-left verb. 
 * 
 * @param sg: sentence semantic graph
 * @param wordsList: list of words from which to choose "root" from
 * @return
 */
public static IndexedWord getVerbRootFromWordList(SemanticGraph sg, ObjectArrayList<IndexedWord> wordList){
    IndexedWord constituentRoot = null;
    IntArrayList shortestDirectedPathDistances = new IntArrayList();
    
    int minPathToRoot = Integer.MAX_VALUE;
    int pathToRoot = -1;
    
    for (int i = 0; i < wordList.size(); i++){
        // The words with index -2 are the ones that cannot be found in the semantic graph (synthetic words)
        // This happens in the relations (see in clausie.ClauseDetector.java), and those words are the head words
        if (wordList.get(i).index() == -2){
            return wordList.get(i);
        }
        pathToRoot = sg.getShortestDirectedPathNodes(sg.getFirstRoot(), wordList.get(i)).size();
        if (pathToRoot < minPathToRoot){
            minPathToRoot = pathToRoot;
        }
        shortestDirectedPathDistances.add(pathToRoot);
    }
    
    // If the shortest path is one element, return it, else, return the first verb containing that index
    if (FastUtil.countElement(minPathToRoot, shortestDirectedPathDistances) == 1)
        return wordList.get(shortestDirectedPathDistances.indexOf(minPathToRoot));
    else {
        for (int i = 0; i < shortestDirectedPathDistances.size(); i++){
            if (shortestDirectedPathDistances.getInt(i) == minPathToRoot){
                if (isVerb(wordList.get(i).tag())){
                    constituentRoot = wordList.get(i);
                    break;
                }
            }
        }
    }
    
    return constituentRoot;
}