网站首页 > 开源技术 正文
大家好,我是皮皮。
一、前言
前几天在Python最强王者交流群【Wendy Zheng】问了一个英文文本中统计关键词的问题,这里拿出来给大家分享下。
二、实现过程
针对这个问题,本文给出一个思路方法,也许有帮助,首先我们需要将Excel中的文本进行导入到一个文本文件中去,代码如下:
# coding: utf-8
import pandas as pd
df = pd.read_excel('./文本.xlsx')
# print(df.head())
# df['专业关键词']
for text in df['工作要求']:
# print(text)
if text is not None:
with open('工作要求.txt', mode='a', encoding='utf-8') as file:
file.write(str(text))
print('写入完成')
接下来就可以针对这个文本文件进行相关的词频统计了,如果你有自己自定义的关键词,也可以就着关键词去统计,没有的话,就自己在关键词范围内,任意取多少个关键词都可以,相关的代码如下所示:
from collections import Counter
import pandas as pd
df = pd.read_excel('./文本.xlsx')
# print(df.head())
words = []
with open('工作要求.txt', 'r', encoding='utf-8') as f:
line = f.readlines()
for word in line[0].split(' '):
words.append(word)
print(len(words))
counter = Counter(words)
# print(counter)
# df['专业关键词']
for text in df['专业关键词']:
for k, v in counter.items():
if k == text:
print(k, v)
这个代码对于英文文本还是适用的,不过有个小问题,如下。
最后这里也给出中文分词的代码和可视化代码,两者结合在一起的,感兴趣的小伙伴们可以试试看。
from collections import Counter # 统计词频
from pyecharts.charts import Bar
from pyecharts import options as opts
from snownlp import SnowNLP
import jieba # 分词
with open('text_分词后_outputs.txt', 'r',encoding='utf-8') as f:
read = f.read()
with open('stop_word.txt', 'r', encoding='utf-8') as f:
stop_word = f.read()
word = jieba.cut(read)
words = []
for i in list(word):
if i not in stop_word:
words.append(i)
columns = []
data = []
for k, v in dict(Counter(words).most_common(10)).items():
columns.append(k)
data.append(v)
bar = (
Bar()
.add_xaxis(columns)
.add_yaxis("词频", data)
.set_global_opts(title_opts=opts.TitleOpts(title="词频top10"))
)
bar.render("词频.html")
三、总结
大家好,我是皮皮。这篇文章主要盘点了一个英文文本中统计关键词方法处理的问题,文中针对该问题,给出了具体的解析和代码实现,帮助粉丝顺利解决了问题。
最后感谢粉丝【Wendy Zheng】提问,感谢【Python进阶者】给出的思路和代码解析,感谢【Python狗】等人参与学习交流。
猜你喜欢
- 2024-09-12 《小美好》短评文本情感分析+生成词云
- 2024-09-12 怎么自学python,大概要多久?(自学python怎么样)
- 2024-09-12 推荐 4 个 Python 新手实战项目(python入门教程推荐)
- 2024-09-12 基于 Python 的网易民谣歌词数据分析
- 2024-09-12 B 站鬼畜区热门评论情感分析(b站鬼畜区热度)
- 2024-09-12 淘宝上“飞机杯”的销量究竟有多大?
- 2024-09-12 教你对抓取的文本进行分词、词频统计、词云可视化和情感分析
- 2024-09-12 20行代码教会Python分析商品评价(python爬取商品评论)
- 2024-09-12 AC娘是宇宙的主宰——坛友ac娘表情使用情况分析以及些许干货
- 2024-09-12 新手怎么自学python,大概要多久?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)