【问题标题】:My WordCloud is missing the letter 's' at the end of words我的 WordCloud 缺少单词末尾的字母 's'
【发布时间】:2021-01-26 16:01:51
【问题描述】:

起初我以为问题出在我的数据上,我在清理数据时犯了一个错误。但是我检查了它,事实并非如此。

我正在使用此代码:

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()

现在我的 wordcloud 会显示诸如“coronaviru”、“viru”、“crisi”之类的词。使用collocations=True 它会显示完整的词以及其他词,例如“coronavirus case”“coronavirus 大流行”。 有谁知道如何解决这一问题? 就像我说的那样,我检查了数据,那里总是正确的完整单词。所以我猜这个错误发生在 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

【问题讨论】:

  • 发布您的数据样本
  • @gtomer 我添加了它。

标签: python pandas matplotlib word-cloud


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你做错了,你的代码对我有用:

    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()
    

    这是输出:

    【讨论】:

    • 当我尝试使用包含例如“冠状病毒”的特定行时。该行的 wordcloud 显示拼写正确的“coronavirus”。一旦我将它应用到整个数据集,我就会得到带有 coronaviru 的 wordcloud。我用过滤器搜索了 excel 中的数据集列(讨厌我),只找到了正确拼写的单词。真的不知道问题出在哪里。编辑:还使用df.full_text.str.count("coronavirus").sum() 在代码中检查它,它等于f.full_text.str.count("coronaviru").sum()。所以它总是正确拼写
    • 那么你如何解释上面的代码有效呢?你还用 DF 做什么?
    • 我什么都不做。我使用的正是那个代码。我的想法是(请记住,我是个新手):DF 的结构可能在某些方面存在缺陷。我早些时候遇到了问题。但是现在看它看起来很完美。也许它与 \s 的事情有关,但没有线索。另一个天真的想法是 wordcloud 插件试图不将单词显示为复数形式,并因此删除了“s”。虽然没有太大意义,但我没有找到任何相关信息。
    • 我现在通过将拼写错误的单词添加到停用词列表中来修复它,以便将它们排除在外。现在 wordcloud 实际上显示了大小相似的正确拼写的单词。很奇怪。仍然不知道是什么导致了根本问题。
    • 它不应该这样表现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多