Skip to content

Instantly share code, notes, and snippets.

@paddy74
Created January 21, 2022 20:39
Show Gist options
  • Save paddy74/28a295837005664e5c72869a2d9daacd to your computer and use it in GitHub Desktop.
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.
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