180
181
182
183
184
185
186
187
188
189
190
191
192
193
...
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
...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
...
539
540
541
542
543
544
545
546
547
548
549
550
551
552
...
605
606
607
608
609
610
611
612
613
614
615
616
617
618
...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
...
799
800
801
802
803
804
805
806
807
808
809
810
811
812
|
local qpack = function(f) return {
encode = packer(f);
decode = unpacker(f);
} end
local parse, marshal
fmt.string = qpack "s4"
fmt.u8 = qpack "I1"
fmt.u16 = qpack "I2"
fmt.u24 = qpack "I3"
fmt.u32 = qpack "I4"
fmt.list = function(t,ty) ty = ty or fmt.u32
return {
encode = function(a)
................................................................................
return m
end;
}
end
fmt.form = {
{'form', fmt.u16};
{'text', fmt.string};
}
fmt.note = {
{'kind', fmt.string};
{'paras', fmt.list(fmt.string)};
}
fmt.meaning = {
{'lit', fmt.string};
{'notes', fmt.list(fmt.note,fmt.u8)};
}
fmt.def = {
{'part', fmt.u8};
{'branch', fmt.list(fmt.string,fmt.u8)};
{'means', fmt.list(fmt.meaning,fmt.u8)};
{'forms', fmt.list(fmt.form,fmt.u16)};
}
fmt.word = {
{'defs', fmt.list(fmt.def,fmt.u8)};
}
fmt.dictHeader = {
{'lang', fmt.string};
{'meta', fmt.string};
{'partsOfSpeech', fmt.list(fmt.string,fmt.u16)};
}
fmt.dict = {
{'header', fmt.dictHeader};
{'words', fmt.map(fmt.string,fmt.word)};
}
function marshal(ty, val)
if ty.encode then
return ty.encode(val)
end
local ac = {}
................................................................................
def.part = atomizePoS(def.part)
end
end
d.header.partsOfSpeech = {}
for v,i in pairs(posMap) do
d.header.partsOfSpeech[i] = v
end
return marshal(fmt.dict, d)
end
local function
readDict(file)
local d = parse(fmt.dict, stream(file))
-- handle atoms
for lit,w in pairs(d.words) do
for j,def in ipairs(w.defs) do
def.part = d.header.partsOfSpeech[def.part]
end
end
return d
................................................................................
else
id10t('[lit %s %s] is not a valid filter, “%s” should be either “pfx” or “sfx”',val,op,op)
end
end;
};
form = {
help = 'match against word\'s inflected forms';
syntax = '(<inflect> | <form> (set | is <inflect> | pfx <prefix> | sfx <suffix>))';
fn = function(e, k, op, v)
end;
};
part = {
help = 'word has definitions for every <part> of speech';
syntax = '<part>…';
fn = function(e,...)
................................................................................
local matches = 0
for j,r in ipairs(d.branch) do
if map[r] then matches = matches + 1 end
end
if matches == tgt then return true end
end
end
};
}
end
local function
safeopen(file,...)
if type(file) == 'string' then
................................................................................
header = {
lang = lang;
meta = "";
partsOfSpeech = {};
branch = {};
};
words = {};
}
local o = writeDict(new);
fd:write(o)
fd:close()
end;
};
coin = {
................................................................................
help = "add a meaning to a definition";
syntax = "<word> <def#> <meaning>";
write = true;
exec = function(ctx,word,dn,m)
local _,d = safeNavWord(ctx,word,dn)
table.insert(d.means, {lit=m,notes={}})
end;
};
mod = {
help = "move, merge, split, or delete words or definitions";
syntax = {
"<path> (drop | [move|merge|clobber] <path> | out [<part> [<root>…]])";
"path ::= <word>[(@<def#>[/<meaning#>[:<note#>]]|.)]";
};
................................................................................
syntax = "[<command>]";
};
predicates = {
help = "show available filter predicates";
nofile = true;
syntax = "[<predicate>]";
};
dump = {
exec = function(ctx) print(dump(ctx.dict)) end
};
ls = {
help = "list all words that meet any given <filter>";
syntax = {"[<filter>…]";
"filter ::= (<word>|<pred> <arg>…)";
................................................................................
for j, def in ipairs(w.word.defs) do
d=d .. '\n' .. meanings(def,true,j)
end
end
io.stdout:write(d..'\n')
end
end
function cmds.mod.exec(ctx, orig, oper, dest, ...)
if (not orig) or not oper then
id10t '`mod` requires at least an origin and an operation'
end
local op, dp = parsePath(orig)
local w,d,m,n = safeNavWord(ctx, op.w,op.dn,op.mn,op.nn)
|
>
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
...
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
...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
...
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
...
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
...
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
|
local qpack = function(f) return {
encode = packer(f);
decode = unpacker(f);
} end
local parse, marshal
fmt.string = qpack "s4"
fmt.label = qpack "s2"
fmt.tag = qpack "s1"
fmt.u8 = qpack "I1"
fmt.u16 = qpack "I2"
fmt.u24 = qpack "I3"
fmt.u32 = qpack "I4"
fmt.list = function(t,ty) ty = ty or fmt.u32
return {
encode = function(a)
................................................................................
return m
end;
}
end
fmt.form = {
{'form', fmt.u16};
{'text', fmt.label};
}
fmt.note = {
{'kind', fmt.tag};
{'paras', fmt.list(fmt.string)};
}
fmt.meaning = {
{'lit', fmt.string};
{'notes', fmt.list(fmt.note,fmt.u8)};
}
fmt.def = {
{'part', fmt.u8};
{'branch', fmt.list(fmt.label,fmt.u8)};
{'means', fmt.list(fmt.meaning,fmt.u8)};
{'forms', fmt.list(fmt.form,fmt.u16)};
}
fmt.word = {
{'defs', fmt.list(fmt.def,fmt.u8)};
}
fmt.dictHeader = {
{'lang', fmt.tag};
{'meta', fmt.string};
{'partsOfSpeech', fmt.list(fmt.tag,fmt.u16)};
}
fmt.synonymSet = {
{'uid', fmt.u32};
-- IDs are persistent random values so they can be used
-- as reliable identifiers even when merging exports in
-- a parvan-unaware VCS
{'members', fmt.list({
{'word', fmt.label}, {'def', fmt.u8};
},fmt.u16)};
}
fmt.dict = {
{'header', fmt.dictHeader};
{'words', fmt.map(fmt.string,fmt.word)};
{'synonyms', fmt.list(fmt.synonymSet)};
}
function marshal(ty, val)
if ty.encode then
return ty.encode(val)
end
local ac = {}
................................................................................
def.part = atomizePoS(def.part)
end
end
d.header.partsOfSpeech = {}
for v,i in pairs(posMap) do
d.header.partsOfSpeech[i] = v
end
return 'PV0\2'..marshal(fmt.dict, d)
end
local function
readDict(file)
local s = stream(file)
local magic = s:next 'c4'
if magic ~= 'PV0\2' then
id10t 'not a parvan file'
end
local d = parse(fmt.dict, s)
-- handle atoms
for lit,w in pairs(d.words) do
for j,def in ipairs(w.defs) do
def.part = d.header.partsOfSpeech[def.part]
end
end
return d
................................................................................
else
id10t('[lit %s %s] is not a valid filter, “%s” should be either “pfx” or “sfx”',val,op,op)
end
end;
};
form = {
help = 'match against word\'s inflected forms';
syntax = '(<inflect> | <form> (set | is <inflect> | (pfx|sfx|match) <affix>))';
fn = function(e, k, op, v)
end;
};
part = {
help = 'word has definitions for every <part> of speech';
syntax = '<part>…';
fn = function(e,...)
................................................................................
local matches = 0
for j,r in ipairs(d.branch) do
if map[r] then matches = matches + 1 end
end
if matches == tgt then return true end
end
end
};
note = {
help = 'word has a matching note';
syntax = '([kind <kind> [<term>]] | term <term> | (min|max|count) <n>)';
fn = function(e, op, k, t)
if op == 'kind' or op == 'term' then
if op == 'term' and t then
id10t('too many arguments for [note term <term>]')
end
for _,d in ipairs(e.word.defs) do
for _,m in ipairs(d.means) do
for _,n in ipairs(m.notes) do
if op=='term' or n.kind == k then
if op=='kind' and t == nil then return true end
if string.find(table.concat(n.paras,'\n'), t or k, 1, true) ~= nil then return true end
end
end end end
elseif op == 'min' or op == 'max' or op == 'count' then
if t then
id10t('too many arguments for [note %s <n>]',op)
end
local n = math.floor(tonumber(k))
local total = 0
for i,d in ipairs(e.word.defs) do
for j,m in ipairs(d.means) do
total = total + #m.notes
if op == 'min' and total >= n then return true end
if op == 'max' and total > n then return false end
end end
if op == 'count' then return total == n end
if op == 'max' then return total <= n end
return false
end
end;
};
}
end
local function
safeopen(file,...)
if type(file) == 'string' then
................................................................................
header = {
lang = lang;
meta = "";
partsOfSpeech = {};
branch = {};
};
words = {};
synonyms = {};
}
local o = writeDict(new);
fd:write(o)
fd:close()
end;
};
coin = {
................................................................................
help = "add a meaning to a definition";
syntax = "<word> <def#> <meaning>";
write = true;
exec = function(ctx,word,dn,m)
local _,d = safeNavWord(ctx,word,dn)
table.insert(d.means, {lit=m,notes={}})
end;
};
syn = {
help = "manage synonym groups";
syntax = {
"(show|purge) <path>";
"(link|drop) <word> <group#> <path>…";
"new <path> <path>…";
"clear <word> [<group#>]";
};
write = true;
exec = function(ctx, op, tgtw, ...)
local groups = {}
local wp = parsePath(tgtw)
local w,d = safeNavWord(ctx, wp.w, wp.dn)
if not (op=='new' or op=='link' or op=='drop' or op=='clear' or op=='show' or op=='purge') then
id10t('invalid operation “%s” for `syn`', op)
end
if op == 'new' then
local links = {{word = wp.w, def = wp.dn or 1}}
for i,l in ipairs{...} do
local parsed = parsePath(l)
links[i+1] = {word = parsed.w, def = parsed.dn or 1}
end
table.insert(ctx.dict.synonyms, {
uid=math.random(0,0xffffFFFF);
members=links;
})
else -- assemble a list of groups
for i,ss in ipairs(ctx.dict.synonyms) do
for j,s in ipairs(ss.members) do
if s.word == wp.w and (wp.dn == nil or s.def == wp.dn) then
table.insert(groups, {set = ss, mem = s})
break
end
end
end
if op == 'show' then
for i, g in ipairs(groups) do
local w,d = safeNavWord(ctx, g.mem.word, g.mem.def)
local function label(wd,defn)
local fulldef = {}
for i,v in ipairs(defn.means) do
fulldef[i] = v.lit
end
fulldef = table.concat(fulldef, '; ')
return string.format("%s(%s): %s",wd,defn.part,fulldef)
end
local others = {}
for j, o in ipairs(g.set.members) do
if not (o.word == g.mem.word and o.def == (wp.dn or 1)) then
local ow, od = safeNavWord(ctx, o.word,o.def)
table.insert(others, ' '..label(o.word,od))
end
end
io.stdout:write(string.format("% 4u) %s\n%s", i, label(g.mem.word,d),table.concat(others,'\n')))
end
elseif op == 'link' or op == 'drop' then
local tgtn, paths = (...), { select(2, ...) }
end
end
end;
};
mod = {
help = "move, merge, split, or delete words or definitions";
syntax = {
"<path> (drop | [move|merge|clobber] <path> | out [<part> [<root>…]])";
"path ::= <word>[(@<def#>[/<meaning#>[:<note#>]]|.)]";
};
................................................................................
syntax = "[<command>]";
};
predicates = {
help = "show available filter predicates";
nofile = true;
syntax = "[<predicate>]";
};
export = {
help = "create a text file dump compatible with source control";
};
dump = {
exec = function(ctx) print(dump(ctx.dict)) end
};
ls = {
help = "list all words that meet any given <filter>";
syntax = {"[<filter>…]";
"filter ::= (<word>|<pred> <arg>…)";
................................................................................
for j, def in ipairs(w.word.defs) do
d=d .. '\n' .. meanings(def,true,j)
end
end
io.stdout:write(d..'\n')
end
end
function cmds.export.exec(ctx)
local function san(str)
local d = 0
local r = {}
for i,cp in utf8.codes(str) do
-- insert backslashes for characters that would
-- disrupt strwords() parsing
if cp == 0x5b then
d = d + 1
elseif cp == 0x5d then
if d >= 1 then
d = d - 1
else
table.insert(r, 0x5c)
end
end
table.insert(r, cp)
end
return '[' .. utf8.char(table.unpack(r)) .. ']'
end
local function o(...) io.stdout:write(string.format(...)..'\n') end
local d = ctx.dict
o('pv0 %s %s', san(d.header.lang), san(d.header.meta))
for lit, w in pairs(d.words) do
o('w %s',san(lit))
for i,def in ipairs(w.defs) do
o('d %s',san(def.part))
for _,s in ipairs(d.synonyms) do
for _,sm in ipairs(s.members) do
if sm.word == w and sm.def == i then
o('ds %u',s.uid)
break
end
end
end
for j,r in ipairs(def.branch) do
o('dr %s',san(r))
end
for j,m in ipairs(def.means) do
o('m %s', san(m.lit))
for k,n in ipairs(m.notes) do
o('n %s', san(n.kind))
for a,p in ipairs(n.paras) do
o('np %s', san(p))
end
end
end
end
end
for _,s in ipairs(d.synonyms) do o('s %u', s.uid) end
end
function cmds.mod.exec(ctx, orig, oper, dest, ...)
if (not orig) or not oper then
id10t '`mod` requires at least an origin and an operation'
end
local op, dp = parsePath(orig)
local w,d,m,n = safeNavWord(ctx, op.w,op.dn,op.mn,op.nn)
|