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