blob: 0d703a3e5ada973fec36923fee960d5f41412438 [file] [log] [blame]
Akron310905f2021-08-11 13:49:50 +02001! This tokenizer is based on work by
2! - StandardTokenizerImpl by the Lucene project
3! under the Apache License
4! - https://github.com/dlwh/epic by David Hall (2014)
5! under the Apacahe License
6! - KorAPTokenizerImpl.jflex by Marc Kupietz (2016)
7! under the Apache License
8! - https://github.com/coltekin/TRmorph/tokenize.xfst by Çağrı Çöltekin (2011-2015)
9! under the MIT License
10!
11! The abbreviation list is part of the sentence splitter tool
12! of the IDS.
13
Akron4af79f12021-08-11 14:48:17 +020014define NLout "@_TOKEN_SYMBOL_@";
Akron3de361e2021-08-17 09:56:42 +020015! define NLout "\u000a";
Akron310905f2021-08-11 13:49:50 +020016
17define Digit [%0|1|2|3|4|5|6|7|8|9];
Akrone8837b52021-08-11 17:29:58 +020018define AsciiLetter [a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z];
Akrona854faa2021-10-22 19:31:08 +020019define EOT "\u0004";
Akron310905f2021-08-11 13:49:50 +020020
21!!!!!!!!!!!!!!!!!
22! <from tmorph> !
23!!!!!!!!!!!!!!!!!
Akron3de361e2021-08-17 09:56:42 +020024define WS [" "|"\u0009"|"\u00a0"|"\u1680"|
Akron310905f2021-08-11 13:49:50 +020025 "\u2000"|"\u2001"|"\u2002"|"\u2003"|"\u2004"|"\u2005"|
26 "\u2006"|"\u2007"|"\u2008"|"\u2009"|"\u200a"|
Akron3de361e2021-08-17 09:56:42 +020027 "\u202f"|"\u205f"|"\u3000"];
28
Akrona854faa2021-10-22 19:31:08 +020029define NL ["\u000a"|"\u000b"|"\u000c"|"\u000d"|"\u0085"|"\u2028"|"\u2029"|EOT];
Akron310905f2021-08-11 13:49:50 +020030
31! Punctuation that ends sentences
32! Differs!
Akron3de361e2021-08-17 09:56:42 +020033define SP [["."|"?"|"!"]+|"…"];
34
Akron310905f2021-08-11 13:49:50 +020035! Left punctuation
36define LP ["("|"["|"{"|
37 "“"|"‘"|"‹"|"«"|
38 "'"|%"|
39 ! differs
40 ["'" "'"] |
41 "*"|"/"|"_"| ! Can be Markdown
42 ! from book
43 [%, %,]];
Akron3de361e2021-08-17 09:56:42 +020044
Akron310905f2021-08-11 13:49:50 +020045! Right punctuation - excluding the characters that can be used as apostrophe
46define RP [SP|","|";"|":"|
47 ")"|"]"|"}"|
48 ""|""|"»"|
49 %"|
50 ! differs
51 ["'" "'"]|
52 "*"|"/"|"_"| ! Can be Markdown
53 ! from book
54 [%‘ %‘]|[%’ %’]];
55
56define Sym ["-"|"+"|"<"|">"|"*"|"/"|%=|%@];
57define Apos %'|%’|%`;
Akron4c2a1ad2021-08-31 00:35:53 +020058define Punct [LP|RP|Sym];
Akron310905f2021-08-11 13:49:50 +020059!define nonSym \[WS|LP|RP|Sym];
60!!!!!!!!!!!!!!!!!!
61! </from tmorph> !
62!!!!!!!!!!!!!!!!!!
63
64define Emdash [%- %- (%-)+ | ["\u2014"|"\u2015"|"\u2e3a"|"\u2e3b"|"\ufe58"]+];
65define Dash ["-"|"\u2011"|"\u2012"|"\u2013"|"\u2e1a"|"\ufe63"|"\uff0d"];
66define Slash ["⁄"|"∕"|"/"|"/"];
67define Asterisk ["*"];
68
Akron3de361e2021-08-17 09:56:42 +020069define Char \[WS|NL|Punct|Apos]; ! |¨;
Akron310905f2021-08-11 13:49:50 +020070
71! source lexicon.xfst
72! define Word;
73define Word Char+ ([Dash|Apos|Asterisk] Char+)*;
74
75define URLChar [Char|[Sym - ["<"|">"|%"]]];
76!define Alpha ["a"|"b"|"c"|"d"|"e"|"f"|"g"|"h"|"i"|"j"|"k"|"l"|"m"|"n"|"o"|"p"|"q"|"r"|"s"|"t"|"u"|"v"|"w"|"x"|"y"|"z"|"_"];
77
78define Caseinsensitive [
79a (->) A,
80b (->) B,
81c (->) C,
82d (->) D,
83e (->) E,
84f (->) F,
85g (->) G,
86h (->) H,
87i (->) I,
88j (->) J,
89k (->) K,
90l (->) L,
91m (->) M,
92n (->) N,
93o (->) O,
94p (->) P,
95q (->) Q,
96r (->) R,
97s (->) S,
98t (->) T,
99u (->) U,
100v (->) V,
101w (->) W,
102x (->) X,
103y (->) Y,
104z (->) Z,
105ö (->) Ö,
106ü (->) Ü,
107ä (->) Ä,
108ß (->) {SS}
109];
110
Akron3de361e2021-08-17 09:56:42 +0200111define Abbr @txt"txt/abbrv.txt" %.;
Akron310905f2021-08-11 13:49:50 +0200112
Akron57d01612021-08-11 17:53:19 +0200113define Plusampersand @txt"txt/plusampersand.txt";
114
Akron310905f2021-08-11 13:49:50 +0200115! A solution to the "(author): problem" may be to add ) at the end of any
116! string as a possible ending
117
118define Years ["(" Digit+ (".") ")"] | ["[" Digit+ (".") "]"];
119
120source emoji.xfst
121define Emoji;
122
123! acronyms: U.S.A., I.B.M., etc.
124! use a post-filter to remove dots
125define AcronymDep Char %. [Char %.]+;
126
127define Dot "."|[["["|"("] "d" "o" "t" [")"|"]"]] .o. Caseinsensitive;
128define At "@"|[["["|"("] "a" "t" [")"|"]"]] .o. Caseinsensitive;
129
130define TldEnd [{org}|{de}|{com}] .o. Caseinsensitive;
131
132! Very relaxed URL scheme, not based on the strict Lucene implementation
133define URL [ [ [{http} (s) | {ftp} | {file}] ":" "/" "/"] | [{www} Dot] ]
134URLChar [URLChar|SP]* URLChar
135.o. Caseinsensitive;
136
137define Domain Char+ [Dash Char+]* Dot TldEnd;
138
Akron4c2a1ad2021-08-31 00:35:53 +0200139! XML rule
140define XMLns [AsciiLetter [AsciiLetter|Digit|%-]* (%: AsciiLetter [AsciiLetter|Digit|%-]*)] .o. Caseinsensitive;
141define XML [
142 "<" [
143 [
144 XMLns
145 [WS+ XMLns WS*
146 (%= WS*
147 [[%" [? - %" - %>]+ %"] | [%' [? - %' - %>]+ %']]
148 )
149 ]*
150 ]
151 |
152 [
153 "/" XMLns
154 ]
155 ] WS* ">"
156].u;
Akron310905f2021-08-11 13:49:50 +0200157
158!define Email [Alpha [URLChar-At]* At Alpha URLChar* [Dot [[Alpha URLChar+]-Dot-At]]+];
159define Email URLChar+ At URLChar+ [Dot URLChar+]+;
160
161! Twitter user, hashtag, Google+
162define SNS ["@"|"#"|"+"] Char+;
163
164define FileEnd [
165 [{htm} ("l")]|
166 [{doc} ("x")]|
167 {pdf}|
168 ["j" "p" ("e") "g"]|
169 ["m" "p" ["3"|"4"]]|
170 {ogg}|
171 {png}|
172 {avi}|
173 {txt}|
174 {xls}|
175 {xml}|
176 {aac}|
Akrone8837b52021-08-11 17:29:58 +0200177 {gif}|
178 {exe}
Akron310905f2021-08-11 13:49:50 +0200179 ] .o. Caseinsensitive;
Akrone8837b52021-08-11 17:29:58 +0200180
181define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd;
Akron310905f2021-08-11 13:49:50 +0200182
Akrona0bded52021-08-11 15:48:02 +0200183define Streetname Word {str} %.;
Akron4af79f12021-08-11 14:48:17 +0200184
Akron310905f2021-08-11 13:49:50 +0200185! Also supports
186! 19.4.2015, 19/4/2015 etc.
187define DigitPunct ["_"|"-"|"."|","|Slash];
188define Num Digit+ [DigitPunct Digit+]* (Char+);
189
Akrona0bded52021-08-11 15:48:02 +0200190! ordinals
191define Ord Digit ( Digit (Digit) ) %.;
192
Akron310905f2021-08-11 13:49:50 +0200193! TODO:
194! floating point, serial, model numbers, ip addresses, etc.
195! every other segment must have at least one digit
196
197! Omission words like "fu**ing!"
198define Omission Char+ Asterisk Asterisk+ Char*;
199
200
201! TODO: Name words with ' and `
202
Akron310905f2021-08-11 13:49:50 +0200203! Support ASCII elements, like
204! +---------------+
205! <---->, -->, <--
206! +---------------+
207! <---> | Worker Node N |
208! +---------------+
209! |============= Core =============|
210
211
Akron4c2a1ad2021-08-31 00:35:53 +0200212echo - Compile Real Token
Akron310905f2021-08-11 13:49:50 +0200213
Akron4c2a1ad2021-08-31 00:35:53 +0200214define RealToken [Punct|Word|XML|Email|URL|SNS|Domain|AcronymDep|File|Ord|Num|Years];
Akron310905f2021-08-11 13:49:50 +0200215
216echo - Introduce Token splitter
Akron4c2a1ad2021-08-31 00:35:53 +0200217
218define Token [
219 RealToken @-> ... NLout,
220 Abbr @-> ... NLout,
221 Plusampersand @-> ... NLout,
222 Emoji @-> ... NLout,
223 [Streetname|Omission|Emdash] @-> ... NLout
224 ]
Akrona854faa2021-10-22 19:31:08 +0200225.o. [[WS|NL]+ @-> 0 || NLout _ ]
Akronf6bdfdb2021-10-23 15:56:53 +0200226.o. [[WS|NL]+ @-> 0 || .#. _ ]
Akron310905f2021-08-11 13:49:50 +0200227;
228
229echo - Introduce Sentence splitter
Akron4c2a1ad2021-08-31 00:35:53 +0200230read regex Token .o. [[["."|"!"|"?"]+|"…"] @-> ... NLout \/ NLout _ ];
Akron310905f2021-08-11 13:49:50 +0200231
Akrona854faa2021-10-22 19:31:08 +0200232! foma -e "source tokenizer.xfst" -q -s && cat text.txt | flookup tokenizer.fst -x -b