TokensregexNER 应该使用哪些设置

Which settings should be used for TokensregexNER

当我尝试 regexner 时,它使用以下设置和数据按预期工作;

props.setProperty("annotators", "tokenize, cleanxml, ssplit, pos, lemma, regexner");

Bachelor of Laws DEGREE
Bachelor of (Arts|Laws|Science|Engineering|Divinity) DEGREE

我想做的是使用 TokenRegex。例如

Bachelor of Laws DEGREE
Bachelor of ([{tag:NNS}] [{tag:NNP}]) DEGREE

我读到要做到这一点,我应该使用 TokensregexNERAnnotator。

我试过如下使用,没用

Pipeline.addAnnotator(new TokensRegexNERAnnotator("expressions.txt", true));

或者我尝试以其他方式设置注释器,

props.setProperty("annotators", "tokenize, cleanxml, ssplit, pos, lemma, tokenregexner");    
props.setProperty("customAnnotatorClass.tokenregexner", "edu.stanford.nlp.pipeline.TokensRegexNERAnnotator");

我尝试了不同的 TokenRegex 格式,但要么注释器找不到表达式,要么出现 SyntaxException。

在 NER 数据文件上使用 TokenRegex(查询带标签的标记)的正确方法是什么?

顺便说一句,我刚刚在 TokensRegexNERAnnotator.java 文件中看到一条评论。不确定是不是相关的 pos 标签不适用于 RegexNerAnnotator。

if (entry.tokensRegex != null) {
    // TODO: posTagPatterns...
    pattern = TokenSequencePattern.compile(env, entry.tokensRegex);
  }

首先您需要制作一个 TokensRegex 规则文件 (sample_degree.rules)。这是一个例子:

ner = { type: "CLASS", value: "edu.stanford.nlp.ling.CoreAnnotations$NamedEntityTagAnnotation" }

{ pattern: (/Bachelor/ /of/ [{tag:NNP}]), action: Annotate([=10=], ner, "DEGREE") }

为了解释一下规则,pattern 字段指定要匹配的模式类型。 action 字段是说注释整个匹配中的每个标记(即 [=15=] 表示的),注释 ner 字段(注意我们在规则中指定了 ner = ...文件,第三个参数表示将字段设置为字符串 "DEGREE").

然后为命令制作此 .props 文件 (degree_example.props):

customAnnotatorClass.tokensregex = edu.stanford.nlp.pipeline.TokensRegexAnnotator

tokensregex.rules = sample_degree.rules

annotators = tokenize,ssplit,pos,lemma,ner,tokensregex

然后运行这个命令:

java -Xmx8g edu.stanford.nlp.pipeline.StanfordCoreNLP -props degree_example.props -file sample-degree-sentence.txt -outputFormat text

您应该会看到您想要标记为 "DEGREE" 的三个标记将被标记。

我想我将对代码进行更改以使 tokensregex link 成为 TokensRegexAnnotator,这样您就不必将其指定为自定义注释器。 但现在您需要在 .props 文件中添加该行。

这个例子应该有助于实现这一点。如果您想了解更多信息,这里有更多资源:

http://nlp.stanford.edu/software/tokensregex.shtml#TokensRegexRules

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ling/tokensregex/SequenceMatchRules.html

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ling/tokensregex/types/Expressions.html