import jieba
import jieba.posseg as psg
from collections import Counter
text = '我现在在jupyter notebook上写文本分析的代码!'
for w in jieba.cut(text):
print(w)
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\83854\AppData\Local\Temp\jieba.cache
Loading model cost 0.417 seconds.
Prefix dict has been built successfully.
我
现在
在
jupyter
notebook
上
写
文本
分析
的
代码
!
import jieba;
seg_list = jieba.cut(
"甜豆腐脑和咸豆腐脑哪个更好吃呢?"
)
for w in seg_list:
print(w)
甜
豆腐脑
和
咸
豆腐脑
哪个
更
好吃
呢
?
甜豆腐脑和咸豆腐脑属于常用词,我们换成专业名词,圣遗物“华馆梦醒形骸记”和“来歆余响”
import jieba;
seg_list = jieba.cut(
"华馆梦醒形骸记和来歆余响哪个更适合神里呢?"
)
for w in seg_list:
print(w)
华馆
梦醒
形骸
记和来歆
余响
哪个
更
适合
神里
呢
?
可以看出,jieba并没有成功断开,现在我们导入词库
import jieba;
jieba.add_word('华馆梦醒形骸记')#添加词库
jieba.add_word('来歆余响')
seg_list = jieba.cut(
"华馆梦醒形骸记和来歆余响哪个更适合神里呢?"
)
for w in seg_list:
print(w)
华馆梦醒形骸记
和
来歆余响
哪个
更
适合
神里
呢
?
现在我们可以看到,再加入特定词的词库后,jieba成功进行了分词
我们可以用jieba.load_userdict(‘路径.txt’)方法一次性导入整个词库,txt文件中为每行一个特定的词
import jieba
import jieba.analyse
import jieba
import jieba.posseg as psg
from collections import Counter
# 待分词的文本路径
sourceTxt = r"C://Users//83854//Documents//shujvji//genshin.txt"
# 分好词后的文本路径
targetTxt = r"C://Users//83854//Documents//shujvji//fenci2.txt"
# 对文本进行操作
with open(sourceTxt, 'r', encoding = 'utf-8') as sourceFile, open(targetTxt, 'a+', encoding = 'utf-8') as targetFile:
for line in sourceFile:
seg = jieba.cut(line.strip(), cut_all = False)
# 分好词之后之间用空格隔断
output = ' '.join(seg)
targetFile.write(output)
targetFile.write('\n')
print('写入成功!')
# 提取关键词
with open(targetTxt, 'r', encoding = 'utf-8') as file:
text = file.readlines()
"""
几个参数解释:
* text : 待提取的字符串类型文本
* topK : 返回TF-IDF权重最大的关键词的个数,默认为20个
* withWeight : 是否返回关键词的权重值,默认为False
* allowPOS : 包含指定词性的词,默认为空
"""
keywords = jieba.analyse.extract_tags(str(text), topK = 100, withWeight=True, allowPOS=())
print(keywords)
print('提取完毕!')
写入成功!
[('音乐', 0.30376152967529807), ('须弥', 0.1346632360175712), ('原神', 0.12497199124472864), ('陈老师', 0.08320263205482652), ('幕后', 0.06823193339024688), ('真的', 0.06515894124271353), ('hoyomix', 0.06115650635380338), ('原宝', 0.058497527816681494), ('mix', 0.05052059220531584), ('团队', 0.04872411839589858), ('那契', 0.03988467805682829), ('hoyo', 0.03988467805682829), ('视频', 0.039425474014386126), ('感谢', 0.03666087076072954), ('喜欢', 0.035514340588202846), ('米哈', 0.035251415848932384), ('好听', 0.03470912869932385), ('数列', 0.033130735337255336), ('yyds', 0.029248763908340747), ('大佬', 0.0274163437665347), ('呜呜', 0.027097537520004447), ('斐波', 0.02658978537121886), ('枫丹', 0.02658978537121886), ('感觉', 0.026350321191172154), ('游戏', 0.02558340660825623), ('期待', 0.025368599082068503), ('HOYO', 0.023930806834096974), ('稻妻', 0.023930806834096974), ('陈致', 0.023930806834096974), ('旅人', 0.023280005443727755), ('前瞻', 0.023228101079139234), ('曲子', 0.02311102483163701), ('啊啊啊', 0.022779475735409255), ('就是', 0.022408588749270462), ('战斗', 0.02202536894074733), ('老师', 0.021263640122108542), ('节奏', 0.02124655568899911), ('创作', 0.020903159805560498), ('但是', 0.020422561798879003), ('角色', 0.020304841056014235), ('可以', 0.019490209364768682), ('配乐', 0.01754026047), ('摩斯', 0.017058200780604982), ('出来', 0.016520537436521352), ('世界', 0.016514234852021796), ('虽然', 0.016161079021601424), ('那兰', 0.015953871222731317), ('...', 0.015953871222731317), ('不够看', 0.015953871222731317), ('卧槽', 0.015458938669928826), ('各位', 0.015299619790215747), ('永远', 0.015225989643251779), ('旅行者', 0.015167384782104092), ('一个', 0.015040307680409253), ('冰箱', 0.015034945192900358), ('每次', 0.014728756026823843), ('急急', 0.014410247823131671), ('时候', 0.014308539813523131), ('沙漠', 0.014100965526338969), ('音乐会', 0.014064057475676158), ('听到', 0.014041888104221531), ('加油', 0.013711853238031585), ('语言', 0.013708846917571176), ('原石', 0.01329489268560943), ('bgm', 0.01329489268560943), ('MiX', 0.01329489268560943), ('遐想', 0.013227349117540038), ('一直', 0.013174950427226424), ('雨林', 0.0131551953525), ('审美', 0.013021219682989324), ('钢琴', 0.012852894857720195), ('落水', 0.012637714661650356), ('不同', 0.012618082724641905), ('太棒了', 0.012606459402246441), ('现在', 0.012311491455160141), ('带来', 0.012270374586959518), ('故事', 0.012251639474339411), ('剧情', 0.01219551207808719), ('花絮', 0.012184429129003558), ('已经', 0.012172770980925268), ('玩家', 0.012024459773687721), ('这个', 0.011901662937644573), ('拍子', 0.011879228010564945), ('还是', 0.011520793034270465), ('一次', 0.011412670121385676), ('小草', 0.011153777403358541), ('什么', 0.011106902198087187), ('国度', 0.010924310729615215), ('看到', 0.010820306320840748), ('中式', 0.010799625122275355), ('用心', 0.010760160860511566), ('演奏', 0.01064112761478203), ('锁屏', 0.010635914148487545), ('我要', 0.010635914148487545), ('哔哩', 0.010635914148487545), ('妮露', 0.010635914148487545), ('吐槽', 0.010635914148487545), ('死域', 0.010635914148487545), ('太牛', 0.010635914148487545), ('Mix', 0.010635914148487545)]
提取完毕!
import jieba.analyse
import matplotlib as mpl
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
content = open(r"C:\Users\83854\Documents\shujvji\fenci2.txt", encoding = 'UTF-8').read()
tags = jieba.analyse.extract_tags(content,topK=200,withWeight=False)
text = ' '.join(tags)
wc = WordCloud(font_path=r"C:\Users\83854\Documents\shujvji\华文行楷.ttf",
background_color='white',max_words=100,
max_font_size=120,min_font_size=10,
random_state=42,width=1200,height=900)
wc.generate(text)
plt.imshow(wc)
plt.axis('off')
plt.show()
我们改成前20个词语,并对参数做一些修改
content = open(r"C:\Users\83854\Documents\shujvji\fenci2.txt", encoding = 'UTF-8').read()
tags = jieba.analyse.extract_tags(content,topK=20,withWeight=False)
text = ' '.join(tags)
wc = WordCloud(font_path=r"C:\Users\83854\Documents\shujvji\华文行楷.ttf",
background_color='white',max_words=100,
max_font_size=1000,min_font_size=10,
random_state=42,width=1200,height=900)
wc.generate(text)
plt.imshow(wc)
plt.axis('off')
plt.show()
import pandas as pd
from snownlp import SnowNLP
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
sns.set_style('whitegrid',{'font.sans-serif':['simhei','Arial']})
comments = pd.read_csv("C://Users//83854//Documents//shujvji//genshinmusic.csv",'gbk',index_col=0)
comments.head()
C:\Users\83854\AppData\Local\Temp\ipykernel_53740\1497606228.py:1: FutureWarning: In a future version of pandas all arguments of read_csv except for the argument 'filepath_or_buffer' will be keyword-only.
comments = pd.read_csv("C://Users//83854//Documents//shujvji//genshinmusic.csv",'gbk',index_col=0)
C:\Users\83854\AppData\Local\Temp\ipykernel_53740\1497606228.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
comments = pd.read_csv("C://Users//83854//Documents//shujvji//genshinmusic.csv",'gbk',index_col=0)
评论 |
---|
超爱看幕后纪录有不一样的感动 |
"科普一下音乐知识, |
“主题”:一段可以辨认的旋律,是作品的核心,可以由音乐动机组成,可以用来象征某物某人 |
“模进”:类似把同样一句话轮流交给不同的人去说 |
“非均分律动”:把句子重音节奏等等进行不规则划分。假如简单理解唐诗是“均分律动”,那么宋词就是“非均分律动”" |
# 读如数据并显示数据的前五条
import pandas as pd
comments = pd.read_csv(r"C:\Users\83854\Documents\shujvji\genshinmusic.csv") #去掉一些特殊符号,只留下纯文本评论,去掉无
comments.head()
print(len(comments))
390
# 使用SnowNLP计算对每条标题的文字评估情绪得分
# 新建“情绪”一列
comments['情绪']=None
#comments.iloc[i,1]=None
# 所有文本长度
lenOrig=len(comments)
i=0
# 计算情绪得分SnowNLP(数据二维表.iloc[行,列]).sentiments
while(i<lenOrig):
s=SnowNLP(comments.iloc[i,0]).sentiments
comments.iloc[i,1]=s
i=i+1
# 输出每行的情绪得分
print('情绪得分:')
# 前五行
print(comments.head())
print(comments)
情绪得分:
评论 情绪
0 超爱看幕后纪录有不一样的感动 0.942552
1 科普一下音乐知识,\n“主题”:一段可以辨认的旋律,是作品的核心,可以由音乐动机组成,可以用... 1.0
2 虽然一直很喜欢HOYO-MIX的音乐,但此前专心欣赏这些配乐仅限于在游戏外,而须弥的配乐,是... 1.0
3 感觉这次不止是侧重陈老师,介绍了更多团队人员 0.995487
4 直播间别刷兑换码了你但凡加个原神群都会有人贴出来,每次前瞻都会给,不用老是刷屏直播间 0.003928
评论 情绪
0 超爱看幕后纪录有不一样的感动 0.942552
1 科普一下音乐知识,\n“主题”:一段可以辨认的旋律,是作品的核心,可以由音乐动机组成,可以用... 1.0
2 虽然一直很喜欢HOYO-MIX的音乐,但此前专心欣赏这些配乐仅限于在游戏外,而须弥的配乐,是... 1.0
3 感觉这次不止是侧重陈老师,介绍了更多团队人员 0.995487
4 直播间别刷兑换码了你但凡加个原神群都会有人贴出来,每次前瞻都会给,不用老是刷屏直播间 0.003928
.. ... ...
385 幕后故事超有趣 0.966248
386 流金疾驰已经难到,这么专业的队伍都得分段完成了么?!真想有朝一日能看现场啊 0.482386
387 快把前瞻端上来罢 0.829795
388 原宝贴贴 0.462915
389 牛牛牛,整多点这样的,爱看 0.846666
[390 rows x 2 columns]