我在 python 中创建了一个文本内容分析器,用于分析来自文件的输入和输出
- 总字数
- 唯一词的计数
- 句子数
这是代码:
import re
import string
import os
import sys
def function(s):
return re.sub("[%s]" % re.escape(string.punctuation), '', s.lower())
def main():
words_list = []
with open(sys.argv[1], "r") as f:
for line in f:
words_list.extend(line.split())
print "Total word count:", len(words_list)
new_words = map(function, words_list)
print "Unique words:", len(set(new_words))
nb_sentence = 0
for word in words_list:
if re.search(r'[.!?][' "'" '"]*', word):
nb_sentence += 1
print "Sentences:", nb_sentence
if __name__ == "__main__":
main()
我现在正在尝试计算单词的平均句子长度,找到经常使用的短语(使用超过 3 次的 3 个或更多单词的短语),并按频率降序排列使用的单词列表。有人可以帮忙吗?