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);
}
Aggregations