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

The following examples show how to use edu.stanford.nlp.semgraph.SemanticGraphEdge#getRelation() . 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: 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 2
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 3
Source File: CoreNLPUtils.java    From minie with GNU General Public License v3.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 4
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 5
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

				}

			}

		}
	}

}
 
Example 6
Source File: SupportCandidateType.java    From uncc2014watsonsim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Find simple statements of type in regular text, such as "Diabetes is a
 * common disease"
 * 
 * Subclasses are very similarly stated, such as "A hummingbird is a kind
 * of bird." But we don't distinguish between these yet. We should though.
 * 
 * @return Pairs of nouns and their types.
 */
public static List<Pair<String, String>> extract(Phrase p) {
	List<Pair<String, String>> names_and_types = new ArrayList<>();
	for (SemanticGraph graph: p.getGraphs()){
		StringBuilder theory = new StringBuilder();
		// Load data into a model
		
		// Add all the edges
		for (SemanticGraphEdge edge : graph.edgeIterable()) {
			// I like the specific prepositions better
			// so change them to match
			GrammaticalRelation rel = edge.getRelation();
			String relation_name = rel.getShortName();
			if ( (rel.getShortName().equals("prep")
					|| rel.getShortName().equals("conj"))
					&& rel.getSpecific() != null
					&& !rel.getSpecific().isEmpty()) {
				relation_name = rel.getShortName() + "_" + CharSetUtils.keep(rel.getSpecific().toLowerCase(), "abcdefghijklmnopqrstuvwxyz");
			}
			theory.append(relation_name);
			theory.append('(');
			theory.append(wordID(edge.getGovernor()));
			theory.append(',');
			theory.append(wordID(edge.getDependent()));
			theory.append(").\n");
		}
		// Index the words
		for (IndexedWord word : graph.vertexSet()) {
			theory.append("tag(");
			theory.append(wordID(word));
			theory.append(',');
			String tag = clean(word.tag());
			theory.append(tag.isEmpty() ? "misc" : tag);
			theory.append(").\n");
		}

		Prolog engine = new Prolog();
		try {
			engine.setTheory(new Theory(
					Files.toString(new File("src/main/parse.pl"), Charset.forName("UTF-8"))));
			log.debug(theory);
			engine.addTheory(new Theory(theory.toString()));
			
			SolveInfo info = engine.solve("type_c(X, Y).");

			// Get the resulting matches
			while (info.isSuccess()) {
				IndexedWord subj_idx = idWord(graph, info.getTerm("X").toString());
				IndexedWord obj_idx = idWord(graph, info.getTerm("Y").toString());
				if (subj_idx.tag().startsWith("NN")
						&& obj_idx.tag().startsWith("NN")) {
					String noun = Trees.concatNoun(graph, subj_idx);
					String type = obj_idx.originalText(); //concatNoun(graph, obj_idx);
					log.info("Discovered " + noun + " is a(n) " + type);
					names_and_types.add(new Pair<>(noun,type));
				}
				if (engine.hasOpenAlternatives()) {
					info = engine.solveNext();
				} else {
					break;
				}
			}
			
		} catch (IOException | InvalidTheoryException
				| MalformedGoalException | NoSolutionException
				| NoMoreSolutionException | UnknownVarException e) {
               System.out.println(theory);
			e.printStackTrace();
		}
	}
	return names_and_types;
}