如何使用python计算一个文本文件中包含100个句子的平均句子长度(以单词为单位) [英] how to count average sentence length (in words) from a text file contains 100 sentences using python
本文介绍了如何使用python计算一个文本文件中包含100个句子的平均句子长度(以单词为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含100个句子的文本文件.我想编写一个python脚本,该脚本将从包含100个句子的文本文件中计算平均句子长度(以单词为单位). 谢谢
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
推荐答案
幼稚的方式:
sents = text.split('.')
avg_len = sum(len(x.split()) for x in sents) / len(sents)
严肃的方法:根据目标语言规则,使用 nltk 标记文本.
The serious way: use nltk to tokenize the text according to the target language rules.
这篇关于如何使用python计算一个文本文件中包含100个句子的平均句子长度(以单词为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文