我想使用斯坦福NLP共引用sinle。这意味着我做了分词器和句子 spiliter,所有的工作都需要 coref。我构造文档注释并在其上完成所有注释。但是当我们想使用 coref 时,它有错误,因为我不使用斯坦福核心NLP 类这是我的代码:
edu.stanford.nlp.pipeline.Annotation document=new edu.stanford.nlp.pipeline.Annotation(doc.toString());
Properties props = new Properties();
ArrayList <edu.stanford.nlp.ling.CoreLabel> tokenAnnotate=new ArrayList<>();
//document.set(edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation.class,doc.toString());
int countToken=0;
int countSentence=0;
for(CoreMap sentence: sentences) {
ArrayList <edu.stanford.nlp.ling.CoreLabel> tokenAnnotateCoreMap=new ArrayList<>();
// traversing the words in the current sentence
// a CoreLabel is a CoreMap with additional token-specific methods
edu.stanford.nlp.util.CoreMap stanfordCorMap=new edu.stanford.nlp.pipeline.Annotation(sentence.toString());
int countFirstToken=countToken;
for (CoreLabel token: sentence.get(com.mobin.tp.textAnnotator.common.dto.CoreAnnotations.TokensAnnotation.class)) {
// this is the text of the token
countToken++;
edu.stanford.nlp.ling.CoreLabel coreLabel=mobinStanfordConverter.mobinToStanfordCorelabelConvertor(token);
tokenAnnotateCoreMap.add(coreLabel);
tokenAnnotate.add(coreLabel);
}
stanfordCorMap.set(edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation.class,tokenAnnotateCoreMap);
stanfordCorMap.set(edu.stanford.nlp.ling.CoreAnnotations.TokenBeginAnnotation.class,countFirstToken);
stanfordCorMap.set(edu.stanford.nlp.ling.CoreAnnotations.TokenEndAnnotation.class,countToken);
stanfordCorMap.set(CoreAnnotations.SentenceIndexAnnotation.class,countSentence);
stanfordsnetence.add(stanfordCorMap);
countSentence++;
// this is the parse tree of the current sentence
//Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class);
// this is the Stanford dependency graph of the current sentence
//SemanticGraph dependencies = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
}
document.set(edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation.class,tokenAnnotate);
document.set(edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation.class,stanfordsnetence);
Annotator annotator=new ParserAnnotator(false,0);
annotator.annotate(document);
annotator=new DeterministicCorefAnnotator(props);
annotator.annotate(document);
这是我的:错误:
attempted to fetch annotator "parse" before the annotator pool was created!
java.lang.AssertionError
at edu.stanford.nlp.dcoref.RuleBasedCorefMentionFinder.getParser(RuleBasedCorefMentionFinder.java:345)
at edu.stanford.nlp.dcoref.RuleBasedCorefMentionFinder.parse(RuleBasedCorefMentionFinder.java:338)
at edu.stanford.nlp.dcoref.RuleBasedCorefMentionFinder.findSyntacticHead(RuleBasedCorefMentionFinder.java:273)
at edu.stanford.nlp.dcoref.RuleBasedCorefMentionFinder.findHead(RuleBasedCorefMentionFinder.java:215)
at edu.stanford.nlp.dcoref.RuleBasedCorefMentionFinder.extractPredictedMentions(RuleBasedCorefMentionFinder.java:88)
at edu.stanford.nlp.pipeline.DeterministicCorefAnnotator.annotate(DeterministicCorefAnnotator.java:89)
我所知,斯坦福的NLP库使用多通道筛算法来解决共引用。 您可以参考此答案以查看如何使用该库和此 JavaScript 以获取完整的文档。
这是我测试结果的代码:
public class CoReferenceAnalyzer
{
public static void main(String[] args)
{
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "My horse, whom I call Steve, is my best friend. He comforts me when I ride him";
Annotation document = new Annotation(text);
pipeline.annotate(document);
Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);
System.out.println("Graph: " + graph.toString());
for(Map.Entry<Integer, CorefChain> entry : graph.entrySet())
{
CorefChain chain = entry.getValue();
CorefMention repMention = chain.getRepresentativeMention();
System.out.println("Chain: " + chain.toString());
System.out.println("Rep: " + repMention.toString());
}
}
}
您将看到如下所示的输出:
Graph: {1=CHAIN1-["Steve" in sentence 1, "He" in sentence 2, "him" in sentence 2], 2=CHAIN2-["My horse , whom I call Steve" in sentence 1], 3=CHAIN3-["My horse" in sentence 1], 4=CHAIN4-["My" in sentence 1, "I" in sentence 1, "my" in sentence 1, "me" in sentence 2, "I" in sentence 2], 6=CHAIN6-["my best friend" in sentence 1], 8=CHAIN8-["He comforts me when I ride him" in sentence 2]}
Chain: CHAIN1-["Steve" in sentence 1, "He" in sentence 2, "him" in sentence 2]
Rep: "Steve" in sentence 1
我想我已经和你提出了同样的问题。问题可能是 maven 发布的 jar 与 edu.stanford.nlp.simple.Document 中的源代码不同.java在同一版本 3.6.0 上。
在源代码中,Document 的构造函数如下所示:
public Document(String text) {
StanfordCoreNLP.getDefaultAnnotatorPool(EMPTY_PROPS, new AnnotatorImplementations()); // cache the annotator pool
this.impl = CoreNLPProtos.Document.newBuilder().setText(text);
}
但是在 maven jar 代码中,它看起来像这样:
public Document(String text) {
this.impl = CoreNLPProtos.Document.newBuilder().setText(text);
}
区别非常明显。
所以解决上述问题的方法是从https://github.com/stanfordnlp/CoreNLP 并使用ANT制作一个名为stanford-corenlp.jar的新罐子。然后用新罐子替换旧罐子。
希望该方法对您有用。