![](/img/trans.png)
[英]How to escape regular expression special characters in CoreNLP TokenRegex pattern?
[英]How to modify TokenRegex rule in StanfordNLP?
我有tokenregex的规则文件为
$EDU_FIRST_KEYWORD = (/Education/|/Course[s]?/|/Educational/|/Academic/|/Education/ /and/?|/Professional/|/Certification[s]?/ /and/?)
$EDU_LAST_KEYWORD = (/Background/|/Qualification[s]?/|/Training[s]?/|/Detail[s]?/|/Record[s]?/)
tokens = { type: "CLASS", value: "edu.stanford.nlp.ling.CoreAnnotations$NamedEntityTagAnnotation" }
{ ruleType: "tokens", pattern: ( $EDU_FIRST_KEYWORD $EDU_LAST_KEYWORD ?), result: "EDUCATION" }
我要匹配EDU_FIRST_KEYWORD
然后匹配EDU_LAST_KEYWORD
。 如果两个部分都不匹配,请检查EDU_FIRST_KEYWORD
与给定字符串匹配。
例如1.培训和课程
匹配的输出:教育(因为它与课程匹配,所以不应该发生)
预期输出:无输出
这是因为它与字符串的第一部分或完整的字符串都不匹配。
匹配的输出:教育
预期产出:教育
我尝试将pattern: ( $EDU_FIRST_KEYWORD $EDU_LAST_KEYWORD ?)
更改为pattern: ( $EDU_FIRST_KEYWORD + $EDU_LAST_KEYWORD ?)
但这无济于事。
我尝试了stanfordNLP tokenregex文档,但无法获得实现方法。 有人可以帮我更改规则文件吗? 提前致谢。
您想使用TokenSequenceMatcher的matchs matches()
方法使规则针对整个String运行。
如果使用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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.