-6

I have a text file which contains 100 sentences. i want to write a python script that will count average sentence length (in words) from a text file contains 100 sentences. Thanks

Wild Freelancer
  • 11
  • 1
  • 1
  • 2

3 Answers3

7

The naive way:

sents = text.split('.')
avg_len = sum(len(x.split()) for x in sents) / len(sents)

The serious way: use nltk to tokenize the text according to the target language rules.

georg
  • 211,518
  • 52
  • 313
  • 390
3
wordcounts = []
with open(filepath) as f:
    text = f.read()
    sentences = text.split('.')
    for sentence in sentences:
        words = sentence.split(' ')
        wordcounts.append(len(words))
average_wordcount = sum(wordcounts)/len(wordcounts)
snurre
  • 3,045
  • 2
  • 24
  • 31
0

this should help you out. but this is basic stuff, you should have at least tried something yourself.

this code assumes each sentence is on a new line.

if that is not the case, you can either correct the code, or reflect that in your question, which is unclear about this.

def read_lines_from_file(file_name):
    with open(file_name, 'r') as f:
        for line in f:
            yield line.strip()

def average_words(sentences):
    counts = []
    for sentence in sentences:
        counts.append(sentence.split())
    return float(sum(counts)/len(counts))

print average_words(read_lines_from_file(file_name))
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131