-
Notifications
You must be signed in to change notification settings - Fork 0
/
tok.foma
101 lines (74 loc) · 3.49 KB
/
tok.foma
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
# Copyright (C) 2023 Darko Milosevic <daremc86@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3.0 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
source cyrillic_alphabet.foma
source latin_alphabet.foma
define VowelLetter LVowelLetter | CVowelLetter ;
define ConsonantLetter LConsonantLetter | CConsonantLetter ;
define ucLetter ucLLetter | ucCLetter ;
define lcLetter lcLLetter | lcCLetter ;
define Letter VowelLetter | ConsonantLetter ;
define Word [Letter+] ;
define vocalicR [R | r | Р | р ];
define LetterSequence ConsonantLetter+ - [ConsonantLetter+ vocalicR ConsonantLetter+] -[В|в|V|v];
# The word B must not be named as a letter. However, its Latin equivalent, follwoed by a dot, is an abbev. ...
define NZDigit 1|2|3|4|5|6|7|8|9;
define Digit %0|NZDigit;
define FormattedNumber NZDigit Digit^<3 [%. Digit^3]^{1,4} ;
define Decimal FormattedNumber %, Digit+; # 0.1 2.0000
# foreign decimal of form 0.12, 0.1234 ... but NOT 01.23
define ForeignDecimal [[NZDigit Digit*] | %0] %. [Digit^{1,2} | Digit^>3]; # 0.1 2.0000
source romans.foma
source ordinals.foma
source abbrevs.foma
define BasicTokenizer
Abbreviation @-> ... word || _ \[Letter|num|%.] | [ %. [\num|.#.]] | .#. ,,
Word @-> ... word || _ \[Letter|num|%.] | [ %. [\num|.#.]] | .#. ,, # Roman Number might have been here first, so do not mess with labelled Num
[FormattedNumber (OrdSuffix)] @-> ... num || _ [.#. | \[Digit|Letter]] ,,
Decimal @-> ... num || _ .#. | \Digit ,,
ForeignDecimal @-> ... num || _ .#. | \Digit ,,
NZDigit Digit* OrdSuffix @-> ... num || _ [.#. | \[Digit|Letter]] ,,
NZDigit Digit* @-> ... num ,,
%0 Digit+ @-> ... dig ,,
%0 @-> ... num ,,
\[Letter | Digit | num] @-> ... sym || _ [\num | .#.] # again, protect Roman Numerals like V. with a dot
;
define UntagLargeNumbers
num -> dig || Digit^16 _ ;
define CamelCase
ucLetter lcLetter* @-> ...word || [.#.|word|num|dig|sym] _ ucLetter lcLetter ;
source acronyms.foma
define TagLetterSequences
word -> lseq || [.#.|word|num|dig|sym] [Acronym | LetterSequence] _ ;
define TagLetterSequenceAbbreviations
lseq -> word || [.#.|word|lseq|num|dig|sym] Abbreviation _ ;
# retrieve decimals
define DecimalNumbers
[num %, sym] -> %, || .#. Digit+ _ Digit+ num .#. ; # pass 12,34 as a number
define GeneralTokenizer
[[\eos]+ (eos)] .o. # Define what we expect as input, so the priority union works properly
[ eos -> 0 || _ ] .o.
[ RomanNumeral @-> ... num || .#. _ .#. .o. BasicTokenizer ]
.o.
DecimalNumbers .o.
CamelCase .o.
TagLetterSequences .o.
TagLetterSequenceAbbreviations .o.
UntagLargeNumbers
;
define SingleLetterException Letter [eos : lseq] ;
define Exception SingleLetterException ;
define Tokenizer Exception .P. GeneralTokenizer ;
define TokenTypeTag num|dig|sym|word|lseq ;
define TokenOfType(tag) [[[\[eos | TokenTypeTag]]+ (eos)] .o. Tokenizer .o. [[\TokenTypeTag]+ [tag:0]]].l ;
define NumberToken TokenOfType(num);
define WordToken TokenOfType(word);
define LSeqToken TokenOfType(lseq);
regex Tokenizer ;