Debian Codegraph @

texmf-dist/tex/latex/wordcloud/wordcloud.lua

raw
1 --[[
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% wordcloud %%
4 %% drawing wordclouds %%
5 %% with METAPOST and Lua %%
6 %% [email protected] %%
7 %% Version 0.3 (vovembre 2025) %%
8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9 %
10 % This work may be distributed and/or modified under the conditions of
11 % the LaTeX Project Public License, either version 1.3c of this license
12 % or (at your option) any later version. The latest version of this
13 % license is in http://www.latex-project.org/lppl.txt
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16 ]]
17 function wc_file2string(file)
18 local f = assert(io.open(file, "rb"))
19 local content = f:read("*all")
20 f:close()
21 return content
22 end
23
24 function wc_table_concat(t1,t2)
25 for i=1,#t2 do
26 t1[#t1+1] = t2[i]
27 end
28 return t1
29 end
30
31 function wc_ignor_fr()
32 local output;
33 output= {"et", "donc", "alors", "les", "ainsi", "des", "que", "qui", "sont", "par", "dans","est","pour","une","peut","avec","sans","Les","son","sur","ces","ses","tandis","quand"}
34 return output
35 end
36
37 function wc_ignor_en()
38 local output;
39 output= {"and", "so", "the", "the", "hence", "some", "that", "are", "is", "have", "has","in","on","could","should","with","be","this","these","where","when","not","with","can",}
40 return output
41 end
42
43 -- global variable for ignored words
44 wordcloud_ignor_words = {};
45 wc_table_concat(wordcloud_ignor_words,wc_ignor_fr())
46 wc_table_concat(wordcloud_ignor_words,wc_ignor_en())
47
48 -- global variable for alt text (PDF tagging)
49 wc_alt_text = "Wordcloud image composed of the following list of words and associated weights to scale them: "
50
51 -- function to add list of words to be ignored
52 function wc_add_ignored_words(tab)
53 wc_table_concat(wordcloud_ignor_words,tab)
54 end
55
56 -- function to build a tab from a string of the form
57 -- word1,word2,word3,etc.
58 function wc_string_to_tab(strWord)
59 strWord=string.gsub(strWord, "%s+", "") -- space delete
60 local tab
61 tab = string.explode(strWord, ",")
62 return tab
63 end
64
65 function wc_build_word_table(mystr)
66 local loc_str = mystr;
67 local ignor_chars = {";","’","'","\"","{","}","[","]","(",")","…",".","?","!","$","\\","#","<",">","«","»","+","*","-","/","=","%%", "€",":","~"}
68 loc_str = loc_str:gsub('[%p%c]+',' ')
69 for i=1,#ignor_chars do
70 loc_str = loc_str:gsub('['..ignor_chars[i]..']+',' ')
71 end
72 for i=1,#wordcloud_ignor_words do
73 loc_str = loc_str:gsub(' '..wordcloud_ignor_words[i]..' ',' ')
74 end
75 loc_str = loc_str:gsub(' LaTeX ',' \\LaTeX ')
76 loc_str = loc_str:gsub(' TeX ',' \\TeX ')
77 local t = {}
78 for i in loc_str:gmatch("%S+") do
79 t[#t + 1] = i
80 end
81 -- delete 2 size words
82 local id_two={}
83 for i=1,#t do
84 if(string.len(t[i])<=2) then
85 table.insert(id_two,i)
86 end
87 end
88 for i=#id_two,1,-1 do
89 table.remove(t,id_two[i])
90 end
91 return t
92 end
93
94 function wc_build_table_weight(t)
95 local weight = {}
96 for _, v in ipairs(t) do
97 weight[v] = (weight[v] or 0) + 1
98 end
99 return weight
100 end
101
102 function wc_table_to_tabular(table_weight)
103 local total_occ = 0
104 local tabular_weight = {}
105 for k,v in pairs(table_weight) do
106 total_occ=total_occ+v
107 end
108 local i=0
109 for k,v in pairs(table_weight) do
110 i=i+1
111 tabular_weight[i] ={}
112 tabular_weight[i][1]=k
113 tabular_weight[i][2]=v/total_occ
114 end
115 table.sort(tabular_weight,function (k1, k2) return k1[2] > k2[2] end)
116 return tabular_weight
117 end
118
119 function wc_build_mp_code(table_weight,maximum,rotation)
120 -- optional arguments
121 maximum = maximum or 50
122 rotation = rotation or 0
123
124 local total_occ = 0
125 local tabular_weight = wc_table_to_tabular(table_weight)
126
127
128 local str_mp=[[
129 string words[];
130 numeric weights[];
131 ]]
132 local i=0
133 for i=1, #tabular_weight do
134 str_mp=str_mp.."words["..i.."]:=\""..tabular_weight[i][1].."\";"
135 str_mp=str_mp.."weights["..i.."]:="..tabular_weight[i][2]..";"
136 if (i>=maximum) then
137 break
138 end
139 end
140 str_mp=str_mp.."draw_wordcloud(words,weights,"..rotation..","..math.min(maximum,#tabular_weight)..");"
141 return str_mp
142 end
143
144 function wc_list_to_table(list)
145 -- list of words and weights (word1,weight1);(word2,weight2); etc.
146 local table_weight = {}
147 local pair = string.explode(list, ";")
148 local lgth=#pair
149 for i=1,lgth do
150 word,weight=string.match(pair[i],"%((.+),(.+)%)")
151 table_weight[word]=weight
152 end
153 return table_weight
154 end
155
156 function wc_size_of_table(table)
157 local lengthNum = 0
158
159 for k, v in pairs(table) do -- for every key in the table with a corresponding non-nil value
160 lengthNum = lengthNum + 1
161 end
162 return lengthNum
163 end
164
165
166 function wc_build_color_list(colors)
167 -- list of LaTeX colors separated with colons
168 local out = ""
169 local pair = string.explode(colors:sub(1,-2), ";") -- sub(1,-2) to delete last ;
170 local lgth=#pair
171 for i=1,lgth do
172 out=out.."wordcloud_colors["..i.."]:="..pair[i]..";";
173 end
174 out=out.."wordcloud_colors_number:="..lgth..";"
175 return out
176 end
177
178 -- build mp code for the wordcloud of a list given in LaTeX command
179 function wc_build_wordcloud(str,rotation,scale,margin,usecolor,colors)
180 maximum = maximum or 50
181 local table = wc_list_to_table(str)
182 local lgth_table = wc_size_of_table(table)
183 local output
184 output= "\\begin{mplibcode}[wordcloud,alt={"..wc_alt_text..str:gsub("\\","").."}]"
185 output = output..[[input wordcloud
186 beginfig(0);
187 ]]
188 if(usecolor=="true") then
189 output = output.."wordcloud_use_color(true);"
190 if(colors~="") then
191 output=output..wc_build_color_list(colors)
192 end
193 end
194 output = output.."set_wordcloud_scale("..scale..");"
195 output = output.."set_box_margin("..margin..");"
196 output = output..wc_build_mp_code(table,lgth_table,rotation)
197 output = output.."endfig;\\end{mplibcode}"
198 tex.sprint(output)
199 end
200
201 -- build list of (word,weight) for tagging a wordcloud file
202 function wc_build_list_tag(table_weight,number)
203 local tabular_weight = wc_table_to_tabular(table_weight)
204 local output = ""
205 local i=0
206 for i=1, number do
207 output = output.."("..tabular_weight[i][1]:gsub("\\","")..","..string.format("%.2f",tabular_weight[i][2])..");"
208 end
209 return output
210 end
211
212 -- build mp code for the wordcloud of a file given in LaTeX command
213 function wc_build_wordcloud_file(file,number,rotation,scale,margin,usecolor,colors)
214 local str = wc_file2string(file)
215
216 local words = wc_build_word_table(str)
217
218 local table_weight = wc_build_table_weight(words)
219 local output
220
221 output= "\\begin{mplibcode}[wordcloud,alt={"..wc_alt_text..wc_build_list_tag(table_weight,number).."}]"
222 output = output..[[input wordcloud
223 beginfig(0);
224 ]]
225 if(usecolor=="true") then
226 output = output.."wordcloud_use_color(true);"
227 if(colors~="") then
228 output=output..wc_build_color_list(colors)
229 end
230 end
231 output = output.."set_wordcloud_scale("..scale..");"
232 output = output.."set_box_margin("..margin..");"
233 output = output..wc_build_mp_code(table_weight,number,rotation)
234 output = output.."endfig;\\end{mplibcode}"
235 tex.sprint(output)
236 end