我的WordCloud在单词末尾缺少字母's'

问题描述

起初,我认为问题出在我的数据上,我在清理数据时犯了一个错误。但是我检查了一下,情况并非如此。

我正在使用以下代码

import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

allWords = ' '.join([twts for twts in df['full_text']])
wordCloud = WordCloud(collocations=True,width = 1000,height=600,random_state = 21,max_font_size = 120).generate(allWords)

plt.imshow(wordCloud,interpolation = "bilinear")
plt.axis('off')
plt.show()

现在,我的词云显示出诸如“ coronaviru”,“ viru”,“ crisi”之类的单词。使用collocations=True可以显示完整单词,并与诸如“冠状病毒病例”,“冠状病毒大流行”之类的其他单词结合使用。 有谁知道如何解决这一问题? 就像我说的那样,我检查了数据,它始终是正确的完整单词。所以我想这个错误是在wordcloud上发生的。

我的数据如下:

    created_at                        id                full_text
0   Sat Aug 01 00:25:53 +0000 2020    28934685093219    life is hard with coronavirus
1   Sat Aug 01 00:25:53 +0000 2020    28934685093219    coronavirus sucks

解决方法

您做错了什么,您的代码对我有用:

import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud

array = {'full_text': ['life is hard with coronavirus','coronavirus sucks']}
df = pd.DataFrame(array)

plt.style.use('fivethirtyeight')
allWords = ' '.join([twts for twts in df['full_text']])
wordCloud = WordCloud(collocations=True,width = 1000,height=600,random_state = 21,max_font_size = 120).generate(allWords)

plt.imshow(wordCloud,interpolation = "bilinear")
plt.axis('off')
plt.show()

这是输出:

enter image description here

,

您需要更改 WordCloud 函数中的一个参数:normalize_plurals=False。 参考:https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html

normalize_plurals:bool,默认值=True。是否删除尾随的‘s’ 从词。如果 True 并且出现一个带有或不带有尾随的单词 ‘s’,带有尾随‘s’的那个被删除,它的计数被添加到 没有尾随“s”的版本——除非单词以“ss”结尾。 如果使用 generate_from_frequencies,则忽略。