Search in sources :

Example 1 with TokenSequencePattern

use of edu.stanford.nlp.ling.tokensregex.TokenSequencePattern in project CoreNLP by stanfordnlp.

the class RelationTripleSegmenter method extract.

/**
 * Extract the nominal patterns from this sentence.
 *
 * @see RelationTripleSegmenter#NOUN_TOKEN_PATTERNS
 * @see RelationTripleSegmenter#NOUN_DEPENDENCY_PATTERNS
 *
 * @param parse The parse tree of the sentence to annotate.
 * @param tokens The tokens of the sentence to annotate.
 * @return A list of {@link RelationTriple}s. Note that these do not have an associated tree with them.
 */
@SuppressWarnings("unchecked")
public List<RelationTriple> extract(SemanticGraph parse, List<CoreLabel> tokens) {
    List<RelationTriple> extractions = new ArrayList<>();
    Set<Triple<Span, String, Span>> alreadyExtracted = new HashSet<>();
    // 
    for (TokenSequencePattern tokenPattern : NOUN_TOKEN_PATTERNS) {
        TokenSequenceMatcher tokenMatcher = tokenPattern.matcher(tokens);
        while (tokenMatcher.find()) {
            boolean missingPrefixBe;
            boolean missingSuffixOf = false;
            // Create subject
            List<? extends CoreMap> subject = tokenMatcher.groupNodes("$subject");
            Span subjectSpan = Util.extractNER(tokens, Span.fromValues(((CoreLabel) subject.get(0)).index() - 1, ((CoreLabel) subject.get(subject.size() - 1)).index()));
            List<CoreLabel> subjectTokens = new ArrayList<>();
            for (int i : subjectSpan) {
                subjectTokens.add(tokens.get(i));
            }
            // Create object
            List<? extends CoreMap> object = tokenMatcher.groupNodes("$object");
            Span objectSpan = Util.extractNER(tokens, Span.fromValues(((CoreLabel) object.get(0)).index() - 1, ((CoreLabel) object.get(object.size() - 1)).index()));
            if (Span.overlaps(subjectSpan, objectSpan)) {
                continue;
            }
            List<CoreLabel> objectTokens = new ArrayList<>();
            for (int i : objectSpan) {
                objectTokens.add(tokens.get(i));
            }
            // Create relation
            if (subjectTokens.size() > 0 && objectTokens.size() > 0) {
                List<CoreLabel> relationTokens = new ArrayList<>();
                // (add the 'be')
                missingPrefixBe = true;
                // (add a complement to the 'be')
                List<? extends CoreMap> beofComp = tokenMatcher.groupNodes("$beof_comp");
                if (beofComp != null) {
                    // (add the complement
                    for (CoreMap token : beofComp) {
                        if (token instanceof CoreLabel) {
                            relationTokens.add((CoreLabel) token);
                        } else {
                            relationTokens.add(new CoreLabel(token));
                        }
                    }
                    // (add the 'of')
                    missingSuffixOf = true;
                }
                // Add extraction
                String relationGloss = StringUtils.join(relationTokens.stream().map(CoreLabel::word), " ");
                if (!alreadyExtracted.contains(Triple.makeTriple(subjectSpan, relationGloss, objectSpan))) {
                    RelationTriple extraction = new RelationTriple(subjectTokens, relationTokens, objectTokens);
                    // noinspection ConstantConditions
                    extraction.isPrefixBe(missingPrefixBe);
                    extraction.isSuffixOf(missingSuffixOf);
                    extractions.add(extraction);
                    alreadyExtracted.add(Triple.makeTriple(subjectSpan, relationGloss, objectSpan));
                }
            }
        }
        // 
        for (SemgrexPattern semgrex : NOUN_DEPENDENCY_PATTERNS) {
            SemgrexMatcher matcher = semgrex.matcher(parse);
            while (matcher.find()) {
                boolean missingPrefixBe = false;
                boolean missingSuffixBe = false;
                boolean istmod = false;
                // Get relaux if applicable
                String relaux = matcher.getRelnString("relaux");
                String ignoredArc = relaux;
                if (ignoredArc == null) {
                    ignoredArc = matcher.getRelnString("arc");
                }
                // Create subject
                IndexedWord subject = matcher.getNode("subject");
                List<IndexedWord> subjectTokens = new ArrayList<>();
                Span subjectSpan;
                if (subject.ner() != null && !"O".equals(subject.ner())) {
                    subjectSpan = Util.extractNER(tokens, Span.fromValues(subject.index() - 1, subject.index()));
                    for (int i : subjectSpan) {
                        subjectTokens.add(new IndexedWord(tokens.get(i)));
                    }
                } else {
                    subjectTokens = getValidChunk(parse, subject, VALID_SUBJECT_ARCS, Optional.ofNullable(ignoredArc), true).orElse(Collections.singletonList(subject));
                    subjectSpan = Util.tokensToSpan(subjectTokens);
                }
                // Create object
                IndexedWord object = matcher.getNode("object");
                List<IndexedWord> objectTokens = new ArrayList<>();
                Span objectSpan;
                if (object.ner() != null && !"O".equals(object.ner())) {
                    objectSpan = Util.extractNER(tokens, Span.fromValues(object.index() - 1, object.index()));
                    for (int i : objectSpan) {
                        objectTokens.add(new IndexedWord(tokens.get(i)));
                    }
                } else {
                    objectTokens = getValidChunk(parse, object, VALID_OBJECT_ARCS, Optional.ofNullable(ignoredArc), true).orElse(Collections.singletonList(object));
                    objectSpan = Util.tokensToSpan(objectTokens);
                }
                // Check that the pair is valid
                if (Span.overlaps(subjectSpan, objectSpan)) {
                    // We extracted an identity
                    continue;
                }
                if (subjectSpan.end() == objectSpan.start() - 1 && (tokens.get(subjectSpan.end()).word().matches("[\\.,:;\\('\"]") || "CC".equals(tokens.get(subjectSpan.end()).tag()))) {
                    // We're straddling a clause
                    continue;
                }
                if (objectSpan.end() == subjectSpan.start() - 1 && (tokens.get(objectSpan.end()).word().matches("[\\.,:;\\('\"]") || "CC".equals(tokens.get(objectSpan.end()).tag()))) {
                    // We're straddling a clause
                    continue;
                }
                // Get any prepositional edges
                String expected = relaux == null ? "" : relaux.substring(relaux.indexOf(":") + 1).replace("_", " ");
                IndexedWord prepWord = null;
                // (these usually come from the object)
                boolean prepositionIsPrefix = false;
                for (SemanticGraphEdge edge : parse.outgoingEdgeIterable(object)) {
                    if (edge.getRelation().toString().equals("case")) {
                        prepWord = edge.getDependent();
                    }
                }
                // (...but sometimes from the subject)
                if (prepWord == null) {
                    for (SemanticGraphEdge edge : parse.outgoingEdgeIterable(subject)) {
                        if (edge.getRelation().toString().equals("case")) {
                            prepositionIsPrefix = true;
                            prepWord = edge.getDependent();
                        }
                    }
                }
                List<IndexedWord> prepChunk = Collections.EMPTY_LIST;
                if (prepWord != null && !expected.equals("tmod")) {
                    Optional<List<IndexedWord>> optionalPrepChunk = getValidChunk(parse, prepWord, Collections.singleton("mwe"), Optional.empty(), true);
                    if (!optionalPrepChunk.isPresent()) {
                        continue;
                    }
                    prepChunk = optionalPrepChunk.get();
                    Collections.sort(prepChunk, (a, b) -> {
                        double val = a.pseudoPosition() - b.pseudoPosition();
                        if (val < 0) {
                            return -1;
                        }
                        if (val > 0) {
                            return 1;
                        } else {
                            return 0;
                        }
                    });
                // ascending sort
                }
                // Get the relation
                if (subjectTokens.size() > 0 && objectTokens.size() > 0) {
                    LinkedList<IndexedWord> relationTokens = new LinkedList<>();
                    IndexedWord relNode = matcher.getNode("relation");
                    if (relNode != null) {
                        // Case: we have a grounded relation span
                        // (add the relation)
                        relationTokens.add(relNode);
                        // (add any prepositional case markings)
                        if (prepositionIsPrefix) {
                            // We're almost certainly missing a suffix 'be'
                            missingSuffixBe = true;
                            for (int i = prepChunk.size() - 1; i >= 0; --i) {
                                relationTokens.addFirst(prepChunk.get(i));
                            }
                        } else {
                            relationTokens.addAll(prepChunk);
                        }
                        if (expected.equalsIgnoreCase("tmod")) {
                            istmod = true;
                        }
                    } else {
                        // (mark it as missing a preceding 'be'
                        if (!expected.equals("poss")) {
                            missingPrefixBe = true;
                        }
                        // (add any prepositional case markings)
                        if (prepositionIsPrefix) {
                            for (int i = prepChunk.size() - 1; i >= 0; --i) {
                                relationTokens.addFirst(prepChunk.get(i));
                            }
                        } else {
                            relationTokens.addAll(prepChunk);
                        }
                        if (expected.equalsIgnoreCase("tmod")) {
                            istmod = true;
                        }
                        // (some fine-tuning)
                        if (allowNominalsWithoutNER && "of".equals(expected)) {
                            // prohibit things like "conductor of electricity" -> "conductor; be of; electricity"
                            continue;
                        }
                    }
                    // Add extraction
                    String relationGloss = StringUtils.join(relationTokens.stream().map(IndexedWord::word), " ");
                    if (!alreadyExtracted.contains(Triple.makeTriple(subjectSpan, relationGloss, objectSpan))) {
                        RelationTriple extraction = new RelationTriple(subjectTokens.stream().map(IndexedWord::backingLabel).collect(Collectors.toList()), relationTokens.stream().map(IndexedWord::backingLabel).collect(Collectors.toList()), objectTokens.stream().map(IndexedWord::backingLabel).collect(Collectors.toList()));
                        extraction.istmod(istmod);
                        extraction.isPrefixBe(missingPrefixBe);
                        extraction.isSuffixBe(missingSuffixBe);
                        extractions.add(extraction);
                        alreadyExtracted.add(Triple.makeTriple(subjectSpan, relationGloss, objectSpan));
                    }
                }
            }
        }
    }
    // 
    // Filter downward polarity extractions
    // 
    Iterator<RelationTriple> iter = extractions.iterator();
    while (iter.hasNext()) {
        RelationTriple term = iter.next();
        boolean shouldRemove = true;
        for (CoreLabel token : term) {
            if (token.get(NaturalLogicAnnotations.PolarityAnnotation.class) == null || !token.get(NaturalLogicAnnotations.PolarityAnnotation.class).isDownwards()) {
                shouldRemove = false;
            }
        }
        if (shouldRemove) {
            // Don't extract things in downward polarity contexts.
            iter.remove();
        }
    }
    // Return
    return extractions;
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) Span(edu.stanford.nlp.ie.machinereading.structure.Span) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) TokenSequenceMatcher(edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher) SemanticGraphEdge(edu.stanford.nlp.semgraph.SemanticGraphEdge) RelationTriple(edu.stanford.nlp.ie.util.RelationTriple) CoreLabel(edu.stanford.nlp.ling.CoreLabel) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 2 with TokenSequencePattern

