本文整理汇总了Java中edu.stanford.nlp.semgraph.SemanticGraph类的典型用法代码示例。如果您正苦于以下问题:Java SemanticGraph类的具体用法?Java SemanticGraph怎么用?Java SemanticGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SemanticGraph类属于edu.stanford.nlp.semgraph包,在下文中一共展示了SemanticGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSubgraphFromWords
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* 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;
}
示例2: getSubgraph
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
private static SemanticGraph getSubgraph(ObjectArrayList<TypedDependency> tds, SemanticGraph sg, IndexedWord parent,
SemanticGraphEdge e, int maxPathLength, ObjectArrayList<IndexedWord> words){
Set<IndexedWord> children = sg.getChildren(parent);
for (IndexedWord child: children){
if (((sg.getShortestDirectedPathEdges(sg.getFirstRoot(), child)).size() <= maxPathLength) &&
words.contains(child)){
e = sg.getEdge(parent, child);
tds.add(new TypedDependency(e.getRelation(), parent, child));
if (sg.hasChildren(child))
getSubgraph(tds, sg, child, e, maxPathLength, words);
} // else break;
}
TreeGraphNode rootTGN = new TreeGraphNode(new CoreLabel(parent));
EnglishGrammaticalStructure gs = new EnglishGrammaticalStructure(tds, rootTGN);
return SemanticGraphFactory.generateUncollapsedDependencies(gs);
}
示例3: setQuantitiesFromWordList
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* A helper function used in detectQuantities. When we have a list of quantity words, quantity edges and the
* sentence semantic graph, add quantities to the list of quantities and clear the reusable lists.
* If there are quantities in the phrase, replace them with the word SOME_n_i, where i = the place of the quantity
* (0 - subject, 1 - relation, 2 - object) and j = # of quantity within the phrase.
*
* @param qWords: list of quantity indexed words
* @param qEdges: list of semantic graph edges (reusable)
* @param sentSemGraph: sentence semantic graph
* @param i: used for ID-ying purposes of the quantities' annotations
* @param j: used for ID-ying purposes of the quantities' annotations
*/
private void setQuantitiesFromWordList(ObjectArrayList<IndexedWord> qWords, ObjectArrayList<SemanticGraphEdge> qEdges,
SemanticGraph sentSemGraph, int i, int j){
// Quantity ID
StringBuffer sbId = new StringBuffer();
if (i == 0)
sbId.append(Quantity.SUBJECT_ID);
else if (i == 1)
sbId.append(Quantity.RELATION_ID);
else
sbId.append(Quantity.OBJECT_ID);
sbId.append(CHARACTER.UNDERSCORE);
sbId.append(j + 1); // Indexing starts from 1
for (IndexedWord w: qWords){
qEdges.add(sentSemGraph.getEdge(sentSemGraph.getParent(w), w));
}
// Add the quantity to the list
this.quantities.add(new Quantity(qWords, qEdges, sbId.toString()));
// Clear the lists
qWords.clear();
qEdges.clear();
}
示例4: getPolarity
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* Given a phrase and its sentence semantic graph, detect the polarity type. If negative polarity is found, add the
* negative words and edges to their appropriate lists from the Polarity class.
*
* @param phrase: phrase (essentially, list of words, which are part of some sentence)
* @param sentenceSemGraph: the semantic graph of the phrase's sentence
* @return polarity object
*/
public static Polarity getPolarity(AnnotatedPhrase phrase, SemanticGraph sentenceSemGraph){
Polarity pol = new Polarity();
for (int i = 0; i < phrase.getWordList().size(); i++){
// Check for negative adverbs
if (CoreNLPUtils.isAdverb(phrase.getWordList().get(i).tag())){
if (Polarity.NEG_ADVERBS.contains(phrase.getWordList().get(i).lemma())){
Polarity.setNegPol(pol, phrase.getWordList().get(i), sentenceSemGraph.getEdge(
sentenceSemGraph.getParent(phrase.getWordList().get(i)),
phrase.getWordList().get(i)));
}
}
// Check for negative determiners
else if (phrase.getWordList().get(i).tag().equals(POS_TAG.DT)){
if (Polarity.NEG_DETERMINERS.contains(phrase.getWordList().get(i).lemma())){
Polarity.setNegPol(pol, phrase.getWordList().get(i), sentenceSemGraph.getEdge(
sentenceSemGraph.getParent(phrase.getWordList().get(i)),
phrase.getWordList().get(i)));
}
}
}
return pol;
}
示例5: minimize
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* Given an input sentence, dependency parse, mode and a dictionary, make extractions and then minimize them accordingly.
* The parsing occurs OUTSIDE this function.
*
* @param sentence - input sentence
* @param sg - semantic graph object (dependency parse of the sentence)
* @param mode - minimization mode
* @param d - dictionary (for MinIE-D)
*/
public void minimize(String sentence, SemanticGraph sg, Mode mode, Dictionary d) {
// Run ClausIE first
ClausIE clausie = new ClausIE();
clausie.setSemanticGraph(sg);
clausie.detectClauses();
clausie.generatePropositions(clausie.getSemanticGraph());
// Start minimizing by annotating
this.setSemanticGraph(clausie.getSemanticGraph());
this.setPropositions(clausie);
this.setPolarity();
this.setModality();
// Minimize according to the modes (COMPLETE mode doesn't minimize)
if (mode == Mode.SAFE)
this.minimizeSafeMode();
else if (mode == Mode.DICTIONARY)
this.minimizeDictionaryMode(d.words());
else if (mode == Mode.AGGRESSIVE)
this.minimizeAggressiveMode();
this.removeDuplicates();
}
示例6: minimizeSubject
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
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);
}
示例7: minimizeRelation
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* Minimize only the relations that are considered to have "non-frequent patterns"
* @param rel: the relation phrase
* @param sg: semantic graph of the sentence
* @param freqRels: dictionary of multi-word expressions (frequent relations)
*/
public static void minimizeRelation(AnnotatedPhrase rel, SemanticGraph sg, ObjectOpenHashSet<String> collocations){
// Do the safe minimization first
RelSafeMinimization.minimizeRelation(rel, sg);
// If the subject is frequent, don't minimize anything
if (collocations.contains(CoreNLPUtils.listOfWordsToLemmaString(rel.getWordList()).toLowerCase())){
return;
}
// Do the safe minimization first
RelSafeMinimization.minimizeRelation(rel, sg);
// 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<>();
// Move to the dict. minimization of the noun phrases within the relation
Minimization simp = new Minimization(rel, sg, collocations);
simp.nounPhraseDictMinimization(remWords, matchWords);
simp.namedEntityDictionaryMinimization(remWords, matchWords);
}
示例8: minimizeObject
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* 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);
}
示例9: isDescendant
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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;
}
示例10: generatePhrase
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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;
}
示例11: findTypeDependency
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/**
* Finds type dependencies in semantics graph.
*/
private TypedDependency findTypeDependency(SemanticGraph semanticGraph, CoreLabel coreLabel) {
Collection<TypedDependency> deps = semanticGraph.typedDependencies();
for (TypedDependency dependency : deps) {
if (dependency.dep().backingLabel() == coreLabel) {
return dependency;
}
}
//throw new RuntimeException("TypeDependency not found");
return null;
}
示例12: findDescendantRelativeRelation
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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;
}
示例13: removeEdges
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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);
}
}
}
示例14: subgraph
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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;
}
示例15: disconectClauses
import edu.stanford.nlp.semgraph.SemanticGraph; //导入依赖的package包/类
/** 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);
}
}
}