import re
from openpyxl import Workbook
import warnings
warnings.filterwarnings('ignore')
wb = Workbook()
ws1 = wb.create_sheet('词频统计')
ws1['A1'] = '排序'
ws1['B1'] = '单词'
ws1['C1'] = '词频'
wb.save('./斯宾塞自传词频统计.xlsx')
print("词频工作表创建好啦!")
txt = open('spencer.txt',errors='ignore').read().lower()
txt = re.sub(r'[^a-zA-Z]',' ', txt)
words = txt.split()
print("文本单词处理好啦!")
stop_words = open('stop_words.txt').read()
stop_words = re.sub(r'[^a-zA-Z]',' ', stop_words)
stop_words = stop_words.split()
print("停用词表处理好啦!")
words = [x for x in words if x not in stop_words]
print("已经从文本中排除停用词啦!")
counts = {}
for word in words:
if word in counts:
counts[word] = counts[word] + 1
elif word not in counts:
counts[word] = 1
print("所有单词的词频计算好啦!")
lst = list(counts.items())
lst.sort(key = lambda k:k[1], reverse = True)
print("词频排序好啦!下面是排序好的词频")
x = 2
i = 0
while i <= 10000:
word,count = lst[i]
ws1['A'+str(x)] = i+1
ws1['B'+str(x)] = word
ws1['C'+str(x)] = count
i += 1
x += 1
print("词频已经全部写入Excel表格啦!")
wb.save('./斯宾塞自传词频统计.xlsx')
wb.close()
print("工作表已经关掉啦!")
|