word-cloud-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWord Cloud Generator
词云生成器
Create visually appealing word clouds from text, files, or word frequency dictionaries. Customize shapes, colors, fonts, and export to multiple formats.
可从文本、文件或词频字典生成视觉美观的词云,支持自定义形状、颜色、字体,并可导出为多种格式。
Quick Start
快速开始
python
from scripts.wordcloud_gen import WordCloudGeneratorpython
from scripts.wordcloud_gen import WordCloudGeneratorFrom text
From text
wc = WordCloudGenerator("Python is amazing. Python is powerful. Code is fun.")
wc.generate().save("cloud.png")
wc = WordCloudGenerator("Python is amazing. Python is powerful. Code is fun.")
wc.generate().save("cloud.png")
From file
From file
wc = WordCloudGenerator.from_file("article.txt")
wc.colors("plasma").max_words(100).generate().save("cloud.png")
wc = WordCloudGenerator.from_file("article.txt")
wc.colors("plasma").max_words(100).generate().save("cloud.png")
From frequency dict
From frequency dict
frequencies = {"python": 50, "code": 30, "data": 25, "analysis": 20}
wc = WordCloudGenerator(frequencies=frequencies)
wc.shape("circle").generate().save("cloud.png")
undefinedfrequencies = {"python": 50, "code": 30, "data": 25, "analysis": 20}
wc = WordCloudGenerator(frequencies=frequencies)
wc.shape("circle").generate().save("cloud.png")
undefinedFeatures
功能特性
- Multiple Input Sources: Raw text, text files, or word frequency dictionaries
- Shape Options: Rectangle, circle, or custom mask images
- Color Schemes: 20+ matplotlib colormaps or custom color lists
- Font Selection: Use system fonts or custom font files
- Stopword Filtering: Built-in English stopwords + custom additions
- Export Formats: PNG, SVG
- 多输入源:原始文本、文本文件或词频字典
- 形状选项:矩形、圆形或自定义蒙版图片
- 配色方案:20+种matplotlib配色映射或自定义颜色列表
- 字体选择:可使用系统字体或自定义字体文件
- 停用词过滤:内置英文停用词+可添加自定义停用词
- 导出格式:PNG、SVG
API Reference
API参考
Initialization
初始化
python
undefinedpython
undefinedFrom text string
From text string
wc = WordCloudGenerator("Your text here")
wc = WordCloudGenerator("Your text here")
From frequency dictionary
From frequency dictionary
wc = WordCloudGenerator(frequencies={"word": count, ...})
wc = WordCloudGenerator(frequencies={"word": count, ...})
From file
From file
wc = WordCloudGenerator.from_file("path/to/file.txt")
undefinedwc = WordCloudGenerator.from_file("path/to/file.txt")
undefinedConfiguration Methods
配置方法
All methods return for chaining.
selfpython
undefined所有方法均返回以支持链式调用。
selfpython
undefinedShape options
Shape options
wc.shape("rectangle") # Default rectangle
wc.shape("circle") # Circular cloud
wc.shape(mask="logo.png") # Custom shape from image
wc.shape("rectangle") # Default rectangle
wc.shape("circle") # Circular cloud
wc.shape(mask="logo.png") # Custom shape from image
Color schemes (matplotlib colormaps)
Color schemes (matplotlib colormaps)
wc.colors("viridis") # Default
wc.colors("plasma") # Purple-yellow gradient
wc.colors("coolwarm") # Blue-red diverging
wc.colors("Set2") # Categorical colors
wc.colors(custom=["#FF0000", "#00FF00", "#0000FF"]) # Custom colors
wc.colors("viridis") # Default
wc.colors("plasma") # Purple-yellow gradient
wc.colors("coolwarm") # Blue-red diverging
wc.colors("Set2") # Categorical colors
wc.colors(custom=["#FF0000", "#00FF00", "#0000FF"]) # Custom colors
Font settings
Font settings
wc.font("/path/to/font.ttf") # Custom font file
wc.font("/path/to/font.ttf") # Custom font file
Stopwords
Stopwords
wc.stopwords(use_default=True) # Use built-in stopwords
wc.stopwords(words=["custom", "words"]) # Add custom stopwords
wc.stopwords(words=["the", "and"], use_default=False) # Only custom
wc.stopwords(use_default=True) # Use built-in stopwords
wc.stopwords(words=["custom", "words"]) # Add custom stopwords
wc.stopwords(words=["the", "and"], use_default=False) # Only custom
Word limits
Word limits
wc.max_words(200) # Maximum words to display (default: 200)
wc.min_word_length(3) # Minimum word length (default: 1)
wc.max_words(200) # Maximum words to display (default: 200)
wc.min_word_length(3) # Minimum word length (default: 1)
Size
Size
wc.size(800, 400) # Width x Height in pixels
undefinedwc.size(800, 400) # Width x Height in pixels
undefinedGeneration and Export
生成与导出
python
undefinedpython
undefinedGenerate the word cloud
Generate the word cloud
wc.generate()
wc.generate()
Save to file
Save to file
wc.save("output.png") # PNG format
wc.save("output.svg") # SVG format (auto-detected)
wc.save("output.png", format="png") # Explicit format
wc.save("output.png") # PNG format
wc.save("output.svg") # SVG format (auto-detected)
wc.save("output.png", format="png") # Explicit format
Display (matplotlib)
Display (matplotlib)
wc.show()
wc.show()
Get word frequencies
Get word frequencies
freqs = wc.get_frequencies() # Returns dict of word: frequency
undefinedfreqs = wc.get_frequencies() # Returns dict of word: frequency
undefinedColor Schemes
配色方案
Popular Colormaps
热门配色映射
| Name | Description |
|---|---|
| Blue-green-yellow (default) |
| Purple-orange-yellow |
| Black-red-yellow |
| Black-purple-white |
| Blue-yellow (colorblind-safe) |
| Blue-white-red |
| Red-yellow-blue |
| Rainbow spectrum |
| Bold categorical |
| Muted categorical |
| Soft categorical |
| Dark categorical |
| 名称 | 描述 |
|---|---|
| 蓝-绿-黄渐变(默认) |
| 紫-橙-黄渐变 |
| 黑-红-黄渐变 |
| 黑-紫-白渐变 |
| 蓝-黄渐变(色盲友好) |
| 蓝-白-红渐变 |
| 红-黄-蓝渐变 |
| 彩虹光谱色 |
| 鲜明分类色 |
| 柔和分类色 |
| 浅淡分类色 |
| 深色分类色 |
Custom Colors
自定义颜色
python
undefinedpython
undefinedHex colors
Hex colors
wc.colors(custom=["#1a1a2e", "#16213e", "#0f3460", "#e94560"])
wc.colors(custom=["#1a1a2e", "#16213e", "#0f3460", "#e94560"])
Named colors
Named colors
wc.colors(custom=["navy", "royalblue", "cornflowerblue", "lightsteelblue"])
undefinedwc.colors(custom=["navy", "royalblue", "cornflowerblue", "lightsteelblue"])
undefinedShape Masks
形状蒙版
Built-in Shapes
内置形状
python
wc.shape("rectangle") # Default
wc.shape("circle") # Circular
wc.shape("square") # Squarepython
wc.shape("rectangle") # Default
wc.shape("circle") # Circular
wc.shape("square") # SquareCustom Mask Images
自定义蒙版图片
Use any image where white areas are filled with words:
python
undefined可使用任意图片,其中白色区域会被词填充:
python
undefinedUse logo or shape image as mask
Use logo or shape image as mask
wc.shape(mask="company_logo.png")
wc.shape(mask="heart.png")
wc.shape(mask="map_outline.png")
**Mask Image Requirements:**
- White (#FFFFFF) areas will be filled with words
- Black/dark areas will be empty
- Works best with high-contrast images
- Recommended: 800x800 pixels or largerwc.shape(mask="company_logo.png")
wc.shape(mask="heart.png")
wc.shape(mask="map_outline.png")
**蒙版图片要求:**
- 白色(#FFFFFF)区域会被词填充
- 黑色/深色区域为空白
- 高对比度图片效果最佳
- 推荐尺寸:800x800像素或更大CLI Usage
CLI使用方法
bash
undefinedbash
undefinedBasic usage
Basic usage
python wordcloud_gen.py --text "Your text here" --output cloud.png
python wordcloud_gen.py --text "Your text here" --output cloud.png
From file
From file
python wordcloud_gen.py --file article.txt --output cloud.png
python wordcloud_gen.py --file article.txt --output cloud.png
With options
With options
python wordcloud_gen.py --file data.txt
--shape circle
--colors plasma
--max-words 150
--output cloud.png
--shape circle
--colors plasma
--max-words 150
--output cloud.png
python wordcloud_gen.py --file data.txt \
--shape circle \
--colors plasma \
--max-words 150 \
--output cloud.png
Custom mask
Custom mask
python wordcloud_gen.py --file speech.txt
--mask logo.png
--colors Set2
--output branded_cloud.png
--mask logo.png
--colors Set2
--output branded_cloud.png
python wordcloud_gen.py --file speech.txt \
--mask logo.png \
--colors Set2 \
--output branded_cloud.png
SVG output
SVG output
python wordcloud_gen.py --text "Hello World" --output cloud.svg
undefinedpython wordcloud_gen.py --text "Hello World" --output cloud.svg
undefinedCLI Arguments
CLI参数
| Argument | Description | Default |
|---|---|---|
| Input text string | - |
| Input text file path | - |
| Output file path | |
| Shape: rectangle, circle, square | |
| Custom mask image path | - |
| Colormap name | |
| Maximum words | 200 |
| Minimum word length | 1 |
| Image width | 800 |
| Image height | 400 |
| Custom font file | - |
| Disable stopword filtering | False |
| Additional stopwords (comma-separated) | - |
| 参数 | 描述 | 默认值 |
|---|---|---|
| 输入文本字符串 | - |
| 输入文本文件路径 | - |
| 输出文件路径 | |
| 形状:rectangle、circle、square | |
| 自定义蒙版图片路径 | - |
| 配色映射名称 | |
| 最大显示词数 | 200 |
| 最小词长度 | 1 |
| 图片宽度 | 800 |
| 图片高度 | 400 |
| 自定义字体文件路径 | - |
| 禁用停用词过滤 | False |
| 额外停用词(逗号分隔) | - |
Examples
示例
Basic Word Cloud
基础词云
python
text = """
Python is a versatile programming language. Python is used for web development,
data science, machine learning, and automation. Python's simplicity makes it
perfect for beginners while its power serves experts.
"""
wc = WordCloudGenerator(text)
wc.colors("plasma").generate().save("python_cloud.png")python
text = """
Python is a versatile programming language. Python is used for web development,
data science, machine learning, and automation. Python's simplicity makes it
perfect for beginners while its power serves experts.
"""
wc = WordCloudGenerator(text)
wc.colors("plasma").generate().save("python_cloud.png")Frequency-Based Cloud
基于词频的词云
python
undefinedpython
undefinedWord frequencies from analysis
Word frequencies from analysis
tech_terms = {
"Python": 100,
"JavaScript": 85,
"Machine Learning": 70,
"API": 65,
"Database": 60,
"Cloud": 55,
"DevOps": 45,
"Kubernetes": 40,
"Docker": 38,
"React": 35
}
wc = WordCloudGenerator(frequencies=tech_terms)
wc.shape("circle").colors("Set2").generate().save("tech_cloud.png")
undefinedtech_terms = {
"Python": 100,
"JavaScript": 85,
"Machine Learning": 70,
"API": 65,
"Database": 60,
"Cloud": 55,
"DevOps": 45,
"Kubernetes": 40,
"Docker": 38,
"React": 35
}
wc = WordCloudGenerator(frequencies=tech_terms)
wc.shape("circle").colors("Set2").generate().save("tech_cloud.png")
undefinedBranded Word Cloud
品牌化词云
python
undefinedpython
undefinedUse company logo as mask
Use company logo as mask
wc = WordCloudGenerator.from_file("company_values.txt")
wc.shape(mask="logo_white_bg.png")
wc.colors(custom=["#003366", "#0066cc", "#3399ff"])
wc.font("/fonts/OpenSans-Bold.ttf")
wc.generate().save("branded_cloud.png")
undefinedwc = WordCloudGenerator.from_file("company_values.txt")
wc.shape(mask="logo_white_bg.png")
wc.colors(custom=["#003366", "#0066cc", "#3399ff"])
wc.font("/fonts/OpenSans-Bold.ttf")
wc.generate().save("branded_cloud.png")
undefinedArticle Analysis
文章分析
python
undefinedpython
undefinedAnalyze article removing common words
Analyze article removing common words
wc = WordCloudGenerator.from_file("news_article.txt")
wc.stopwords(words=["said", "according", "reported"]) # Add domain stopwords
wc.min_word_length(4)
wc.max_words(100)
wc.colors("RdYlBu")
wc.generate().save("article_themes.png")
wc = WordCloudGenerator.from_file("news_article.txt")
wc.stopwords(words=["said", "according", "reported"]) # Add domain stopwords
wc.min_word_length(4)
wc.max_words(100)
wc.colors("RdYlBu")
wc.generate().save("article_themes.png")
Get top words
Get top words
top_words = wc.get_frequencies()
for word, freq in list(top_words.items())[:10]:
print(f"{word}: {freq}")
undefinedtop_words = wc.get_frequencies()
for word, freq in list(top_words.items())[:10]:
print(f"{word}: {freq}")
undefinedDependencies
依赖项
wordcloud>=1.9.0
matplotlib>=3.7.0
Pillow>=10.0.0
numpy>=1.24.0wordcloud>=1.9.0
matplotlib>=3.7.0
Pillow>=10.0.0
numpy>=1.24.0Limitations
限制说明
- SVG export converts from PNG (not native vector)
- Very large texts may be slow to process
- Custom fonts must be TrueType (.ttf) or OpenType (.otf)
- Mask images work best with simple, high-contrast shapes
- SVG导出是从PNG转换而来(非原生矢量图)
- 超大文本处理速度可能较慢
- 自定义字体必须是TrueType(.ttf)或OpenType(.otf)格式
- 蒙版图片在简单、高对比度形状下效果最佳 ",