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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
# Lexd Syntax
## Invocation
The `lexd` binary generates [AT&T format] transducers.
[AT&T Format]: https://wiki.apertium.org/wiki/ATT_format
Sample, save to `verb.lexd`:
```verb.lexd
PATTERNS
VerbRoot VerbInfl
LEXICON VerbRoot
sing
walk
dance
LEXICON VerbInfl
<v><pres>:
<v><pres><p3><sg>:s
```
Compile it (without flag diacritics) to ATT transducer format:
```
$ lexd verb.lexd > verb-generator.att
```
To compile to an `lttoolbox` transducer binary dictionary, use
`lt-comp`; this can be used for lookup with `lt-proc`:
```
$ lt-comp rl verb-generator.att verb-analyser.bin
main@standard 17 19
$ echo 'sings' | lt-proc verb-analyser.bin
^sings/sing<v><pres><p3><sg>$
```
To extract forms, use the [HFST] to first compile to `hfst` binary
format:
[HFST]: https://hfst.github.io/
```
$ hfst-txt2fst verb-generator.att -o verb-generator.hfst
```
Then you can use `hfst-fst2strings`:
```
$ hfst-fst2strings verb-generator.hfst
sing<v><pres>:sing
sing<v><pres><p3><sg>:sings
walk<v><pres>:walk
walk<v><pres><p3><sg>:walks
dance<v><pres>:dance
dance<v><pres><p3><sg>:dances
```
## Basic Syntax
A Lexd rule file defines lexicons and patterns. Each lexicon consists of a list of entries which have an analysis side and a generation side, similar to lexicons in HFST Lexc. Patterns, meanwhile, replace Lexc's continuation lexicons. Each pattern consists of a list of lexicons or named patterns which the compiler concatenates in that order.
```
PATTERNS
VerbRoot VerbInfl
LEXICON VerbRoot
sing
walk
dance
LEXICON VerbInfl
<v><pres>:
<v><pres><p3><sg>:s
```
forms generated:
```
sing/sing<v><pres>
sings/sing<v><pres><p3><sg>
walk/walk<v><pres>
walks/walk<v><pres><p3><sg>
dance/dance<v><pres>
dances/dance<v><pres><p3><sg>
```
Symbols enclosed in angle brackets or braces will be automatically interpreted as multicharacter symbols (presumably tags and archiphonemes, respectively):
```
PATTERNS
X
LEXICON X
x<ij>:x{i}
```
resulting ATT file:
```
0 1 x x 0.000000
1 2 <ij> {i} 0.000000
2 0.000000
```
Any character can be escaped with a backslash:
```
PATTERNS
X
LEXICON X
x\<ij>:x{i}
```
resulting ATT file:
```
0 1 x x 0.000000
1 2 < {i} 0.000000
2 3 i @0@ 0.000000
3 4 j @0@ 0.000000
4 5 > @0@ 0.000000
5 0.000000
```
And comments begin with `#`.
## Alignment
Patterns can list different sides of each lexicon in different places. When the compiler encounters a one-sided lexicon reference in a pattern, it attaches all entries from that side of that lexicon to the transducer and then builds the rest of the pattern, attaching a separate copy for each entry. However, in these copies, for any subsequent mentions of that lexicon, only the corresponding segment of that entry will be attached, thus avoiding over-generation. The same lexicon can be mentioned arbitrarily many times, making it straightforward to write rules for phenomena such as reduplication.
```
PATTERNS
:VerbInfl VerbRoot VerbInfl:
:VerbInfl :VerbRoot VerbRoot VerbInfl: Redup
LEXICON VerbRoot
bloop
vroom
LEXICON VerbInfl
<v><pres>:en
LEXICON Redup
<redup>:
```
forms generated:
```
enbloop/bloop<v><pres>
envroom/vroom<v><pres>
enbloopbloop/bloop<v><pres><redup>
envroomvroom/vroom<v><pres><redup>
```
To handle more complex cases, such as infixation and Semitic triliteral roots, lexicon entries can have multiple segments which patterns can refer to independently.
```
PATTERNS
C(1) :V(1) C(2) :V(2) C(3) V(2):
LEXICON C(3)
sh m r
y sh v
LEXICON V(2)
:a <v><p3><sg>:a
:o <v><pprs>:e
```
forms generated:
```
shamar/shmr<v><p3><sg>
shomer/shmr<v><pprs>
yashav/yshv<v><p3><sg>
yoshev/yshv<v><pprs>
```
It is also possible to give lexicons multiple names using the `ALIAS` command, which allows patterns to refer to multiple independent copies, which can then be used for productive compounding.
```
PATTERNS
NounStem NounInfl
NounStem NounInflComp Comp NounStem2 NounInfl
LEXICON Comp
<comp>+:
LEXICON NounStem
shoop
blarg
ALIAS NounStem NounStem2
LEXICON NounInfl
<n><sg>:
<n><pl>:ah
LEXICON NounInflComp
<n>:a
```
forms generated:
```
shoop/shoop<n><sg>
shoopah/shoop<n><pl>
shoopashoop/shoop<n><comp>+shoop<n><sg>
shoopashoopah/shoop<n><comp>+shoop<n><pl>
shoopablarg/shoop<n><comp>+blarg<n><sg>
shoopablargah/shoop<n><comp>+blarg<n><pl>
blarg/blarg<n><sg>
blargah/blarg<n><pl>
blargashoop/blarg<n><comp>+shoop<n><sg>
blargashoopah/blarg<n><comp>+shoop<n><pl>
blargablarg/blarg<n><comp>+blarg<n><sg>
blargablargah/blarg<n><comp>+blarg<n><pl>
```
Patterns can be named and included in other patterns. In addition to being less repetitive to write, it also compiles faster.
```
PATTERN VerbStem
VerbRoot
VerbRoot Causative
AuxRoot
PATTERNS
VerbStem Tense PersonNumber
```
This is equivalent to
```
PATTERNS
VerbRoot Tense PersonNumber
VerbRoot Causative Tense PersonNumber
AuxRoot Tense PersonNumber
```
## Pattern Operators
Some simple operators are supported to help write patterns concisely:
- the option quantifier `?` can be applied to a single token
```
PATTERNS
Negation? Adjective
# equivalent to:
# Negation Adjective
# Adjective
```
Placing the `?` quantifier between a lexicon name and the segment number will
make that lexicon as a whole optional in that pattern:
```
PATTERNS
OptionalCircumfix?(1) Stem OptionalCircumfix?(2)
# equivalent to:
# OptionalCircumfix(1) Stem OptionalCircumfix(2)
# Stem
```
Note that in this case the column specification after the `?` is required.
```
PATTERNS
:Prefix?(1) Stem Prefix?(1):
# equivalent to
# :Prefix(1) Stem Prefix(1): => :Prefix Stem Prefix:
# Stem
:Prefix? Stem Prefix:?
# equivalent to
# :Prefix Stem Prefix:
# :Prefix Stem
# Stem Prefix:
# Stem
```
The quantifiers `*` (repeat 0 or more times) and `+` (repeat 1 or more times)
function similarly, though they only support modification of a single token
and not distributed modification of a lexicon across an entire line.
- the alternation operator `|` between two tokens causes one pattern
for each alternate
```
PATTERNS
VerbStem Case
PATTERN Case
Absolutive
Oblique Ergative|Genitive
# equivalent to:
# Oblique Ergative
# Oblique Genitive
```
- the sieve operators `<` and `>` allow left and right extensions
```
PATTERNS
VerbStem > Nominalisation > Case
# equivalent to:
# VerbStem
# VerbStem Nominalisation
# VerbStem Nominalisation Case
```
## Anonymous Lexicons and Patterns
Patterns can contain anonymous lexicons to avoid needing to explicitly
declare lexicons for very simple things.
```
PATTERNS
NounStem [<n>:] NounNumber
LEXICON NounStem
sock
ninja
LEXICON NounNumber
<sg>:
<pl>:s
```
forms generated:
```
ninja/ninja<n><sg>
ninjas/ninja<n><pl>
sock/sock<n><sg>
socks/sock<n><pl>
```
Anonymous patterns function similarly:
```
PATTERNS
(VerbRoot Causative?) | AuxRoot Tense PersonNumber
# equivalent to:
# PATTERN VerbStem
# VerbRoot Causative?
# PATTERNS
# VerbStem|AuxRoot Tense PersonNumber
```
Anonymous patterns can be nested and both patterns and lexicons can be quantified:
```
PATTERNS
NounRoot ([<n>:] (Number Case)?) | (Verbalizer Tense)
```
## Tags
Lexicon entries can be tagged using square brackets:
```
LEXICON NounRoot
sock[count]
rice[mass]
sand[count,mass]
```
Or tags can be applied-by-default to an entire block:
```
LEXICON NounRoot[count]
sock
rice[mass,-count]
sand[mass]
```
When referring the lexicon, these tags can then be selected for:
```
PATTERNS
NounRoot[count] [<n>:] Number # 'sock' and 'sand', but not 'rice'
NounRoot[mass] [<n>:] # 'rice' and 'sand', but not 'sock'
```
The absense of a tag can also be selected for:
```
PATTERNS
NounRoot[-count] [<n>:] # 'rice' only
```
Tag selectors can also be applied to patterns:
```
PATTERN NounStem
NounRoot [<n>:]
PATTERNS
NounStem[count] Number
NounStem[mass]
```
Distribution rules are as follows:
```
(A B)[x] = (A[x] B) | (A B[x])
(A B)[-x] = A[-x] B[-x]
```
Union and symmetric difference are implemented with the following
syntax:
```
A[|[x,y]] = A[x] | A[y] # union / logical or
A[^[x,y]] = A[x,-y] | A[-x,y] # symmetric difference / exclusive-or
```
These can be useful with lexically conditioned patterns. Here's an
example showing declension paradigms and noun class:
```
PATTERNS
(NounStem CaseEnding)[^[Decl1,Decl2],^[N,M,F]]
LEXICON NounStem
mensa:mens[Decl1,F] # table
poeta:poet[Decl1,M] # poet
dominus:domin[Decl2,M] # master
bellum:bell[Decl2,N] # war
LEXICON CaseEnding[Decl2]
<nom>:>us[M]
<nom>:>um[N]
<acc>:>um # M or N
LEXICON CaseEnding[Decl1]
<nom>:>a # any gender
<acc>:>am # any gender
```
produces the forms (through `lexd | hfst-txt2fst | hfst-fst2strings`)
```
poeta<nom>:poet>a
poeta<acc>:poet>am
mensa<nom>:mens>a
mensa<acc>:mens>am
bellum<nom>:bell>um
bellum<acc>:bell>um
dominus<nom>:domin>us
dominus<acc>:domin>um
```
The exclusive or filter in this example will produce pairs of stems and case
endings such that between them, one has a declension tag and one (possibly the
same one) has a gender tag, and neither has any other declension or gender tag.
Thus, if we were to add a third declension such as
```
arbor:arbor[Decl3,F]
```
but we then forgot to add `Decl3` to the filter in the pattern, then from the
perspective of the filter, it wouldn't have a declension tag, so it would get
paired with every case ending that does have a declension tag (which in this
example is all of them).
## Regular Expressions
If a lexicon entry begins with a forward slash, it is interpreted as a regular expression.
```
PATTERNS
SomeLexicon
LEXICON SomeLexicon
/x(y|zz)?[n-p]/
```
produces the forms
```
xn
xo
xp
xyn
xyo
xyp
xzzn
xzzo
xzzp
```
### Currently Supported Syntax
- Grouping with `()`
- Quantification with `?`, `*`, and `+`
- Currently, quantifiers may only be applied to fully parenthesized groups, so `x+` is an error and must be written as `(x)+`.
- Alternation with `|`
- Character classes with `[]`
- Character ranges such as `[a-z]`
- Multichar symbols, following the same rules are normal lexicon entries
- Two-sided strings using `:`
- `a:b` is the same as in a normal entry
- `[ab]:c` is equivalent to `(a:c)|(b:c)`
|