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 | c840636 | 2021-11-09 20:17:50 +0100 | [diff] [blame] | 146 | ! XML sources |
| 147 | source xml.xfst |
| 148 | define XML; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 149 | |
Akron | 6742b96 | 2021-11-09 01:17:20 +0100 | [diff] [blame] | 150 | ! XML entities |
| 151 | source entities.xfst |
| 152 | define XMLEntities; |
| 153 | |
| 154 | |
| 155 | ! Email addresses |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 156 | define Email URLChar+ At URLChar+ [Dot URLChar+]+; |
| 157 | |
| 158 | ! Twitter user, hashtag, Google+ |
| 159 | define SNS ["@"|"#"|"+"] Char+; |
| 160 | |
| 161 | define FileEnd [ |
| 162 | [{htm} ("l")]| |
| 163 | [{doc} ("x")]| |
| 164 | {pdf}| |
| 165 | ["j" "p" ("e") "g"]| |
| 166 | ["m" "p" ["3"|"4"]]| |
| 167 | {ogg}| |
| 168 | {png}| |
Akron | 11a05d9 | 2021-11-06 13:17:11 +0100 | [diff] [blame] | 169 | [{ppt} ("x")]| |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 170 | {avi}| |
| 171 | {txt}| |
| 172 | {xls}| |
| 173 | {xml}| |
| 174 | {aac}| |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 175 | {gif}| |
| 176 | {exe} |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 177 | ] .o. Caseinsensitive; |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 178 | |
| 179 | define File (( AsciiLetter ":" %\ | "/" ) [ Char | "_" | "-" | Char [ %\ | "/" ] ]*) [Char | "-" | "_" ]+ "." FileEnd; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 180 | |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 181 | define Streetname Word {str} %.; |
Akron | 4af79f1 | 2021-08-11 14:48:17 +0200 | [diff] [blame] | 182 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 183 | ! Also supports |
| 184 | ! 19.4.2015, 19/4/2015 etc. |
| 185 | define DigitPunct ["_"|"-"|"."|","|Slash]; |
| 186 | define Num Digit+ [DigitPunct Digit+]* (Char+); |
| 187 | |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 188 | ! ordinals |
| 189 | define Ord Digit ( Digit (Digit) ) %.; |
| 190 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 191 | ! 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!" |
| 196 | define Omission Char+ Asterisk Asterisk+ Char*; |
| 197 | |
| 198 | |
| 199 | ! TODO: Name words with ' and ` |
| 200 | |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 201 | ! Support ASCII elements, like |
| 202 | ! +---------------+ |
| 203 | ! <---->, -->, <-- |
| 204 | ! +---------------+ |
| 205 | ! <---> | Worker Node N | |
| 206 | ! +---------------+ |
| 207 | ! |============= Core =============| |
| 208 | |
| 209 | |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 210 | echo - Compile Real Token |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 211 | |
Akron | fac8abc | 2021-11-10 07:19:59 +0100 | [diff] [blame] | 212 | define RealToken [Punct|Emdash|Word|SNS|AcronymDep|Ord|Num|Years|Times|XMLEntities|Omission]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 213 | |
| 214 | echo - Introduce Token splitter |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 215 | |
| 216 | define Token [ |
Akron | fac8abc | 2021-11-10 07:19:59 +0100 | [diff] [blame] | 217 | [Abbr|Streetname] @-> ... NLout, |
Akron | f1106ec | 2021-11-05 13:04:44 +0100 | [diff] [blame] | 218 | RealToken @-> ... NLout, |
Akron | 7198645 | 2021-11-09 01:36:30 +0100 | [diff] [blame] | 219 | XML @-> ... NLout, |
| 220 | URL @-> ... NLout, |
| 221 | Email @-> ... NLout, |
| 222 | File @-> ... NLout, |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 223 | Plusampersand @-> ... NLout, |
Akron | 7198645 | 2021-11-09 01:36:30 +0100 | [diff] [blame] | 224 | Domain @-> ... NLout, |
Akron | fac8abc | 2021-11-10 07:19:59 +0100 | [diff] [blame] | 225 | Emoji @-> ... NLout |
| 226 | ] .o. [[WS|NL]+ @-> 0 || [ .#. | NLout ] _ ]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 227 | |
| 228 | echo - Introduce Sentence splitter |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 229 | read regex Token .o. [[["."|"!"|"?"]+|"…"] @-> ... NLout \/ NLout _ ]; |
Akron | 310905f | 2021-08-11 13:49:50 +0200 | [diff] [blame] | 230 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 231 | ! foma -e "source tokenizer.xfst" -q -s && cat text.txt | flookup tokenizer.fst -x -b |