Skip to content

Commit ea43e10

Browse files
author
guoyongzhi
committed
more interfaces & more documents
1 parent 3832973 commit ea43e10

File tree

13 files changed

+214
-123
lines changed

13 files changed

+214
-123
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.ipynb_checkpoints
22
.vscode
3+
*.cov
34
res/*/*
45
address_compare
5-
guxiang_animation
6+
guxiang_animation
7+
/*.png
8+
test/*.jpg

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ wordcloud in Julia
55
* **Exact** Words with the same weight have the exact same size. The algorithm will never scale the word to fit the blank.
66

77
---
8-
8+
# Installation
9+
```julia
10+
import Pkg; Pkg.add("WordCloud")
11+
```
912
# Basic Usage
1013
```julia
11-
]add WordCloud
1214
using WordCloud
1315
words = "天地玄黄宇宙洪荒日月盈昃辰宿列张寒来暑往秋收冬藏闰余成岁律吕调阳云腾致雨露结为霜金生丽水玉出昆冈剑号巨阙珠称夜光果珍李柰菜重芥姜海咸河淡鳞潜羽翔龙师火帝鸟官人皇始制文字乃服衣裳推位让国有虞陶唐吊民伐罪周发殷汤坐朝问道垂拱平章"
1416
words = [string(c) for c in words]
@@ -17,27 +19,34 @@ wc = wordcloud(words, weights)
1719
generate!(wc)
1820
paint(wc, "qianziwen.png")
1921
```
20-
*Run the command `runexample(:qianziwen)` to get the result.*
22+
*Run the command `runexample(:qianziwen)` or `showexample(:qianziwen)` to get the result.*
2123
# More Complex Usage
2224
```julia
25+
using WordCloud
2326
wc = wordcloud(
2427
process(open(pkgdir(WordCloud)*"/res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"]),
2528
mask = loadmask(pkgdir(WordCloud)*"/res/alice_mask.png", color="#faeef8"),
26-
colors = (WordCloud.colorschemes[:Set1_5].colors..., ),
29+
colors = :Set1_5,
2730
angles = (0, 90),
2831
fillingrate = 0.7) |> generate!
29-
paint(wc, "alice.png", ratio=0.5)
32+
paint(wc, "alice.png", ratio=0.5, background=outline(wc.mask, color="purple", linewidth=1))
3033
```
31-
*Run the command `runexample(:alice)` to get the result.*
34+
*Run the command `runexample(:alice)` or `showexample(:alice)` to get the result.*
3235
![alice](res/alice.png)
3336

34-
# More
37+
# More Examples
38+
## Training animation
3539
![animation](res/animation.gif)
36-
[Training Animation](./examples/animation.jl)
37-
*Run the command `runexample(:animation)` to get the result.*
40+
[Training animation](./examples/animation.jl)
41+
*Run the command `runexample(:animation)` or `showexample(:animation)` to get the result.*
42+
## Specifies the style of a particular word
43+
![specifystyle](res/specifystyle.png)
44+
[Specifies the style of a particular word](./examples/specifystyle.jl)
45+
*Run the command `runexample(:specifystyle)` or `showexample(:specifystyle)` to get the result.*
46+
## Comparison
3847
![compare](res/compare.png)
3948
[Comparison of Obama's and Trump's inaugural address](./examples/compare.jl)
40-
*Run the command `runexample(:compare)` to get the result.*
49+
*Run the command `runexample(:compare)` or `showexample(:compare)` to get the result.*
4150

4251
***
4352
* [x] 排序 & 预放置

examples/alice.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ using WordCloud
22
wc = wordcloud(
33
process(open(pkgdir(WordCloud)*"/res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"]),
44
mask = loadmask(pkgdir(WordCloud)*"/res/alice_mask.png", color="#faeef8"),
5-
colors = (WordCloud.colorschemes[:Set1_5].colors..., ),
5+
colors = :Set1_5,
66
angles = (0, 90),
77
fillingrate = 0.7) |> generate!
8+
println("save results to alice.png")
89
paint(wc, "alice.png", background=outline(wc.mask, color="purple", linewidth=1))
910
wc

examples/animation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ texts = df[!, "Column2"]
77
weights = df[!, "Column3"]
88

99
wc = wordcloud(texts, weights, fillingrate=0.8)
10-
11-
gifdirectory = "gg/guxiang_animation"
10+
println("save results to guxiang_animation")
11+
gifdirectory = "guxiang_animation"
1212
generate_animation!(wc, 100, outputdir=gifdirectory)
1313
wc

examples/compare.jl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using WordCloud
22

33
stwords = ["us", "will"];
4+
45
println("==Obama's==")
56
cs = WordCloud.randomscheme()
67
as = WordCloud.randomangles()
@@ -10,42 +11,42 @@ wca = wordcloud(
1011
colors = cs,
1112
angles = as,
1213
fillingrate = fr) |> generate!
14+
1315
println("==Trump's==")
14-
tb, wb = process(open(pkgdir(WordCloud)*"/res/Donald Trump's Inaugural Address.txt"), stopwords=WordCloud.stopwords_en stwords)
15-
samemask = tb .∈ Ref(wca.words)
16-
println(sum(samemask), " same words")
17-
csb = Iterators.take(WordCloud.iter_expand(cs), length(tb)) |> collect
18-
asb = Iterators.take(WordCloud.iter_expand(as), length(tb)) |> collect
19-
wainds = Dict(zip(wca.words, Iterators.countfrom(1)))
20-
for i in 1:length(tb)
21-
if samemask[i]
22-
ii = wainds[tb[i]]
23-
csb[i] = wca.params[:colors][ii]
24-
asb[i] = wca.params[:angles][ii]
25-
end
26-
end
2716
wcb = wordcloud(
28-
(tb,wb),
29-
mask = wca.mask,
30-
colors = csb,
31-
angles = asb,
32-
fillingrate = fr)
33-
for i in 1:length(tb)
34-
if samemask[i]
35-
ii = wainds[tb[i]]
36-
cxy = WordCloud.QTree.center(wca.qtrees[ii])
37-
WordCloud.QTree.setcenter!(wcb.qtrees[i], cxy)
38-
end
17+
process(open(pkgdir(WordCloud)*"/res/Donald Trump's Inaugural Address.txt"), stopwords=WordCloud.stopwords_en stwords),
18+
mask = getmask(wca),
19+
colors = cs,
20+
angles = as,
21+
fillingrate = fr,
22+
run = x->nothing, #turn off the useless initword! and placement! in advance
23+
)
24+
25+
samewords = getword(wca) getword(wcb)
26+
println(length(samewords), " same words")
27+
28+
for w in samewords
29+
setcolor!(wcb, w, getcolor(wca, w))
30+
setangle!(wcb, w, getangle(wca, w))
3931
end
32+
#Follow these steps to generate result: initword! -> placement! -> generate!
33+
initword!(wcb)
34+
4035
println("=ignore defferent words=")
41-
ignore(wcb, .!samemask) do
36+
ignore(wcb, getword(wcb) .∉ Ref(samewords)) do
37+
@assert Set(wcb.words) == Set(samewords)
38+
centers = getposition.(wca, samewords, type=getcenter)
39+
setposition!.(wcb, samewords, centers, type=setcenter!) #manually initialize the position,
40+
setstate!(wcb, :placement!) #and set the state flag
4241
generate!(wcb, 1000, patient=-1, retry=1) #patient=-1 means no teleport; retry=1 means no rescale
4342
end
43+
4444
println("=pin same words=")
45-
pin(wcb, samemask) do
45+
pin(wcb, samewords) do
4646
placement!(wcb)
4747
generate!(wcb, 1000, retry=1) #allow teleport but don‘t allow rescale
4848
end
49+
4950
if getstate(wcb) != :generate!
5051
println("=overall tuning=")
5152
generate!(wcb, 1000, patient=-1, retry=2) #allow rescale but don‘t allow teleport
@@ -54,10 +55,9 @@ end
5455
ma = paint(wca)
5556
mb = paint(wcb)
5657
h,w = size(ma)
57-
space = loadmask(shape(box, w÷20, h))
58-
space .= WordCloud.ARGB(0,0,0,0)
58+
space = fill(mb[1], (h, w÷20))
5959
try mkdir("address_compare") catch end
60-
60+
println("save results to address_compare")
6161
WordCloud.ImageMagick.save("address_compare/compare.png", [ma space mb])
6262

6363
gif = WordCloud.GIF("address_compare")

examples/specifystyle.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using WordCloud
22
wc = wordcloud(
3-
process(open(pkgdir(WordCloud)*"/res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"], maxweight=1, maxnum=200),
3+
process(open("res/alice.txt"), stopwords=WordCloud.stopwords_en ["said"], maxweight=1, maxnum=300),
44
mask = padding(shape(ellipse, 600, 500, color=(0.98, 0.97, 0.99), bgcolor=0.97), 0.1),
5-
colors = (WordCloud.colorschemes[:seaborn_dark].colors..., ),
5+
colors = :seaborn_dark,
66
angles = -90:90,
77
run=x->x, #turn off the useless initword! and placement! in advance
88
)
99

10-
setangle!(wc, "Alice", 0)
11-
setcolor!(wc, "Alice", "purple");
12-
initword!(wc, "Alice", 2size(wc.mask, 2)/length("Alice"))
13-
setposition!(wc, "Alice", reverse((size(wc.mask) .- size(getimage(wc, "Alice"))) 2))
10+
setword!(wc, "Alice", "Alice in Wonderland") # replace the word 'Alice' with 'Alice in Wonderland'
11+
setangle!(wc, "Alice in Wonderland", 0) # make it horizontal
12+
setcolor!(wc, "Alice in Wonderland", "purple");
13+
initword!(wc, "Alice in Wonderland", 2size(wc.mask, 2)/length("Alice in Wonderland")) # set a big font size
14+
setposition!(wc, 1, reverse(size(wc.mask)) 2, type=setcenter!) # center it
1415

15-
pin(wc, "Alice") do
16-
initword!(wc)
16+
pin(wc, "Alice in Wonderland") do
17+
initword!(wc) #init inside `pin` to reset the size of other words
1718
generate!(wc)
1819
end
20+
21+
println("save results to specifystyle.png")
1922
paint(wc, "specifystyle.png")
2023
wc

src/WordCloud.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module WordCloud
22
export wordcloud, shape, ellipse, box, paint, loadmask, process, outline, padding,
33
train!, Momentum, generate!, generate_animation!
4-
export record, parsecolor, placement!, imageof, bitor, take, ignore, pin, runexample, showexample
5-
export getstate, getcolor, getangle, getweight, setcolor!, setangle!, setweight!,
6-
getposition, setposition!, getimage, initword!
4+
export getshift, getcenter, setshift!, setcenter!
5+
export record, parsecolor, placement!, rescale!, imageof, bitor, take, ignore, pin, runexample, showexample
6+
export getstate, setstate!, getcolor, getangle, getword, getweight, setcolor!, setangle!, setweight!, setword!,
7+
getposition, setposition!, getimage, getmask, initword!
78

89
include("qtree.jl")
910
include("rendering.jl")

0 commit comments

Comments
 (0)