WordCloud.jl Documentation
Adding WordCloud.jl
julia> using Pkgjulia> Pkg.add("WordCloud")Updating registry at `~/.julia/registries/General.toml` Resolving package versions... Installed WordCloud ─ v1.3.2 Updating `~/work/WordCloud.jl/WordCloud.jl/docs/Project.toml` [6385f0a0] ~ WordCloud v1.3.3 `~/work/WordCloud.jl/WordCloud.jl` ⇒ v1.3.2 [loaded: v1.3.3] Updating `~/work/WordCloud.jl/WordCloud.jl/docs/Manifest.toml` [6385f0a0] ~ WordCloud v1.3.3 `~/work/WordCloud.jl/WordCloud.jl` ⇒ v1.3.2 [loaded: v1.3.3] Precompiling packages... 4255.7 ms ✓ WordCloud 1 dependency successfully precompiled in 6 seconds. 190 already precompiled. 1 dependency precompiled but a different version is currently loaded. Restart julia to access the new version. Otherwise, loading dependents of this package may trigger further precompilation to work with the unexpected version.
Documentation
WordCloud.WordCloud — Module
The WordCloud.jl package is a flexible, faithful and efficient word cloud generator in Julia.
using WordCloud
wc = wordcloud("It's easy to generate a beautiful word cloud.") |> generate!
paint(wc, "wordcloud.svg")Please visit the repository at: https://github.com/guo-yong-zhi/WordCloud.jl
sourceWordCloud.configsvgimages! — Method
For editing SVGs of words. The 1st argument is wordcloud, the 2nd optional argument is index which can be string, number, list, or any other standard supported index. There are two kinds of keyword arguments, children and wrappers. The nodes in children will be linked under the root node of the SVG. The nodes in wrappers will be inserted between the SVG root node and all its child nodes. The children will be wraped by the wrapper node. A node is represented as a String Pair. e.g.
- child
"title"=>"word"for<title>word</title> - child
"title"=>("word", :lang=>"en-us")for<title lang="en-us">word</title> - child
"animate" => ["attributeName"=>"opacity", "to"=>"0", "dur"=>"6s"]for<animate attributeName="opacity" to="0" dur="6s"/> - wrapper
"a"=>("href"=>"https://www.google.com")for<a href="https://www.google.com">and</a>
Arguments children and wrappers can be a Pair, or a Tuple of Pairs to add multiple nodes to a SVG. Again, giving a list of Tuples of Pairs is ok to edit multiple SVGs corresponding to the index argument.
WordCloud.fit! — Method
Positional Arguments
- wc: the word cloud object generated by the
wordcloudfunction, which needs to be fitted. - epochs: the number of training epochs
Keyword Arguments
- patience: the number of epochs before repositioning
- reposition: a boolean value that determines whether repositioning is enabled or disabled. Additionally, it can accept a float value p (0 ≤ p ≤ 1) to indicate the repositioning ratio, an integer value n to specify the minimum index for repositioning, a function index::Int -> repositionable::Bool to customize the repositioning behavior, or a whitelist for specific indexes.
- trainer: specify a training engine
WordCloud.generate! — Method
Positional Arguments
- wc: the word cloud object generated by the
wordcloudfunction, which needs to be fitted. - epochs: the number of training epochs
Keyword Arguments
- retry: the number of attempts for shrinking and retraining, default is 3; set to 1 to disable shrinking
- patience: the number of epochs before repositioning
- reposition: a boolean value that determines whether repositioning is enabled or disabled. Additionally, it can accept a float value p (0 ≤ p ≤ 1) to indicate the repositioning ratio, an integer value n to specify the minimum index for repositioning, a function index::Int -> repositionable::Bool to customize the repositioning behavior, or a whitelist for specific indexes.
- trainer: specify a training engine
WordCloud.getangles — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getcolors — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getfonts — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getfontsizes — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getimages — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getpositions — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values. The keyword argument mode can be either getshift or getcenter.
WordCloud.getscheme — Method
getscheme(wc::WC)Returns the scheme of an existing word cloud, which can be used to create a new word cloud with the same styling. e.g., wc1 = wordcloud("a word cloud"); wc2 = wordcloud("a new word cloud"; getscheme(wc1)...)
WordCloud.getsvgimages — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getweights — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.getwords — Function
This function accepts two positional arguments: a wordcloud object and an index. The index can be a string, number, list, or any other supported type of index. The index argument is optional, and omitting it will retrieve all the values.
sourceWordCloud.ignore — Method
Exclude specific words as if they do not exist, and then execute the function. It functions as the opposite of keep.
- ignore(fun, wc, ws::String) # ignore a word
- ignore(fun, wc, ws::Set{String}) # ignore all words in ws
- ignore(fun, wc, ws::Vector{String}) # ignore all words in ws
- ignore(fun, wc, inds::Union{Integer, Vector{Integer}})
- ignore(fun, wc::WC, mask::AbstractArray{Bool}) # ignore words. The
maskmust have the same length aswc
WordCloud.initialize! — Method
Initialize the images and other resources associated with words using the specified style.
sourceWordCloud.keep — Method
Retain specific words and ignore the rest, and then execute the function. It functions as the opposite of ignore.
- keep(fun, wc, ws::String) # keep a word
- keep(fun, wc, ws::Set{String}) # keep all words in ws
- keep(fun, wc, ws::Vector{String}) # keep all words in ws
- keep(fun, wc, inds::Union{Integer, Vector{Integer}})
- keep(fun, wc::WC, mask::AbstractArray{Bool}) # keep words. The
maskmust have the same length aswc
WordCloud.layout! — Method
The layout! function is employed to establish an initial layout for the word cloud.
- layout!(wc)
- layout!(wc, style=:uniform)
- layout!(wc, style=:gathering)
- layout!(wc, style=:gathering, level=5) # The
levelparameter controls the intensity of gathering, typically ranging from 4 to 6. The default value is 5. - layout!(wc, style=:gathering, level=6, rt=0) # rt=0 for rectangle, rt=1 for ellipse, rt=2 for rhombus. The default value is 1.
There is also a keyword argument centralword available. For example, centralword=1, centralword="Alice" or centralword=false. When you have set style=:gathering, you should also disable repositioning in generate!, especially for big words. For example, generate!(wc, reposition=0.7). The keyword argument reorder is a function used to reorder the words, which affects the order of placement. For example, you can use reverse or WordCloud.shuffle.
WordCloud.loadmask — Method
Load an image as a mask, recolor it, or resize it, among other options.
Examples
- loadmask(open("res/heart.jpg"), 256, 256) # resize to 256*256
- loadmask("res/heart.jpg", ratio=0.3) # scaled by 0.3
- loadmask("res/heart.jpg", color="red", ratio=2) # set forecolor
- loadmask("res/heart.jpg", transparent=rgba->maximum(rgba[1:3])*(rgba[4]/255)>128) # set transparent using a Function
- loadmask("res/heart.jpg", color="red", transparent=(1,1,1)) # set forecolor and transparent
- loadmask("res/heart.svg") # only a subset of arguments is supported
padding: an Integer or a tuple of two Integers. For other keyword arguments like outline, linecolor, and smoothness, refer to the function outline.
WordCloud.paint — Method
Paint the wordcloud generated object into an image or an SVG file. See also: paintsvg
Examples
- paint(wc::WC)
- paint(wc::WC, background=false) # without background
- paint(wc::WC, background=outline(wc.mask)) # use a different background
- paint(wc::WC, ratio=0.5) # resize the output
- paint(wc::WC, "result.png", ratio=0.5) # save as png file
- paint(wc::WC, "result.svg") # save as svg file
WordCloud.paintcloud — Method
Generate a word cloud image from text. This function acts as a convenient shortcut for paint(generate!(wordcloud(...))...). It accepts the same arguments as wordcloud and paint.
other arguments
- quiet: suppresses the output of the generation process. defaults to true.
See also: paintsvgcloud
Examples
- paintcloud("holly bible")
- paintcloud("holly bible", "result.svg", quiet=false)
- paintcloud(["holly", "bible"], [0.7, 0.3], "result.png", background=false)
- paintcloud("holly bible", angles=(0, 90), ratio=0.5)
WordCloud.paintsvg — Method
WordCloud.paintsvgcloud — Method
Similar to paintcloud, but exports SVG
WordCloud.pin — Method
Fix specific words as if they were part of the background, and then execute the function.
- pin(fun, wc, ws::String) # pin an single word
- pin(fun, wc, ws::Set{String}) # pin all words in ws
- pin(fun, wc, ws::Vector{String}) # pin all words in ws
- pin(fun, wc, inds::Union{Integer, Vector{Integer}})
- pin(fun, wc::WC, mask::AbstractArray{Bool}) # pin words. # pin words. The
maskmust have the same length aswc.
WordCloud.recolor! — Method
Recolor the words according to the pixel color in the background image. The styles supported are :average, :main, :clipping, :blending, and :reset (to undo all effects of the other styles).
Examples
- recolor!(wc, style=:average)
- recolor!(wc, style=:main)
- recolor!(wc, style=:clipping, background=blur(getmask(wc))) # The
backgroundparameter is optional - recolor!(wc, style=:blending, alpha=0.3) # The
alphaparameter is optional - recolor!(wc, style=:reset)
The effects of :average, :main, and :clipping are determined solely by the background. However, the effect of :blending is also influenced by the previous color of the word. Therefore, :blending can also be used in combination with other styles. The results of clipping and blending cannot be exported as SVG files; PNG should be used instead.
WordCloud.rescale! — Method
rescale!(wc::WC, ratio::Real)Resize all words proportionally. Use a ratio < 1 to shrink the size, and a ratio > 1 to expand the size.
sourceWordCloud.runexample — Function
Available values: [:alice, :animation1, :animation2, :benchmark, :compare, :compare2, :custom, :fromweb, :gathering, :highdensity, :hyperlink, :japanese, :juliadoc, :languages, :lettermask, :logo, :nomask, :outline, :pattern, :qianziwen, :random, :recolor, :semantic, :series, :svgconfig, :中文]
sourceWordCloud.setangles! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setcolors! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setfontcandidates! — Method
WordCloud.setfonts! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setfontsizes! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setimages! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setpositions! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index. The keyword argument mode can be either setshift! or setcenter!.
WordCloud.setsvgimages! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setweights! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.setwords! — Method
This function accepts three positional arguments: a wordcloud object, an index, and a value. The index can be a string, number, list, or any other supported type of index.
sourceWordCloud.showexample — Function
Available values: [:alice, :animation1, :animation2, :benchmark, :compare, :compare2, :custom, :fromweb, :gathering, :highdensity, :hyperlink, :japanese, :juliadoc, :languages, :lettermask, :logo, :nomask, :outline, :pattern, :qianziwen, :random, :recolor, :semantic, :series, :svgconfig, :中文]
sourceWordCloud.wordcloud — Method
Positional Arguments
The positional arguments are used to specify words and weights in various forms, such as Tuple or Dict.
- words::AbstractVector{<:AbstractString}, weights::AbstractVector{<:Real}
- words_weights::Tuple
- counter::AbstractDict
- counter::AbstractVector{<:Pair}
Optional Keyword Arguments
text-related keyword arguments
For more sophisticated text processing, please utilize the function processtext.
- language: language of the text, default is
:auto. - stopwords: a set of words, default is
:autowhich means decided by language. - stopwords_extra: an additional set of stopwords. By setting this while keeping the
stopwordsargument as:auto, the built-in stopword list will be preserved. - maxnum: maximum number of words, default is 500
style-related keyword arguments
- colors = "black" # same color for all words
- colors = ("black", (0.5,0.5,0.7), "yellow", "#ff0000", 0.2) # entries are randomly chosen
- colors = ["black", (0.5,0.5,0.7), "yellow", "red", (0.5,0.5,0.7), 0.2, ......] # elements are used in a cyclic manner
- colors = :seaborn_dark # Using a preset scheme. See
WordCloud.colorschemesfor all supported Symbols.WordCloud.displayschemes()may be helpful. - angles = 0 # same angle for all words
- angles = (0, 90, 45) # randomly select entries
- angles = 0:180 # randomly select entries
- angles = [0, 22, 4, 1, 100, 10, ......] # use elements in a cyclic manner
- fonts = "Serif Bold" # same font for all words
- fonts = ("Arial", "Times New Roman", "Tahoma") # randomly select entries
- fonts = ["Arial", "Times New Roman", "Tahoma", ......] # use elements in a cyclic manner
- minfontsize: The minimum font size in pixel.
- maxfontsize: The maximum font size in pixel.
- avgfontsize: The average font size in pixel, default is 12. It is used to control the size of the generated picture when
masksizeis not specified. - density = 0.55 # default is 0.5
- spacing = 1 # minimum spacing between words, default is :auto
mask-related keyword arguments
- mask = loadmask("res/heart.jpg", 256, 256) # refer to the documentation of
loadmask - mask = loadmask("res/heart.jpg", color="red", ratio=2) # refer to the documentation of
loadmask - mask = "res/heart.jpg" # shortcut for loadmask("res/heart.jpg")
- mask = shape(ellipse, 800, 600, color="white", backgroundcolor=(0,0,0,0)) # refer to the documentation of
shape. - mask = box # mask can also be one of
box,ellipse,squircle,ngon,star,bezingonorbezistar. Refer to the documentation ofshape. - masksize: It can be a tuple
(width, height), a single number indicating the side length, or one of the symbols:original,:default, or:auto. - backgroundsize: Refer to
shape. It is used withmasksizeto specify the padding size. - maskcolor: It can take various values that represent colors, such as
"black","#f000f0",(0.5, 0.5, 0.7), or0.2. Alternatively, it can be set to one of the following options::default,:original(to maintain its original color), or:auto(to automatically recolor the mask). - backgroundcolor: It can take various values that represent colors. Alternatively, it can be set to one of the following options:
:default,:original,:maskcolor, or:auto(which randomly selects between:originaland:maskcolor). - outline, linecolor, smoothness: Refer to the
shapeandoutlinefunctions. - transparent = (1,0,0) # interpret the color
(1,0,0)as transparent - transparent = nothing # no transparent color
- transparent = c->(c[1]+c[2]+c[3])/3*(c[4]/255)>128) # set transparency using a function.
cis an (r,g,b,a) Tuple.
- Notes
getschemeis useful when you want to create a new word cloud with the same style as an existing word cloud.- Some arguments depend on whether the
maskis provided or on the type of the providedmask.
other keyword arguments
- style, centralword, reorder, rt, level: Configure the layout style of word cloud. Refer to the documentation of
layout!. - The keyword argument
stateis a function. It will be called after the wordcloud object is constructed, which sets the object to a specific state.- state = initialize! # only initializes resources, such as word images
- state = layout! # It is the default setting that initializes the position of words
- state = generate! # get the result directly
- state = identity # do nothing
- Notes
- After obtaining the wordcloud object, the following steps are required to obtain the resulting picture: initialize! -> layout! -> generate! -> paint
- You can skip
initialize!and/orlayout!, and these operations will be automatically performed with default parameters - You can use
paintcloudandpaintsvgcloudto obtain the final picture directly.
WordCloud.TextProcessing.countwords — Method
countwords(text_or_counter; counter=Dict{String,Int}(), language=:auto, regexp=r"(?:\S[\s\S]*)?[^0-9_\W](?:[\s\S]*\S)?")Count words in text. And save results into counter. text_or_counter can be a String, a Vector of Strings, an opend file (IO) or a Dict. regexp is a regular expression to partially match and filter words. For example, regexp=r"\S(?:[\s\S]*\S)?" will trim whitespaces then eliminate empty words.
WordCloud.TextProcessing.lemmatizer_eng — Method
only handles the simple cases of plural nouns and third person singular verbs
sourceWordCloud.TextProcessing.processtext — Method
Process the text, filter the words, and adjust the weights. Return a vector of words and a vector of weights.
Positional Arguments
- textorcounter: a string, a vector of words, an opened file (IO), a Dict{<:String, <:Real}, a Vector{Pair}, a Vector{Tuple}, or two Vectors.
Optional Keyword Arguments
- language: language of the text, default is
:auto. - stopwords: a set of words, default is
:autowhich means decided by language. - stopwords_extra: an additional set of stopwords. By setting this while keeping the
stopwordsargument as:auto, the built-in stopword list will be preserved. - minlength, maxlength: minimum and maximum length of a word to be included
- minfrequency: minimum frequency of a word to be included
- maxnum: maximum number of words, default is 500
- minweight, maxweight: within 0 ~ 1, set to adjust extreme weight
- regexp: a regular expression to partially match and filter words. For example,
regexp=r"\S(?:[\s\S]*\S)?"will trim whitespaces then eliminate empty words. This argument is not available whentext_or_counteris a counter. - process: a function to process word count dict, default is
rescaleweights(identity, 0) ∘ casemerge!
WordCloud.TextProcessing.rescaleweights — Function
rescaleweights(func=identity, p=0)This function takes word length into account. Therefore, the rescaled weights can be used as font size coefficients. The function func(w::Real)->Real is used to remap the weight, expressed as weight = func(weight); p represents the exponent of the power mean. We set weight = powermean(1*fontsize, wordlength*fontsize) = ((fontsize^p + (wordlength*fontsize)^p)/2) ^ (1/p). That is, weight = fontsize * powermean(1, wordlength). Overall, this gives us fontsize = func(weight) / powermean(1, wordlength). When p is -Inf, the power mean is the minimum value, resulting in fontsize=weight. When p is Inf, the power mean is the maximum value, resulting in fontsize=weight/wordlength. When p is -1, the power mean is the harmonic mean. When p is 0, the power mean is the geometric mean, preserving the word area. When p is 1, the power mean is the arithmetic mean. When p is 2, the power mean is the root mean square, preserving the diagonal length.
WordCloud.Render.intersection_region — Function
Return the intersecting region views of img1 and img2, where img2 is positioned in img1 with its top left corner located at coordinates (x, y).
WordCloud.Render.outline — Method
Positional Arguments
- img: a bitmap image
Keyword Arguments
- linewidth: 0 <= linewidth
- color: line color
- transparent: the color of the transparent area, default is :auto
- smoothness: 0 <= smoothness <= 1, smoothness of the line, default is 0.5
WordCloud.Render.overlay! — Function
Place img2 onto img1 at coordinates (x, y).
sourceWordCloud.Render.shape — Method
Generate an SVG image of a box, ellipse, squircle, ngon, star, bezingon, or bezistar.
Positional Arguments
- shape: one of
box,ellipse,squircle,ngon,star,bezingon, orbezistar - width: width of the shape
- height: height of the shape
Keyword Arguments
- outline: an integer indicating the width of the outline
- padding: an integer or a tuple of two integers indicating the padding size
- backgroundsize: a tuple of two integers indicating the size of the background
- color, linecolor, backgroundcolor: any value that can be parsed as a color.
- npoints, starratio, orientation, cornerradius, rt: see the Examples section below
Examples
- shape(box, 80, 50) # box with dimensions 80*50
- shape(box, 80, 50, cornerradius=4) # box with corner radius 4
- shape(squircle, 80, 50, rt=0.7) # squircle or superellipse. rt=0 for rectangle, rt=1 for ellipse, rt=2 for rhombus.
- shape(ngon, 120, 100, npoints=12, orientation=π/6) # regular dodecagon (12 corners) oriented by π/6
- shape(star, 120, 100, npoints=5) # pentagram (5 tips)
- shape(star, 120, 100, npoints=5, starratio=0.7, orientation=π/2) # 0.7 specifies the ratio of the smaller and larger radii; oriented by π/2
- shape(ellipse, 80, 50, color="red") # red ellipse with dimensions 80*50
- shape(box, 80, 50, backgroundcolor=(0,1,0), backgroundsize=(100, 100)) # 8050 box on a 100100 green background
- shape(squircle, 80, 50, outline=3, linecolor="red", backgroundcolor="gray") # add a red outline to the squircle
Gallery
Index
WordCloud.WordCloudWordCloud.Render.intersection_regionWordCloud.Render.outlineWordCloud.Render.overlay!WordCloud.Render.shapeWordCloud.TextProcessing.countwordsWordCloud.TextProcessing.lemmatizer_engWordCloud.TextProcessing.processtextWordCloud.TextProcessing.rescaleweightsWordCloud.TextProcessing.setlemmatizer!WordCloud.TextProcessing.setstopwords!WordCloud.TextProcessing.settokenizer!WordCloud.configsvgimages!WordCloud.fit!WordCloud.generate!WordCloud.getanglesWordCloud.getcolorsWordCloud.getfontsWordCloud.getfontsizesWordCloud.getimagesWordCloud.getpositionsWordCloud.getschemeWordCloud.getsvgimagesWordCloud.getweightsWordCloud.getwordsWordCloud.ignoreWordCloud.initialize!WordCloud.keepWordCloud.layout!WordCloud.loadmaskWordCloud.paintWordCloud.paintcloudWordCloud.paintsvgWordCloud.paintsvgcloudWordCloud.pinWordCloud.recolor!WordCloud.rescale!WordCloud.runexampleWordCloud.setangles!WordCloud.setcolors!WordCloud.setfontcandidates!WordCloud.setfonts!WordCloud.setfontsizes!WordCloud.setimages!WordCloud.setpositions!WordCloud.setsvgimages!WordCloud.setweights!WordCloud.setwords!WordCloud.showexampleWordCloud.wordcloud