gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:57:25 25 4
gpt4 key购买 nike

我想知道如何使用斯坦福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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com