word_cloud README

The project's README from github.com/amueller/word_cloud. As one markdown file: README.md.

licence DOI

word_cloud

A little word cloud generator in Python. Read more about it on the blog post or the website.

The code is tested against Python 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13.

Installation

If you are using pip:

pip install wordcloud

If you are using conda, you can install from the conda-forge channel:

conda install -c conda-forge wordcloud

Installation notes

wordcloud depends on numpy, pillow, and matplotlib.

If there are no wheels available for your version of python, installing the package requires having a C compiler set up. Before installing a compiler, report an issue describing the version of python and operating system being used.

Examples

Check out examples/simple.py for a short intro. A sample output is:

Constitution

Or run examples/masked.py to see more options. A sample output is:

Alice in Wonderland

Getting fancy with some colors: Parrot with rainbow colors

Generating wordclouds for Arabic:

Arabic wordlcloud

Command-line usage

The wordcloud_cli tool can be used to generate word clouds directly from the command-line:

$ wordcloud_cli --text mytext.txt --imagefile wordcloud.png

If you're dealing with PDF files, then pdftotext, included by default with many Linux distribution, comes in handy:

$ pdftotext mydocument.pdf - | wordcloud_cli --imagefile wordcloud.png

In the previous example, the - argument orders pdftotext to write the resulting text to stdout, which is then piped to the stdin of wordcloud_cli.py.

Use wordcloud_cli --help so see all available options.

Licensing

The wordcloud library is MIT licenced, but contains DroidSansMono.ttf, a true type font by Google, that is apache licensed. The font is by no means integral, and any other font can be used by setting the font_path variable when creating a WordCloud object.

word_cloud (amueller/word_cloud)

Searching Database…

这是做什么的?word_cloud 是一个用 Python 写的小型词云生成工具:它把一段文本变成一张图片,图里每个词的大小取决于它出现的频率。它既是一个 Python 库,也附带一个叫 wordcloudcli 的小命令行工具。
它是库还是命令行工具?两者都是。在 Python 代码里你可以创建一个 WordCloud 对象并用 matplotlib 来渲染;同一个包还会安装 wordcloudcli,让你可以直接在终端里生成词云。
怎么把它装到我的环境里?用 pip 的话,运行 pip install wordcloud;如果你用的是 conda,可以从 conda-forge 频道安装:conda install -c conda-forge wordcloud。
它会顺带装哪些依赖?它依赖 numpy、pillow 和 matplotlib —— 用 pip 或 conda 安装时会自动一并下载。
在任何环境都能顺利安装吗?基本上可以:代码在 Python 3.7 到 3.13 上都测试过。但要注意,如果你的 Python 版本和操作系统没有对应的预编译 wheel,安装这个包就需要先配置好 C 编译器。
这个许可证对我的项目来说安全吗?这个库本身采用 MIT 许可证,但它内置了 DroidSansMono.ttf,这是 Google 的一款字体,采用 Apache 许可证。这个字体并不是必需的 —— 在创建 WordCloud 对象时设置 fontpath 变量,就可以换成其他任何字体。
能不能不用写代码,直接从一个文本文件生成词云?可以 —— 这个包自带 wordcloudcli。运行 wordcloudcli --text mytext.txt --imagefile wordcloud.png,再用 wordcloudcli --help 查看所有选项。如果是 PDF,可以先把 pdftotext 的输出通过管道传给它:pdftotext mydocument.pdf - | wordcloudcli --imagefile wordcloud.png。
除了普通矩形词云,输出还能是什么样子?示例里有好几种风格:simple.py 是一个简短的入门示例,masked.py 让词云贴合一个遮罩形状,还有一个变体在配色上玩出了花样,以及一个专门生成阿拉伯语词云的示例。
我能用自己的字体吗?可以。默认内置的字体是 Google 的 DroidSansMono.ttf,但它完全不是必需的 —— 在创建 WordCloud 对象时设置 fontpath 变量,就可以使用任何其他 TrueType 字体。
Creator
amueller
Size
117 MB
Stars
10.5K
Age
13y
Commits · 90 Days