use of edu.stanford.nlp.ling.tokensregex.TokenSequencePattern in project CoreNLP by stanfordnlp.

the class ScorePhrases method runParallelApplyPats.

private void runParallelApplyPats(Map<String, DataInstance> sents, String label, E pattern, TwoDimensionalCounter<CandidatePhrase, E> wordsandLemmaPatExtracted, CollectionValuedMap<E, Triple<String, Integer, Integer>> matchedTokensByPat, Set<CandidatePhrase> alreadyLabeledWords) {
    Redwood.log(Redwood.DBG, "Applying pattern " + pattern + " to a total of " + sents.size() + " sentences ");
    List<String> notAllowedClasses = new ArrayList<>();
    List<String> sentids = CollectionUtils.toList(sents.keySet());
    if (constVars.doNotExtractPhraseAnyWordLabeledOtherClass) {
        for (String l : constVars.getAnswerClass().keySet()) {
            if (!l.equals(label)) {
                notAllowedClasses.add(l);
            }
        }
        notAllowedClasses.add("OTHERSEM");
    }
    Map<TokenSequencePattern, E> surfacePatternsLearnedThisIterConverted = null;
    Map<SemgrexPattern, E> depPatternsLearnedThisIterConverted = null;
    if (constVars.patternType.equals(PatternFactory.PatternType.SURFACE)) {
        surfacePatternsLearnedThisIterConverted = new HashMap<>();
        String patternStr = null;
        try {
            patternStr = pattern.toString(notAllowedClasses);
            TokenSequencePattern pat = TokenSequencePattern.compile(constVars.env.get(label), patternStr);
            surfacePatternsLearnedThisIterConverted.put(pat, pattern);
        } catch (Exception e) {
            log.info("Error applying pattern " + patternStr + ". Probably an ill formed pattern (can be because of special symbols in label names). Contact the software developer.");
            throw e;
        }
    } else if (constVars.patternType.equals(PatternFactory.PatternType.DEP)) {
        depPatternsLearnedThisIterConverted = new HashMap<>();
        SemgrexPattern pat = SemgrexPattern.compile(pattern.toString(notAllowedClasses), new edu.stanford.nlp.semgraph.semgrex.Env(constVars.env.get(label).getVariables()));
        depPatternsLearnedThisIterConverted.put(pat, pattern);
    } else {
        throw new UnsupportedOperationException();
    }
    // Apply the patterns and extract candidate phrases
    int num;
    int numThreads = constVars.numThreads;
    // If number of sentences is less, do not create so many threads
    if (sents.size() < 50)
        numThreads = 1;
    if (numThreads == 1)
        num = sents.size();
    else
        num = sents.size() / (numThreads - 1);
    ExecutorService executor = Executors.newFixedThreadPool(constVars.numThreads);
    List<Future<Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>>>> list = new ArrayList<>();
    for (int i = 0; i < numThreads; i++) {
        Callable<Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>>> task = null;
        if (pattern.type.equals(PatternFactory.PatternType.SURFACE))
            // Redwood.log(Redwood.DBG, "Applying pats: assigning sentences " + i*num + " to " +Math.min(sentids.size(), (i + 1) * num) + " to thread " + (i+1));
            task = new ApplyPatterns(sents, num == sents.size() ? sentids : sentids.subList(i * num, Math.min(sentids.size(), (i + 1) * num)), surfacePatternsLearnedThisIterConverted, label, constVars.removeStopWordsFromSelectedPhrases, constVars.removePhrasesWithStopWords, constVars);
        else
            task = new ApplyDepPatterns(sents, num == sents.size() ? sentids : sentids.subList(i * num, Math.min(sentids.size(), (i + 1) * num)), depPatternsLearnedThisIterConverted, label, constVars.removeStopWordsFromSelectedPhrases, constVars.removePhrasesWithStopWords, constVars);
        Future<Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>>> submit = executor.submit(task);
        list.add(submit);
    }
    // Now retrieve the result
    for (Future<Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>>> future : list) {
        try {
            Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>> result = future.get();
            Redwood.log(ConstantsAndVariables.extremedebug, "Pattern " + pattern + " extracted phrases " + result.first());
            wordsandLemmaPatExtracted.addAll(result.first());
            matchedTokensByPat.addAll(result.second());
            alreadyLabeledWords.addAll(result.third());
        } catch (Exception e) {
            executor.shutdownNow();
            throw new RuntimeException(e);
        }
    }
    executor.shutdown();
}
Also used : Env(edu.stanford.nlp.ling.tokensregex.Env) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) TwoDimensionalCounter(edu.stanford.nlp.stats.TwoDimensionalCounter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplyDepPatterns(edu.stanford.nlp.patterns.dep.ApplyDepPatterns) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 3 with TokenSequencePattern

use of edu.stanford.nlp.ling.tokensregex.TokenSequencePattern in project CoreNLP by stanfordnlp.

the class ApplyPatterns method call.

@Override
public Triple<TwoDimensionalCounter<CandidatePhrase, E>, CollectionValuedMap<E, Triple<String, Integer, Integer>>, Set<CandidatePhrase>> call() throws Exception {
    // CollectionValuedMap<String, Integer>();
    try {
        Set<CandidatePhrase> alreadyLabeledPhrases = new HashSet<>();
        TwoDimensionalCounter<CandidatePhrase, E> allFreq = new TwoDimensionalCounter<>();
        CollectionValuedMap<E, Triple<String, Integer, Integer>> matchedTokensByPat = new CollectionValuedMap<>();
        for (String sentid : sentids) {
            List<CoreLabel> sent = sents.get(sentid).getTokens();
            for (Entry<TokenSequencePattern, E> pEn : patterns.entrySet()) {
                if (pEn.getKey() == null)
                    throw new RuntimeException("why is the pattern " + pEn + " null?");
                TokenSequenceMatcher m = pEn.getKey().getMatcher(sent);
                // //Setting this find type can save time in searching - greedy and reluctant quantifiers are not enforced
                // m.setFindType(SequenceMatcher.FindType.FIND_ALL);
                // Higher branch values makes the faster but uses more memory
                m.setBranchLimit(5);
                while (m.find()) {
                    int s = m.start("$term");
                    int e = m.end("$term");
                    assert e - s <= PatternFactory.numWordsCompoundMapped.get(label) : "How come the pattern " + pEn.getKey() + " is extracting phrases longer than numWordsCompound of " + PatternFactory.numWordsCompoundMapped.get(label) + " for label " + label;
                    String phrase = "";
                    String phraseLemma = "";
                    boolean useWordNotLabeled = false;
                    boolean doNotUse = false;
                    // find if the neighboring words are labeled - if so - club them together
                    if (constVars.clubNeighboringLabeledWords) {
                        for (int i = s - 1; i >= 0; i--) {
                            if (!sent.get(i).get(constVars.getAnswerClass().get(label)).equals(label)) {
                                s = i + 1;
                                break;
                            }
                        }
                        for (int i = e; i < sent.size(); i++) {
                            if (!sent.get(i).get(constVars.getAnswerClass().get(label)).equals(label)) {
                                e = i;
                                break;
                            }
                        }
                    }
                    // to make sure we discard phrases with stopwords in between, but include the ones in which stop words were removed at the ends if removeStopWordsFromSelectedPhrases is true
                    boolean[] addedindices = new boolean[e - s];
                    for (int i = s; i < e; i++) {
                        CoreLabel l = sent.get(i);
                        l.set(PatternsAnnotations.MatchedPattern.class, true);
                        if (!l.containsKey(PatternsAnnotations.MatchedPatterns.class) || l.get(PatternsAnnotations.MatchedPatterns.class) == null)
                            l.set(PatternsAnnotations.MatchedPatterns.class, new HashSet<>());
                        SurfacePattern pSur = (SurfacePattern) pEn.getValue();
                        assert pSur != null : "Why is " + pEn.getValue() + " not present in the index?!";
                        assert l.get(PatternsAnnotations.MatchedPatterns.class) != null : "How come MatchedPatterns class is null for the token. The classes in the key set are " + l.keySet();
                        l.get(PatternsAnnotations.MatchedPatterns.class).add(pSur);
                        for (Entry<Class, Object> ig : constVars.getIgnoreWordswithClassesDuringSelection().get(label).entrySet()) {
                            if (l.containsKey(ig.getKey()) && l.get(ig.getKey()).equals(ig.getValue())) {
                                doNotUse = true;
                            }
                        }
                        boolean containsStop = containsStopWord(l, constVars.getCommonEngWords(), PatternFactory.ignoreWordRegex);
                        if (removePhrasesWithStopWords && containsStop) {
                            doNotUse = true;
                        } else {
                            if (!containsStop || !removeStopWordsFromSelectedPhrases) {
                                if (label == null || l.get(constVars.getAnswerClass().get(label)) == null || !l.get(constVars.getAnswerClass().get(label)).equals(label)) {
                                    useWordNotLabeled = true;
                                }
                                phrase += " " + l.word();
                                phraseLemma += " " + l.lemma();
                                addedindices[i - s] = true;
                            }
                        }
                    }
                    for (int i = 0; i < addedindices.length; i++) {
                        if (i > 0 && i < addedindices.length - 1 && addedindices[i - 1] == true && addedindices[i] == false && addedindices[i + 1] == true) {
                            doNotUse = true;
                            break;
                        }
                    }
                    if (!doNotUse) {
                        matchedTokensByPat.add(pEn.getValue(), new Triple<>(sentid, s, e - 1));
                        phrase = phrase.trim();
                        if (!phrase.isEmpty()) {
                            phraseLemma = phraseLemma.trim();
                            CandidatePhrase candPhrase = CandidatePhrase.createOrGet(phrase, phraseLemma);
                            allFreq.incrementCount(candPhrase, pEn.getValue(), 1.0);
                            if (!useWordNotLabeled)
                                alreadyLabeledPhrases.add(candPhrase);
                        }
                    }
                }
            }
        }
        return new Triple<>(allFreq, matchedTokensByPat, alreadyLabeledPhrases);
    } catch (Exception e) {
        logger.error(e);
        throw e;
    }
}
Also used : CollectionValuedMap(edu.stanford.nlp.util.CollectionValuedMap) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) TokenSequenceMatcher(edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher) TwoDimensionalCounter(edu.stanford.nlp.stats.TwoDimensionalCounter) Triple(edu.stanford.nlp.util.Triple) CoreLabel(edu.stanford.nlp.ling.CoreLabel)

