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