use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class DepPatternFactory method getContext.
static Set<DepPattern> getContext(IndexedWord w, SemanticGraph graph, Set<CandidatePhrase> stopWords, DataInstance sent) {
Set<DepPattern> patterns = new HashSet<>();
IndexedWord node = w;
int depth = 1;
while (depth <= upDepth) {
IndexedWord parent = graph.getParent(node);
if (parent == null)
break;
GrammaticalRelation rel = graph.reln(parent, node);
for (Pattern tagPattern : allowedTagPatternForTrigger) {
if (tagPattern.matcher(parent.tag()).matches()) {
if (!ifIgnoreRel(rel) && !stopWords.contains(CandidatePhrase.createOrGet(parent.word())) && parent.word().length() > 1) {
Pair<IndexedWord, GrammaticalRelation> pattern = new Pair<>(parent, rel);
DepPattern patterndep = patternToDepPattern(pattern, sent);
if (depth <= upDepth) {
patterns.add(patterndep);
}
// if (depth <= maxDepth) {
// Counter<String> phrasesForPattern = phrasesForPatternForSent.get(patternStr);
// if (phrasesForPattern == null)
// phrasesForPattern = new ClassicCounter<String>();
// phrasesForPattern.incrementCount(phrase);
// phrasesForPatternForSent.put(patternStr, phrasesForPattern);
// }
// if (DEBUG >= 1)
// System.out.println("for phrase " + phrase + " pattern is " + patternStr);
}
}
}
node = parent;
depth++;
}
return patterns;
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class ExtractPhraseFromPattern method descendantsHelper.
private static void descendantsHelper(SemanticGraph g, IndexedWord curr, Set<IndexedWord> descendantSet, List<String> allCutOffRels, List<IndexedWord> doNotAddThese, List<IndexedWord> seenNodes, boolean ignoreCommonTags, Function<CoreLabel, Boolean> acceptWord, CollectionValuedMap<Integer, String> feat) throws Exception {
if (seenNodes.contains(curr))
return;
seenNodes.add(curr);
if (descendantSet.contains(curr) || (doNotAddThese != null && doNotAddThese.contains(curr)) || !acceptWord.apply(curr.backingLabel())) {
return;
}
if (!ignoreCommonTags || !ignoreTags.contains(curr.tag().trim())) {
descendantSet.add(curr);
}
for (IndexedWord child : g.getChildren(curr)) {
boolean dontuse = false;
if (doNotAddThese != null && doNotAddThese.contains(child))
dontuse = true;
GrammaticalRelation rel = null;
if (dontuse == false) {
rel = g.reln(curr, child);
dontuse = checkIfSatisfiesRelConstrains(g, curr, child, allCutOffRels, rel);
}
if (dontuse == false) {
for (String cutOffTagRegex : cutoffTags) {
if (child.tag().matches(cutOffTagRegex)) {
if (DEBUG >= 5)
System.out.println("ignored tag " + child + " because it satisfied " + cutOffTagRegex);
dontuse = true;
break;
}
}
}
if (dontuse == false) {
if (!feat.containsKey(curr.index())) {
feat.put(curr.index(), new ArrayList<>());
}
GetPatternsFromDataMultiClass.getFeatures(g, curr, false, feat.get(curr.index()), rel);
//feat.add(curr.index(), "REL-" + rel.getShortName());
descendantsHelper(g, child, descendantSet, allCutOffRels, doNotAddThese, seenNodes, ignoreCommonTags, acceptWord, feat);
}
}
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class ProtobufAnnotationSerializer method fromProto.
/**
* Deserialize a dependency tree, allowing for cross-sentence arcs.
* This is primarily here for deserializing OpenIE triples.
*
* @see ProtobufAnnotationSerializer#fromProto(CoreNLPProtos.DependencyGraph, List, String)
*/
private static SemanticGraph fromProto(CoreNLPProtos.DependencyGraph proto, List<CoreLabel> sentence, String docid, Optional<Annotation> document) {
SemanticGraph graph = new SemanticGraph();
// first construct the actual nodes; keep them indexed by their index
// This block is optimized as one of the places which take noticeable time
// in datum caching
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (CoreNLPProtos.DependencyGraph.Node in : proto.getNodeList()) {
min = in.getIndex() < min ? in.getIndex() : min;
max = in.getIndex() > max ? in.getIndex() : max;
}
TwoDimensionalMap<Integer, Integer, IndexedWord> nodes = TwoDimensionalMap.hashMap();
for (CoreNLPProtos.DependencyGraph.Node in : proto.getNodeList()) {
CoreLabel token;
if (document.isPresent()) {
// token index starts at 1!
token = document.get().get(SentencesAnnotation.class).get(in.getSentenceIndex()).get(TokensAnnotation.class).get(in.getIndex() - 1);
} else {
// index starts at 1!
token = sentence.get(in.getIndex() - 1);
}
IndexedWord word;
if (in.hasCopyAnnotation() && in.getCopyAnnotation() > 0) {
// TODO: if we make a copy wrapper CoreLabel, use it here instead
word = new IndexedWord(new CoreLabel(token));
word.setCopyCount(in.getCopyAnnotation());
} else {
word = new IndexedWord(token);
}
// these fields set, but annotations older than August 2014 might not
if (word.docID() == null && docid != null) {
word.setDocID(docid);
}
if (word.sentIndex() < 0 && in.getSentenceIndex() >= 0) {
word.setSentIndex(in.getSentenceIndex());
}
if (word.index() < 0 && in.getIndex() >= 0) {
word.setIndex(in.getIndex());
}
assert in.getIndex() == word.index();
nodes.put(in.getIndex(), in.getCopyAnnotation(), word);
graph.addVertex(word);
}
// add all edges to the actual graph
for (CoreNLPProtos.DependencyGraph.Edge ie : proto.getEdgeList()) {
IndexedWord source = nodes.get(ie.getSource(), ie.getSourceCopy());
assert (source != null);
IndexedWord target = nodes.get(ie.getTarget(), ie.getTargetCopy());
assert (target != null);
synchronized (globalLock) {
// this is not thread-safe: there are static fields in GrammaticalRelation
assert ie.hasDep();
GrammaticalRelation rel = GrammaticalRelation.valueOf(fromProto(ie.getLanguage()), ie.getDep());
graph.addEdge(source, target, rel, 1.0, ie.hasIsExtra() && ie.getIsExtra());
}
}
if (proto.getRootCount() > 0) {
Collection<IndexedWord> roots = proto.getRootList().stream().map(rootI -> nodes.get(rootI, 0)).collect(Collectors.toList());
graph.setRoots(roots);
} else {
// compute root nodes if non-empty
if (!graph.isEmpty()) {
graph.resetRoots();
}
}
return graph;
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class AddDep method createEngAddDep.
/**
* Creates an EnglishGrammaticalRelation AddDep edit.
* @param newNode String representation of new dependent IndexedFeatureNode map.
*/
public static AddDep createEngAddDep(String govNodeName, String engRelation, String newNode) {
GrammaticalRelation relation = EnglishGrammaticalRelations.valueOf(engRelation);
// IndexedWord newNodeObj = new IndexedWord(CoreLabel.fromAbstractMapLabel(IndexedFeatureLabel.valueOf(newNode, MapFactory.HASH_MAP_FACTORY)));
IndexedWord newNodeObj = fromCheapString(newNode);
return new AddDep(govNodeName, relation, newNodeObj);
}
use of edu.stanford.nlp.trees.GrammaticalRelation in project CoreNLP by stanfordnlp.
the class SemanticGraphUtils method enRepairEdges.
/**
* Given a graph, ensures all edges are EnglishGrammaticalRelations
* NOTE: this is English specific
* NOTE: currently EnglishGrammaticalRelations does not link collapsed prep string forms
* back to their object forms, for its valueOf relation. This may need to be repaired if
* generated edges indeed do have collapsed preps as strings.
*/
public static void enRepairEdges(SemanticGraph sg, boolean verbose) {
for (SemanticGraphEdge edge : sg.edgeIterable()) {
if (edge.getRelation().isFromString()) {
GrammaticalRelation newReln = EnglishGrammaticalRelations.valueOf(edge.getRelation().toString());
if (newReln != null) {
IndexedWord gov = edge.getGovernor();
IndexedWord dep = edge.getDependent();
double weight = edge.getWeight();
boolean isExtra = edge.isExtra();
sg.removeEdge(edge);
sg.addEdge(gov, dep, newReln, weight, isExtra);
} else {
if (verbose)
log.info("Warning, could not find matching GrammaticalRelation for reln=" + edge.getRelation());
}
}
}
}
Aggregations