Example 4 with TokenSequencePattern

use of edu.stanford.nlp.ling.tokensregex.TokenSequencePattern in project CoreNLP by stanfordnlp.

the class TokensRegexMatcher method main.

public static void main(String[] args) throws IOException {
    if (args.length < 2) {
        System.err.println("TokensRegexMatcher rules file [outFile]");
        return;
    }
    String rules = args[0];
    PrintWriter out;
    if (args.length > 2) {
        out = new PrintWriter(args[2]);
    } else {
        out = new PrintWriter(System.out);
    }
    StanfordCoreNLP pipeline = new StanfordCoreNLP(PropertiesUtils.asProperties("annotators", "tokenize,ssplit,pos,lemma,ner"));
    Annotation annotation = new Annotation(IOUtils.slurpFileNoExceptions(args[1]));
    pipeline.annotate(annotation);
    // Load lines of file as TokenSequencePatterns
    List<TokenSequencePattern> tokenSequencePatterns = new ArrayList<TokenSequencePattern>();
    for (String line : ObjectBank.getLineIterator(rules)) {
        TokenSequencePattern pattern = TokenSequencePattern.compile(line);
        tokenSequencePatterns.add(pattern);
    }
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    int i = 0;
    for (CoreMap sentence : sentences) {
        List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
        out.println("Sentence #" + ++i);
        out.print("  Tokens:");
        for (CoreLabel token : tokens) {
            out.print(' ');
            out.print(token.toShortString("Text", "PartOfSpeech", "NamedEntityTag"));
        }
        out.println();
        MultiPatternMatcher<CoreMap> multiMatcher = TokenSequencePattern.getMultiPatternMatcher(tokenSequencePatterns);
        List<SequenceMatchResult<CoreMap>> answers = multiMatcher.findNonOverlapping(tokens);
        int j = 0;
        for (SequenceMatchResult<CoreMap> matched : answers) {
            out.println("  Match #" + ++j);
            for (int k = 0; k <= matched.groupCount(); k++) {
                out.println("    group " + k + " = " + matched.group(k));
            }
        }
    }
    out.flush();
}
Also used : ArrayList(java.util.ArrayList) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) SequenceMatchResult(edu.stanford.nlp.ling.tokensregex.SequenceMatchResult) PrintWriter(java.io.PrintWriter)

