java - 查找段落中句子的起点和终点斯坦福CoreNLP

标签 java indexing split stanford-nlp sentence

我想知道如何使用斯坦福CoreNLP 找到段落中句子的开始和结束位置。现在我正在使用 DocumentPreprocessor 将段落拆分成句子。是否可以获得该句子在原文中实际位置的开始和结束索引?

我正在使用此处提出的另一个问题中的代码。

String paragraph = "My 1st sentence. “Does it work for questions?” My third sentence.";
Reader reader = new StringReader(paragraph);
DocumentPreprocessor dp = new DocumentPreprocessor(reader);
List<String> sentenceList = new ArrayList<String>();

for (List<HasWord> sentence : dp) {
   String sentenceString = Sentence.listToString(sentence);
   sentenceList.add(sentenceString.toString());
}

for (String sentence : sentenceList) {
   System.out.println(sentence);
}

取自:How can I split a text into sentences using the Stanford parser?

谢谢

最佳答案

快速但肮脏的方法是:

import edu.stanford.nlp.simple.*;

Document doc = new Document("My 1st sentence. “Does it work for questions?” My third sentence.");
for (Sentence sentence : doc.sentences()) {
  System.out.println(sentence.characterOffsetBegin(0) + " -- " + sentence.characterOffsetEnd(sentence.length() - 1));
}

否则,您可以从 CoreLabel 中提取 CharacterOffsetBeginAnnotationCharacterOffsetEndAnnotation,并使用它来查找标记在原始文本中的偏移量。

关于java - 查找段落中句子的起点和终点斯坦福CoreNLP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304604/

相关文章:

java - 如何解析这个 JSONObject 以从 twitter 搜索 API 获取数组内的值来获取每条推文(名称、正文)?

javascript - 在 Javascript 中设置多维对象或数组属性的值

php - pdftk 将 pdf 拆分为多页,但总大小增加

javascript - 如何根据 javaScript 中的特定字符序列拆分文本

java - OSGi - 将 bundle 的类添加到特定的类加载器

java - 如何在java中的jtextArea中追加50 mb数据(来自文本文件)?

java - 为什么我不应该在添加组件之前调用 setVisible(true)?

matlab - 大于阈值的值的数量

indexing - 软404出现在空的内部搜索结果中,怎么办?

python - 通过 Dataframe 的字符串拆分循环