栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用Python统计英文书词频,排除停用词,用openpyxl写进Excel

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Python统计英文书词频,排除停用词,用openpyxl写进Excel

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("工作表已经关掉啦!")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号