I was wondering how I can find the start and end position of a sentence in a paragraph using StanfordCoreNLP. Right now I am using DocumentPreprocessor to split the paragraph into sentences. Is it possible to get the start and end index of where the sentence is actually located in the original text?
I am using the code from another question asked on here.
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);
}
Taken from: How can I split a text into sentences using the Stanford parser?
Thanks
The quick and dirty way to do this would be:
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));
}
Otherwise, you can extract the CharacterOffsetBeginAnnotation
and CharacterOffsetEndAnnotation
from a CoreLabel, and use that to find the token's offset in the original text.