guo-yong-zhi / WordCloud.jl

word cloud generator in julia
MIT License
105 stars 2 forks source link
collision-detection julia layout-algorithm nlp packing-algorithm visualization wordcloud

WordCloud.jl

juliadoc
Dev Binder CI CI-nightly codecov DOI
Word cloud (tag cloud or wordle) is a novelty visual representation of text data. The importance of each word is shown with its font size, position, or color. WordCloud.jl is the perfect tool for generating word clouds, offering several advantages:


🌐 Try the online generator 🌐

✨ Go to the gallery ✨


Installation

import Pkg; Pkg.add("WordCloud")

Basic Usage

using WordCloud
using Random
words = [randstring(rand(1:8)) for i in 1:300]
weights = randexp(length(words))
wc = wordcloud(words, weights)
generate!(wc)
paint(wc, "random.svg")

Alternatively, it could be

wc = wordcloud("It's easy to generate word clouds") |> generate! # from a string
wc = wordcloud(open(pkgdir(WordCloud)*"/res/alice.txt")) |> generate! # from a file
wc = wordcloud(["中文", "需要", "提前", "分词"], fonts="") |> generate! # from a list
wc = wordcloud(["the"=>1.0, "to"=>0.51, "and"=>0.50,
                  "of"=>0.47, "a"=>0.44, "in"=>0.33]) |> generate! # from pairs or a dict

Advanced Usage

using WordCloud
textfile = pkgdir(WordCloud)*"/res/alice.txt"
maskfile = pkgdir(WordCloud)*"/res/alice_mask.png"
wc = wordcloud(
    open(textfile),
    stopwords_extra = ["said"],
    maxnum = 500, 
    mask = maskfile,
    maskcolor = "#faeef8",
    outline = 4,
    linecolor = "purple",
    colors = :Set1_5,
    angles = (0, 90),
    fonts = "Tahoma",
    density = 0.55,
    spacing = 3,) |> generate!
paint(wc, "alice.png", ratio=0.5)

try runexample(:alice) or showexample(:alice)
alice

More Examples

Gathering style

gathering
try runexample(:gathering) or showexample(:gathering)

Recolor

recolor
try runexample(:recolor) or showexample(:recolor)

Semantic

semantic
try runexample(:semantic) or showexample(:semantic)
The variable WordCloud.examples holds all available examples.

About Implementation

WordCloud.jl stands out from other tools due to its unique approach based on image local gradient optimization. Unlike conventional algorithms, WordCloud.jl utilizes a non-greedy algorithm that enables words to be repositioned even after their initial placement. This dynamic adjustment process provides unparalleled freedom in assigning words to any desired position, irrespective of potential overlaps. Furthermore, it eliminates the necessity of scaling words during the adjustment phase. This ingenious design choice maximizes the generator's flexibility, opening up boundless possibilities for customization. For a more detailed understanding of the algorithm, you can refer to the Stuffing.jl - Algorithm Description.

Other word cloud generators