创建文字wordclouds的精美内容丰富的表示形式

Wordclouds are often mislabeled as being clunky and old-fashioned. In reality, they can be elegant and creative communication methods of text, both as exploratory analysis but also in presentation. Additionally, they’re very easy to create in Python —so let’s get into it!

Wordcloud通常被错误地标记为笨拙且过时的。 实际上,它们既可以作为探索性分析,也可以作为表示形式的优雅,创造性的文本交流方式。 此外,它们很容易在Python中创建-因此让我们开始吧!

Let’s copy-paste the content from this COVID-19 article and paste it into a text file named covid_article.txt. The content of this text file will be stored into a variable named content.

让我们复制并粘贴此COVID-19文章中的内容,并将其粘贴到名为covid_article.txt的文本文件中。 该文本文件的内容将存储到名为content的变量中。

content = open("covid_article.txt").read()

In order to make sure one word the same as another, we need to remove punctuation and capitalization, such that ‘hello’ is the same as ‘Hello’, which is the same as ‘hello!’. We will also need to make sure that the characters are all alphabetic — we can accomplish this with list comprehension (alternatively with regular expressions).

为了确保一个单词与另一个单词相同,我们需要删除标点符号和大写字母,以使“ hello”与“ Hello”相同,而与“ hello!”相同。 我们还需要确保所有字符都是字母-我们可以通过列表理解(或者使用正则表达式)来实现。

import string
for punc_char in string.punctuation:
    content = content.replace(punc_char,'') #remove punctuation
content = content.lower() #make lowercase
content = ''.join([char for char in content if char in ' abcdefghijklmnopqrstuvwxyz']) #only alphabetic characters
Image for post

There are noticeably some small things that need to be fixed, but generally this is just a string of words and we’ll go ahead for now. We’ll need to import the wordcloud module (install using pip install wordcloud) and the matplotlib library to display the image.

显然有一些小问题需要修复,但是通常这只是一句话,我们现在继续。 我们需要导入wordcloud模块(使用pip install wordcloud )和matplotlib库来显示图像。

from wordcloud import WordCloud
import matplotlib.pyplot as plt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值