我有 tokenregex 的规则文件为
$EDU_FIRST_KEYWORD = (/教育/|/类(class)[s]?/|/教育/|/学术/|/教育//和/?|/专业/|/认证[s]?//和/?)
$EDU_LAST_KEYWORD = (/背景/|/资格[s]?/|/培训[s]?/|/详细信息[s]?/|/记录[s]?/)
tokens = { 类型:“CLASS”,值:“edu.stanford.nlp.ling.CoreAnnotations$NamedEntityTagAnnotation” }
{ 规则类型:“ token ”,模式:( $EDU_FIRST_KEYWORD $EDU_LAST_KEYWORD ?),
结果:“教育”
}
我想要匹配 EDU_FIRST_KEYWORD
后跟 EDU_LAST_KEYWORD
。如果两个部分都不匹配,则检查 EDU_FIRST_KEYWORD
是否与给定字符串匹配。
例如1. 培训类(class)
匹配输出:教育(因为它与类(class)匹配,这不应该发生)
预期输出:无输出
这是因为它与字符串的第一部分或完整字符串都不匹配。
- 教育背景
匹配输出:教育
预期输出:教育
我尝试更改模式:( $EDU_FIRST_KEYWORD $EDU_LAST_KEYWORD ?)
为
模式:( $EDU_FIRST_KEYWORD + $EDU_LAST_KEYWORD ?)
但这没有帮助。
我尝试了 stanfordNLP tokenregex 文档,但无法了解如何实现这一点。有人可以帮我更改规则文件吗? 提前致谢。
最佳答案
您希望使用 TokenSequenceMatcher 的 matches()
方法来针对整个字符串运行您的规则。
如果您使用find()
,它将搜索整个字符串...如果您使用matches()
,它将查看整个字符串是否与模式匹配。
目前我不确定 TokensRegexAnnotator 是否可以对句子执行完整的字符串匹配,因此您可能需要使用如下代码:
package edu.stanford.nlp.examples;
import edu.stanford.nlp.util.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.ling.tokensregex.Env;
import edu.stanford.nlp.ling.tokensregex.TokenSequencePattern;
import edu.stanford.nlp.ling.tokensregex.TokenSequenceMatcher;
import edu.stanford.nlp.pipeline.*;
import java.util.*;
public class TokensRegexExactMatch {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("annotators", "tokenize");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = new Annotation("Training & Courses");
pipeline.annotate(annotation);
//System.err.println(IOUtils.stringFromFile("course.rules"));
Env env = TokenSequencePattern.getNewEnv();
env.bind("$EDU_WORD_ONE", "/Education|Educational|Courses/");
env.bind("$EDU_WORD_TWO", "/Background|Qualification/");
TokenSequencePattern pattern = TokenSequencePattern.compile(env, "$EDU_WORD_ONE $EDU_WORD_TWO?");
List<CoreLabel> tokens = annotation.get(CoreAnnotations.TokensAnnotation.class);
TokenSequenceMatcher matcher = pattern.getMatcher(tokens);
// matcher.matches()
while (matcher.find()) {
System.err.println("---");
String matchedString = matcher.group();
List<CoreMap> matchedTokens = matcher.groupNodes();
System.err.println(matchedTokens);
}
}
}
关于java - 如何修改StanfordNLP中的TokenRegex规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43732780/