blob: 6d21d8d284ab2677bdbaad6235355be45a6d4b62 [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 +020014! define NLout "\u000a";
15define NLout "@_TOKEN_SYMBOL_@";
Akron3de361e2021-08-17 09:56:42 +020016! define NLout "\u000a";
Akron310905f2021-08-11 13:49:50 +020017
18define Digit [%0|1|2|3|4|5|6|7|8|9];
Akrone8837b52021-08-11 17:29:58 +020019define 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];
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
29define NL ["\u000a"|"\u000b"|"\u000c"|"\u000d"|"\u0085"|"\u2028"|"\u2029"];
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 %'|%’|%`;
58define Punct LP|RP|Sym;
59!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
139!define XML "<" Alpha URLChar* (">");
140define XML "<" URLChar+ (">");
141
142!define Email [Alpha [URLChar-At]* At Alpha URLChar* [Dot [[Alpha URLChar+]-Dot-At]]+];
143define Email URLChar+ At URLChar+ [Dot URLChar+]+;
144
145! Twitter user, hashtag, Google+
146define SNS ["@"|"#"|"+"] Char+;
147
148define FileEnd [
149 [{htm} ("l")]|
150 [{doc} ("x")]|
151 {pdf}|
152 ["j" "p" ("e") "g"]|
153 ["m" "p" ["3"|"4"]]|
154 {ogg}|
155 {png}|
156 {avi}|
157 {txt}|
158 {xls}|
159 {xml}|
160 {aac}|
Akrone8837b52021-08-11 17:29:58 +0200161 {gif}|
162 {exe}
Akron310905f2021-08-11 13:49:50 +0200163 ] .o. Caseinsensitive;
Akrone8837b52021-08-11 17:29:58 +0200164
165define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd;
Akron310905f2021-08-11 13:49:50 +0200166
Akrona0bded52021-08-11 15:48:02 +0200167define Streetname Word {str} %.;
Akron4af79f12021-08-11 14:48:17 +0200168
Akron310905f2021-08-11 13:49:50 +0200169! Also supports
170! 19.4.2015, 19/4/2015 etc.
171define DigitPunct ["_"|"-"|"."|","|Slash];
172define Num Digit+ [DigitPunct Digit+]* (Char+);
173
Akrona0bded52021-08-11 15:48:02 +0200174! ordinals
175define Ord Digit ( Digit (Digit) ) %.;
176
Akron310905f2021-08-11 13:49:50 +0200177! TODO:
178! floating point, serial, model numbers, ip addresses, etc.
179! every other segment must have at least one digit
180
181! Omission words like "fu**ing!"
182define Omission Char+ Asterisk Asterisk+ Char*;
183
184
185! TODO: Name words with ' and `
186
Akron310905f2021-08-11 13:49:50 +0200187! Support ASCII elements, like
188! +---------------+
189! <---->, -->, <--
190! +---------------+
191! <---> | Worker Node N |
192! +---------------+
193! |============= Core =============|
194
195
196
Akron3de361e2021-08-17 09:56:42 +0200197define RealToken [XML|Email|URL|SNS|Abbr|Plusampersand|Streetname|Omission|Domain|AcronymDep|File|Emdash|Punct|Ord|Num|Years|Emoji|Word];
Akron310905f2021-08-11 13:49:50 +0200198
199echo - Introduce Token splitter
200define Token [RealToken @-> ... NLout]
Akron3de361e2021-08-17 09:56:42 +0200201! .o. [NL -> 0]
Akron310905f2021-08-11 13:49:50 +0200202.o. [WS+ @-> 0]
203;
204
205echo - Introduce Sentence splitter
Akron3de361e2021-08-17 09:56:42 +0200206read regex Token .o. [[["."|"!"|"?"]+|"…"] @-> ... NLout \/ NLout _];
Akron310905f2021-08-11 13:49:50 +0200207
208! foma -e "source tokenizer.xfst" -q -s && cat text.txt | flookup tokenizer.fst -x -b
209
210! In a second pass, XML tags need to be combined. This requires tagging "<..." with ~xmls before \n
211! and anything with > with ~xmle.
212! In case this is part of an emoticon ( >:-P ), this needs to be split again .
213! The same is true for ( and )