I am trying to write code that reads text from a text file, and counts the number of sentences in the text via counting the number of ‘sentence ending’ punctuation (.?!). I initially had issues with the code counting extra full stops (such as those in B.Sc). I solved this by making the code only count full stops with a space after them, however this is now not counting the full stops that end a paragraph. e.g:
This is a sentence.
# that full stop isn't being counted.
This is another sentence.
This is my code so far:
def count_possible_sentences(file_name):
file=open(file_name)
text=file.read()
file.close()
numfs=0
numem=0
numqm=0
numfs=text.count(". ")
print(numfs)
numem=text.count("!")
print(numem)
numqm=text.count("?")
print(numqm)
count=numfs+numem+numqm
print(count)
I appreciate any help.