Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing out number of sentences in a text file

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.

like image 503
Spencer Vreugdenhil-Beauclerc Avatar asked Dec 21 '25 22:12

Spencer Vreugdenhil-Beauclerc


1 Answers

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.

like image 199
Joshua Smith Avatar answered Dec 24 '25 10:12

Joshua Smith