让斯坦福 NLP 识别具有多个单词的命名实体
Getting Stanford NLP to recognise named entities with multiple words
首先让我说我是一个完全的 NLP 新手。虽然,当您继续阅读时,这可能会变得非常明显。
我正在解析维基百科页面以查找所有提及页面标题的内容。为此,我通过 CorefChainAnnotations 查找 "proper" 提及 - 然后我假设最常见的提及页面标题。我通过 运行 这样做:
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String content = "Abraham Lincoln was an American politician and lawyer who served as the 16th President of the United States from March 1861 until his assassination in April 1865. Lincoln led the United States through its Civil War—its bloodiest war and perhaps its greatest moral, constitutional, and political crisis.";
Annotation document = new Annotation(content);
pipeline.annotate(document);
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
List<CorefChain.CorefMention> corefMentions = cc.getMentionsInTextualOrder();
for (CorefChain.CorefMention cm : corefMentions) {
if (cm.mentionType == Dictionaries.MentionType.PROPER) {
log("Proper ref using " + cm.mentionSpan + ", " + cm.mentionType);
}
}
}
这个returns:
Proper ref using the United States
Proper ref using the United States
Proper ref using Abraham Lincoln
Proper ref using Lincoln
我已经知道 "Abraham Lincoln" 绝对是我要找的东西,我可以推测,因为 "Lincoln" 也经常出现,所以这一定是谈论主题的另一种方式。 (我意识到现在最常见的命名实体是 "the United States",但是一旦我将整个页面都提供给它,它就可以正常工作了)。
在我有一个像 "Gone with the Wind" 这样的页面之前,这一直很好用。如果我更改我的代码以使用它:
String content = "Gone with the Wind has been criticized as historical revisionism glorifying slavery, but nevertheless, it has been credited for triggering changes to the way African-Americans are depicted cinematically.";
然后我就完全没有得到适当的提及。我怀疑这是因为标题中的 none 个词被识别为命名实体。
有什么方法可以让 Stanford NLP 将 "Gone with the Wind" 识别为 already-known 命名实体?从互联网上看,它似乎涉及训练一个模型,但我希望它是一个已知的命名实体,只为这个单一 运行,我不希望模型以后记住这个训练。
我可以想象 NLP 专家对这种方法的可怕之处翻白眼,但它变得更好了!在将文本传递给 Stanford NLP 之前,我想到了将页面标题的任何出现更改为 "Thingamijig" 的好主意,这对于 "Gone with the Wind" 非常有效,但对于 "Abraham Lincoln" 则失败,因为(我认为)NER 不再将 "Lincoln" 与 corefMentions 中的 "Thingamijig" 相关联。
在我的梦想世界里,我会做这样的事情:
pipeline.addKnownNamedEntity("Gone with the Wind");
但这似乎不是我能做的事情,我也不确定该怎么做。
您可以提交包含任何您想要的短语的字典,并将它们识别为命名实体。
java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner -ner.additional.regexner.mapping additional.rules -file example.txt -outputFormat text
additional.rules
Gone With The Wind MOVIE MISC 1
请注意,上面的列应该以制表符分隔。 additional.rules
文件中的行数不限。
一个警告,每次出现令牌模式时都会对其进行标记。
首先让我说我是一个完全的 NLP 新手。虽然,当您继续阅读时,这可能会变得非常明显。
我正在解析维基百科页面以查找所有提及页面标题的内容。为此,我通过 CorefChainAnnotations 查找 "proper" 提及 - 然后我假设最常见的提及页面标题。我通过 运行 这样做:
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String content = "Abraham Lincoln was an American politician and lawyer who served as the 16th President of the United States from March 1861 until his assassination in April 1865. Lincoln led the United States through its Civil War—its bloodiest war and perhaps its greatest moral, constitutional, and political crisis.";
Annotation document = new Annotation(content);
pipeline.annotate(document);
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
List<CorefChain.CorefMention> corefMentions = cc.getMentionsInTextualOrder();
for (CorefChain.CorefMention cm : corefMentions) {
if (cm.mentionType == Dictionaries.MentionType.PROPER) {
log("Proper ref using " + cm.mentionSpan + ", " + cm.mentionType);
}
}
}
这个returns:
Proper ref using the United States
Proper ref using the United States
Proper ref using Abraham Lincoln
Proper ref using Lincoln
我已经知道 "Abraham Lincoln" 绝对是我要找的东西,我可以推测,因为 "Lincoln" 也经常出现,所以这一定是谈论主题的另一种方式。 (我意识到现在最常见的命名实体是 "the United States",但是一旦我将整个页面都提供给它,它就可以正常工作了)。
在我有一个像 "Gone with the Wind" 这样的页面之前,这一直很好用。如果我更改我的代码以使用它:
String content = "Gone with the Wind has been criticized as historical revisionism glorifying slavery, but nevertheless, it has been credited for triggering changes to the way African-Americans are depicted cinematically.";
然后我就完全没有得到适当的提及。我怀疑这是因为标题中的 none 个词被识别为命名实体。
有什么方法可以让 Stanford NLP 将 "Gone with the Wind" 识别为 already-known 命名实体?从互联网上看,它似乎涉及训练一个模型,但我希望它是一个已知的命名实体,只为这个单一 运行,我不希望模型以后记住这个训练。
我可以想象 NLP 专家对这种方法的可怕之处翻白眼,但它变得更好了!在将文本传递给 Stanford NLP 之前,我想到了将页面标题的任何出现更改为 "Thingamijig" 的好主意,这对于 "Gone with the Wind" 非常有效,但对于 "Abraham Lincoln" 则失败,因为(我认为)NER 不再将 "Lincoln" 与 corefMentions 中的 "Thingamijig" 相关联。
在我的梦想世界里,我会做这样的事情:
pipeline.addKnownNamedEntity("Gone with the Wind");
但这似乎不是我能做的事情,我也不确定该怎么做。
您可以提交包含任何您想要的短语的字典,并将它们识别为命名实体。
java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner -ner.additional.regexner.mapping additional.rules -file example.txt -outputFormat text
additional.rules
Gone With The Wind MOVIE MISC 1
请注意,上面的列应该以制表符分隔。 additional.rules
文件中的行数不限。
一个警告,每次出现令牌模式时都会对其进行标记。