Java 如何使用Stanford NLP替换句子(CoreMap)中的标记(CoreLabel)?
像往常一样,我在for Java循环中遍历带注释文档的句子:Java 如何使用Stanford NLP替换句子(CoreMap)中的标记(CoreLabel)?,java,nlp,stanford-nlp,Java,Nlp,Stanford Nlp,像往常一样,我在for Java循环中遍历带注释文档的句子: for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { ... } 然后,在其中,我从句子中删除一个单词,例如,教师使用单词索引,我使用CoreLabel方法setWord将新词文本设置为John,最后我在同一索引的句子中添加更新的单词: sentence.get(CoreAnnotations.TokensAnno
for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
...
}
然后,在其中,我从句子中删除一个单词,例如,教师使用单词索引,我使用CoreLabel方法setWord将新词文本设置为John,最后我在同一索引的句子中添加更新的单词:
sentence.get(CoreAnnotations.TokensAnnotations.class).remove(token.get(CoreAnnotations.IndexAnnotation.class));
token.setWord("John");
sentence.get(CoreAnnotations.TokensAnnotation.class).add(token.get(CoreAnnotations.IndexAnnotation.class),token);
问题是这个句子保持原样。即使我在删除后立即打印句子文本,它也不会改变。我做错什么了吗?有没有更合理的方法?我敢说,即使你改变了单词,你也没有改变原文。一般来说,你应该对这些类型的转换有点警惕-它们可能会产生各种奇怪的效果,例如,你的角色偏移量会被破坏,但是如果你感到勇敢并想修复手边的错误,你应该能够通过设置:
token.setOriginalText("John");