作者:男人好好疼h爱你的女人吧 | 来源:互联网 | 2023-07-31 16:08
样本数据集关于酒店评论的语料效果图代码实现:importjieba.possegaspsegfromitertoolsimportchainfromwordcloudimportW
样本数据集
效果图
代码实现:
import jieba.posseg as pseg
from itertools import chain
from wordcloud import WordCloud
# 获取形容词
def get_a(text):
a_list=[]
for f in pseg.lcut(text):
if f.flag=='a':
a_list.append(f.word)
return a_list
# 词云生成器
def wordclouds(word_list): wd=WordCloud(font_path='./SimHei.ttf',max_words=100,background_color='white')
word_string=' '.join(word_list)
wd.generate(word_string) plt.figure()
plt.imshow(wd, interpolation="bilinear")
plt.axis("off")
plt.show()# 获取训练集正样本的文本
train_data_1=train_data[train_data['label']==1]['sentence']
# 获取正样本的形容词
train_a_list_1=chain(*map(lambda x:get_a(x),train_data_1))
# 获取训练集负样本的文本
train_data_0=train_data[train_data['label']==0]['sentence']
# 获取负样本的形容词
train_a_list_0=chain(*map(lambda x:get_a(x),train_data_0))
wordclouds(train_a_list_1)
wordclouds(train_a_list_0)