如何使用斯坦福解析器将文本或段落分割成句子?
是否有类似于getSentencesFromString()
的方法来提取句子,就像Ruby中提供的那样?
回答:
你可以查看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) { // SentenceUtils not Sentence String sentenceString = SentenceUtils.listToString(sentence); sentenceList.add(sentenceString);}for (String sentence : sentenceList) { System.out.println(sentence);}