问题
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
回答1:
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.
回答2:
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)
回答3:
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))
来源:https://stackoverflow.com/questions/13970203/how-to-count-average-sentence-length-in-words-from-a-text-file-contains-100-se