I have a String
:
"Hello world... I am here. Please respond."
and I would like to count the number of sentences within the String
. I had an idea to use a Scanner
as well as the useDelimiter
method to split
any String
into sentences.
Scanner in = new Scanner(file);
in.useDelimiter("insert here");
I'd like to create a regular expression which can go through the String
I have shown above and identify it to have two sentences. I initially tried using the delimiter:
[^?.]
It gets hung up on the ellipses.
A regular expression probably isn't the right tool for this. English is not a regular language, so regular expressions get hung up- a lot. For one thing you can't even be sure a period in the middle of the text is an end of sentence- abbreviations (like Mr.), acronyms with periods, and initials will screw you up as well. Its not the right tool.
this could help :
public int getNumSentences() { List<String> tokens = getTokens( "[^!?.]+" ); return tokens.size(); }
and you can also add enter button as separator and make it independent on your OS by the following line of code
actually you can find more about the
and hence finally the method becomes :
hope this could help :) !
You could use a regular expression that checks for a non end of sentence, followed by an end of sentence like:
Although as @Gabe Sechan points out, a regular expression may not be accurate when the sentence includes abbreviated words such as Dr., Rd., St., etc.
For your sentence : "Hello world... I am here. Please respond."
The code will be :