how to count average sentence length (in words) from a text file contains 100 sentences using python [closed]

ぃ、小莉子 提交于 2019-12-13 10:09:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!