Search in sources :

Example 36 with GrammaticalRelation

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;
}
Also used : Pattern(java.util.regex.Pattern) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord) Pair(edu.stanford.nlp.util.Pair)

Example 37 with GrammaticalRelation

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);
        }
    }
}
Also used : GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 38 with GrammaticalRelation

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;
}
Also used : ExtractionObject(edu.stanford.nlp.ie.machinereading.structure.ExtractionObject) java.util(java.util) CorefChain(edu.stanford.nlp.coref.data.CorefChain) edu.stanford.nlp.util(edu.stanford.nlp.util) Tree(edu.stanford.nlp.trees.Tree) Dictionaries(edu.stanford.nlp.coref.data.Dictionaries) MachineReadingAnnotations(edu.stanford.nlp.ie.machinereading.structure.MachineReadingAnnotations) TimeAnnotations(edu.stanford.nlp.time.TimeAnnotations) RelationMention(edu.stanford.nlp.ie.machinereading.structure.RelationMention) Mention(edu.stanford.nlp.coref.data.Mention) CoreAnnotation(edu.stanford.nlp.ling.CoreAnnotation) SemanticGraphCoreAnnotations(edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations) Language(edu.stanford.nlp.international.Language) RNNCoreAnnotations(edu.stanford.nlp.neural.rnn.RNNCoreAnnotations) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) LabeledScoredTreeNode(edu.stanford.nlp.trees.LabeledScoredTreeNode) Timex(edu.stanford.nlp.time.Timex) IndexedWord(edu.stanford.nlp.ling.IndexedWord) TreeCoreAnnotations(edu.stanford.nlp.trees.TreeCoreAnnotations) CoreLabel(edu.stanford.nlp.ling.CoreLabel) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) edu.stanford.nlp.naturalli(edu.stanford.nlp.naturalli) SentimentCoreAnnotations(edu.stanford.nlp.sentiment.SentimentCoreAnnotations) NumberNormalizer(edu.stanford.nlp.ie.NumberNormalizer) Collectors(java.util.stream.Collectors) EntityMention(edu.stanford.nlp.ie.machinereading.structure.EntityMention) SegmenterCoreAnnotations(edu.stanford.nlp.ling.SegmenterCoreAnnotations) SpeakerInfo(edu.stanford.nlp.coref.data.SpeakerInfo) Span(edu.stanford.nlp.ie.machinereading.structure.Span) Word(edu.stanford.nlp.ling.Word) java.io(java.io) CorefCoreAnnotations(edu.stanford.nlp.coref.CorefCoreAnnotations) CoreLabel(edu.stanford.nlp.ling.CoreLabel) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 39 with GrammaticalRelation

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);
}
Also used : GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 40 with GrammaticalRelation

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());
            }
        }
    }
}
Also used : GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Aggregations

GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)49 IndexedWord (edu.stanford.nlp.ling.IndexedWord)38 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)13 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)13 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)11 CoreLabel (edu.stanford.nlp.ling.CoreLabel)11 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)9 ArrayList (java.util.ArrayList)5 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)4 IntTuple (edu.stanford.nlp.util.IntTuple)4 Tree (edu.stanford.nlp.trees.Tree)3 Word (edu.stanford.nlp.ling.Word)2 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)2 TypedDependency (edu.stanford.nlp.trees.TypedDependency)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)1 CorefChain (edu.stanford.nlp.coref.data.CorefChain)1 Dictionaries (edu.stanford.nlp.coref.data.Dictionaries)1 Mention (edu.stanford.nlp.coref.data.Mention)1 SpeakerInfo (edu.stanford.nlp.coref.data.SpeakerInfo)1