Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 1 | ! 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 | |
Akron | 4af79f1 | 2021-08-11 14:48:17 +0200 | [diff] [blame] | 14 | define NLout "@_TOKEN_SYMBOL_@"; |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 15 | ! define NLout "\u000a"; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 16 | |
| 17 | define Digit [%0|1|2|3|4|5|6|7|8|9]; |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 18 | define 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]; |
Akron | 11a05d9 | 2021-11-06 13:17:11 +0100 | [diff] [blame] | 19 | define HexLetter [Digit|a|A|b|B|c|C|d|D|e|E|f|F]; |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 20 | define EOT "\u0004"; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 21 | |
| 22 | !!!!!!!!!!!!!!!!! |
| 23 | ! <from tmorph> ! |
| 24 | !!!!!!!!!!!!!!!!! |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 25 | define WS [" "|"\u0009"|"\u00a0"|"\u1680"| |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 26 | "\u2000"|"\u2001"|"\u2002"|"\u2003"|"\u2004"|"\u2005"| |
| 27 | "\u2006"|"\u2007"|"\u2008"|"\u2009"|"\u200a"| |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 28 | "\u202f"|"\u205f"|"\u3000"]; |
| 29 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 30 | define NL ["\u000a"|"\u000b"|"\u000c"|"\u000d"|"\u0085"|"\u2028"|"\u2029"|EOT]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 31 | |
| 32 | ! Punctuation that ends sentences |
| 33 | ! Differs! |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 34 | define SP [["."|"?"|"!"]+|"…"]; |
| 35 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 36 | ! Left punctuation |
| 37 | define LP ["("|"["|"{"| |
| 38 | "“"|"‘"|"‹"|"«"| |
| 39 | "'"|%"| |
| 40 | ! differs |
| 41 | ["'" "'"] | |
| 42 | "*"|"/"|"_"| ! Can be Markdown |
| 43 | ! from book |
| 44 | [%, %,]]; |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 45 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 46 | ! Right punctuation - excluding the characters that can be used as apostrophe |
| 47 | define RP [SP|","|";"|":"| |
| 48 | ")"|"]"|"}"| |
| 49 | "”"|"›"|"»"| |
| 50 | %"| |
| 51 | ! differs |
| 52 | ["'" "'"]| |
| 53 | "*"|"/"|"_"| ! Can be Markdown |
| 54 | ! from book |
| 55 | [%‘ %‘]|[%’ %’]]; |
| 56 | |
Akron | 6742b96 | 2021-11-09 01:17:20 +0100 | [diff] [blame^] | 57 | define Sym ["-"|"+"|"<"|">"|"*"|"/"|%=|%@|%&]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 58 | define Apos %'|%’|%`; |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 59 | define Punct [LP|RP|Sym]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 60 | !define nonSym \[WS|LP|RP|Sym]; |
| 61 | !!!!!!!!!!!!!!!!!! |
| 62 | ! </from tmorph> ! |
| 63 | !!!!!!!!!!!!!!!!!! |
| 64 | |
| 65 | define Emdash [%- %- (%-)+ | ["\u2014"|"\u2015"|"\u2e3a"|"\u2e3b"|"\ufe58"]+]; |
| 66 | define Dash ["-"|"\u2011"|"\u2012"|"\u2013"|"\u2e1a"|"\ufe63"|"\uff0d"]; |
| 67 | define Slash ["⁄"|"∕"|"/"|"/"]; |
| 68 | define Asterisk ["*"]; |
| 69 | |
Akron | 3de361e | 2021-08-17 09:56:42 +0200 | [diff] [blame] | 70 | define Char \[WS|NL|Punct|Apos]; ! |¨; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 71 | |
| 72 | ! source lexicon.xfst |
| 73 | ! define Word; |
| 74 | define Word Char+ ([Dash|Apos|Asterisk] Char+)*; |
| 75 | |
| 76 | define URLChar [Char|[Sym - ["<"|">"|%"]]]; |
| 77 | !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"|"_"]; |
| 78 | |
| 79 | define Caseinsensitive [ |
| 80 | a (->) A, |
| 81 | b (->) B, |
| 82 | c (->) C, |
| 83 | d (->) D, |
| 84 | e (->) E, |
| 85 | f (->) F, |
| 86 | g (->) G, |
| 87 | h (->) H, |
| 88 | i (->) I, |
| 89 | j (->) J, |
| 90 | k (->) K, |
| 91 | l (->) L, |
| 92 | m (->) M, |
| 93 | n (->) N, |
| 94 | o (->) O, |
| 95 | p (->) P, |
| 96 | q (->) Q, |
| 97 | r (->) R, |
| 98 | s (->) S, |
| 99 | t (->) T, |
| 100 | u (->) U, |
| 101 | v (->) V, |
| 102 | w (->) W, |
| 103 | x (->) X, |
| 104 | y (->) Y, |
| 105 | z (->) Z, |
| 106 | ö (->) Ö, |
| 107 | ü (->) Ü, |
| 108 | ä (->) Ä, |
| 109 | ß (->) {SS} |
| 110 | ]; |
| 111 | |
Akron | f1106ec | 2021-11-05 13:04:44 +0100 | [diff] [blame] | 112 | define Letter [ [ AsciiLetter | ö | ü | ä | ß ] .o. Caseinsensitive ]; |
| 113 | |
Akron | 11a05d9 | 2021-11-06 13:17:11 +0100 | [diff] [blame] | 114 | ! Abbreviations and Initials |
Akron | f1106ec | 2021-11-05 13:04:44 +0100 | [diff] [blame] | 115 | define Abbr [ @txt"txt/abbrv.txt" | Letter ] %.; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 116 | |
Akron | 57d0161 | 2021-08-11 17:53:19 +0200 | [diff] [blame] | 117 | define Plusampersand @txt"txt/plusampersand.txt"; |
| 118 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 119 | ! A solution to the "(author): problem" may be to add ) at the end of any |
| 120 | ! string as a possible ending |
| 121 | |
| 122 | define Years ["(" Digit+ (".") ")"] | ["[" Digit+ (".") "]"]; |
| 123 | |
Akron | 17984c8 | 2021-10-30 11:44:37 +0200 | [diff] [blame] | 124 | ! 20:00 Uhr, 00:12:25,34 Minuten |
| 125 | define Times [ ( [%0|1|2|3|4|5] ) Digit [ ":" [%0|1|2|3|4|5] Digit ]^{1,2} ( "," [ Digit ]^{1,3} ) ]; |
Akron | 78dba06 | 2021-10-28 19:30:46 +0200 | [diff] [blame] | 126 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 127 | source emoji.xfst |
| 128 | define Emoji; |
| 129 | |
| 130 | ! acronyms: U.S.A., I.B.M., etc. |
| 131 | ! use a post-filter to remove dots |
Akron | f1106ec | 2021-11-05 13:04:44 +0100 | [diff] [blame] | 132 | define AcronymDep Letter %. [Letter %.]+; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 133 | |
| 134 | define Dot "."|[["["|"("] "d" "o" "t" [")"|"]"]] .o. Caseinsensitive; |
| 135 | define At "@"|[["["|"("] "a" "t" [")"|"]"]] .o. Caseinsensitive; |
| 136 | |
| 137 | define TldEnd [{org}|{de}|{com}] .o. Caseinsensitive; |
| 138 | |
| 139 | ! Very relaxed URL scheme, not based on the strict Lucene implementation |
| 140 | define URL [ [ [{http} (s) | {ftp} | {file}] ":" "/" "/"] | [{www} Dot] ] |
| 141 | URLChar [URLChar|SP]* URLChar |
| 142 | .o. Caseinsensitive; |
| 143 | |
| 144 | define Domain Char+ [Dash Char+]* Dot TldEnd; |
| 145 | |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 146 | ! XML rule |
| 147 | define XMLns [AsciiLetter [AsciiLetter|Digit|%-]* (%: AsciiLetter [AsciiLetter|Digit|%-]*)] .o. Caseinsensitive; |
| 148 | define XML [ |
| 149 | "<" [ |
| 150 | [ |
| 151 | XMLns |
| 152 | [WS+ XMLns WS* |
| 153 | (%= WS* |
| 154 | [[%" [? - %" - %>]+ %"] | [%' [? - %' - %>]+ %']] |
| 155 | ) |
| 156 | ]* |
Akron | 066d99c | 2021-10-28 19:04:59 +0200 | [diff] [blame] | 157 | (WS* "/") |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 158 | ] |
| 159 | | |
| 160 | [ |
| 161 | "/" XMLns |
| 162 | ] |
| 163 | ] WS* ">" |
| 164 | ].u; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 165 | |
Akron | 6742b96 | 2021-11-09 01:17:20 +0100 | [diff] [blame^] | 166 | ! XML entities |
| 167 | source entities.xfst |
| 168 | define XMLEntities; |
| 169 | |
| 170 | |
| 171 | ! Email addresses |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 172 | define Email URLChar+ At URLChar+ [Dot URLChar+]+; |
| 173 | |
| 174 | ! Twitter user, hashtag, Google+ |
| 175 | define SNS ["@"|"#"|"+"] Char+; |
| 176 | |
| 177 | define FileEnd [ |
| 178 | [{htm} ("l")]| |
| 179 | [{doc} ("x")]| |
| 180 | {pdf}| |
| 181 | ["j" "p" ("e") "g"]| |
| 182 | ["m" "p" ["3"|"4"]]| |
| 183 | {ogg}| |
| 184 | {png}| |
Akron | 11a05d9 | 2021-11-06 13:17:11 +0100 | [diff] [blame] | 185 | [{ppt} ("x")]| |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 186 | {avi}| |
| 187 | {txt}| |
| 188 | {xls}| |
| 189 | {xml}| |
| 190 | {aac}| |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 191 | {gif}| |
| 192 | {exe} |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 193 | ] .o. Caseinsensitive; |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 194 | |
| 195 | define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 196 | |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 197 | define Streetname Word {str} %.; |
Akron | 4af79f1 | 2021-08-11 14:48:17 +0200 | [diff] [blame] | 198 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 199 | ! Also supports |
| 200 | ! 19.4.2015, 19/4/2015 etc. |
| 201 | define DigitPunct ["_"|"-"|"."|","|Slash]; |
| 202 | define Num Digit+ [DigitPunct Digit+]* (Char+); |
| 203 | |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 204 | ! ordinals |
| 205 | define Ord Digit ( Digit (Digit) ) %.; |
| 206 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 207 | ! TODO: |
| 208 | ! floating point, serial, model numbers, ip addresses, etc. |
| 209 | ! every other segment must have at least one digit |
| 210 | |
| 211 | ! Omission words like "fu**ing!" |
| 212 | define Omission Char+ Asterisk Asterisk+ Char*; |
| 213 | |
| 214 | |
| 215 | ! TODO: Name words with ' and ` |
| 216 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 217 | ! Support ASCII elements, like |
| 218 | ! +---------------+ |
| 219 | ! <---->, -->, <-- |
| 220 | ! +---------------+ |
| 221 | ! <---> | Worker Node N | |
| 222 | ! +---------------+ |
| 223 | ! |============= Core =============| |
| 224 | |
| 225 | |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 226 | echo - Compile Real Token |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 227 | |
Akron | 78dba06 | 2021-10-28 19:30:46 +0200 | [diff] [blame] | 228 | define RealToken [Punct|Word|XML|Email|URL|SNS|Domain|AcronymDep|File|Ord|Num|Years|Times]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 229 | |
| 230 | echo - Introduce Token splitter |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 231 | |
| 232 | define Token [ |
Akron | 6742b96 | 2021-11-09 01:17:20 +0100 | [diff] [blame^] | 233 | XMLEntities @-> ... NLout, |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 234 | Abbr @-> ... NLout, |
Akron | f1106ec | 2021-11-05 13:04:44 +0100 | [diff] [blame] | 235 | RealToken @-> ... NLout, |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 236 | Plusampersand @-> ... NLout, |
| 237 | Emoji @-> ... NLout, |
| 238 | [Streetname|Omission|Emdash] @-> ... NLout |
| 239 | ] |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 240 | .o. [[WS|NL]+ @-> 0 || NLout _ ] |
Akron | f6bdfdb | 2021-10-23 15:56:53 +0200 | [diff] [blame] | 241 | .o. [[WS|NL]+ @-> 0 || .#. _ ] |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 242 | ; |
| 243 | |
| 244 | echo - Introduce Sentence splitter |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 245 | read regex Token .o. [[["."|"!"|"?"]+|"…"] @-> ... NLout \/ NLout _ ]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 246 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 247 | ! foma -e "source tokenizer.xfst" -q -s && cat text.txt | flookup tokenizer.fst -x -b |