Im working on a problem set to count sentences. I decided to implement by using regular expressions to split the string at the characters "?, ., !". When I pass my text to re.split, it is including an empty string at the end of the list.
source code:
from cs50 import get_string
import re
def main():
text = get_string("Text: ")
cole_liau(text)
# Implement 0.0588 * L - 0.296 * S - 15.8; l = avg num of letters / 100 words , S = avg num of sentences / 100 words
def cole_liau(intext):
words = []
letters = []
sentences = re.split(r"[.!?]+", intext)
print(sentences)
print(len(sentences))
main()
Output:
Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!
['Congratulations', ' Today is your day', " You're off to Great Places", " You're off and away", '']
5
I tried adding the + expression to make sure it was matching at least 1 [.!?] but that did not work either.