blob: 3d63e5bb98868af6b64027224dcb1fd944bb8737 [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];
Akron11a05d92021-11-06 13:17:11 +010019define HexLetter [Digit|a|A|b|B|c|C|d|D|e|E|f|F];
Akrona854faa2021-10-22 19:31:08 +020020define EOT "\u0004";
Akron310905f2021-08-11 13:49:50 +020021
22!!!!!!!!!!!!!!!!!
23! <from tmorph> !
24!!!!!!!!!!!!!!!!!
Akron3de361e2021-08-17 09:56:42 +020025define WS [" "|"\u0009"|"\u00a0"|"\u1680"|
Akron310905f2021-08-11 13:49:50 +020026 "\u2000"|"\u2001"|"\u2002"|"\u2003"|"\u2004"|"\u2005"|
27 "\u2006"|"\u2007"|"\u2008"|"\u2009"|"\u200a"|
Akron3de361e2021-08-17 09:56:42 +020028 "\u202f"|"\u205f"|"\u3000"];
29
Akrona854faa2021-10-22 19:31:08 +020030define NL ["\u000a"|"\u000b"|"\u000c"|"\u000d"|"\u0085"|"\u2028"|"\u2029"|EOT];
Akron310905f2021-08-11 13:49:50 +020031
32! Punctuation that ends sentences
33! Differs!
Akron3de361e2021-08-17 09:56:42 +020034define SP [["."|"?"|"!"]+|"…"];
35
Akron310905f2021-08-11 13:49:50 +020036! Left punctuation
37define LP ["("|"["|"{"|
38 "“"|"‘"|"‹"|"«"|
39 "'"|%"|
40 ! differs
41 ["'" "'"] |
42 "*"|"/"|"_"| ! Can be Markdown
43 ! from book
44 [%, %,]];
Akron3de361e2021-08-17 09:56:42 +020045
Akron310905f2021-08-11 13:49:50 +020046! Right punctuation - excluding the characters that can be used as apostrophe
Akrone96895f2022-03-08 19:58:37 +010047define RPS ["”"|"›"|"»"|%"|[%’ %’]|["'" "'"]|[%‘ %‘]];
48define RP [SP|RPS|","|";"|":"|
Akron310905f2021-08-11 13:49:50 +020049 ")"|"]"|"}"|
Akron310905f2021-08-11 13:49:50 +020050 ! differs
Akrone96895f2022-03-08 19:58:37 +010051 "*"|"/"|"_"]; ! Can be Markdown
Akron310905f2021-08-11 13:49:50 +020052
Akron6742b962021-11-09 01:17:20 +010053define Sym ["-"|"+"|"<"|">"|"*"|"/"|%=|%@|%&];
Akron310905f2021-08-11 13:49:50 +020054define Apos %'|%’|%`;
Akron4c2a1ad2021-08-31 00:35:53 +020055define Punct [LP|RP|Sym];
Akron310905f2021-08-11 13:49:50 +020056!define nonSym \[WS|LP|RP|Sym];
57!!!!!!!!!!!!!!!!!!
58! </from tmorph> !
59!!!!!!!!!!!!!!!!!!
60
61define Emdash [%- %- (%-)+ | ["\u2014"|"\u2015"|"\u2e3a"|"\u2e3b"|"\ufe58"]+];
62define Dash ["-"|"\u2011"|"\u2012"|"\u2013"|"\u2e1a"|"\ufe63"|"\uff0d"];
63define Slash ["⁄"|"∕"|"/"|"/"];
64define Asterisk ["*"];
65
Akron3de361e2021-08-17 09:56:42 +020066define Char \[WS|NL|Punct|Apos]; ! |¨;
Akron310905f2021-08-11 13:49:50 +020067
Akronb02ad072022-01-19 12:41:44 +010068define Word Char+ ([Apos|Asterisk] Char+)* ([s|S] [%’|%`]);
Akron936c0f52021-12-07 11:30:53 +010069
70define Plusampersand @txt"txt/plusampersand.txt";
71define Word [Plusampersand | Word] (Dash [Plusampersand | Word])*;
72
Akron310905f2021-08-11 13:49:50 +020073
74define URLChar [Char|[Sym - ["<"|">"|%"]]];
75!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"|"_"];
76
77define Caseinsensitive [
78a (->) A,
79b (->) B,
80c (->) C,
81d (->) D,
82e (->) E,
83f (->) F,
84g (->) G,
85h (->) H,
86i (->) I,
87j (->) J,
88k (->) K,
89l (->) L,
90m (->) M,
91n (->) N,
92o (->) O,
93p (->) P,
94q (->) Q,
95r (->) R,
96s (->) S,
97t (->) T,
98u (->) U,
99v (->) V,
100w (->) W,
101x (->) X,
102y (->) Y,
103z (->) Z,
104ö (->) Ö,
105ü (->) Ü,
106ä (->) Ä,
107ß (->) {SS}
108];
109
Akronf1106ec2021-11-05 13:04:44 +0100110define Letter [ [ AsciiLetter | ö | ü | ä | ß ] .o. Caseinsensitive ];
111
Akron11a05d92021-11-06 13:17:11 +0100112! Abbreviations and Initials
Akronf1106ec2021-11-05 13:04:44 +0100113define Abbr [ @txt"txt/abbrv.txt" | Letter ] %.;
Akron310905f2021-08-11 13:49:50 +0200114
115! 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
Akron17984c82021-10-30 11:44:37 +0200120! 20:00 Uhr, 00:12:25,34 Minuten
121define Times [ ( [%0|1|2|3|4|5] ) Digit [ ":" [%0|1|2|3|4|5] Digit ]^{1,2} ( "," [ Digit ]^{1,3} ) ];
Akron78dba062021-10-28 19:30:46 +0200122
Akron310905f2021-08-11 13:49:50 +0200123source emoji.xfst
124define Emoji;
125
126! acronyms: U.S.A., I.B.M., etc.
127! use a post-filter to remove dots
Akron54ed7e72022-01-04 12:05:00 +0100128define AcronymDep Letter %. Letter %. [Letter %.]+;
Akron310905f2021-08-11 13:49:50 +0200129
130define Dot "."|[["["|"("] "d" "o" "t" [")"|"]"]] .o. Caseinsensitive;
131define At "@"|[["["|"("] "a" "t" [")"|"]"]] .o. Caseinsensitive;
132
133define TldEnd [{org}|{de}|{com}] .o. Caseinsensitive;
134
135! Very relaxed URL scheme, not based on the strict Lucene implementation
136define URL [ [ [{http} (s) | {ftp} | {file}] ":" "/" "/"] | [{www} Dot] ]
137URLChar [URLChar|SP]* URLChar
138.o. Caseinsensitive;
139
140define Domain Char+ [Dash Char+]* Dot TldEnd;
141
Akronc8406362021-11-09 20:17:50 +0100142! XML sources
143source xml.xfst
144define XML;
Akron310905f2021-08-11 13:49:50 +0200145
Akron6742b962021-11-09 01:17:20 +0100146! XML entities
147source entities.xfst
148define XMLEntities;
149
150
151! Email addresses
Akron310905f2021-08-11 13:49:50 +0200152define Email URLChar+ At URLChar+ [Dot URLChar+]+;
153
154! Twitter user, hashtag, Google+
155define SNS ["@"|"#"|"+"] Char+;
156
157define FileEnd [
158 [{htm} ("l")]|
159 [{doc} ("x")]|
160 {pdf}|
161 ["j" "p" ("e") "g"]|
162 ["m" "p" ["3"|"4"]]|
163 {ogg}|
164 {png}|
Akron11a05d92021-11-06 13:17:11 +0100165 [{ppt} ("x")]|
Akron310905f2021-08-11 13:49:50 +0200166 {avi}|
167 {txt}|
168 {xls}|
169 {xml}|
170 {aac}|
Akrone8837b52021-08-11 17:29:58 +0200171 {gif}|
172 {exe}
Akron310905f2021-08-11 13:49:50 +0200173 ] .o. Caseinsensitive;
Akrone8837b52021-08-11 17:29:58 +0200174
175define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd;
Akron310905f2021-08-11 13:49:50 +0200176
Akrona0bded52021-08-11 15:48:02 +0200177define Streetname Word {str} %.;
Akron4af79f12021-08-11 14:48:17 +0200178
Akron310905f2021-08-11 13:49:50 +0200179! Also supports
180! 19.4.2015, 19/4/2015 etc.
181define DigitPunct ["_"|"-"|"."|","|Slash];
182define Num Digit+ [DigitPunct Digit+]* (Char+);
183
Akrona0bded52021-08-11 15:48:02 +0200184! ordinals
185define Ord Digit ( Digit (Digit) ) %.;
186
Akron310905f2021-08-11 13:49:50 +0200187! TODO:
188! floating point, serial, model numbers, ip addresses, etc.
189! every other segment must have at least one digit
190
191! Omission words like "fu**ing!"
192define Omission Char+ Asterisk Asterisk+ Char*;
193
194
195! TODO: Name words with ' and `
196
Akron310905f2021-08-11 13:49:50 +0200197! Support ASCII elements, like
198! +---------------+
199! <---->, -->, <--
200! +---------------+
201! <---> | Worker Node N |
202! +---------------+
203! |============= Core =============|
204
205
Akron4c2a1ad2021-08-31 00:35:53 +0200206echo - Compile Real Token
Akron310905f2021-08-11 13:49:50 +0200207
Akrone87906b2021-11-24 10:39:14 +0100208define RealToken [Punct|Emdash|Abbr|Streetname|Word|SNS|AcronymDep|Ord|Num|Years|Times|XMLEntities|Omission];
Akron310905f2021-08-11 13:49:50 +0200209
210echo - Introduce Token splitter
Akron4c2a1ad2021-08-31 00:35:53 +0200211
212define Token [
Akronf1106ec2021-11-05 13:04:44 +0100213 RealToken @-> ... NLout,
Akron71986452021-11-09 01:36:30 +0100214 XML @-> ... NLout,
215 URL @-> ... NLout,
216 Email @-> ... NLout,
217 File @-> ... NLout,
Akron71986452021-11-09 01:36:30 +0100218 Domain @-> ... NLout,
Akronfac8abc2021-11-10 07:19:59 +0100219 Emoji @-> ... NLout
Akrone96895f2022-03-08 19:58:37 +0100220];
Akron310905f2021-08-11 13:49:50 +0200221
222echo - Introduce Sentence splitter
Akrone96895f2022-03-08 19:58:37 +0100223! And compose Whitespace ignorance
224read regex Token .o. [
225 SP NLout %" @-> ... NLout \/ _ NLout \%,
226] .o. [
227 SP @-> ... NLout \/ NLout _ NLout \%"
228] .o. [
229 [WS|NL]+ @-> 0 || [ .#. | NLout ] _
230];
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