You can check the DocumentPreprocessor class. Below is a short snippet. I think there may be other ways to do what you want.
String paragraph = "My first sentence. My second sentence."; Reader reader = new StringReader(paragraph); DocumentPreprocessor dp = new DocumentPreprocessor(reader); List<String> sentenceList = new LinkedList<String>(); Iterator<List<HasWord>> it = dp.iterator(); while (it.hasNext()) { StringBuilder sentenceSb = new StringBuilder(); List<HasWord> sentence = it.next(); for (HasWord token : sentence) { if(sentenceSb.length()>1) { sentenceSb.append(" "); } sentenceSb.append(token); } sentenceList.add(sentenceSb.toString()); } for(String sentence:sentenceList) { System.out.println(sentence); }
Read full article from java - How can I split a text into sentences using the Stanford parser? - Stack Overflow
No comments:
Post a Comment