11using WordCloud
22
33stwords = [" us" , " will" ];
4+
45println (" ==Obama's==" )
56cs = WordCloud. randomscheme ()
67as = WordCloud. randomangles ()
@@ -10,42 +11,42 @@ wca = wordcloud(
1011 colors = cs,
1112 angles = as,
1213 fillingrate = fr) |> generate!
14+
1315println (" ==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
2716wcb = 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))
3931end
32+ # Follow these steps to generate result: initword! -> placement! -> generate!
33+ initword! (wcb)
34+
4035println (" =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
4342end
43+
4444println (" =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
4848end
49+
4950if getstate (wcb) != :generate!
5051 println (" =overall tuning=" )
5152 generate! (wcb, 1000 , patient= - 1 , retry= 2 ) # allow rescale but don‘t allow teleport
5455ma = paint (wca)
5556mb = paint (wcb)
5657h,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 ))
5959try mkdir (" address_compare" ) catch end
60-
60+ println ( " save results to address_compare " )
6161WordCloud. ImageMagick. save (" address_compare/compare.png" , [ma space mb])
6262
6363gif = WordCloud. GIF (" address_compare" )
0 commit comments