Created
January 21, 2022 20:39
-
-
Save paddy74/28a295837005664e5c72869a2d9daacd to your computer and use it in GitHub Desktop.
Regex based Python function to count the number of sentences in a given string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def count_sentences(text: str) -> int: | |
""" | |
Count the number of sentences in a given string. | |
This function is based on the Stack Overflow answer https://stackoverflow.com/a/38589115/7706917 | |
Inputs | |
-------- | |
text : str | |
Returns | |
-------- | |
int | |
The number of sentences in the given string. | |
""" | |
exp = r"[!?]+|(?<!\.)\.(?!(?<=\d.)\d)(?!\.)" | |
sentences = re.split(exp, text.replace("\n", "")) | |
sentences = sentences[:-1] | |
return len(sentences) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment