Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex To Split String Into Sentences

Tags:

java

string

regex

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.

like image 690
MalikAbiola Avatar asked Jul 22 '26 01:07

MalikAbiola


2 Answers

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

  • "Walter White Jr. has money"
  • "Mr. Pink does not give tips"
like image 119
mishadoff Avatar answered Jul 24 '26 14:07

mishadoff


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.
like image 44
Avinash Raj Avatar answered Jul 24 '26 16:07

Avinash Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!