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