I need to split a string containing sentences such as:
"this is a sentence. this is another. Rawlings, G. stated foo and bar."
into
["this is a sentence.", "this is another.", "Rawlings, G. stated foo and bar."]
using regular expressions.
The other solutions I found split the third sentence into "Rawlings, G." and "stated foo and bar." which is not what I want.
Regular expressions generally do not solve this problem.
You need a sentence detection algorithm, OpenNLP has one
It's simple enough to use:
String sentences[] = sentenceDetector.sentDetect(yourString);
And handles a lot of tricky cases
Through nested lookbehinds.
Just split your input string according to the below regex. The below regex would split the input string according to the boundary which exists just after to a dot and also it check for the preceding character of dot. It splits only if the preceding character of dot is not an upppercase letter.
String s = "this is a sentence. this is another. Rawlings, G. stated foo and bar.";
String[] tok = s.split("(?<=(?<![A-Z])\\.)");
System.out.println(Arrays.toString(tok));
Output:
[this is a sentence., this is another., Rawlings, G. stated foo and bar.]
Explanation:
(?<=(?<![A-Z])\\.) Matches the boundary which exists just after to dot but the dot wouldn't be preceded by an uppercase letter.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With