1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
if not modules then modules = { } end modules ['mtx-patterns'] = {
version = 1.001,
comment = "companion to mtxrun.lua",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local find, gsub, match = string.find, string.gsub, string.match
local concat = table.concat
local P, R, S, C, Ct, Cmt, Cc, Cs = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cc, lpeg.Cs
local patterns = lpeg.patterns
local lpegmatch = lpeg.match
local helpinfo = [[
<?xml version="1.0"?>
<application>
<metadata>
<entry name="name">mtx-spell</entry>
<entry name="detail">ConTeXt Word Filtering</entry>
<entry name="version">0.10</entry>
</metadata>
<flags>
<category name="basic">
<subcategory>
<flag name="expand"><short>expand hunspell dics and aff files</short></flag>
<flag name="dictionary"><short>word file (.dics)</short></flag>
<flag name="specification"><short>affix specification file (.aff)</short></flag>
<flag name="result"><short>destination file</short></flag>
</subcategory>
</category>
</flags>
<examples>
<category>
<title>Examples</title>
<subcategory>
<example><command>mtxrun --script spell --expand --dictionary="en_US.dic" --specification="en_US.txt" --result="data-us.txt"</command></example>
</subcategory>
</category>
</examples>
</application>
]]
local application = logs.application {
name = "mtx-spell",
banner = "ConTeXt Word Filtering 0.10",
helpinfo = helpinfo,
}
local report = application.report
local trace = false
scripts = scripts or { }
scripts.spell = scripts.spell or { }
---------------
require("char-def")
require("char-utf")
-- nl: ij => ij
do
local prefixes, suffixes, affixes, continue, collected
local function resetall()
prefixes = table.setmetatableindex("table")
suffixes = table.setmetatableindex("table")
affixes = table.setmetatableindex("table")
continue = { }
collected = { }
end
local uppers = { }
local chardata = characters.data
for k, v in next, chardata do
if v.category == "lu" then
uppers[utf.char(k)] = true
end
end
local newline = patterns.newline
local digit = patterns.digit
local skipped = digit + lpeg.utfchartabletopattern(uppers)
local ignored = 1 - newline
local garbage = S("'-")
local function fixeddata(data)
data = gsub(data,"ij","ij")
return data
end
local function registersuffix(tag,f)
table.insert(suffixes[tag],f)
table.insert(affixes [tag],f)
end
local function registerprefix(tag,f)
table.insert(prefixes[tag],f)
table.insert(affixes [tag],f)
end
local function getfixes(specification)
local data = fixeddata(io.loaddata(specification) or "")
local lines = string.splitlines(data)
-- /* in two
-- Y/N continuation
-- [^...] [...] ...
local p0 = nil
local p1 = P("[^") * Cs((1-P("]"))^1) * P("]") / function(s)
local t = utf.split(s)
local p = 1 - lpeg.utfchartabletopattern(t)
p0 = p0 and (p0 * p) or p
end
local p2 = P("[") * Cs((1-P("]"))^1) * P("]") / function(s)
local t = utf.split(s)
local p = lpeg.utfchartabletopattern(t)
p0 = p0 and (p0 * p) or p
end
local p3 = (patterns.utf8char - S("[]"))^1 / function(s)
local p = P(s)
p0 = p0 and (p0 * p) or p
end
local p = (p1 + p2 + p3)^1
local function makepattern(s)
p0 = nil
lpegmatch(p,s)
return p0
end
local i = 1
while i <= #lines do
local line = lines[i]
local tag, continuation, n = match(line,"PFX%s+(%S+)%s+(%S+)%s+(%d+)")
if tag then
n = tonumber(n) or 0
continue[tag] = continuation == "Y"
for j=1,n do
i = i + 1
line = lines[i]
if not find(line,"[-']") then
local tag, one, two, three = match(line,"PFX%s+(%S+)%s+(%S+)%s+([^%s/]+)%S*%s+(%S+)")
if tag then
if one == "0" and two and three == "." then
-- simple case: PFX A 0 re .
registerprefix(tag,function(str)
local new = two .. str
if trace then
print("p 1",str,new)
end
return new
end)
elseif one == "0" and two and three then
-- strip begin
if trace then
print('2',line)
end
elseif one and two and three then
if trace then
print('3',line)
end
else
if trace then
print('4',line)
end
end
end
end
end
end
local tag, continuation, n = match(line,"SFX%s+(%S+)%s+(%S+)%s+(%S+)")
if tag then
n = tonumber(n) or 0
continue[tag] = continuation == "Y"
for j=1,n do
i = i + 1
line = lines[i]
if not find(line,"[-']") then
local tag, one, two, three = match(line,"SFX%s+(%S+)%s+(%S+)%s+([^%s/]+)%S*%s+(%S+)")
if tag then
if one == "0" and two and three == "." then
-- SFX Y 0 ly .
registersuffix(tag,function(str)
local new = str .. two
if trace then
print("s 1",str,new)
end
return new
end)
elseif one == "0" and two and three then
-- SFX G 0 ing [^e]
local final = makepattern(three) * P(-1)
local check = (1 - final)^0 * final
registersuffix(tag,function(str)
if lpegmatch(check,str) then
local new = str .. two
if trace then
print("s 2",str,new)
end
return new
end
end)
elseif one and two and three then
-- SFX G match$ suffix old$ (dutch has sloppy matches, use english as reference)
local final = makepattern(three) * P(-1)
local check = (1 - final)^1 * final
local final = makepattern(one) * P(-1)
local replace = Cs((1 - final)^1 * (final/two))
registersuffix(tag,function(str)
if lpegmatch(check,str) then
local new = lpegmatch(replace,str)
if new then
if trace then
print("s 3",str,new)
end
return new
end
end
end)
else
if trace then
print('4',line)
end
end
end
end
end
end
i = i + 1
end
end
local function expand(_,_,word,spec)
if spec then
local w = { word }
local n = 1
for i=1,#spec do
local s = spec[i]
local affix = affixes[s]
if affix then
for i=1,#affix do
local ai = affix[i]
local wi = ai(word)
if wi then
n = n + 1
w[n] = wi
if not continue[s] then
break
end
end
end
end
end
for i=1,n do
collected[w[i]] = true
end
elseif not find(word,"/") then
collected[word] = true
end
return true
end
local function getwords(dictionary)
local data = fixeddata(io.loaddata(dictionary) or "")
local keys = { }
for k, v in next, prefixes do
keys[k] = true
end
for k, v in next, suffixes do
keys[k] = true
end
local validkeys = lpeg.utfchartabletopattern(keys)
local specifier = P("/") * Ct(C(validkeys)^1)^0 * newline
local pattern = (
newline^1
+ skipped * (1-newline)^0
+ Cmt(C((1-specifier-newline-garbage)^1) * specifier^0, expand)
+ ignored^1 * newline^1
)^0
lpegmatch(pattern,data)
collected = table.keys(collected)
table.sort(collected)
return collected
end
local function saveall(result)
if result then
io.savedata(result,concat(collected,"\n"))
end
end
function scripts.spell.expand(arguments)
if arguments then
local dictionary = environment.arguments.dictionary
local specification = environment.arguments.specification
local result = environment.arguments.result
if type(dictionary) ~= "string" or dictionary == "" then
report("missing --dictionary=name")
elseif type(specification) ~= "string" or specification == "" then
report("missing --specification=name")
elseif type(result) ~= "string" or result == "" then
resetall()
getfixes(specification)
getwords(dictionary)
saveall(result)
return collected
end
end
end
end
-- spell.dicaff {
-- dictionary = "e:/context/spell/lo/en_US.dic.txt",
-- specification = "e:/context/spell/lo/en_US.aff.txt",
-- result = "e:/context/spell/lo/data-en.txt",
-- }
-- spell.dicaff {
-- dictionary = "e:/context/spell/lo/en_GB.dic.txt",
-- specification = "e:/context/spell/lo/en_GB.aff.txt",
-- result = "e:/context/spell/lo/data-uk.txt",
-- }
-- spell.dicaff {
-- dictionary = "e:/context/spell/lo/nl_NL.dic.txt",
-- specification = "e:/context/spell/lo/nl_NL.aff.txt",
-- result = "e:/context/spell/lo/data-nl.txt",
-- }
if environment.argument("expand") then
scripts.spell.expand(environment.arguments)
elseif environment.argument("exporthelp") then
application.export(environment.argument("exporthelp"),environment.files[1])
else
application.help()
end
|