Example 5 with TokenSequencePattern

use of edu.stanford.nlp.ling.tokensregex.TokenSequencePattern in project CoreNLP by stanfordnlp.

the class TokensRegexMatcherDemo method main.

public static void main(String[] args) {
    StanfordCoreNLP pipeline = new StanfordCoreNLP(PropertiesUtils.asProperties("annotators", "tokenize,ssplit,pos,lemma,ner"));
    Annotation annotation = new Annotation("Casey is 21. Sally Atkinson's age is 30.");
    pipeline.annotate(annotation);
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    List<TokenSequencePattern> tokenSequencePatterns = new ArrayList<>();
    String[] patterns = { "(?$who [ ner: PERSON]+ ) /is/ (?$age [ pos: CD ] )", "(?$who [ ner: PERSON]+ ) /'s/ /age/ /is/ (?$age [ pos: CD ] )" };
    for (String line : patterns) {
        TokenSequencePattern pattern = TokenSequencePattern.compile(line);
        tokenSequencePatterns.add(pattern);
    }
    MultiPatternMatcher<CoreMap> multiMatcher = TokenSequencePattern.getMultiPatternMatcher(tokenSequencePatterns);
    int i = 0;
    for (CoreMap sentence : sentences) {
        List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
        System.out.println("Sentence #" + ++i);
        System.out.print("  Tokens:");
        for (CoreLabel token : tokens) {
            System.out.print(' ');
            System.out.print(token.toShortString("Text", "PartOfSpeech", "NamedEntityTag"));
        }
        System.out.println();
        List<SequenceMatchResult<CoreMap>> answers = multiMatcher.findNonOverlapping(tokens);
        int j = 0;
        for (SequenceMatchResult<CoreMap> matched : answers) {
            System.out.println("  Match #" + ++j);
            System.out.println("    match: " + matched.group(0));
            System.out.println("      who: " + matched.group("$who"));
            System.out.println("      age: " + matched.group("$age"));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) Annotation(edu.stanford.nlp.pipeline.Annotation) TokenSequencePattern(edu.stanford.nlp.ling.tokensregex.TokenSequencePattern) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) SequenceMatchResult(edu.stanford.nlp.ling.tokensregex.SequenceMatchResult)

Aggregations

TokenSequencePattern (edu.stanford.nlp.ling.tokensregex.TokenSequencePattern)6 CoreLabel (edu.stanford.nlp.ling.CoreLabel)4 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)3 Annotation (edu.stanford.nlp.pipeline.Annotation)3 CoreMap (edu.stanford.nlp.util.CoreMap)3 SequenceMatchResult (edu.stanford.nlp.ling.tokensregex.SequenceMatchResult)2 TokenSequenceMatcher (edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher)2 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)2 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)2 TwoDimensionalCounter (edu.stanford.nlp.stats.TwoDimensionalCounter)2 ArrayList (java.util.ArrayList)2 Span (edu.stanford.nlp.ie.machinereading.structure.Span)1 RelationTriple (edu.stanford.nlp.ie.util.RelationTriple)1 IndexedWord (edu.stanford.nlp.ling.IndexedWord)1 Env (edu.stanford.nlp.ling.tokensregex.Env)1 ApplyDepPatterns (edu.stanford.nlp.patterns.dep.ApplyDepPatterns)1 ProtobufAnnotationSerializer (edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer)1 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)1 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)1 CollectionValuedMap (edu.stanford.nlp.util.CollectionValuedMap)1