Skip to content

Commit 85795bb

Browse files
committed
feat(WordCloud): add paintcloud and paintsvgcloud functions
1 parent f310ce3 commit 85795bb

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ wc = wordcloud(open(pkgdir(WordCloud)*"/res/alice.txt")) |> generate! # from a f
3939
wc = wordcloud(["中文", "需要", "提前", "分词"]) |> generate! # from a list
4040
```
4141
```julia
42-
wc = wordcloud(["the"=>1.0, "to"=>0.51, "and"=>0.50,
43-
"of"=>0.47, "a"=>0.44, "in"=>0.33]) |> generate! # from pairs or a dict
42+
wc = wordcloud(["the"=>1.0, "to"=>0.51, "and"=>0.50]) |> generate! # from pairs or a dict
43+
```
44+
```julia
45+
paintcloud("It's easy to generate word clouds") # obtain the final picture directly
4446
```
4547
# Advanced Usage
4648
```julia

src/WordCloud.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Please visit the repository at: <https://github.com/guo-yong-zhi/WordCloud.jl>
1212
module WordCloud
1313
export wordcloud, processtext, html2text, countwords, casemerge!, rescaleweights
1414
export parsecolor, rendertext, shape, ellipse, box, squircle, star, ngon, bezistar, bezingon,
15-
loadmask, outline, padding, paint, paintsvg, svgstring
15+
loadmask, outline, padding, paint, paintsvg, paintcloud, paintsvgcloud, svgstring
1616
export imageof, showmask, showmask!
1717
export record, @record, layout!, rescale!, recolor!, keep, ignore, pin, runexample, showexample, generate!, fit!
1818
export getparameter, setparameter!, hasparameter, getstate, setstate!,

src/wc-class.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ For more sophisticated text processing, please utilize the function [`processtex
7272
* Notes
7373
* After obtaining the wordcloud object, the following steps are required to obtain the resulting picture: initialize! -> layout! -> generate! -> paint
7474
* You can skip `initialize!` and/or `layout!`, and these operations will be automatically performed with default parameters
75+
* You can use [`paintcloud`](@ref) and [`paintsvgcloud`](@ref) to obtain the final picture directly.
7576
"""
7677
wordcloud(wordsweights::Tuple; kargs...) = wordcloud(wordsweights...; kargs...)
7778
wordcloud(counter::AbstractDict; kargs...) = wordcloud(keys(counter) |> collect, values(counter) |> collect; kargs...)

src/wc-helper.jl

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function loadmask(file, args...; kargs...)
8888
loadmask(mask, args...; kargs...)
8989
end
9090

91-
"Similar to `paint`, but exports SVG"
91+
"Similar to [`paint`](@ref), but exports SVG"
9292
function paintsvg(wc::WC; background=true)
9393
imgs = getsvgimages(wc)
9494
poss = getpositions(wc)
@@ -123,6 +123,8 @@ function paintsvg(wc::WC, file, args...; kargs...)
123123
end
124124

125125
"""
126+
Paint the [`wordcloud`](@ref) generated object into an image or an SVG file.
127+
See also: [`paintsvg`](@ref)
126128
# Examples
127129
* paint(wc::WC)
128130
* paint(wc::WC, background=false) # without background
@@ -159,7 +161,37 @@ function paint(wc::WC, file, args...; kargs...)
159161
Render.save(file, img)
160162
img
161163
end
162-
164+
165+
"""
166+
Generate a word cloud image from text. This function serves as a shortcut for `paint(generate!(wordcloud(...))...)`.
167+
For details on supported arguments, see:
168+
- [`wordcloud`](@ref)
169+
- [`paint`](@ref)
170+
See also: [`paintsvgcloud`](@ref)
171+
# Examples
172+
* paintcloud("holly bible")
173+
* paintcloud("holly bible", "result.svg")
174+
* paintcloud(["holly", "bible"], [0.7, 0.3], "result.png", background=false)
175+
* paintcloud("holly bible", angles=(0, 90), ratio=0.5)
176+
"""
177+
function paintcloud(args...; paintfunc=paint, kargs...)
178+
if length(args) > 1 && last(args) isa AbstractString
179+
args_w, args_p = args[1:end-1], args[end:end]
180+
else
181+
args_w, args_p = args, ()
182+
end
183+
pkw = (:background, :ratio)
184+
kargs_w = filter(kw -> first(kw) pkw, kargs)
185+
kargs_p = filter(kw -> first(kw) pkw, kargs)
186+
redirect_stdio(stdout=devnull, stderr=devnull) do
187+
paintfunc(generate!(wordcloud(args_w...; kargs_w...)), args_p...; kargs_p...)
188+
end
189+
end
190+
"Similar to [`paintcloud`](@ref), but exports SVG"
191+
function paintsvgcloud(args...; paintfunc=paintsvg, kargs...)
192+
paintcloud(args...; paintfunc=paintfunc, kargs...)
193+
end
194+
163195
function frame(wc::WC, label::AbstractString, args...; kargs...)
164196
overlay!(paint(wc, args...; kargs...), rendertextoutlines(label, 32, color="black", linecolor="white", linewidth=1), 20, 20)
165197
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ include("test_textprocessing.jl")
1616
paint(wc, "test.jpg", background=outline(wc.mask, color=(1, 0, 0.2, 0.7), linewidth=2), ratio=0.5)
1717
paint(wc, "test.svg", background=WordCloud.tobitmap(wc.mask))
1818
paint(wc, "test.svg")
19+
paintsvgcloud("holly bible", "test.svg")
20+
paintcloud("holly bible", angles=(0, 90), ratio=0.5)
1921
show(wc)
2022
@test getparameter(wc, :volume) == WordCloud.occupancy(WordCloud.QTrees.kernel(wc.maskqtree[1]), WordCloud.QTrees.FULL)
2123
# animation

0 commit comments

Comments
 (0)