Java 让Stanford NLP识别具有多个单词的命名实体

Java 让Stanford NLP识别具有多个单词的命名实体,java,stanford-nlp,Java,Stanford Nlp,首先让我说我是NLP的一个完全的新手。尽管,当你继续读下去,这可能会变得非常明显 我正在分析维基百科的页面,以找到所有提到的页面标题。我通过查看Corefchainan注释来找到“适当的”提及,然后我假设最常见的提及是关于页面标题。我通过运行以下命令来实现: Properties props = new Properties(); props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref"

首先让我说我是NLP的一个完全的新手。尽管,当你继续读下去,这可能会变得非常明显

我正在分析维基百科的页面,以找到所有提到的页面标题。我通过查看Corefchainan注释来找到“适当的”提及,然后我假设最常见的提及是关于页面标题。我通过运行以下命令来实现:

    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);
            }
        }
    }
我已经知道“亚伯拉罕·林肯”绝对是我要找的,我可以猜测,因为“林肯”也经常出现,那一定是谈论主题的另一种方式。(我意识到现在最常见的命名实体是“美国”,但一旦我把整页都填好了,它就可以正常工作了)

直到我有了一个像《乱世佳人》这样的页面,这一切都非常有效。如果我更改代码以使用该代码:

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.";
那我就没有得到任何适当的回复。我怀疑这是因为标题中没有一个词被认为是命名实体

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
有没有办法让斯坦福NLP将《乱世佳人》识别为一个已知的命名实体?从互联网上看,这似乎涉及到训练一个模型,但我希望这是一个已知的命名实体,只为这一次运行,我不希望模型记住这个训练以后

我可以想象NLP的专家们对这种可怕的方法睁大眼睛,但它会变得更好!我想出了一个好主意,在将文本传递给斯坦福NLP之前,将页面标题的任何出现更改为“Thingamijig”,这对《乱世佳人》很有效,但对《亚伯拉罕·林肯》却失败了,因为(我认为)NER在核心条款中不再将“林肯”与“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
附加规则

Gone With The Wind    MOVIE    MISC    1
请注意,上面的列应以制表符分隔。在
附加.rules
文件中,可以有任意多行

一个警告,每次令牌模式出现时,它都将被标记


更多详细信息:

非常感谢您查看我的问题!当我导入additional.rules文件时,它是仅用于这一次运行,还是为CoreNLP的其他运行保留了数据?如果使用该选项构建管道,它将用于管道的所有运行。请确保使用props.setProperty(“ner.additional.regexner.mapping”,“additional.rules”)指定规则在你的密码里是的,我做到了。非常感谢你!