blob: ccc27c144f50bc5340edaac5fdd21f12f86e9f1c [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
47define RP [SP|","|";"|":"|
48 ")"|"]"|"}"|
49 "”"|"›"|"»"|
50 %"|
51 ! differs
52 ["'" "'"]|
53 "*"|"/"|"_"| ! Can be Markdown
54 ! from book
55 [%‘ %‘]|[%’ %’]];
56
Akron6742b962021-11-09 01:17:20 +010057define Sym ["-"|"+"|"<"|">"|"*"|"/"|%=|%@|%&];
Akron310905f2021-08-11 13:49:50 +020058define Apos %'|%’|%`;
Akron4c2a1ad2021-08-31 00:35:53 +020059define Punct [LP|RP|Sym];
Akron310905f2021-08-11 13:49:50 +020060!define nonSym \[WS|LP|RP|Sym];
61!!!!!!!!!!!!!!!!!!
62! </from tmorph> !
63!!!!!!!!!!!!!!!!!!
64
65define Emdash [%- %- (%-)+ | ["\u2014"|"\u2015"|"\u2e3a"|"\u2e3b"|"\ufe58"]+];
66define Dash ["-"|"\u2011"|"\u2012"|"\u2013"|"\u2e1a"|"\ufe63"|"\uff0d"];
67define Slash ["⁄"|"∕"|"/"|"/"];
68define Asterisk ["*"];
69
Akron3de361e2021-08-17 09:56:42 +020070define Char \[WS|NL|Punct|Apos]; ! |¨;
Akron310905f2021-08-11 13:49:50 +020071
Akron936c0f52021-12-07 11:30:53 +010072define Word Char+ ([Apos|Asterisk] Char+)*;
73
74define Plusampersand @txt"txt/plusampersand.txt";
75define Word [Plusampersand | Word] (Dash [Plusampersand | Word])*;
76
Akron310905f2021-08-11 13:49:50 +020077
78define URLChar [Char|[Sym - ["<"|">"|%"]]];
79!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"|"_"];
80
81define Caseinsensitive [
82a (->) A,
83b (->) B,
84c (->) C,
85d (->) D,
86e (->) E,
87f (->) F,
88g (->) G,
89h (->) H,
90i (->) I,
91j (->) J,
92k (->) K,
93l (->) L,
94m (->) M,
95n (->) N,
96o (->) O,
97p (->) P,
98q (->) Q,
99r (->) R,
100s (->) S,
101t (->) T,
102u (->) U,
103v (->) V,
104w (->) W,
105x (->) X,
106y (->) Y,
107z (->) Z,
108ö (->) Ö,
109ü (->) Ü,
110ä (->) Ä,
111ß (->) {SS}
112];
113
Akronf1106ec2021-11-05 13:04:44 +0100114define Letter [ [ AsciiLetter | ö | ü | ä | ß ] .o. Caseinsensitive ];
115
Akron11a05d92021-11-06 13:17:11 +0100116! Abbreviations and Initials
Akronf1106ec2021-11-05 13:04:44 +0100117define Abbr [ @txt"txt/abbrv.txt" | Letter ] %.;
Akron310905f2021-08-11 13:49:50 +0200118
119! A solution to the "(author): problem" may be to add ) at the end of any
120! string as a possible ending
121
122define Years ["(" Digit+ (".") ")"] | ["[" Digit+ (".") "]"];
123
Akron17984c82021-10-30 11:44:37 +0200124! 20:00 Uhr, 00:12:25,34 Minuten
125define 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 +0200126
Akron310905f2021-08-11 13:49:50 +0200127source emoji.xfst
128define Emoji;
129
130! acronyms: U.S.A., I.B.M., etc.
131! use a post-filter to remove dots
Akronf1106ec2021-11-05 13:04:44 +0100132define AcronymDep Letter %. [Letter %.]+;
Akron310905f2021-08-11 13:49:50 +0200133
134define Dot "."|[["["|"("] "d" "o" "t" [")"|"]"]] .o. Caseinsensitive;
135define At "@"|[["["|"("] "a" "t" [")"|"]"]] .o. Caseinsensitive;
136
137define TldEnd [{org}|{de}|{com}] .o. Caseinsensitive;
138
139! Very relaxed URL scheme, not based on the strict Lucene implementation
140define URL [ [ [{http} (s) | {ftp} | {file}] ":" "/" "/"] | [{www} Dot] ]
141URLChar [URLChar|SP]* URLChar
142.o. Caseinsensitive;
143
144define Domain Char+ [Dash Char+]* Dot TldEnd;
145
Akronc8406362021-11-09 20:17:50 +0100146! XML sources
147source xml.xfst
148define XML;
Akron310905f2021-08-11 13:49:50 +0200149
Akron6742b962021-11-09 01:17:20 +0100150! XML entities
151source entities.xfst
152define XMLEntities;
153
154
155! Email addresses
Akron310905f2021-08-11 13:49:50 +0200156define Email URLChar+ At URLChar+ [Dot URLChar+]+;
157
158! Twitter user, hashtag, Google+
159define SNS ["@"|"#"|"+"] Char+;
160
161define FileEnd [
162 [{htm} ("l")]|
163 [{doc} ("x")]|
164 {pdf}|
165 ["j" "p" ("e") "g"]|
166 ["m" "p" ["3"|"4"]]|
167 {ogg}|
168 {png}|
Akron11a05d92021-11-06 13:17:11 +0100169 [{ppt} ("x")]|
Akron310905f2021-08-11 13:49:50 +0200170 {avi}|
171 {txt}|
172 {xls}|
173 {xml}|
174 {aac}|
Akrone8837b52021-08-11 17:29:58 +0200175 {gif}|
176 {exe}
Akron310905f2021-08-11 13:49:50 +0200177 ] .o. Caseinsensitive;
Akrone8837b52021-08-11 17:29:58 +0200178
179define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd;
Akron310905f2021-08-11 13:49:50 +0200180
Akrona0bded52021-08-11 15:48:02 +0200181define Streetname Word {str} %.;
Akron4af79f12021-08-11 14:48:17 +0200182
Akron310905f2021-08-11 13:49:50 +0200183! Also supports
184! 19.4.2015, 19/4/2015 etc.
185define DigitPunct ["_"|"-"|"."|","|Slash];
186define Num Digit+ [DigitPunct Digit+]* (Char+);
187
Akrona0bded52021-08-11 15:48:02 +0200188! ordinals
189define Ord Digit ( Digit (Digit) ) %.;
190
Akron310905f2021-08-11 13:49:50 +0200191! TODO:
192! floating point, serial, model numbers, ip addresses, etc.
193! every other segment must have at least one digit
194
195! Omission words like "fu**ing!"
196define Omission Char+ Asterisk Asterisk+ Char*;
197
198
199! TODO: Name words with ' and `
200
Akron310905f2021-08-11 13:49:50 +0200201! Support ASCII elements, like
202! +---------------+
203! <---->, -->, <--
204! +---------------+
205! <---> | Worker Node N |
206! +---------------+
207! |============= Core =============|
208
209
Akron4c2a1ad2021-08-31 00:35:53 +0200210echo - Compile Real Token
Akron310905f2021-08-11 13:49:50 +0200211
Akrone87906b2021-11-24 10:39:14 +0100212define RealToken [Punct|Emdash|Abbr|Streetname|Word|SNS|AcronymDep|Ord|Num|Years|Times|XMLEntities|Omission];
Akron310905f2021-08-11 13:49:50 +0200213
214echo - Introduce Token splitter
Akron4c2a1ad2021-08-31 00:35:53 +0200215
216define Token [
Akronf1106ec2021-11-05 13:04:44 +0100217 RealToken @-> ... NLout,
Akron71986452021-11-09 01:36:30 +0100218 XML @-> ... NLout,
219 URL @-> ... NLout,
220 Email @-> ... NLout,
221 File @-> ... NLout,
Akron71986452021-11-09 01:36:30 +0100222 Domain @-> ... NLout,
Akronfac8abc2021-11-10 07:19:59 +0100223 Emoji @-> ... NLout
224] .o. [[WS|NL]+ @-> 0 || [ .#. | NLout ] _ ];
Akron310905f2021-08-11 13:49:50 +0200225
226echo - Introduce Sentence splitter
Akron4c2a1ad2021-08-31 00:35:53 +0200227read regex Token .o. [[["."|"!"|"?"]+|"…"] @-> ... NLout \/ NLout _ ];
Akron310905f2021-08-11 13:49:50 +0200228
Akrona854faa2021-10-22 19:31:08 +0200229! foma -e "source tokenizer.xfst" -q -s && cat text.txt | flookup tokenizer.fst -x -b