-
Notifications
You must be signed in to change notification settings - Fork 0
/
tri.bra
350 lines (323 loc) · 9.55 KB
/
tri.bra
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
347
348
349
350
{tri.bra
𝕌𝕋𝔽-𝟠
Collect trigrams from lemmas
Usage
-----
* Edit the line specifying the input (input=yourinput). Remember to escape backslashes. You can use forward slashes instead.
* Start Bracmat
* after the prompt, type get$"tri.bra"
* Output is named <your input's name>".trigramFrequenciesSorted"
* CSTlemma can use the output to disambiguate between lemma candidates (-T option). This does not always give better results overall. It works well for Middle Low German.
Input
-----
First column: full forms (not used)
Second column: lemmas
(Optionally more columns, which will not be used)
Empty lines are allowed
Example:
bekluendern beklundern
beklundern beklundern
behalp behelpen
behelffen behelpen
behelpe behelpen
beholtnisse beholtnisse
beholtnysse beholtnisse
behodersche behodersche
}
X=
"The first declaration of input is what counts."
(input="toklemSort.tab.ph")
(input="C:\\projects\\affixtrain\\dagammel\\flexliste_ods.csv.ph")
( doit
=
. ( sort
= L M a b
. !arg:?L
& whl
' ( !L:% %
& :?M
& whl
' ( !L:%?a %?b ?L
& !a+!b !M:?M
)
& !L !M:?L
)
& mop$((=.!arg).!L.(=+))
)
& vap
$ ( (
=
. @(!arg:?tok \t ?lem)
& ( @(!lem:?L \t ?)
& !L:?lem
| @(!lem:?L \r)
& !L:?lem
|
)
& (!tok:!lem|)
|
)
. get$(!arg,STR)
. \n
)
: ?lemmas
& lst$(lemmas,Alemmas,NEW,WYD,BIN)
& ( tri
=
. :?trs
& ( " " vap$((=.!arg).!arg) " "
: ?
( %@ %@ %@:?TRI
& str$!TRI !trs:?trs
)
(?&~)
| !trs
)
)
& map$(tri.!lemmas):?tris
& lst$(tris,Atris,NEW,WYD,BIN)
& str
$ ( map
$ ( (
=
. !arg:(?w.?n)
& !w \t !n \n
)
. sort
$ ( map
$ ( (
=
. !arg:#?n*%@?w&(!w.!n)
)
. sort$!tris
)
)
)
)
: ?trigramFrequenciesSorted
& put
$ ( !trigramFrequenciesSorted
, str$(!(its.input) ".trigramFrequenciesSorted")
, NEW
, BIN
)
& sort
$ ( map
$ ( (
= .!arg:#?n*%@?w&(!n.!w)
)
. sort$!tris
)
)
: ?trigramFrequencies
& lst
$ (trigramFrequencies,trigramFrequencies,NEW,WYD,BIN)
& put
$ ( str
$ ( map
$ ( (
=
. !arg:(?F.?T)
& !T \t !F \n
)
. !trigramFrequencies
)
)
, str$(!(its.input) ".trigramFrequencies.tab")
, NEW
, BIN
)
& " trigramFrequencies: (freq.trigram)
freq: # occurrences of trigram in lemma types in all of corpus.
So each lemma type is surveyed only once.
trigram: Sequence of three UTF-8 characters. First trigram of lemma includes
a space preceding the lemma. The last trigram includes a space
following the lemma.
Excerpt from trigramFrequencies:
(16.\" Pr\")
(16.\" fe\")
(16.\" fi\")
(16.\" fu\")
(16.\" of\")
(16.Bon)
(16.Eng)
(16.Gar)
(16.Hey)
(16.Rey)
(16.Sar)
(16.Str)
(16.ado)
(16.afg)
(16.aha)
(16.arq)
(16.\"ay \")
(16.ayn)
"
& new$hash:?Tha
& map
$ ( (
=
. !arg:(?n.?t)
& (Tha..insert)$(!t.!n)
)
. !trigramFrequencies
)
& new
$ ( UFP
,
' ( (s.p) (s.len)
. divide$(log$!p,log$!len)*10000:?P
)
)
: ?weightedProb
& ( Weight
= p x len
. 1:?p
& map
$ ( (
=
. ( (Tha..find)$!arg:(?.?x)
| 0:?x
)
& !x*!p:?p
)
. tri$!arg:? [?len
)
& "Better is !p^((log$!len)^-1) or log$!p*(log$!len)^-1"
& (weightedProb..calculate)$(!p,!len)
& (weightedProb..export)$(N,P):?P
& (!len.!p.!arg)
)
& map$(Weight.!lemmas):?valued
& lst$(valued,valued,NEW,BIN,WYD)
& sort$!valued:?valuedsrt
& lst$(valuedsrt,valuedsrt,NEW,BIN,WYD)
& :?weights
& !valuedsrt:(?prv.?W.?) ?valuedsrt
& !W:?sum
& 1:?pro
& whl
' ( !valuedsrt:(?L.?W.?w) ?valuedsrt
& ( !prv:!L&!W !sum:?sum
| !sum:? [?NN
& div$(!NN,2):?MM
& !sum:? [!MM %@?we ?
& out$(!prv div$(!we.!pro))
& !we:?pro
& (!prv.!we) !weights:?weights
& !L:?prv
& !W:?sum
)
)
& !sum:? [?NN
& div$(!NN,2):?MM
& !sum:? [!MM %?we ?
& out$(div$(!we.!pro))
& (!prv.!we) !weights:?weights
& "
weights: First column is the length of a lemma, not counting preceding and trailing spaces.
Second column is the median of all products of all trigram frequencies of all lemmas with the length in the first column.
A lemma is prefixed and postfixed with white space characters before computing the product of its trigrams.
The idea is that an unknown lemma has a fifty-fifty chance of having a higher value of the product of all its trigrams than the lemma that is in the middle of the sorted list of all lemmas with the same length.
Example
-------
These are the lists of all lemmas with 19 or 20 characters that are found in the corpus. For each lemma the product of the frequencies of the trigrams are in the middle column.
The rows are sorted. The medians are the frequencies for 'borgermessterszchen' (position 2 out of 4, rounding down) and 'achteundetwintigeste'.
Shorter lemma lengths are much more frequent and the median value is much closer to neighbouring values.
Input for computing weights:
(19.52542451667883479662522411980901632000000.Nuchterndantzessche)
(19.1487957110758891484571058530928384376512000.borgermessterszchen)
(19.1472812739740914286058054210496699039744000000.schlantanterlantant)
(19.32829108139444624057908656123547838936320000000.mannichvoldichliken)
(20.30961471807240324922966821824312115200000.hannepspinnerambacht)
(20.171097420672132576940263987416693429491466240.achteundetwintigeste)
(20.15872442651235660217416112340615363116804352000000.beckenwerchtenstrate)
weights, based on the data above:
(20.171097420672132576940263987416693429491466240)
(19.1487957110758891484571058530928384376512000)
"
& lst$(weights,weights,NEW,BIN,WYD)
& put
$ ( str
$ ( :?Lst
& whl
' ( !weights:(?L.?W) ?weights
& flt$(!W,5) \n !Lst:?Lst
)
& !Lst
)
, str$(!(its.input) ".weights.tab")
, NEW
, BIN
)
& whl
' ( out$woord?
& get':?wrd
& !wrd:~
& out$(tri$!wrd)
& sort
$ ( map
$ ( (
=
. Weight$!arg:(?ln.?p.?)
& !weights:? (!ln.?W) ?
& (!p*!W^-1.!arg)
)
. !wrd
)
)
: ?bestlast
& map
$ ( (
=
. !arg:(?V.?L)
& out$(flt$(!V,3) !L)
)
. !bestlast
)
)
& out$KLAAR
)
(new=.~|(its.doit)$!(its.input));
r=
get'("tri.bra",TXT)
& rmv$(str$(tri ".bak"))
& ren$("tri.bra".str$(tri ".bak"))
& put
$ ( "{tri.bra
𝕌𝕋𝔽-𝟠
Collect trigrams from lemmas
Usage
-----
* Edit the line specifying the input (input=yourinput). Remember to escape backslashes. You can use forward slashes instead.
* Start Bracmat
* after the prompt, type get$\"tri.bra\"
* Output is named <your input's name>\".trigramFrequenciesSorted\"
* CSTlemma can use the output to disambiguate between lemma candidates (-T option). This does not always give better results overall. It works well for Middle Low German.
Input
-----
First column: full forms (not used)
Second column: lemmas
(Optionally more columns, which will not be used)
Empty lines are allowed
Example:
bekluendern beklundern
beklundern beklundern
behalp behelpen
behelffen behelpen
behelpe behelpen
beholtnisse beholtnisse
beholtnysse beholtnisse
behodersche behodersche
}
"
, "tri.bra"
, NEW
, BIN
)
& lst'(X,"tri.bra",APP)
& put'(\n,"tri.bra",APP,BIN)
& lst'(r,"tri.bra",APP)
& put$(str$("\nnew'" X ";\n"),"tri.bra",APP,BIN)
& done;
new'X;