![](/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.