@@ -8,7 +8,7 @@ function initqtree!(wc, i::Integer; backgroundcolor=(0,0,0,0), border=wc.params[
8
8
end
9
9
initqtree! (wc, i; kargs... ) = initqtree! .(wc, index (wc, i); kargs... )
10
10
" Initialize word's images and other resources with specified style"
11
- function initimage ! (wc, i:: Integer ; backgroundcolor= (0 ,0 ,0 ,0 ), border= wc. params[:border ],
11
+ function initimages ! (wc, i:: Integer ; backgroundcolor= (0 ,0 ,0 ,0 ), border= wc. params[:border ],
12
12
fontsize= getfontsizes (wc, i), color= wc. params[:colors ][i],
13
13
angle = wc. params[:angles ][i], font= getfonts (wc, i))
14
14
img, svg = prepareword (wc. words[i], fontsize, color, angle,
@@ -18,8 +18,8 @@ function initimage!(wc, i::Integer; backgroundcolor=(0,0,0,0), border=wc.params[
18
18
initqtree! (wc, i, backgroundcolor= backgroundcolor, border= border)
19
19
nothing
20
20
end
21
- initimage ! (wc, i; kargs... ) = initimage! .(wc, index (wc, i); kargs... )
22
- function initimage ! (wc:: WC ; maxiter= 5 , error= 0.02 )
21
+ initimages ! (wc, i; kargs... ) = initimage! .(wc, index (wc, i); kargs... )
22
+ function initimages ! (wc:: WC ; maxiter= 5 , error= 0.02 )
23
23
params = wc. params
24
24
mask = wc. mask
25
25
@@ -36,10 +36,10 @@ function initimage!(wc::WC; maxiter=5, error=0.02)
36
36
scale = find_weight_scale! (wc, density= params[:density ], maxiter= maxiter, error= error)
37
37
println (" density set to $(params[:density ]) , with scale=$scale , font minimum is $(getfontsizes (wc, length (wc. words))) " )
38
38
initimage! .(wc, 1 : length (words))
39
- params[ :state ] = nameof (initimages!)
39
+ setstate! (wc, nameof (initimages!) )
40
40
wc
41
41
end
42
- initimages ! = initimage !
42
+ initimage ! = initimages !
43
43
"""
44
44
* placement!(wc)
45
45
* placement!(wc, style=:uniform)
@@ -50,7 +50,7 @@ p=1 produces a rhombus, p=2 produces an ellipse, p>2 produces a rectangle with r
50
50
When you have set `style=:gathering`, you should disable teleporting in `generate!` at the same time(`generate!(wc, patient=-1)`).
51
51
"""
52
52
function placement! (wc:: WC ; style= :uniform , kargs... )
53
- if getstate (wc) == nameof (wordcloud)
53
+ if STATEIDS[ getstate (wc)] < STATEIDS[ :initimages! ]
54
54
initimages! (wc)
55
55
end
56
56
@assert style in [:uniform , :gathering ]
@@ -69,7 +69,7 @@ function placement!(wc::WC; style=:uniform, kargs...)
69
69
end
70
70
if ind === nothing error (" no room for placement" ) end
71
71
end
72
- wc . params[ :state ] = nameof (placement!)
72
+ setstate! (wc, nameof (placement!) )
73
73
wc
74
74
end
75
75
@@ -151,6 +151,30 @@ function recolor!(wc, args...; style=:average, kargs...)
151
151
end
152
152
nothing
153
153
end
154
+
155
+ function fit! (wc, args... ; krags... )
156
+ if STATEIDS[getstate (wc)] < STATEIDS[:placement! ]
157
+ placement! (wc)
158
+ end
159
+ qtrees = [wc. maskqtree, wc. qtrees... ]
160
+ ep, nc = train! (qtrees, args... ; krags... )
161
+ wc. params[:epoch ] += ep
162
+ if nc == 0
163
+ setstate! (wc, nameof (fit!))
164
+ end
165
+ wc
166
+ end
167
+ function printcollisions (wc)
168
+ qtrees = [wc. maskqtree, wc. qtrees... ]
169
+ colllist = first .(batchcollision (qtrees))
170
+ get_text (i) = i> 1 ? wc. words[i- 1 ] : " #MASK#"
171
+ collwords = [(get_text (i), get_text (j)) for (i,j) in colllist]
172
+ if length (colllist) > 0
173
+ println (" have $(length (colllist)) collisions." ,
174
+ " try setting a larger `nepoch` and `retry`, or lower `density` in `wordcloud` to fix that" )
175
+ println (" $collwords " )
176
+ end
177
+ end
154
178
"""
155
179
# Positional Args
156
180
* wc: the wordcloud to train
161
185
* trainer: appoint a training engine
162
186
"""
163
187
function generate! (wc:: WC , args... ; retry= 3 , krags... )
164
- if getstate (wc) != nameof ( placement!) && getstate (wc) != nameof (generate!)
188
+ if STATEIDS[ getstate (wc)] < STATEIDS[ : placement!]
165
189
placement! (wc)
166
190
end
167
- qtrees = [wc. maskqtree, wc. qtrees... ]
168
191
ep, nc = - 1 , - 1
169
192
for r in 1 : retry
170
193
if r != 1
@@ -175,28 +198,19 @@ function generate!(wc::WC, args...; retry=3, krags...)
175
198
else
176
199
println (" #$r . scale = $(wc. params[:scale ]) " )
177
200
end
178
- ep, nc = train! (qtrees, args... ; krags... )
179
- wc. params[:epoch ] += ep
180
- if nc == 0
201
+ fit! (wc, args... ; krags... )
202
+ if getstate (wc) == :fit!
181
203
break
182
204
end
183
205
end
184
- println ( " $ep epochs, $nc collisions " )
185
- if nc == 0
186
- wc . params[ :state ] = nameof (generate!)
206
+ if STATEIDS[ getstate (wc)] >= STATEIDS[ :fit! ]
207
+ println ( " $(wc . params[ :epoch ]) epochs " )
208
+ setstate! (wc, nameof (generate!) )
187
209
# @assert isempty(outofkernelbounds(wc.maskqtree, wc.qtrees))
188
210
# colllist = first.(batchcollision(qtrees))
189
211
# @assert length(colllist) == 0
190
212
else # check
191
- colllist = first .(batchcollision (qtrees))
192
- get_text (i) = i> 1 ? wc. words[i- 1 ] : " #MASK#"
193
- collwords = [(get_text (i), get_text (j)) for (i,j) in colllist]
194
- if length (colllist) > 0
195
- wc. params[:completed ] = false
196
- println (" have $(length (colllist)) collisions." ,
197
- " try setting a larger `nepoch` and `retry`, or lower `density` in `wordcloud` to fix that" )
198
- println (" $collwords " )
199
- end
213
+ printcollisions (wc)
200
214
end
201
215
wc
202
216
end
@@ -213,6 +227,10 @@ function generate_animation!(wc::WC, args...; outputdir="gifresult", overwrite=o
213
227
re
214
228
end
215
229
230
+ STATES = nameof .([wordcloud, initimages!, placement!, fit!, generate!])
231
+ STATEIDS = Dict ([s=> i for (i,s) in enumerate (STATES)])
232
+
233
+
216
234
"""
217
235
keep some words and ignore the others, then execute the function. It's the opposite of `ignore`.
218
236
* keep(fun, wc, ws::String) #keep a word
0 commit comments