| 1 | /* liblouis Braille Translation and Back-Translation Library |
| 2 | |
| 3 | Copyright (C) 2017 Bert Frees |
| 4 | |
| 5 | This file is part of liblouis. |
| 6 | |
| 7 | liblouis is free software: you can redistribute it and/or modify it |
| 8 | under the terms of the GNU Lesser General Public License as published |
| 9 | by the Free Software Foundation, either version 2.1 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | liblouis is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with liblouis. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include "internal.h" |
| 27 | |
| 28 | static const TranslationTableHeader *table; |
| 29 | static const DisplayTableHeader *displayTable; |
| 30 | |
| 31 | extern void |
| 32 | loadTable(const char *tableList) { |
| 33 | _lou_getTable(tableList, tableList, &table, &displayTable); |
| 34 | } |
| 35 | |
| 36 | extern int |
| 37 | hyphenationEnabled() { |
| 38 | return table->hyphenStatesArray; |
| 39 | } |
| 40 | |
| 41 | extern int |
| 42 | isLetter(widechar c) { |
| 43 | static unsigned long int hash; |
| 44 | static TranslationTableOffset offset; |
| 45 | static TranslationTableCharacter *character; |
| 46 | hash = _lou_charHash(c); |
| 47 | offset = table->characters[hash]; |
| 48 | while (offset) { |
| 49 | character = (TranslationTableCharacter *)&table->ruleArea[offset]; |
| 50 | if (character->value == c) return character->attributes & CTC_Letter; |
| 51 | offset = character->next; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | extern widechar |
| 57 | toLowercase(widechar c) { |
| 58 | static TranslationTableOffset offset; |
| 59 | static TranslationTableCharacter *character; |
| 60 | offset = table->characters[_lou_charHash(c)]; |
| 61 | while (offset) { |
| 62 | character = (TranslationTableCharacter *)&table->ruleArea[offset]; |
| 63 | if (character->value == c) { |
| 64 | if (character->mode & CTC_UpperCase) { |
| 65 | const TranslationTableCharacter *c = character; |
| 66 | if (c->basechar) |
| 67 | c = (TranslationTableCharacter *)&table->ruleArea[c->basechar]; |
| 68 | while (1) { |
| 69 | if ((c->mode & (character->mode & ~CTC_UpperCase)) == |
| 70 | (character->mode & ~CTC_UpperCase)) |
| 71 | return c->value; |
| 72 | if (!c->linked) break; |
| 73 | c = (TranslationTableCharacter *)&table->ruleArea[c->linked]; |
| 74 | } |
| 75 | } |
| 76 | return character->value; |
| 77 | } |
| 78 | offset = character->next; |
| 79 | } |
| 80 | return c; |
| 81 | } |
| 82 | |
| 83 | extern void |
| 84 | toDotPattern(widechar *braille, char *pattern) { |
| 85 | int length; |
| 86 | widechar *dots; |
| 87 | int i; |
| 88 | for (length = 0; braille[length]; length++); |
| 89 | dots = (widechar *)malloc((length + 1) * sizeof(widechar)); |
| 90 | for (i = 0; i < length; i++) dots[i] = _lou_getDotsForChar(braille[i], displayTable); |
| 91 | strcpy(pattern, _lou_showDots(dots, length)); |
| 92 | free(dots); |
| 93 | } |
| 94 | |
| 95 | extern int |
| 96 | printRule(TranslationTableRule *rule, widechar *rule_string) { |
| 97 | switch (rule->opcode) { |
| 98 | case CTO_Context: |
| 99 | case CTO_Correct: |
| 100 | case CTO_SwapCd: |
| 101 | case CTO_SwapDd: |
| 102 | case CTO_Pass2: |
| 103 | case CTO_Pass3: |
| 104 | case CTO_Pass4: |
| 105 | return 0; |
| 106 | default: { |
| 107 | int l = 0; |
| 108 | if (rule->nocross) |
| 109 | for (char *c = "nocross "; *c; c++) rule_string[l++] = *c; |
| 110 | const char *opcode = _lou_findOpcodeName(rule->opcode); |
| 111 | for (size_t k = 0; k < strlen(opcode); k++) rule_string[l++] = opcode[k]; |
| 112 | rule_string[l++] = '\t'; |
| 113 | for (int k = 0; k < rule->charslen; k++) rule_string[l++] = rule->charsdots[k]; |
| 114 | rule_string[l++] = '\t'; |
| 115 | for (int k = 0; k < rule->dotslen; k++) { |
| 116 | rule_string[l] = _lou_getCharForDots( |
| 117 | rule->charsdots[rule->charslen + k], displayTable); |
| 118 | if (rule_string[l] == '\0') { |
| 119 | // if a dot pattern can not be displayed, print an error message |
| 120 | char *message = (char *)malloc(50 * sizeof(char)); |
| 121 | sprintf(message, "ERROR: provide a display rule for dots %s", |
| 122 | _lou_showDots(&rule->charsdots[rule->charslen + k], 1)); |
| 123 | l = 0; |
| 124 | while (message[l]) { |
| 125 | rule_string[l] = message[l]; |
| 126 | l++; |
| 127 | } |
| 128 | rule_string[l++] = '\0'; |
| 129 | free(message); |
| 130 | return 1; |
| 131 | } |
| 132 | l++; |
| 133 | } |
| 134 | rule_string[l++] = '\0'; |
| 135 | return 1; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #define DEBUG 0 |
| 141 | |
| 142 | #if DEBUG |
| 143 | #define debug(fmt, ...) \ |
| 144 | do { \ |
| 145 | if (DEBUG) printf("%*s" fmt "\n", debug_indent, "", ##__VA_ARGS__); \ |
| 146 | } while (0) |
| 147 | #else |
| 148 | #define debug(fmt, ...) |
| 149 | #endif |
| 150 | |
| 151 | static int |
| 152 | find_matching_rules(widechar *text, int text_len, widechar *braille, int braille_len, |
| 153 | char *data, int clear_data) { |
| 154 | TranslationTableOffset offset; |
| 155 | TranslationTableRule *rule; |
| 156 | TranslationTableCharacter *character; |
| 157 | char *data_save; |
| 158 | int hash_len, k; |
| 159 | #if DEBUG |
| 160 | static int initial_text_len = 0; |
| 161 | int debug_indent = 0; |
| 162 | if (data[-1] == '^') { |
| 163 | initial_text_len = text_len; |
| 164 | for (k = 0; k < text_len; k++) printf("%c", text[k]); |
| 165 | printf(" <=> "); |
| 166 | for (k = 0; k < braille_len; k++) printf("%c", braille[k]); |
| 167 | printf("\n"); |
| 168 | } else |
| 169 | debug_indent = initial_text_len - text_len; |
| 170 | #endif |
| 171 | |
| 172 | /* finish */ |
| 173 | if (text_len == 0 && braille_len == 0) { |
| 174 | data[-1] = '$'; |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | /* save data */ |
| 179 | data_save = (char *)malloc(text_len * sizeof(char)); |
| 180 | memcpy(data_save, data, text_len); |
| 181 | |
| 182 | for (k = 0; k < text_len; k++) |
| 183 | if (data[k] == ')') |
| 184 | data[k] = '>'; |
| 185 | else if (clear_data) |
| 186 | data[k] = '-'; |
| 187 | debug("%s", data); |
| 188 | |
| 189 | /* iterate over rules */ |
| 190 | for (hash_len = 2; hash_len >= 1; hash_len--) { |
| 191 | offset = 0; |
| 192 | switch (hash_len) { |
| 193 | case 2: |
| 194 | if (text_len < 2) break; |
| 195 | offset = table->forRules[_lou_stringHash(text, 1, table)]; |
| 196 | break; |
| 197 | case 1: |
| 198 | offset = table->characters[_lou_charHash(text[0])]; |
| 199 | while (offset) { |
| 200 | character = (TranslationTableCharacter *)&table->ruleArea[offset]; |
| 201 | if (character->value == text[0]) { |
| 202 | offset = character->otherRules; |
| 203 | break; |
| 204 | } else |
| 205 | offset = character->next; |
| 206 | } |
| 207 | } |
| 208 | while (offset) { |
| 209 | rule = (TranslationTableRule *)&table->ruleArea[offset]; |
| 210 | #if DEBUG |
| 211 | widechar print_string[128]; |
| 212 | printRule(rule, print_string); |
| 213 | printf("%*s=> ", debug_indent, ""); |
| 214 | for (k = 0; print_string[k]; k++) printf("%c", print_string[k]); |
| 215 | printf("\n"); |
| 216 | #endif |
| 217 | |
| 218 | /* select rule */ |
| 219 | if (rule->charslen == 0 || rule->dotslen == 0) goto next_rule; |
| 220 | if (rule->charslen > text_len) goto next_rule; |
| 221 | switch (rule->opcode) { |
| 222 | case CTO_WholeWord: |
| 223 | if (data[-1] == '^' && rule->charslen == text_len) break; |
| 224 | goto next_rule; |
| 225 | case CTO_SuffixableWord: |
| 226 | if (data[-1] == '^') break; |
| 227 | goto next_rule; |
| 228 | case CTO_PrefixableWord: |
| 229 | if (rule->charslen == text_len) break; |
| 230 | goto next_rule; |
| 231 | case CTO_BegWord: |
| 232 | if (data[-1] == '^' && rule->charslen < text_len) break; |
| 233 | goto next_rule; |
| 234 | case CTO_BegMidWord: |
| 235 | if (rule->charslen < text_len) break; |
| 236 | goto next_rule; |
| 237 | case CTO_MidWord: |
| 238 | if (data[-1] != '^' && rule->charslen < text_len) break; |
| 239 | goto next_rule; |
| 240 | case CTO_MidEndWord: |
| 241 | if (data[-1] != '^') break; |
| 242 | goto next_rule; |
| 243 | case CTO_EndWord: |
| 244 | if (data[-1] != '^' && rule->charslen == text_len) break; |
| 245 | goto next_rule; |
| 246 | case CTO_Letter: |
| 247 | case CTO_UpperCase: |
| 248 | case CTO_LowerCase: |
| 249 | case CTO_Punctuation: |
| 250 | case CTO_Always: |
| 251 | break; |
| 252 | default: |
| 253 | goto next_rule; |
| 254 | } |
| 255 | for (k = 0; k < rule->charslen; k++) |
| 256 | if (rule->charsdots[k] != text[k]) goto next_rule; |
| 257 | debug("** rule selected **"); |
| 258 | |
| 259 | /* inhibit rule */ |
| 260 | if (rule->dotslen > braille_len || |
| 261 | (rule->charslen == text_len && rule->dotslen < braille_len) || |
| 262 | (rule->dotslen == braille_len && rule->charslen < text_len)) |
| 263 | goto inhibit; |
| 264 | for (k = 0; k < rule->dotslen; k++) |
| 265 | if (_lou_getCharForDots(rule->charsdots[rule->charslen + k], |
| 266 | displayTable) != braille[k]) |
| 267 | goto inhibit; |
| 268 | |
| 269 | /* don't let this rule be inhibited by an earlier rule */ |
| 270 | int inhibit_all = 0; |
| 271 | if (rule->nocross) |
| 272 | for (k = 0; k < rule->charslen - 1; k++) |
| 273 | if (data[k + 1] == '>') { |
| 274 | if (data[-1] == 'x') |
| 275 | inhibit_all = 1; |
| 276 | else |
| 277 | goto next_rule; |
| 278 | } |
| 279 | |
| 280 | /* fill data */ |
| 281 | if (rule->nocross) |
| 282 | ; // deferred: see success |
| 283 | else { |
| 284 | k = 0; |
| 285 | while (k < rule->charslen - 1) { |
| 286 | if (data[k + 1] == '>') { |
| 287 | data[k++] = '1'; |
| 288 | memset(&data[k], '-', text_len - k); |
| 289 | } else |
| 290 | data[k++] = 'x'; |
| 291 | } |
| 292 | } |
| 293 | if (data[rule->charslen] == '>' || data[rule->charslen] == ')') { |
| 294 | data[rule->charslen - 1] = '1'; |
| 295 | memset(&data[rule->charslen], '-', text_len - rule->charslen); |
| 296 | } else |
| 297 | data[rule->charslen - 1] = 'x'; |
| 298 | debug("%s", data); |
| 299 | |
| 300 | /* recur */ |
| 301 | if (find_matching_rules(&text[rule->charslen], text_len - rule->charslen, |
| 302 | &braille[rule->dotslen], braille_len - rule->dotslen, |
| 303 | &data[rule->charslen], inhibit_all)) |
| 304 | goto success; |
| 305 | |
| 306 | inhibit: |
| 307 | debug("** rule inhibited **"); |
| 308 | if (rule->nocross) { |
| 309 | if (rule->charslen < 2) goto abort; |
| 310 | /* inhibited by earlier rule */ |
| 311 | for (k = 0; k < rule->charslen - 1; k++) |
| 312 | if (data[k + 1] == '>' && data[-1] != 'x') goto next_rule; |
| 313 | data[rule->charslen - 1] = ')'; |
| 314 | debug("%s", data); |
| 315 | goto next_rule; |
| 316 | } else { |
| 317 | goto abort; |
| 318 | } |
| 319 | |
| 320 | success: |
| 321 | /* fill data (deferred) */ |
| 322 | if (inhibit_all) data[-1] = '1'; |
| 323 | if (rule->nocross) { |
| 324 | memset(data, '0', rule->charslen - 1); |
| 325 | debug("%s", data); |
| 326 | } |
| 327 | free(data_save); |
| 328 | return 1; |
| 329 | |
| 330 | next_rule: |
| 331 | offset = rule->charsnext; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | abort: |
| 336 | /* restore data */ |
| 337 | memcpy(data, data_save, text_len); |
| 338 | free(data_save); |
| 339 | debug("** abort **"); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * - begin with all - |
| 345 | * - set cursor position right before the word |
| 346 | * - put a ^ |
| 347 | * - match rules |
| 348 | * - when a rule has been selected |
| 349 | * - if the braille does not match: try inhibiting the rule |
| 350 | * - if it's a nocross rule (longer than a single character) |
| 351 | * - if there's a > within or right after the rule and there's no x right before |
| 352 | * the rule |
| 353 | * - already inhibited |
| 354 | * - else: put a ) at the position right after the rule |
| 355 | * - else: abort this match |
| 356 | * - else (the braille does match) |
| 357 | * - if it's a nocross rule |
| 358 | * - if there's a > within or right after the rule |
| 359 | * - if there's a x at the position right before the rule |
| 360 | * - put a 1 at that position |
| 361 | * - reset all > |
| 362 | * - else |
| 363 | * - continue with next matched rule |
| 364 | * - put a 0 at each position within the rule |
| 365 | * - else |
| 366 | * - for each position within the rule |
| 367 | * - if there's a > at the next position |
| 368 | * - put a 1 |
| 369 | * - reset all > |
| 370 | * - else put a x |
| 371 | * - move cursor to the position right after the rule |
| 372 | * - put a $ if we're at the end of the word |
| 373 | * - change all ) to > |
| 374 | * - else if there's a > or a ) at the next position |
| 375 | * - put a 1 |
| 376 | * - reset all > |
| 377 | * - match rules at the new cursor position |
| 378 | * - if match was aborted |
| 379 | * - revert changes |
| 380 | * - try inhibiting the last rule |
| 381 | * - go back to the position before the rule |
| 382 | * - continue with next matched rule |
| 383 | * - else put a x |
| 384 | */ |
| 385 | extern int |
| 386 | suggestChunks(widechar *text, widechar *braille, char *hyphen_string) { |
| 387 | int text_len, braille_len; |
| 388 | for (text_len = 0; text[text_len]; text_len++); |
| 389 | for (braille_len = 0; braille[braille_len]; braille_len++); |
| 390 | if (text_len == 0 || braille_len == 0) return 0; |
| 391 | hyphen_string[0] = '^'; |
| 392 | hyphen_string[text_len + 1] = '\0'; |
| 393 | memset(&hyphen_string[1], '-', text_len); |
| 394 | return find_matching_rules( |
| 395 | text, text_len, braille, braille_len, &hyphen_string[1], 0); |
| 396 | } |
| 397 | |
| 398 | extern void |
| 399 | findRelevantRules(widechar *text, widechar **rules_str) { |
| 400 | int text_len, rules_len; |
| 401 | TranslationTableOffset offset; |
| 402 | TranslationTableCharacter *character; |
| 403 | TranslationTableRule *rule; |
| 404 | TranslationTableRule **rules; |
| 405 | int hash_len, k, m, n; |
| 406 | for (text_len = 0; text[text_len]; text_len++); |
| 407 | for (rules_len = 0; rules_str[rules_len]; rules_len++); |
| 408 | rules = (TranslationTableRule **)malloc( |
| 409 | (rules_len + 1) * sizeof(TranslationTableRule *)); |
| 410 | m = n = 0; |
| 411 | while (text[n]) { |
| 412 | for (hash_len = 2; hash_len >= 1; hash_len--) { |
| 413 | offset = 0; |
| 414 | switch (hash_len) { |
| 415 | case 2: |
| 416 | if (text_len - n < 2) break; |
| 417 | offset = table->forRules[_lou_stringHash(&text[n], 1, table)]; |
| 418 | break; |
| 419 | case 1: |
| 420 | offset = table->characters[_lou_charHash(text[n])]; |
| 421 | while (offset) { |
| 422 | character = (TranslationTableCharacter *)&table->ruleArea[offset]; |
| 423 | if (character->value == text[0]) { |
| 424 | offset = character->otherRules; |
| 425 | break; |
| 426 | } else |
| 427 | offset = character->next; |
| 428 | } |
| 429 | } |
| 430 | while (offset) { |
| 431 | rule = (TranslationTableRule *)&table->ruleArea[offset]; |
| 432 | switch (rule->opcode) { |
| 433 | case CTO_Always: |
| 434 | case CTO_WholeWord: |
| 435 | case CTO_SuffixableWord: |
| 436 | case CTO_PrefixableWord: |
| 437 | case CTO_BegWord: |
| 438 | case CTO_BegMidWord: |
| 439 | case CTO_MidWord: |
| 440 | case CTO_MidEndWord: |
| 441 | case CTO_EndWord: |
| 442 | break; |
| 443 | default: |
| 444 | goto next_rule; |
| 445 | } |
| 446 | if (rule->charslen == 0 || rule->dotslen == 0 || |
| 447 | rule->charslen > text_len - n) |
| 448 | goto next_rule; |
| 449 | for (k = 0; k < rule->charslen; k++) |
| 450 | if (rule->charsdots[k] != text[n + k]) goto next_rule; |
| 451 | rules[m++] = rule; |
| 452 | if (m == rules_len) goto finish; |
| 453 | next_rule: |
| 454 | offset = rule->charsnext; |
| 455 | } |
| 456 | } |
| 457 | n++; |
| 458 | } |
| 459 | finish: |
| 460 | rules_str[m--] = NULL; |
| 461 | for (; m >= 0; m--) printRule(rules[m], rules_str[m]); |
| 462 | free(rules); |
| 463 | } |