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

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraph#typedDependencies() . 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: CoreNLPUtils.java    From minie with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Given the sentence semantic graph and a list of words, get a subgraph containing just the words in the list
 * 'words'. Each typed dependency has each word from the list as a governor.
 * @param sg: sentence semantic graph
 * @param words: list of words which should contain the semantic graph
 * @return subgraph containing the words from 'words'
 * TODO: this needs to be double checked! In some cases we have weird graphs, where there are words missing. 
 * E.g. the sentence 120 from NYT "The International ... ". Try this for getting the subgraph when the source is 
 * detected.
 */
public static SemanticGraph getSubgraphFromWords(SemanticGraph sg, ObjectArrayList<IndexedWord> words){        
    // Determining the root
    int minInd = Integer.MAX_VALUE;
    IndexedWord root = new IndexedWord();
    for (IndexedWord w: words){
        if (w.index() < minInd){
            minInd = w.index();
            root = w;
        }
    }
    
    // Getting the typed dependency
    ObjectArrayList<TypedDependency> tds = new ObjectArrayList<TypedDependency>();
    for (TypedDependency td: sg.typedDependencies()){
        if (words.contains(td.gov()) && words.contains(td.dep()))
            tds.add(td);
    }
    
    // Create the semantic graph
    TreeGraphNode rootTGN = new TreeGraphNode(new CoreLabel(root));
    EnglishGrammaticalStructure gs = new EnglishGrammaticalStructure(tds, rootTGN);
    SemanticGraph phraseSg = SemanticGraphFactory.generateUncollapsedDependencies(gs);
    
    return phraseSg;
}
 
Example 2
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 3
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 4
Source File: CoreNLPDependencyParser.java    From Heracles with GNU General Public License v3.0 4 votes vote down vote up
@Override
	public void validatedProcess(Dataset dataset, String spanTypeOfSentenceUnit) {
		Properties prop1 = new Properties();
		prop1.setProperty("annotators", "depparse");
		
		StanfordCoreNLP pipeline = new StanfordCoreNLP(prop1, false);
		
		for (Span span : dataset.getSpans(spanTypeOfSentenceUnit)){

			
			HashMap<Integer, Word> wordIndex = new HashMap<>();
			Annotation a = CoreNLPHelper.reconstructStanfordAnnotations(span, wordIndex);
			
			
//			Main.debug(span.toString());
			
			pipeline.annotate(a);
						
			for (CoreMap sentence : a.get(SentencesAnnotation.class)){
								
				//per sentence, get the dependencies
				SemanticGraph dependencies = sentence.get(EnhancedPlusPlusDependenciesAnnotation.class);
				
				for (TypedDependency td : dependencies.typedDependencies()){
//					Main.debug(td.toString());
					String relationType = td.reln().getLongName();
					Word dep = wordIndex.get(td.dep().beginPosition());
					DataEntity gov = wordIndex.get(td.gov().beginPosition());
					if (gov == null){
						//this is the root, link to sentence
						gov = span;
					}
					if (dep == null || gov == null){
						Framework.debug(td.toString());
						Framework.debug(td.dep().beginPosition() + "\t" + td.gov().beginPosition());
						Framework.debug(wordIndex.toString());
					}
					Relation rel = new Relation("deps", gov, dep);
					rel.putAnnotation("relationLongName", td.reln().getLongName());
					if (td.reln().getParent() != null)
						rel.putAnnotation("relationParentShortName", td.reln().getParent().getShortName());
					rel.putAnnotation("relationShortName", td.reln().getShortName());
//					rel.putAnnotation("relationSpecific", td.reln().getSpecific());
					dep.getRelations().addRelationToParent(rel);
					gov.getRelations().addRelationToChild(rel);
					
				}				
//				dependencies.prettyPrint();
			}
			
		}

	}
 
Example 5
Source File: DependencyProjectorCoNLL.java    From phrasal with GNU General Public License v3.0 4 votes vote down vote up
public static HashMap<Integer, Integer> getDependenciesFromCoreMap(CoreMap annotation) {

    SemanticGraph semanticGraph = annotation.get(BasicDependenciesAnnotation.class);
    Collection<TypedDependency> dependencies = semanticGraph.typedDependencies();

    
    HashMap<Integer, Integer> reverseDependencies = new HashMap<Integer, Integer>() ;

    for (TypedDependency dep : dependencies) {
      int govIndex = dep.gov().index() - 1;
      int depIndex = dep.dep().index() - 1;
      reverseDependencies.put(depIndex, govIndex);
    }
    
    return reverseDependencies;
  }