|
1 |
| -using WordCloud |
2 | 1 | using Test
|
3 |
| -using Random |
4 | 2 |
|
5 |
| -include("test_render.jl") |
6 |
| -include("test_textprocessing.jl") |
7 |
| - |
8 |
| - |
9 |
| -@testset "WordCloud.jl" begin |
10 |
| - # @show pwd() |
11 |
| - # overall test |
12 |
| - wc = runexample(:random) |
13 |
| - @test getstate(wc) == :generate! |
14 |
| - @test isempty(WordCloud.outofbounds(wc.maskqtree, wc.qtrees)) |
15 |
| - paint(wc) |
16 |
| - paint(wc, "test.jpg", background=outline(wc.mask, color=(1, 0, 0.2, 0.7), linewidth=2), ratio=0.5) |
17 |
| - paint(wc, "test.svg", background=WordCloud.tobitmap(wc.mask)) |
18 |
| - paint(wc, "test.svg") |
19 |
| - paintsvgcloud("holly bible", "test.svg") |
20 |
| - paintcloud("holly bible", angles=(0, 90), ratio=0.5) |
21 |
| - show(wc) |
22 |
| - @test getparameter(wc, :volume) == WordCloud.occupancy(WordCloud.QTrees.kernel(wc.maskqtree[1]), WordCloud.QTrees.FULL) |
23 |
| - # animation |
24 |
| - setpositions!(wc, :, (-1000,-1000)) |
25 |
| - @record "animation1-test" filter=i->i%(2^(i÷100+3))==0 overwrite=true layout!(wc, style=:gathering) |
26 |
| - @record outputdir="animation2-test" filter=i->i%10==0 overwrite=true generate!(wc, 100) |
27 |
| - |
28 |
| - # layout! |
29 |
| - layout!(wc, style=:gathering) |
30 |
| - words = ["." for i in 1:500] |
31 |
| - weights = [1 for i in 1:length(words)] |
32 |
| - @test_throws ErrorException begin # no room |
33 |
| - wc = wordcloud(words, weights, mask=ellipse, masksize=(5, 5), backgroundsize=(10, 10), density=1000, angles=0, maxfontsize=5) |
34 |
| - layout!(wc) |
35 |
| - end |
36 |
| - |
37 |
| - # wordcloud factory |
38 |
| - wc = wordcloud(["singleword" => 12], mask=star, masksize=100, density=0.55, state=generate!) # singleword & Pair |
39 |
| - wc = wordcloud([("loooooooooongword", 42)], mask=shape(box, 200, 150, cornerradius=40, color=0.15), maxfontsize=80, density=0.55) |
40 |
| - wc = wordcloud([("loooooooooongword", 42)], mask=shape(box, 200, 150, cornerradius=40, color=0.15), density=0.55) |
41 |
| - |
42 |
| - wc = wordcloud("giving a single word is ok. giving several words is ok too", |
43 |
| - mask=shape(squircle, 200, 150, color=0.15, rt=2.2), density=0.45, transparent=(1, 1, 1, 0)) # String & small mask |
44 |
| - @test_throws AssertionError wordcloud(["1"], [2,3], density=0.1) |> generate! # length unmatch |
45 |
| - @test_throws AssertionError wordcloud(String[], Int[], density=0.1) |> generate! # empty inputs |
46 |
| - wc2 = wordcloud("a new word cloud"; getscheme(wc)...) |
47 |
| - @test wc.mask === wc2.mask |
48 |
| - @test wc.svgmask === wc2.svgmask |
49 |
| - @test getscheme(wc) == getscheme(wc2) |
50 |
| - |
51 |
| - ##############no mask file |
52 |
| - wc = wordcloud(["test"], [1], maskcolor="green", outline=5) |
53 |
| - @test WordCloud.alpha(parsecolor(getbackgroundcolor(wc))) == 0 |
54 |
| - wc = wordcloud(["test"], [1], backgroundcolor="blue", outline=5) |
55 |
| - @test parsecolor(getmaskcolor(wc)) == parsecolor("blue") |
56 |
| - wc = wordcloud(["test"], [1], maskcolor="green") |
57 |
| - @test getparameter(wc, :outline) == 0 |
58 |
| - wc = wordcloud(["test"], [1], backgroundcolor="blue") |
59 |
| - @test getparameter(wc, :outline) == 0 |
60 |
| - wc = wordcloud(["test"], [1], masksize=(100, 100), outline=0) |
61 |
| - @test all(size(wc.mask) .> 105) |
62 |
| - wc = wordcloud(["test"], [1], masksize=(100, 100), outline=30, padding=30) |
63 |
| - @test all(size(wc.mask) .> 200) |
64 |
| - ##############svg mask |
65 |
| - svgfile = "test.svg" |
66 |
| - wordcloud(["test"], [1], colors="#DE2910", mask=svgfile, maskcolor=:original) |
67 |
| - wordcloud(["test"], [1], mask=open(svgfile)) |
68 |
| - wc = wordcloud(["test"], [1], mask=svgfile, backgroundcolor=0) # warning#can't edit the svg to remove original backgroundcolor, |
69 |
| - # so it's only work when the svgfile has a transparent background |
70 |
| - wc2 = wordcloud(["test"], [1], mask=open(svgfile), padding=20) |
71 |
| - wc3 = wordcloud(["test"], [1], mask=open(svgfile), padding=(10, -10)) |
72 |
| - @test all(size(wc2.mask) .> size(wc.mask)) |
73 |
| - @test all(size(wc2.svgmask) .> size(wc.svgmask)) |
74 |
| - @test (size(wc3.svgmask) .> size(wc.svgmask)) == (true, false) |
75 |
| - wordcloud(["test"], [1], mask=open(svgfile), padding=10, backgroundcolor="red")# warning# |
76 |
| - ##############png mask |
77 |
| - pngfile = pkgdir(WordCloud) * "/res/heart_mask.png" |
78 |
| - wordcloud(["test"], [1], colors="#DE2910", mask=pngfile, maskcolor=:original) |
79 |
| - wc = wordcloud(["test"], [1], mask=open(pngfile), maskcolor="yellow", ratio=0.5) |
80 |
| - @test getbackgroundcolor(wc) in WordCloud.DEFAULTSYMBOLS |
81 |
| - wc = wordcloud(["test"], [1], mask=pngfile, maskcolor="green", outline=5) |
82 |
| - @test getbackgroundcolor(wc) in WordCloud.DEFAULTSYMBOLS |
83 |
| - @test getparameter(wc, :outline) == 5 |
84 |
| - wc = wordcloud(["test"], [1], colors="#DE2910", mask=pngfile, backgroundcolor=0) |
85 |
| - @test getmaskcolor(wc) == getbackgroundcolor(wc) |
86 |
| - wc = wordcloud(["test"], [1], colors="#DE2910", mask=pngfile, maskcolor=1, backgroundcolor=0) |
87 |
| - @test parsecolor(getmaskcolor(wc)) == parsecolor(1) |
88 |
| - @test parsecolor(getbackgroundcolor(wc)) == parsecolor(0) |
89 |
| - wordcloud(["test"], [1], mask=pngfile, backgroundcolor=:maskcolor) # backgroundcolor=maskcolor=:default, didn't change anything |
90 |
| - wordcloud(["test"], [1], mask=pngfile, maskcolor="#faeef8", backgroundcolor=:maskcolor) |
91 |
| - wordcloud(["test"], [1], mask=pngfile, maskcolor="#faeef8", backgroundcolor=:maskcolor, outline=3) |
92 |
| - wordcloud(["test"], [1], mask=pngfile, backgroundcolor=:maskcolor, outline=3, smoothness=0.7) |
93 |
| - wc = wordcloud(["test"], [1], mask=open(pngfile), backgroundcolor=:auto) |
94 |
| - @test getmaskcolor(wc) == getbackgroundcolor(wc) |
95 |
| - wc = wordcloud(["test"], [1], mask=open(pngfile), maskcolor=:auto, masksize=:default) |
96 |
| - @test getbackgroundcolor(wc) == :default |
97 |
| - @test size(wc.mask) == (572, 640) |
98 |
| - wc = wordcloud(["test"], [1], mask=pngfile, masksize=(200, 200)) |
99 |
| - @test size(wc.mask) == (200, 200) |
100 |
| - wc = wordcloud(["test"], [1], mask=pngfile, ratio=0.3, masksize=:default) |
101 |
| - @test all(size(wc.mask) .< 200) |
102 |
| - wc2 = wordcloud(["test"], [1], mask=pngfile, outline=50, ratio=0.3, masksize=:default) |
103 |
| - @test all(size(wc2.mask) .> size(wc.mask)) |
104 |
| - wc3 = wordcloud(["test"], [1], mask=pngfile, padding=(-50, 50), masksize=:default) |
105 |
| - @test (size(wc3.mask) .> (572, 640)) == (false, true) |
106 |
| - wc = wordcloud(["test"], [1], mask=pngfile, padding=100, masksize=:default) |
107 |
| - @test all(size(wc.mask) .> 700) |
108 |
| - # get & set & id |
109 |
| - words, weights = processtext(open("../res/alice.txt"), stopwords_extra=["said"], maxnum=300) |
110 |
| - wc = wordcloud( |
111 |
| - words, weights, |
112 |
| - mask="../res/alice_mask.png", maskcolor="#faeef8", backgroundcolor=0.97, |
113 |
| - colors=(WordCloud.colorschemes[:Set1_5].colors...,), |
114 |
| - angles=(0, 90)); |
115 |
| - rescale!(wc, 1.23) |
116 |
| - @test getwords(wc, WordCloud.ID(2)) == words[2] |
117 |
| - pin(wc, ["head", "voice"]) do |
118 |
| - @test length(wc) == 298 |
119 |
| - setpositions!(wc, 1, (2, 2)) |
120 |
| - setpositions!(wc, [1, "Alice", "eye"], (-1, -2)) |
121 |
| - setpositions!(wc, [1, "Alice", "eye"], [(10, 10),(10, 20),(21, 2)]) |
122 |
| - setpositions!(wc, "time", (0, 0), mode=setcenter!) |
123 |
| - setfontsizes!(wc, [WordCloud.ID(66), "Alice", "eye"], [9, 8, 7.6]) |
124 |
| - @test getwords(wc, WordCloud.ID(12:14)) == words[12:14] |
| 3 | +@testset "multithreading" begin |
| 4 | + using Luxor # hide |
| 5 | + |
| 6 | + tempdir = mktempdir(; cleanup=false) |
| 7 | + cd(tempdir) |
| 8 | + |
| 9 | + function make_drawings(i::Int) |
| 10 | + println("Working on thread ", Threads.threadid()) |
| 11 | + w = 300 |
| 12 | + h = 300 |
| 13 | + filename = "sample" * string(i) * ".png" |
| 14 | + Drawing(w, h, filename) |
| 15 | + origin() |
| 16 | + background("black") |
| 17 | + setopacity(0.5) |
| 18 | + fontsize(250) |
| 19 | + sethue("grey40") |
| 20 | + text(string(i), halign=:center, valign=:middle) |
| 21 | + for k in 1:50 |
| 22 | + pg1 = polycross(rand(BoundingBox()), rand(60:120), 4, vertices=true) |
| 23 | + pg2 = polycross(rand(BoundingBox()), rand(60:120), 4, vertices=true) |
| 24 | + pg3 = polyintersect(pg1, pg2) |
| 25 | + for p in pg3 |
| 26 | + randomhue() |
| 27 | + poly(p, :fill) |
| 28 | + end |
| 29 | + end |
| 30 | + finish() |
| 31 | + return |
125 | 32 | end
|
126 |
| - @test getfontsizes(wc, [WordCloud.ID(66), "Alice", "eye"]) == [9, 8, 7.6] |
127 |
| - @test getwords(wc, WordCloud.ID([112,1])) == words[[112,1]] |
128 |
| - @test getpositions(wc, [1, "Alice", "eye"])[3] == (21, 2) |
129 |
| - @test WordCloud.QTrees.kernelsize(wc.qtrees[WordCloud.index(wc, "time")]) == size(getimages(wc, "time")) |
130 |
| - @test .-reverse(size(getimages(wc, "time"))) .÷ 2 == getpositions(wc, ["time", getwords(wc, 9)])[1] |
131 |
| - w = getweights(wc, getwords(wc, [1,2])) |
132 |
| - setwords!(wc, [1,2], ["zz","yy"]) |
133 |
| - @test getweights(wc, "zz") == w[1] |
134 |
| - configsvgimages!(wc, wrappers=["a"=>("href"=>"https://www.google.com/search?q=$w") for w in getwords(wc)]) |
135 |
| - configsvgimages!(wc, ["zz","yy"], children=("animate"=>["attributeName" => "opacity", "to"=>"0.5", "dur"=>"6s"], "title"=>"tooltip")) |
136 |
| - svgsource = string(paintsvg(wc)) |
137 |
| - @test count("www.google.com", svgsource) == length(wc) |
138 |
| - @test count("attributeName", svgsource) == 2 |
139 |
| - setimages!(wc, [1,2], wc.imgs[[4,5]]) |
140 |
| - setimages!(wc, 1, wc.imgs[[4,5]]) |
141 |
| - setimages!(wc, 1, wc.imgs[4]) |
142 |
| - setsvgimages!(wc, 1, wc.svgs[6]) |
143 |
| - setsvgimages!(wc, 6, wc.svgs[6]) # the results of setsvgimages! and initialize! may not be identical |
144 |
| - @test wc.imgs[1] == wc.imgs[6] |
145 |
| - |
146 |
| - for s = [:main, :reset, :average, :clipping, :blending, :reset] |
147 |
| - recolor!(wc, style=s) |
148 |
| - end |
149 |
| - |
150 |
| - # strategy |
151 |
| - imgs = wc.imgs |
152 |
| - @test all(WordCloud.dilatedoccupancy.(imgs, maximum.(size.(imgs))) .== WordCloud.boxoccupancy.(imgs)) |
153 |
| - @test WordCloud.occupancy(imgs) == WordCloud.dilatedoccupancy(imgs, 0) |
154 |
| - |
155 |
| - # utils |
156 |
| - wc.qtrees[1][1] |> imageof |
157 |
| - bg = getmask(wc) |
158 |
| - istrans = c -> maximum(c[1:3]) < 128 |
159 |
| - mask = WordCloud.imagemask(bg, istrans) |
160 |
| - s = showmask(bg, mask) |
161 |
| - @test all(bg[mask] .== s[mask]) |
162 |
| - @test all(bg[.!mask] .!= s[.!mask]) |
163 |
| - |
164 |
| - # language |
165 |
| - l1 = length(wordcloud("It's easy to generate word clouds")) |
166 |
| - for lang in ["en", "eng", "English"] |
167 |
| - l2 = length(wordcloud("It's easy to generate word clouds", language=lang)) |
168 |
| - @test l1 == l2 |
| 33 | + make_drawings(0) # remove this line, and the test will success |
| 34 | + Threads.@threads :static for i = 1:10 # change 10 to 8, and the test will success |
| 35 | + make_drawings(i) |
169 | 36 | end
|
170 | 37 |
|
171 |
| - # overall |
172 |
| - wordcloud(["中文", "需要", "提前", "分词"], fonts="") |> generate! |
173 |
| - wordcloud(["the"=>1.0, "to"=>0.51, "and"=>0.50, |
174 |
| - "of"=>0.47, "a"=>0.44, "in"=>0.33]) |> generate! |
175 |
| - wordcloud("It's easy to generate word clouds", maxnum=10) |> generate! |
176 |
| - |
177 |
| - # font |
178 |
| - WordCloud.setfontcandidates!("zh", ["CJK"]) |
179 |
| - @test WordCloud.getfontcandidates("zh") == WordCloud.getfontcandidates("zho") == ["CJK"] |
180 | 38 |
|
181 |
| - @show keys(WordCloud.FONT_NAMES) |
182 | 39 | end
|
0 commit comments