Search in sources :

Example 1 with CoreMapExpressionExtractor

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

the class KBPTokensregexExtractor method classify.

@Override
public Pair<String, Double> classify(KBPInput input) {
    // Annotate Sentence
    CoreMap sentenceAsMap = input.sentence.asCoreMap(Sentence::nerTags);
    List<CoreLabel> tokens = sentenceAsMap.get(CoreAnnotations.TokensAnnotation.class);
    // Annotate where the subject is
    for (int i : input.subjectSpan) {
        tokens.get(i).set(Subject.class, "true");
        if ("O".equals(tokens.get(i).ner())) {
            tokens.get(i).setNER(input.subjectType.name);
        }
    }
    // Annotate where the object is
    for (int i : input.objectSpan) {
        tokens.get(i).set(Object.class, "true");
        if ("O".equals(tokens.get(i).ner())) {
            tokens.get(i).setNER(input.objectType.name);
        }
    }
    // Run Rules
    for (RelationType rel : RelationType.values()) {
        if (rules.containsKey(rel) && rel.entityType == input.subjectType && rel.validNamedEntityLabels.contains(input.objectType)) {
            CoreMapExpressionExtractor extractor = rules.get(rel);
            @SuppressWarnings("unchecked") List<MatchedExpression> extractions = extractor.extractExpressions(sentenceAsMap);
            if (extractions != null && extractions.size() > 0) {
                MatchedExpression best = MatchedExpression.getBestMatched(extractions, MatchedExpression.EXPR_WEIGHT_SCORER);
                // Un-Annotate Sentence
                for (CoreLabel token : tokens) {
                    token.remove(Subject.class);
                    token.remove(Object.class);
                }
                return Pair.makePair(rel.canonicalName, best.getWeight());
            }
        }
    }
    // Un-Annotate Sentence
    for (CoreLabel token : tokens) {
        token.remove(Subject.class);
        token.remove(Object.class);
    }
    return Pair.makePair(NO_RELATION, 1.0);
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreMapExpressionExtractor(edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) CoreMap(edu.stanford.nlp.util.CoreMap) Sentence(edu.stanford.nlp.simple.Sentence) MatchedExpression(edu.stanford.nlp.ling.tokensregex.MatchedExpression)

Aggregations

CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 CoreLabel (edu.stanford.nlp.ling.CoreLabel)1 CoreMapExpressionExtractor (edu.stanford.nlp.ling.tokensregex.CoreMapExpressionExtractor)1 MatchedExpression (edu.stanford.nlp.ling.tokensregex.MatchedExpression)1 Sentence (edu.stanford.nlp.simple.Sentence)1 CoreMap (edu.stanford.nlp.util.CoreMap)1