so I really cannot see what I am doing wrong here, the number of sentences keeps saying it is 0, however I am trying to count the number of sentences/stops with the text.count('.')
Is there anythingin my code which would make this print out "0"?
Thanks
def countSentences(fileName) :
"""This is a function to count the number
of sentences in a given text file"""
f = open(fileName, 'r')
text = f.read()
text = text.split()
print("Total sentences : " + str(text.count('.')))
f.close()
in Main() I have
print(countSentences('phrases.txt'))
which passes in a file with numerous sentences.
It would appear from your code that the var text is an array of strings, so the count will find no strings that are just .
Counting sentences is a pretty tricky thing, since the . can show up in a lot of things that are not sentence terminating. I would recommend something like nltk or spacy to accomplish this task more effectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With