dictionary.c 83 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
/*
 * Copyright (C) 2005 to 2014 by Jonathan Duddington
 * email: jonsd@users.sourceforge.net
 * Copyright (C) 2013-2016 Reece H. Dunn
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>

#include <espeak-ng/espeak_ng.h>
#include <espeak/speak_lib.h>

#include "speech.h"
#include "phoneme.h"
#include "synthesize.h"
#include "translate.h"

int dictionary_skipwords;
char dictionary_name[40];

extern void print_dictionary_flags(unsigned int *flags, char *buf, int buf_len);
extern char *DecodeRule(const char *group_chars, int group_length, char *rule, int control);

// accented characters which indicate (in some languages) the start of a separate syllable
static const unsigned short diereses_list[7] = { 0xe4, 0xeb, 0xef, 0xf6, 0xfc, 0xff, 0 };

// convert characters to an approximate 7 bit ascii equivalent
// used for checking for vowels (up to 0x259=schwa)
#define N_REMOVE_ACCENT  0x25e
static unsigned char remove_accent[N_REMOVE_ACCENT] = {
	'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',  // 0c0
	'd', 'n', 'o', 'o', 'o', 'o', 'o',   0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 's',  // 0d0
	'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',  // 0e0
	'd', 'n', 'o', 'o', 'o', 'o', 'o',   0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 'y',  // 0f0

	'a', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'd', 'd',  // 100
	'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g',  // 110
	'g', 'g', 'g', 'g', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i',  // 120
	'i', 'i', 'i', 'i', 'j', 'j', 'k', 'k', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l',  // 130
	'l', 'l', 'l', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o',  // 140
	'o', 'o', 'o', 'o', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's',  // 150
	's', 's', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',  // 160
	'u', 'u', 'u', 'u', 'w', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 's',  // 170
	'b', 'b', 'b', 'b',   0,   0, 'o', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'e', 'e',  // 180
	'e', 'f', 'f', 'g', 'g', 'h', 'i', 'i', 'k', 'k', 'l', 'l', 'm', 'n', 'n', 'o',  // 190
	'o', 'o', 'o', 'o', 'p', 'p', 'y',   0,   0, 's', 's', 't', 't', 't', 't', 'u',  // 1a0
	'u', 'u', 'v', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 'z',   0,   0,   0, 'w',  // 1b0
	't', 't', 't', 'k', 'd', 'd', 'd', 'l', 'l', 'l', 'n', 'n', 'n', 'a', 'a', 'i',  // 1c0
	'i', 'o', 'o', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'e', 'a', 'a',  // 1d0
	'a', 'a', 'a', 'a', 'g', 'g', 'g', 'g', 'k', 'k', 'o', 'o', 'o', 'o', 'z', 'z',  // 1e0
	'j', 'd', 'd', 'd', 'g', 'g', 'w', 'w', 'n', 'n', 'a', 'a', 'a', 'a', 'o', 'o',  // 1f0

	'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o',  // 200
	'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 's', 's', 't', 't', 'y', 'y', 'h', 'h',  // 210
	'n', 'd', 'o', 'o', 'z', 'z', 'a', 'a', 'e', 'e', 'o', 'o', 'o', 'o', 'o', 'o',  // 220
	'o', 'o', 'y', 'y', 'l', 'n', 't', 'j', 'd', 'q', 'a', 'c', 'c', 'l', 't', 's',  // 230
	'z',   0,   0, 'b', 'u', 'v', 'e', 'e', 'j', 'j', 'q', 'q', 'r', 'r', 'y', 'y',  // 240
	'a', 'a', 'a', 'b', 'o', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e'
};

#pragma GCC visibility push(default)
void strncpy0(char *to, const char *from, int size)
{
	// strcpy with limit, ensures a zero terminator
	strncpy(to, from, size);
	to[size-1] = 0;
}
#pragma GCC visibility pop

int Reverse4Bytes(int word)
{
	// reverse the order of bytes from little-endian to big-endian
#ifdef ARCH_BIG
	int ix;
	int word2 = 0;

	for (ix = 0; ix <= 24; ix += 8) {
		word2 = word2 << 8;
		word2 |= (word >> ix) & 0xff;
	}
	return word2;
#else
	return word;
#endif
}

int LookupMnem(MNEM_TAB *table, const char *string)
{
	while (table->mnem != NULL) {
		if (strcmp(string, table->mnem) == 0)
			return table->value;
		table++;
	}
	return table->value;
}

static void InitGroups(Translator *tr)
{
	// Called after dictionary 1 is loaded, to set up table of entry points for translation rule chains
	// for single-letters and two-letter combinations

	int ix;
	char *p;
	char *p_name;
	unsigned int *pw;
	unsigned char c, c2;
	int len;

	tr->n_groups2 = 0;
	for (ix = 0; ix < 256; ix++) {
		tr->groups1[ix] = NULL;
		tr->groups2_count[ix] = 0;
		tr->groups2_start[ix] = 255; // indicates "not set"
	}
	memset(tr->letterGroups, 0, sizeof(tr->letterGroups));
	memset(tr->groups3, 0, sizeof(tr->groups3));

	p = tr->data_dictrules;
	while (*p != 0) {
		if (*p != RULE_GROUP_START) {
			fprintf(stderr, "Bad rules data in '%s_dict' at 0x%x\n", dictionary_name, (unsigned int)(p - tr->data_dictrules));
			break;
		}
		p++;

		if (p[0] == RULE_REPLACEMENTS) {
			pw = (unsigned int *)(((intptr_t)p+4) & ~3); // advance to next word boundary
			tr->langopts.replace_chars = pw;
			while (pw[0] != 0)
				pw += 2; // find the end of the replacement list, each entry is 2 words.
			p = (char *)(pw+1);

#ifdef ARCH_BIG
			pw = (unsigned int *)(tr->langopts.replace_chars);
			while (*pw != 0) {
				*pw = Reverse4Bytes(*pw);
				pw++;
				*pw = Reverse4Bytes(*pw);
				pw++;
			}
#endif
			continue;
		}

		if (p[0] == RULE_LETTERGP2) {
			ix = p[1] - 'A';
			p += 2;
			if ((ix >= 0) && (ix < N_LETTER_GROUPS))
				tr->letterGroups[ix] = p;
		} else {
			len = strlen(p);
			p_name = p;
			c = p_name[0];
			c2 = p_name[1];

			p += (len+1);
			if (len == 1)
				tr->groups1[c] = p;
			else if (len == 0)
				tr->groups1[0] = p;
			else if (c == 1) {
				// index by offset from letter base
				tr->groups3[c2 - 1] = p;
			} else {
				if (tr->groups2_start[c] == 255)
					tr->groups2_start[c] = tr->n_groups2;

				tr->groups2_count[c]++;
				tr->groups2[tr->n_groups2] = p;
				tr->groups2_name[tr->n_groups2++] = (c + (c2 << 8));
			}
		}

		// skip over all the rules in this group
		while (*p != RULE_GROUP_END)
			p += (strlen(p) + 1);
		p++;
	}
}

int LoadDictionary(Translator *tr, const char *name, int no_error)
{
	int hash;
	char *p;
	int *pw;
	int length;
	FILE *f;
	unsigned int size;
	char fname[sizeof(path_home)+20];

	strcpy(dictionary_name, name); // currently loaded dictionary name
	strcpy(tr->dictionary_name, name);

	// Load a pronunciation data file into memory
	// bytes 0-3:  offset to rules data
	// bytes 4-7:  number of hash table entries
	sprintf(fname, "%s%c%s_dict", path_home, PATHSEP, name);
	size = GetFileLength(fname);

	if (tr->data_dictlist != NULL) {
		free(tr->data_dictlist);
		tr->data_dictlist = NULL;
	}

	f = fopen(fname, "rb");
	if ((f == NULL) || (size <= 0)) {
		if (no_error == 0)
			fprintf(stderr, "Can't read dictionary file: '%s'\n", fname);
		if (f != NULL)
			fclose(f);
		return 1;
	}

	if ((tr->data_dictlist = malloc(size)) == NULL) {
		fclose(f);
		return 3;
	}
	size = fread(tr->data_dictlist, 1, size, f);
	fclose(f);

	pw = (int *)(tr->data_dictlist);
	length = Reverse4Bytes(pw[1]);

	if (size <= (N_HASH_DICT + sizeof(int)*2)) {
		fprintf(stderr, "Empty _dict file: '%s\n", fname);
		return 2;
	}

	if ((Reverse4Bytes(pw[0]) != N_HASH_DICT) ||
	    (length <= 0) || (length > 0x8000000)) {
		fprintf(stderr, "Bad data: '%s' (%x length=%x)\n", fname, Reverse4Bytes(pw[0]), length);
		return 2;
	}
	tr->data_dictrules = &(tr->data_dictlist[length]);

	// set up indices into data_dictrules
	InitGroups(tr);

	// set up hash table for data_dictlist
	p = &(tr->data_dictlist[8]);

	for (hash = 0; hash < N_HASH_DICT; hash++) {
		tr->dict_hashtab[hash] = p;
		while ((length = *p) != 0)
			p += length;
		p++; // skip over the zero which terminates the list for this hash value
	}

	if ((tr->dict_min_size > 0) && (size < (unsigned int)tr->dict_min_size))
		fprintf(stderr, "Full dictionary is not installed for '%s'\n", name);

	return 0;
}

/* Generate a hash code from the specified string
    This is used to access the dictionary_2 word-lookup dictionary
 */
int HashDictionary(const char *string)
{
	int c;
	int chars = 0;
	int hash = 0;

	while ((c = (*string++ & 0xff)) != 0) {
		hash = hash * 8 + c;
		hash = (hash & 0x3ff) ^ (hash >> 8); // exclusive or
		chars++;
	}

	return (hash+chars) & 0x3ff; // a 10 bit hash code
}

/* Translate a phoneme string from ascii mnemonics to internal phoneme numbers,
   from 'p' up to next blank .
   Returns advanced 'p'
   outptr contains encoded phonemes, unrecognized phoneme stops the encoding
   bad_phoneme must point to char array of length 2 of more
 */
const char *EncodePhonemes(const char *p, char *outptr, int *bad_phoneme)
{
	int ix;
	unsigned char c;
	int count;     // num. of matching characters
	int max;       // highest num. of matching found so far
	int max_ph;    // corresponding phoneme with highest matching
	int consumed;
	unsigned int mnemonic_word;

	if (bad_phoneme != NULL)
		*bad_phoneme = 0;

	// skip initial blanks
	while ((uint8_t)*p < 0x80 && isspace(*p))
		p++;

	while (((c = *p) != 0) && !isspace(c)) {
		consumed = 0;

		switch (c)
		{
		case '|':
			// used to separate phoneme mnemonics if needed, to prevent characters being treated
			// as a multi-letter mnemonic

			if ((c = p[1]) == '|') {
				// treat double || as a word-break symbol, drop through
				// to the default case with c = '|'
			} else {
				p++;
				break;
			}
		default:
			// lookup the phoneme mnemonic, find the phoneme with the highest number of
			// matching characters
			max = -1;
			max_ph = 0;

			for (ix = 1; ix < n_phoneme_tab; ix++) {
				if (phoneme_tab[ix] == NULL)
					continue;
				if (phoneme_tab[ix]->type == phINVALID)
					continue; // this phoneme is not defined for this language

				count = 0;
				mnemonic_word = phoneme_tab[ix]->mnemonic;

				while (((c = p[count]) > ' ') && (count < 4) &&
				       (c == ((mnemonic_word >> (count*8)) & 0xff)))
					count++;

				if ((count > max) &&
				    ((count == 4) || (((mnemonic_word >> (count*8)) & 0xff) == 0))) {
					max = count;
					max_ph = phoneme_tab[ix]->code;
				}
			}

			if (max_ph == 0) {
				// not recognised, report and ignore
				if (bad_phoneme != NULL)
					utf8_in(bad_phoneme, p);
				*outptr++ = 0;
				return p+1;
			}

			if (max <= 0)
				max = 1;
			p += (consumed + max);
			*outptr++ = (char)(max_ph);

			if (max_ph == phonSWITCH) {
				// Switch Language: this phoneme is followed by a text string
				char *p_lang = outptr;
				while (!isspace(c = *p) && (c != 0)) {
					p++;
					*outptr++ = tolower(c);
				}
				*outptr = 0;
				if (c == 0) {
					if (strcmp(p_lang, "en") == 0) {
						*p_lang = 0; // don't need "en", it's assumed by default
						return p;
					}
				} else
					*outptr++ = '|'; // more phonemes follow, terminate language string with separator
			}
			break;
		}
	}
	// terminate the encoded string
	*outptr = 0;
	return p;
}

void DecodePhonemes(const char *inptr, char *outptr)
{
	// Translate from internal phoneme codes into phoneme mnemonics
	unsigned char phcode;
	unsigned char c;
	unsigned int mnem;
	PHONEME_TAB *ph;
	static const char *stress_chars = "==,,'*  ";

	sprintf(outptr, "* ");
	while ((phcode = *inptr++) > 0) {
		if (phcode == 255)
			continue; // indicates unrecognised phoneme
		if ((ph = phoneme_tab[phcode]) == NULL)
			continue;

		if ((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0)) {
			if (ph->std_length > 1)
				*outptr++ = stress_chars[ph->std_length];
		} else {
			mnem = ph->mnemonic;

			while ((c = (mnem & 0xff)) != 0) {
				*outptr++ = c;
				mnem = mnem >> 8;
			}
			if (phcode == phonSWITCH) {
				while (isalpha(*inptr))
					*outptr++ = *inptr++;
			}
		}
	}
	*outptr = 0; // string terminator
}

// using Kirschenbaum to IPA translation, ascii 0x20 to 0x7f
unsigned short ipa1[96] = {
	0x20,  0x21,  0x22,  0x2b0, 0x24,  0x25,  0x0e6, 0x2c8, 0x28,  0x29,  0x27e, 0x2b,  0x2cc, 0x2d,  0x2e,  0x2f,
	0x252, 0x31,  0x32,  0x25c, 0x34,  0x35,  0x36,  0x37,  0x275, 0x39,  0x2d0, 0x2b2, 0x3c,  0x3d,  0x3e,  0x294,
	0x259, 0x251, 0x3b2, 0xe7,  0xf0,  0x25b, 0x46,  0x262, 0x127, 0x26a, 0x25f, 0x4b,  0x26b, 0x271, 0x14b, 0x254,
	0x3a6, 0x263, 0x280, 0x283, 0x3b8, 0x28a, 0x28c, 0x153, 0x3c7, 0xf8,  0x292, 0x32a, 0x5c,  0x5d,  0x5e,  0x5f,
	0x60,  0x61,  0x62,  0x63,  0x64,  0x65,  0x66,  0x261, 0x68,  0x69,  0x6a,  0x6b,  0x6c,  0x6d,  0x6e,  0x6f,
	0x70,  0x71,  0x72,  0x73,  0x74,  0x75,  0x76,  0x77,  0x78,  0x79,  0x7a,  0x7b,  0x7c,  0x7d,  0x303, 0x7f
};

#define N_PHON_OUT  500  // realloc increment
static char *phon_out_buf = NULL;   // passes the result of GetTranslatedPhonemeString()
static unsigned int phon_out_size = 0;

char *WritePhMnemonic(char *phon_out, PHONEME_TAB *ph, PHONEME_LIST *plist, int use_ipa, int *flags)
{
	int c;
	int mnem;
	int len;
	int first;
	int ix = 0;
	char *p;
	PHONEME_DATA phdata;

	if (ph->code == phonEND_WORD) {
		// ignore
		phon_out[0] = 0;
		return phon_out;
	}

	if (ph->code == phonSWITCH) {
		// the tone_ph field contains a phoneme table number
		p = phoneme_tab_list[plist->tone_ph].name;
		sprintf(phon_out, "(%s)", p);
		return phon_out + strlen(phon_out);
	}

	if (use_ipa) {
		// has an ipa name been defined for this phoneme ?
		phdata.ipa_string[0] = 0;

		if (plist == NULL)
			InterpretPhoneme2(ph->code, &phdata);
		else
			InterpretPhoneme(NULL, 0, plist, &phdata, NULL);

		p = phdata.ipa_string;
		if (*p == 0x20) {
			// indicates no name for this phoneme
			*phon_out = 0;
			return phon_out;
		}
		if ((*p != 0) && ((*p & 0xff) < 0x20)) {
			// name starts with a flags byte
			if (flags != NULL)
				*flags = *p;
			p++;
		}

		len = strlen(p);
		if (len > 0) {
			strcpy(phon_out, p);
			phon_out += len;
			*phon_out = 0;
			return phon_out;
		}
	}

	first = 1;
	for (mnem = ph->mnemonic; (c = mnem & 0xff) != 0; mnem = mnem >> 8) {
		if ((c == '/') && (option_phoneme_variants == 0))
			break; // discard phoneme variant indicator

		if (use_ipa) {
			// convert from ascii to ipa
			if (first && (c == '_'))
				break; // don't show pause phonemes

			if ((c == '#') && (ph->type == phVOWEL))
				break; // # is subscript-h, but only for consonants

			// ignore digits after the first character
			if (!first && IsDigit09(c))
				continue;

			if ((c >= 0x20) && (c < 128))
				c = ipa1[c-0x20];

			ix += utf8_out(c, &phon_out[ix]);
		} else
			phon_out[ix++] = c;
		first = 0;
	}

	phon_out = &phon_out[ix];
	*phon_out = 0;
	return phon_out;
}

const char *GetTranslatedPhonemeString(int phoneme_mode)
{
	/* Called after a clause has been translated into phonemes, in order
	   to display the clause in phoneme mnemonic form.

	   phoneme_mode
	                 bit  1:   use IPA phoneme names
	                 bit  7:   use tie between letters in multi-character phoneme names
	                 bits 8-23 tie or separator character

	 */

	int ix;
	unsigned int len;
	int phon_out_ix = 0;
	int stress;
	int c;
	char *p;
	char *buf;
	int count;
	int flags;
	int use_ipa;
	int use_tie;
	int separate_phonemes;
	char phon_buf[30];
	char phon_buf2[30];
	PHONEME_LIST *plist;

	static const char *stress_chars = "==,,''";

	if (phon_out_buf == NULL) {
		phon_out_size = N_PHON_OUT;
		if ((phon_out_buf = (char *)malloc(phon_out_size)) == NULL) {
			phon_out_size = 0;
			return "";
		}
	}

	use_ipa = phoneme_mode & espeakPHONEMES_IPA;
	if (phoneme_mode & espeakPHONEMES_TIE) {
		use_tie = phoneme_mode >> 8;
		separate_phonemes = 0;
	} else {
		separate_phonemes = phoneme_mode >> 8;
		use_tie = 0;
	}

	for (ix = 1; ix < (n_phoneme_list-2); ix++) {
		buf = phon_buf;

		plist = &phoneme_list[ix];

		WritePhMnemonic(phon_buf2, plist->ph, plist, use_ipa, &flags);
		if (plist->newword)
			*buf++ = ' ';

		if ((!plist->newword) || (separate_phonemes == ' ')) {
			if ((separate_phonemes != 0) && (ix > 1)) {
				utf8_in(&c, phon_buf2);
				if ((c < 0x2b0) || (c > 0x36f)) // not if the phoneme starts with a superscript letter
					buf += utf8_out(separate_phonemes, buf);
			}
		}

		if (plist->synthflags & SFLAG_SYLLABLE) {
			if ((stress = plist->stresslevel) > 1) {
				c = 0;
				if (stress > 5) stress = 5;

				if (use_ipa) {
					c = 0x2cc; // ipa, secondary stress
					if (stress > 3)
						c = 0x02c8; // ipa, primary stress
				} else
					c = stress_chars[stress];

				if (c != 0)
					buf += utf8_out(c, buf);
			}
		}

		flags = 0;
		count = 0;
		for (p = phon_buf2; *p != 0;) {
			p += utf8_in(&c, p);
			if (use_tie != 0) {
				// look for non-inital alphabetic character, but not diacritic, superscript etc.
				if ((count > 0) && !(flags & (1 << (count-1))) && ((c < 0x2b0) || (c > 0x36f)) && iswalpha2(c))
					buf += utf8_out(use_tie, buf);
			}
			buf += utf8_out(c, buf);
			count++;
		}

		if (plist->ph->code != phonSWITCH) {
			if (plist->synthflags & SFLAG_LENGTHEN)
				buf = WritePhMnemonic(buf, phoneme_tab[phonLENGTHEN], plist, use_ipa, NULL);
			if ((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL)) {
				// syllablic consonant
				buf = WritePhMnemonic(buf, phoneme_tab[phonSYLLABIC], plist, use_ipa, NULL);
			}
			if (plist->tone_ph > 0)
				buf = WritePhMnemonic(buf, phoneme_tab[plist->tone_ph], plist, use_ipa, NULL);
		}

		len = buf - phon_buf;
		if ((phon_out_ix + len) >= phon_out_size) {
			// enlarge the phoneme buffer
			phon_out_size = phon_out_ix + len + N_PHON_OUT;
			char *new_phon_out_buf = (char *)realloc(phon_out_buf, phon_out_size);
			if (new_phon_out_buf == NULL) {
				phon_out_size = 0;
				return "";
			} else
				phon_out_buf = new_phon_out_buf;
		}

		phon_buf[len] = 0;
		strcpy(&phon_out_buf[phon_out_ix], phon_buf);
		phon_out_ix += len;
	}

	if (!phon_out_buf)
		return "";

	phon_out_buf[phon_out_ix] = 0;

	return phon_out_buf;
}

static int IsLetterGroup(Translator *tr, char *word, int group, int pre)
{
	// match the word against a list of utf-8 strings
	char *p;
	char *w;
	int len = 0;

	p = tr->letterGroups[group];
	if (p == NULL)
		return 0;

	while (*p != RULE_GROUP_END) {
		if (pre) {
			len = strlen(p);
			w = word - len + 1;
		} else
			w = word;
		while ((*p == *w) && (*w != 0)) {
			w++;
			p++;
		}
		if (*p == 0) {
			if (pre)
				return len;
			return w-word; // matched a complete string
		}

		while (*p++ != 0) // skip to end of string
			;
	}
	return 0;
}

static int IsLetter(Translator *tr, int letter, int group)
{
	int letter2;

	if (tr->letter_groups[group] != NULL) {
		if (wcschr(tr->letter_groups[group], letter))
			return 1;
		return 0;
	}

	if (group > 7)
		return 0;

	if (tr->letter_bits_offset > 0) {
		if (((letter2 = (letter - tr->letter_bits_offset)) > 0) && (letter2 < 0x100))
			letter = letter2;
		else
			return 0;
	} else if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT))
		return tr->letter_bits[remove_accent[letter-0xc0]] & (1L << group);

	if ((letter >= 0) && (letter < 0x100))
		return tr->letter_bits[letter] & (1L << group);

	return 0;
}

int IsVowel(Translator *tr, int letter)
{
	return IsLetter(tr, letter, LETTERGP_VOWEL2);
}

static int Unpronouncable2(Translator *tr, char *word)
{
	int c;
	int end_flags;
	char ph_buf[N_WORD_PHONEMES];

	ph_buf[0] = 0;
	c = word[-1];
	word[-1] = ' '; // ensure there is a space before the "word"
	end_flags = TranslateRules(tr, word, ph_buf, sizeof(ph_buf), NULL, FLAG_UNPRON_TEST, NULL);
	word[-1] = c;
	if ((end_flags == 0) || (end_flags & SUFX_UNPRON))
		return 1;
	return 0;
}

int Unpronouncable(Translator *tr, char *word, int posn)
{
	/* Determines whether a word in 'unpronouncable', i.e. whether it should
	    be spoken as individual letters.

	    This function may be language specific. This is a generic version.
	 */

	int c;
	int c1 = 0;
	int vowel_posn = 9;
	int index;
	int count;
	ALPHABET *alphabet;

	utf8_in(&c, word);
	if ((tr->letter_bits_offset > 0) && (c < 0x241)) {
		// Latin characters for a language with a non-latin alphabet
		return 0;  // so we can re-translate the word as English
	}

	if (((alphabet = AlphabetFromChar(c)) != NULL)  && (alphabet->offset != tr->letter_bits_offset)) {
		// Character is not in our alphabet
		return 0;
	}

	if (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 1)
		return 0;

	if (((c = *word) == ' ') || (c == 0) || (c == '\''))
		return 0;

	index = 0;
	count = 0;
	for (;;) {
		index += utf8_in(&c, &word[index]);
		if ((c == 0) || (c == ' '))
			break;

		if ((c == '\'') && ((count > 1) || (posn > 0)))
			break; // "tv'" but not "l'"

		if (count == 0)
			c1 = c;

		if ((c == '\'') && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 3)) {
			// don't count apostrophe
		} else
			count++;

		if (IsVowel(tr, c)) {
			vowel_posn = count; // position of the first vowel
			break;
		}

		if ((c != '\'') && !iswalpha2(c))
			return 0;
	}

	if ((vowel_posn > 2) && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 2)) {
		// Lookup unpronounable rules in *_rules
		return Unpronouncable2(tr, word);
	}

	if (c1 == tr->langopts.param[LOPT_UNPRONOUNCABLE])
		vowel_posn--; // disregard this as the initial letter when counting

	if (vowel_posn > (tr->langopts.max_initial_consonants+1))
		return 1; // no vowel, or no vowel in first few letters

	return 0;
}

static int GetVowelStress(Translator *tr, unsigned char *phonemes, signed char *vowel_stress, int *vowel_count, int *stressed_syllable, int control)
{
	// control = 1, set stress to 1 for forced unstressed vowels
	unsigned char phcode;
	PHONEME_TAB *ph;
	unsigned char *ph_out = phonemes;
	int count = 1;
	int max_stress = -1;
	int ix;
	int j;
	int stress = -1;
	int primary_posn = 0;

	vowel_stress[0] = 1;
	while (((phcode = *phonemes++) != 0) && (count < (N_WORD_PHONEMES/2)-1)) {
		if ((ph = phoneme_tab[phcode]) == NULL)
			continue;

		if ((ph->type == phSTRESS) && (ph->program == 0)) {
			// stress marker, use this for the following vowel

			if (phcode == phonSTRESS_PREV) {
				// primary stress on preceeding vowel
				j = count - 1;
				while ((j > 0) && (*stressed_syllable == 0) && (vowel_stress[j] < 4)) {
					if ((vowel_stress[j] != 0) && (vowel_stress[j] != 1)) {
						// don't promote a phoneme which must be unstressed
						vowel_stress[j] = 4;

						if (max_stress < 4) {
							max_stress = 4;
							primary_posn = j;
						}

						/* reduce any preceding primary stress markers */
						for (ix = 1; ix < j; ix++) {
							if (vowel_stress[ix] == 4)
								vowel_stress[ix] = 3;
						}
						break;
					}
					j--;
				}
			} else {
				if ((ph->std_length < 4) || (*stressed_syllable == 0)) {
					stress = ph->std_length;

					if (stress > max_stress)
						max_stress = stress;
				}
			}
			continue;
		}

		if ((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) {
			vowel_stress[count] = (char)stress;
			if ((stress >= 4) && (stress >= max_stress)) {
				primary_posn = count;
				max_stress = stress;
			}

			if ((stress < 0) && (control & 1) && (ph->phflags & phUNSTRESSED))
				vowel_stress[count] = 1; // weak vowel, must be unstressed

			count++;
			stress = -1;
		} else if (phcode == phonSYLLABIC) {
			// previous consonant phoneme is syllablic
			vowel_stress[count] = (char)stress;
			if ((stress == 0) && (control & 1))
				vowel_stress[count++] = 1; // syllabic consonant, usually unstressed
		}

		*ph_out++ = phcode;
	}
	vowel_stress[count] = 1;
	*ph_out = 0;

	// has the position of the primary stress been specified by $1, $2, etc?
	if (*stressed_syllable > 0) {
		if (*stressed_syllable >= count)
			*stressed_syllable = count-1; // the final syllable

		vowel_stress[*stressed_syllable] = 4;
		max_stress = 4;
		primary_posn = *stressed_syllable;
	}

	if (max_stress == 5) {
		// priority stress, replaces any other primary stress marker
		for (ix = 1; ix < count; ix++) {
			if (vowel_stress[ix] == 4) {
				if (tr->langopts.stress_flags & S_PRIORITY_STRESS)
					vowel_stress[ix] = 1;
				else
					vowel_stress[ix] = 3;
			}

			if (vowel_stress[ix] == 5) {
				vowel_stress[ix] = 4;
				primary_posn = ix;
			}
		}
		max_stress = 4;
	}

	*stressed_syllable = primary_posn;
	*vowel_count = count;
	return max_stress;
}

static char stress_phonemes[] = {
	phonSTRESS_D, phonSTRESS_U, phonSTRESS_2, phonSTRESS_3,
	phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC
};

void ChangeWordStress(Translator *tr, char *word, int new_stress)
{
	int ix;
	unsigned char *p;
	int max_stress;
	int vowel_count; // num of vowels + 1
	int stressed_syllable = 0; // position of stressed syllable
	unsigned char phonetic[N_WORD_PHONEMES];
	signed char vowel_stress[N_WORD_PHONEMES/2];

	strcpy((char *)phonetic, word);
	max_stress = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 0);

	if (new_stress >= 4) {
		// promote to primary stress
		for (ix = 1; ix < vowel_count; ix++) {
			if (vowel_stress[ix] >= max_stress) {
				vowel_stress[ix] = new_stress;
				break;
			}
		}
	} else {
		// remove primary stress
		for (ix = 1; ix < vowel_count; ix++) {
			if (vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
				vowel_stress[ix] = new_stress;
		}
	}

	// write out phonemes
	ix = 1;
	p = phonetic;
	while (*p != 0) {
		if ((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC)) {
			if ((vowel_stress[ix] == 0) || (vowel_stress[ix] > 1))
				*word++ = stress_phonemes[(unsigned char)vowel_stress[ix]];

			ix++;
		}
		*word++ = *p++;
	}
	*word = 0;
}

void SetWordStress(Translator *tr, char *output, unsigned int *dictionary_flags, int tonic, int control)
{
	/* Guess stress pattern of word.  This is language specific

	   'output' is used for input and output

	   'dictionary_flags' has bits 0-3   position of stressed vowel (if > 0)
	                                     or unstressed (if == 7) or syllables 1 and 2 (if == 6)
	                          bits 8...  dictionary flags

	   If 'tonic' is set (>= 0), replace highest stress by this value.

	   control:  bit 0   This is an individual symbol, not a word
	            bit 1   Suffix phonemes are still to be added
	 */

	unsigned char phcode;
	unsigned char *p;
	PHONEME_TAB *ph;
	int stress;
	int max_stress;
	int max_stress_input; // any stress specified in the input?
	int vowel_count; // num of vowels + 1
	int ix;
	int v;
	int v_stress;
	int stressed_syllable; // position of stressed syllable
	int max_stress_posn;
	int unstressed_word = 0;
	char *max_output;
	int final_ph;
	int final_ph2;
	int mnem;
	int opt_length;
	int done;
	int stressflags;
	int dflags = 0;
	int first_primary;
	int long_vowel;

	signed char vowel_stress[N_WORD_PHONEMES/2];
	char syllable_weight[N_WORD_PHONEMES/2];
	char vowel_length[N_WORD_PHONEMES/2];
	unsigned char phonetic[N_WORD_PHONEMES];

	static char consonant_types[16] = { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };

	/* stress numbers  STRESS_BASE +
	    0  diminished, unstressed within a word
	    1  unstressed, weak
	    2
	    3  secondary stress
	    4  main stress */

	stressflags = tr->langopts.stress_flags;

	if (dictionary_flags != NULL)
		dflags = dictionary_flags[0];

	// copy input string into internal buffer
	for (ix = 0; ix < N_WORD_PHONEMES; ix++) {
		phonetic[ix] = output[ix];
		// check for unknown phoneme codes
		if (phonetic[ix] >= n_phoneme_tab)
			phonetic[ix] = phonSCHWA;
		if (phonetic[ix] == 0)
			break;
	}
	if (ix == 0) return;
	final_ph = phonetic[ix-1];
	final_ph2 =  phonetic[ix-2];

	max_output = output + (N_WORD_PHONEMES-3); // check for overrun

	// any stress position marked in the xx_list dictionary ?
	stressed_syllable = dflags & 0x7;
	if (dflags & 0x8) {
		// this indicates a word without a primary stress
		stressed_syllable = dflags & 0x3;
		unstressed_word = 1;
	}

	max_stress = max_stress_input = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 1);
	if ((max_stress < 0) && dictionary_flags)
		max_stress = 0;

	// heavy or light syllables
	ix = 1;
	for (p = phonetic; *p != 0; p++) {
		if ((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC)) {
			int weight = 0;
			int lengthened = 0;

			if (phoneme_tab[p[1]]->code == phonLENGTHEN)
				lengthened = 1;

			if (lengthened || (phoneme_tab[p[0]]->phflags & phLONG)) {
				// long vowel, increase syllable weight
				weight++;
			}
			vowel_length[ix] = weight;

			if (lengthened) p++; // advance over phonLENGTHEN

			if (consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG))) {
				// followed by two consonants, a long consonant, or consonant and end-of-word
				weight++;
			}
			syllable_weight[ix] = weight;
			ix++;
		}
	}

	switch (tr->langopts.stress_rule)
	{
	case 8:
		// stress on first syllable, unless it is a light syllable followed by a heavy syllable
		if ((syllable_weight[1] > 0) || (syllable_weight[2] == 0))
			break;
		// fallthrough:
	case 1:
		// stress on second syllable
		if ((stressed_syllable == 0) && (vowel_count > 2)) {
			stressed_syllable = 2;
			if (max_stress == 0)
				vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	case 10:  // penultimate, but final if only 1 or 2 syllables
		if (stressed_syllable == 0) {
			if (vowel_count < 4) {
				vowel_stress[vowel_count - 1] = 4;
				max_stress = 4;
				break;
			}
		}
		// fallthrough:
	case 2:
		// a language with stress on penultimate vowel

		if (stressed_syllable == 0) {
			// no explicit stress - stress the penultimate vowel
			max_stress = 4;

			if (vowel_count > 2) {
				stressed_syllable = vowel_count - 2;

				if (stressflags & S_FINAL_SPANISH) {
					// LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
					if (phoneme_tab[final_ph]->type != phVOWEL) {
						mnem = phoneme_tab[final_ph]->mnemonic;

						if (tr->translator_name == L('a', 'n')) {
							if (((mnem != 's') && (mnem != 'n')) || phoneme_tab[final_ph2]->type != phVOWEL)
								stressed_syllable = vowel_count - 1; // stress on last syllable
						} else if (tr->translator_name == L('i', 'a')) {
							if ((mnem != 's') || phoneme_tab[final_ph2]->type != phVOWEL)
								stressed_syllable = vowel_count - 1; // stress on last syllable
						} else {
							if ((mnem == 's') && (phoneme_tab[final_ph2]->type == phNASAL)) {
								// -ns  stress remains on penultimate syllable
							} else if (((phoneme_tab[final_ph]->type != phNASAL) && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL))
								stressed_syllable = vowel_count - 1;
						}
					}
				}

				if (stressflags & S_FINAL_LONG) {
					// stress on last syllable if it has a long vowel, but previous syllable has a short vowel
					if (vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2])
						stressed_syllable = vowel_count - 1;
				}

				if ((vowel_stress[stressed_syllable] == 0) || (vowel_stress[stressed_syllable] == 1)) {
					// but this vowel is explicitly marked as unstressed
					if (stressed_syllable > 1)
						stressed_syllable--;
					else
						stressed_syllable++;
				}
			} else
				stressed_syllable = 1;

			// only set the stress if it's not already marked explicitly
			if (vowel_stress[stressed_syllable] < 0) {
				// don't stress if next and prev syllables are stressed
				if ((vowel_stress[stressed_syllable-1] < 4) || (vowel_stress[stressed_syllable+1] < 4))
					vowel_stress[stressed_syllable] = max_stress;
			}
		}
		break;
	case 3:
		// stress on last vowel
		if (stressed_syllable == 0) {
			// no explicit stress - stress the final vowel
			stressed_syllable = vowel_count - 1;

			while (stressed_syllable > 0) {
				// find the last vowel which is not unstressed
				if (vowel_stress[stressed_syllable] < 0) {
					vowel_stress[stressed_syllable] = 4;
					break;
				} else
					stressed_syllable--;
			}
			max_stress = 4;
		}
		break;
	case 4: // stress on antipenultimate vowel
		if (stressed_syllable == 0) {
			stressed_syllable = vowel_count - 3;
			if (stressed_syllable < 1)
				stressed_syllable = 1;

			if (max_stress == 0)
				vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	case 5:
		// LANG=Russian
		if (stressed_syllable == 0) {
			// no explicit stress - guess the stress from the number of syllables
			static char guess_ru[16] =   { 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11 };
			static char guess_ru_v[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10 }; // for final phoneme is a vowel
			static char guess_ru_t[16] = { 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 10 }; // for final phoneme is an unvoiced stop

			stressed_syllable = vowel_count - 3;
			if (vowel_count < 16) {
				if (phoneme_tab[final_ph]->type == phVOWEL)
					stressed_syllable = guess_ru_v[vowel_count];
				else if (phoneme_tab[final_ph]->type == phSTOP)
					stressed_syllable = guess_ru_t[vowel_count];
				else
					stressed_syllable = guess_ru[vowel_count];
			}
			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	case 6: // LANG=hi stress on the last heaviest syllable
		if (stressed_syllable == 0) {
			int wt;
			int max_weight = -1;

			// find the heaviest syllable, excluding the final syllable
			for (ix = 1; ix < (vowel_count-1); ix++) {
				if (vowel_stress[ix] < 0) {
					if ((wt = syllable_weight[ix]) >= max_weight) {
						max_weight = wt;
						stressed_syllable = ix;
					}
				}
			}

			if ((syllable_weight[vowel_count-1] == 2) &&  (max_weight < 2)) {
				// the only double=heavy syllable is the final syllable, so stress this
				stressed_syllable = vowel_count-1;
			} else if (max_weight <= 0) {
				// all syllables, exclusing the last, are light. Stress the first syllable
				stressed_syllable = 1;
			}

			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	case 7: // LANG=tr, the last syllable for any vowel marked explicitly as unstressed
		if (stressed_syllable == 0) {
			stressed_syllable = vowel_count - 1;
			for (ix = 1; ix < vowel_count; ix++) {
				if (vowel_stress[ix] == 1) {
					stressed_syllable = ix-1;
					break;
				}
			}
			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	case 9: // mark all as stressed
		for (ix = 1; ix < vowel_count; ix++) {
			if (vowel_stress[ix] < 0)
				vowel_stress[ix] = 4;
		}
		break;
	case 12: // LANG=kl (Greenlandic)
		long_vowel = 0;
		for (ix = 1; ix < vowel_count; ix++) {
			if (vowel_stress[ix] == 4)
				vowel_stress[ix] = 3; // change marked stress (consonant clusters) to secondary (except the last)

			if (vowel_length[ix] > 0) {
				long_vowel = ix;
				vowel_stress[ix] = 3; // give secondary stress to all long vowels
			}
		}

		// 'stressed_syllable' gives the last marked stress
		if (stressed_syllable == 0) {
			// no marked stress, choose the last long vowel
			if (long_vowel > 0)
				stressed_syllable = long_vowel;
			else {
				// no long vowels or consonant clusters
				if (vowel_count > 5)
					stressed_syllable = vowel_count - 3; // more than 4 syllables
				else
					stressed_syllable = vowel_count - 1;
			}
		}
		vowel_stress[stressed_syllable] = 4;
		max_stress = 4;
		break;
	case 13: // LANG=ml, 1st unless 1st vowel is short and 2nd is long
		if (stressed_syllable == 0) {
			stressed_syllable = 1;
			if ((vowel_length[1] == 0) && (vowel_count > 2) && (vowel_length[2] > 0))
				stressed_syllable = 2;
			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;
	}

	if ((stressflags & S_FINAL_VOWEL_UNSTRESSED) && ((control & 2) == 0) && (vowel_count > 2) && (max_stress_input < 3) && (vowel_stress[vowel_count - 1] == 4)) {
		// Don't allow stress on a word-final vowel
		// Only do this if there is no suffix phonemes to be added, and if a stress position was not given explicitly
		if (phoneme_tab[final_ph]->type == phVOWEL) {
			vowel_stress[vowel_count - 1] = 1;
			vowel_stress[vowel_count - 2] = 4;
		}
	}

	// now guess the complete stress pattern
	if (max_stress < 4)
		stress = 4; // no primary stress marked, use for 1st syllable
	else
		stress = 3;

	if (unstressed_word == 0) {
		if ((stressflags & S_2_SYL_2) && (vowel_count == 3)) {
			// Two syllable word, if one syllable has primary stress, then give the other secondary stress
			if (vowel_stress[1] == 4)
				vowel_stress[2] = 3;
			if (vowel_stress[2] == 4)
				vowel_stress[1] = 3;
		}

		if ((stressflags & S_INITIAL_2) && (vowel_stress[1] < 0)) {
			// If there is only one syllable before the primary stress, give it a secondary stress
			if ((vowel_count > 3) && (vowel_stress[2] >= 4))
				vowel_stress[1] = 3;
		}
	}

	done = 0;
	first_primary = 0;
	for (v = 1; v < vowel_count; v++) {
		if (vowel_stress[v] < 0) {
			if ((stressflags & S_FINAL_NO_2) && (stress < 4) && (v == vowel_count-1)) {
				// flag: don't give secondary stress to final vowel
			} else if ((stressflags & 0x8000) && (done == 0)) {
				vowel_stress[v] = (char)stress;
				done = 1;
				stress = 3; // use secondary stress for remaining syllables
			} else if ((vowel_stress[v-1] <= 1) && ((vowel_stress[v+1] <= 1) || ((stress == 4) && (vowel_stress[v+1] <= 2)))) {
				// trochaic: give stress to vowel surrounded by unstressed vowels

				if ((stress == 3) && (stressflags & S_NO_AUTO_2))
					continue; // don't use secondary stress

				if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0) && (syllable_weight[v+1] > 0)) {
					// don't put secondary stress on a light syllable which is followed by a heavy syllable
					continue;
				}

				// should start with secondary stress on the first syllable, or should it count back from
				// the primary stress and put secondary stress on alternate syllables?
				vowel_stress[v] = (char)stress;
				done = 1;
				stress = 3; // use secondary stress for remaining syllables
			}
		}

		if (vowel_stress[v] >= 4) {
			if (first_primary == 0)
				first_primary = v;
			else if (stressflags & S_FIRST_PRIMARY) {
				// reduce primary stresses after the first to secondary
				vowel_stress[v] = 3;
			}
		}
	}

	if ((unstressed_word) && (tonic < 0)) {
		if (vowel_count <= 2)
			tonic = tr->langopts.unstressed_wd1; // monosyllable - unstressed
		else
			tonic = tr->langopts.unstressed_wd2; // more than one syllable, used secondary stress as the main stress
	}

	max_stress = 0;
	max_stress_posn = 0;
	for (v = 1; v < vowel_count; v++) {
		if (vowel_stress[v] >= max_stress) {
			max_stress = vowel_stress[v];
			max_stress_posn = v;
		}
	}

	if (tonic >= 0) {
		// find position of highest stress, and replace it by 'tonic'

		// don't disturb an explicitly set stress by 'unstress-at-end' flag
		if ((tonic > max_stress) || (max_stress <= 4))
			vowel_stress[max_stress_posn] = (char)tonic;
		max_stress = tonic;
	}

	// produce output phoneme string
	p = phonetic;
	v = 1;

	if (!(control & 1) && ((ph = phoneme_tab[*p]) != NULL)) {
		while ((ph->type == phSTRESS) || (*p == phonEND_WORD)) {
			p++;
			ph = phoneme_tab[p[0]];
		}

		if ((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL)) {
			// word starts with a vowel

			if ((tr->langopts.vowel_pause & 0x20) && (vowel_stress[1] >= 4))
				*output++ = phonPAUSE_NOLINK; // not to be replaced by link
			else
				*output++ = phonPAUSE_VSHORT; // break, but no pause
		}
	}

	p = phonetic;
	while (((phcode = *p++) != 0) && (output < max_output)) {
		if ((ph = phoneme_tab[phcode]) == NULL)
			continue;

		if (ph->type == phPAUSE)
			tr->prev_last_stress = 0;
		else if (((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC)) {
			// a vowel, or a consonant followed by a syllabic consonant marker

			v_stress = vowel_stress[v];
			tr->prev_last_stress = v_stress;

			if (v_stress <= 1) {
				if ((v > 1) && (max_stress >= 2) && (stressflags & S_FINAL_DIM) && (v == (vowel_count-1))) {
					// option: mark unstressed final syllable as diminished
					v_stress = 0;
				} else if ((stressflags & S_NO_DIM) || (v == 1) || (v == (vowel_count-1))) {
					// first or last syllable, or option 'don't set diminished stress'
					v_stress = 1;
				} else if ((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= 1)) {
					// penultimate syllable, followed by an unstressed final syllable
					v_stress = 1;
				} else {
					// unstressed syllable within a word
					if ((vowel_stress[v-1] < 0) || ((stressflags & S_MID_DIM) == 0)) {
						v_stress = 0; // change to 0 (diminished stress)
						vowel_stress[v] = v_stress;
					}
				}
			}

			if ((v_stress == 0) || (v_stress > 1))
				*output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 1 (unstressed)

			if (vowel_stress[v] > max_stress)
				max_stress = vowel_stress[v];

			if ((*p == phonLENGTHEN) && ((opt_length = tr->langopts.param[LOPT_IT_LENGTHEN]) & 1)) {
				// remove lengthen indicator from non-stressed syllables
				int shorten = 0;

				if (opt_length & 0x10) {
					// only allow lengthen indicator on the highest stress syllable in the word
					if (v != max_stress_posn)
						shorten = 1;
				} else if (v_stress < 4) {
					// only allow lengthen indicator if stress >= 4.
					shorten = 1;
				}

				if (shorten)
					p++;
			}
			v++;
		}

		if (phcode != 1)
			*output++ = phcode;
	}
	*output++ = 0;

	return;
}

void AppendPhonemes(Translator *tr, char *string, int size, const char *ph)
{
	/* Add new phoneme string "ph" to "string"
	    Keeps count of the number of vowel phonemes in the word, and whether these
	   can be stressed syllables.  These values can be used in translation rules
	 */

	const char *p;
	unsigned char c;
	int unstress_mark;
	int length;

	length = strlen(ph) + strlen(string);
	if (length >= size)
		return;

	// any stressable vowel ?
	unstress_mark = 0;
	p = ph;
	while ((c = *p++) != 0) {
		if (c >= n_phoneme_tab) continue;

		if (phoneme_tab[c]->type == phSTRESS) {
			if (phoneme_tab[c]->std_length < 4)
				unstress_mark = 1;
		} else {
			if (phoneme_tab[c]->type == phVOWEL) {
				if (((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
				    (unstress_mark == 0)) {
					tr->word_stressed_count++;
				}
				unstress_mark = 0;
				tr->word_vowel_count++;
			}
		}
	}

	if (string != NULL)
		strcat(string, ph);
}

static void MatchRule(Translator *tr, char *word[], char *word_start, int group_length, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
{
	/* Checks a specified word against dictionary rules.
	    Returns with phoneme code string, or NULL if no match found.

	    word (indirect) points to current character group within the input word
	            This is advanced by this procedure as characters are consumed

	    group:  the initial characters used to choose the rules group

	    rule:  address of dictionary rule data for this character group

	    match_out:  returns best points score

	    word_flags:  indicates whether this is a retranslation after a suffix has been removed
	 */

	unsigned char rb;     // current instuction from rule
	unsigned char letter; // current letter from input word, single byte
	int letter_w;         // current letter, wide character
	int letter_xbytes;    // number of extra bytes of multibyte character (num bytes - 1)
	unsigned char last_letter;

	char *pre_ptr;
	char *post_ptr;       // pointer to first character after group

	char *rule_start;     // start of current match template
	char *p;
	int ix;

	int match_type;       // left, right, or consume
	int failed;
	int unpron_ignore;
	int consumed;         // number of letters consumed from input
	int syllable_count;
	int vowel;
	int letter_group;
	int distance_right;
	int distance_left;
	int lg_pts;
	int n_bytes;
	int add_points;
	int command;
	int check_atstart;
	unsigned int *flags;

	MatchRecord match;
	static MatchRecord best;

	int total_consumed; // letters consumed for best match

	unsigned char condition_num;
	char *common_phonemes; // common to a group of entries
	char *group_chars;
	char word_buf[N_WORD_BYTES];

	group_chars = *word;

	if (rule == NULL) {
		match_out->points = 0;
		(*word)++;
		return;
	}

	total_consumed = 0;
	common_phonemes = NULL;

	best.points = 0;
	best.phonemes = "";
	best.end_type = 0;
	best.del_fwd = NULL;

	// search through dictionary rules
	while (rule[0] != RULE_GROUP_END) {
		unpron_ignore = word_flags & FLAG_UNPRON_TEST;
		match_type = 0;
		consumed = 0;
		letter = 0;
		distance_right = -6; // used to reduce points for matches further away the current letter
		distance_left = -2;
		check_atstart = 0;

		match.points = 1;
		match.end_type = 0;
		match.del_fwd = NULL;

		pre_ptr = *word;
		post_ptr = *word + group_length;

		// work through next rule until end, or until no-match proved
		rule_start = rule;

		failed = 0;
		while (!failed) {
			rb = *rule++;

			if (rb <= RULE_LINENUM) {
				switch (rb)
				{
				case 0: // no phoneme string for this rule, use previous common rule
					if (common_phonemes != NULL) {
						match.phonemes = common_phonemes;
						while (((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES)) {
							if (rb == RULE_CONDITION)
								match.phonemes++; // skip over condition number
							if (rb == RULE_LINENUM)
								match.phonemes += 2; // skip over line number
						}
					} else
						match.phonemes = "";
					rule--; // so we are still pointing at the 0
					failed = 2; // matched OK
					break;
				case RULE_PRE_ATSTART: // pre rule with implied 'start of word'
					check_atstart = 1;
					unpron_ignore = 0;
					match_type = RULE_PRE;
					break;
				case RULE_PRE:
					match_type = RULE_PRE;
					if (word_flags & FLAG_UNPRON_TEST) {
						// checking the start of the word for unpronouncable character sequences, only
						// consider rules which explicitly match the start of a word
						// Note: Those rules now use RULE_PRE_ATSTART
						failed = 1;
					}
					break;
				case RULE_POST:
					match_type = RULE_POST;
					break;
				case RULE_PHONEMES:
					match.phonemes = rule;
					failed = 2; // matched OK
					break;
				case RULE_PH_COMMON:
					common_phonemes = rule;
					break;
				case RULE_CONDITION:
					// conditional rule, next byte gives condition number
					condition_num = *rule++;

					if (condition_num >= 32) {
						// allow the rule only if the condition number is NOT set
						if ((tr->dict_condition & (1L << (condition_num-32))) != 0)
							failed = 1;
					} else {
						// allow the rule only if the condition number is set
						if ((tr->dict_condition & (1L << condition_num)) == 0)
							failed = 1;
					}

					if (!failed)
						match.points++; // add one point for a matched conditional rule
					break;
				case RULE_LINENUM:
					rule += 2;
					break;
				}
				continue;
			}

			add_points = 0;

			switch (match_type)
			{
			case 0:
				// match and consume this letter
				letter = *post_ptr++;

				if ((letter == rb) || ((letter == (unsigned char)REPLACED_E) && (rb == 'e'))) {
					if ((letter & 0xc0) != 0x80)
						add_points = 21; // don't add point for non-initial UTF-8 bytes
					consumed++;
				} else
					failed = 1;
				break;
			case RULE_POST:
				// continue moving fowards
				distance_right += 6;
				if (distance_right > 18)
					distance_right = 19;
				last_letter = letter;
				letter_xbytes = utf8_in(&letter_w, post_ptr)-1;
				letter = *post_ptr++;

				switch (rb)
				{
				case RULE_LETTERGP:
					letter_group = *rule++ - 'A';
					if (IsLetter(tr, letter_w, letter_group)) {
						lg_pts = 20;
						if (letter_group == 2)
							lg_pts = 19; // fewer points for C, general consonant
						add_points = (lg_pts-distance_right);
						post_ptr += letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_LETTERGP2: // match against a list of utf-8 strings
					letter_group = *rule++ - 'A';
					if ((n_bytes = IsLetterGroup(tr, post_ptr-1, letter_group, 0)) > 0) {
						add_points = (20-distance_right);
						post_ptr += (n_bytes-1);
					} else
						failed = 1;
					break;
				case RULE_NOTVOWEL:
					if (IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL)))
						failed = 1;
					else {
						add_points = (20-distance_right);
						post_ptr += letter_xbytes;
					}
					break;
				case RULE_DIGIT:
					if (IsDigit(letter_w)) {
						add_points = (20-distance_right);
						post_ptr += letter_xbytes;
					} else if (tr->langopts.tone_numbers) {
						// also match if there is no digit
						add_points = (20-distance_right);
						post_ptr--;
					} else
						failed = 1;
					break;
				case RULE_NONALPHA:
					if (!iswalpha2(letter_w)) {
						add_points = (21-distance_right);
						post_ptr += letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_DOUBLE:
					if (letter == last_letter)
						add_points = (21-distance_right);
					else
						failed = 1;
					break;
				case RULE_DOLLAR:
					command = *rule++;
					if (command == DOLLAR_UNPR)
						match.end_type = SUFX_UNPRON; // $unpron
					else if (command == DOLLAR_NOPREFIX) { // $noprefix
						if (word_flags & FLAG_PREFIX_REMOVED)
							failed = 1; // a prefix has been removed
						else
							add_points = 1;
					} else if ((command & 0xf0) == 0x10) {
						// $w_alt
						if (dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
							add_points = 23;
						else
							failed = 1;
					} else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) {
						// $list or $p_alt
						// make a copy of the word up to the post-match characters
						ix = *word - word_start + consumed + group_length + 1;
						memcpy(word_buf, word_start-1, ix);
						word_buf[ix] = ' ';
						word_buf[ix+1] = 0;
						LookupFlags(tr, &word_buf[1], &flags);

						if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
							add_points = 23;
						else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
							add_points = 23;
						else
							failed = 1;
					}
					break;
				case '-':
					if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
						add_points = (22-distance_right); // one point more than match against space
					else
						failed = 1;
					break;
				case RULE_SYLLABLE:
				{
					// more than specified number of vowel letters to the right
					char *p = post_ptr + letter_xbytes;
					int vowel_count = 0;

					syllable_count = 1;
					while (*rule == RULE_SYLLABLE) {
						rule++;
						syllable_count += 1; // number of syllables to match
					}
					vowel = 0;
					while (letter_w != RULE_SPACE) {
						if ((vowel == 0) && IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
							// this is counting vowels which are separated by non-vowel letters
							vowel_count++;
						}
						vowel = IsLetter(tr, letter_w, LETTERGP_VOWEL2);
						p += utf8_in(&letter_w, p);
					}
					if (syllable_count <= vowel_count)
						add_points = (18+syllable_count-distance_right);
					else
						failed = 1;
				}
					break;
				case RULE_NOVOWELS:
				{
					char *p = post_ptr + letter_xbytes;
					while (letter_w != RULE_SPACE) {
						if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
							failed = 1;
							break;
						}
						p += utf8_in(&letter_w, p);
					}
					if (!failed)
						add_points = (19-distance_right);
				}
					break;
				case RULE_SKIPCHARS:
				{
					// Used for lang=Tamil, used to match on the next word after an unknown word ending
					// only look until the end of the word (including the end-of-word marker)
					// Jx  means 'skip characters until x', where 'x' may be '_' for 'end of word'
					char *p = post_ptr + letter_xbytes;
					char *p2 = p;
					int rule_w; // skip characters until this
					utf8_in(&rule_w, rule);
					while ((letter_w != rule_w) && (letter_w != RULE_SPACE)) {
						p2 = p;
						p += utf8_in(&letter_w, p);
					}
					if (letter_w == rule_w)
						post_ptr = p2;
				}
					break;
				case RULE_INC_SCORE:
					add_points = 20; // force an increase in points
					break;
				case RULE_DEL_FWD:
					// find the next 'e' in the word and replace by 'E'
					for (p = *word + group_length; p < post_ptr; p++) {
						if (*p == 'e') {
							match.del_fwd = p;
							break;
						}
					}
					break;
				case RULE_ENDING:
				{
					int end_type;
					// next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
					end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);

					if ((tr->word_vowel_count == 0) && !(end_type & SUFX_P) && (tr->langopts.param[LOPT_SUFFIX] & 1))
						failed = 1; // don't match a suffix rule if there are no previous syllables (needed for lang=tr).
					else {
						match.end_type = end_type;
						rule += 3;
					}
				}
					break;
				case RULE_NO_SUFFIX:
					if (word_flags & FLAG_SUFFIX_REMOVED)
						failed = 1; // a suffix has been removed
					else
						add_points = 1;
					break;
				default:
					if (letter == rb) {
						if ((letter & 0xc0) != 0x80) {
							// not for non-initial UTF-8 bytes
							add_points = (21-distance_right);
						}
					} else
						failed = 1;
					break;
				}
				break;
			case RULE_PRE:
				// match backwards from start of current group
				distance_left += 2;
				if (distance_left > 18)
					distance_left = 19;

				last_letter = *pre_ptr;
				pre_ptr--;
				letter_xbytes = utf8_in2(&letter_w, pre_ptr, 1)-1;
				letter = *pre_ptr;

				switch (rb)
				{
				case RULE_LETTERGP:
					letter_group = *rule++ - 'A';
					if (IsLetter(tr, letter_w, letter_group)) {
						lg_pts = 20;
						if (letter_group == 2)
							lg_pts = 19; // fewer points for C, general consonant
						add_points = (lg_pts-distance_left);
						pre_ptr -= letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_LETTERGP2: // match against a list of utf-8 strings
					letter_group = *rule++ - 'A';
					if ((n_bytes = IsLetterGroup(tr, pre_ptr, letter_group, 1)) > 0) {
						add_points = (20-distance_right);
						pre_ptr -= (n_bytes-1);
					} else
						failed = 1;
					break;
				case RULE_NOTVOWEL:
					if (!IsLetter(tr, letter_w, 0)) {
						add_points = (20-distance_left);
						pre_ptr -= letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_DOUBLE:
					if (letter == last_letter)
						add_points = (21-distance_left);
					else
						failed = 1;
					break;
				case RULE_DIGIT:
					if (IsDigit(letter_w)) {
						add_points = (21-distance_left);
						pre_ptr -= letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_NONALPHA:
					if (!iswalpha2(letter_w)) {
						add_points = (21-distance_right);
						pre_ptr -= letter_xbytes;
					} else
						failed = 1;
					break;
				case RULE_DOLLAR:
					command = *rule++;
					if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) {
						// $list or $p_alt
						// make a copy of the word up to the current character
						ix = *word - word_start + 1;
						memcpy(word_buf, word_start-1, ix);
						word_buf[ix] = ' ';
						word_buf[ix+1] = 0;
						LookupFlags(tr, &word_buf[1], &flags);

						if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
							add_points = 23;
						else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
							add_points = 23;
						else
							failed = 1;
					}
					break;
				case RULE_SYLLABLE:
					// more than specified number of vowels to the left
					syllable_count = 1;
					while (*rule == RULE_SYLLABLE) {
						rule++;
						syllable_count++; // number of syllables to match
					}
					if (syllable_count <= tr->word_vowel_count)
						add_points = (18+syllable_count-distance_left);
					else
						failed = 1;
					break;
				case RULE_STRESSED:
					if (tr->word_stressed_count > 0)
						add_points = 19;
					else
						failed = 1;
					break;
				case RULE_NOVOWELS:
				{
					char *p = pre_ptr - letter_xbytes - 1;
					while (letter_w != RULE_SPACE) {
						if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
							failed = 1;
							break;
						}
						p -= utf8_in2(&letter_w, p, 1);
					}
					if (!failed)
						add_points = 3;
				}
					break;
				case RULE_IFVERB:
					if (tr->expect_verb)
						add_points = 1;
					else
						failed = 1;
					break;
				case RULE_CAPITAL:
					if (word_flags & FLAG_FIRST_UPPER)
						add_points = 1;
					else
						failed = 1;
					break;
				case '.':
					// dot in pre- section, match on any dot before this point in the word
					for (p = pre_ptr; *p != ' '; p--) {
						if (*p == '.') {
							add_points = 50;
							break;
						}
					}
					if (*p == ' ')
						failed = 1;
					break;
				case '-':
					if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
						add_points = (22-distance_right); // one point more than match against space
					else
						failed = 1;
					break;
				default:
					if (letter == rb) {
						if (letter == RULE_SPACE)
							add_points = 4;
						else if ((letter & 0xc0) != 0x80) {
							// not for non-initial UTF-8 bytes
							add_points = (21-distance_left);
						}
					} else
						failed = 1;
					break;
				}
				break;
			}

			if (failed == 0)
				match.points += add_points;
		}

		if ((failed == 2) && (unpron_ignore == 0)) {
			// do we also need to check for 'start of word' ?
			if ((check_atstart == 0) || (pre_ptr[-1] == ' ')) {
				if (check_atstart)
					match.points += 4;

				// matched OK, is this better than the last best match ?
				if (match.points >= best.points) {
					memcpy(&best, &match, sizeof(match));
					total_consumed = consumed;
				}

				if ((option_phonemes & espeakPHONEMES_TRACE) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0)) {
					// show each rule that matches, and it's points score
					int pts;
					char decoded_phonemes[80];

					pts = match.points;
					if (group_length > 1)
						pts += 35; // to account for an extra letter matching
					DecodePhonemes(match.phonemes, decoded_phonemes);
					fprintf(f_trans, "%3d\t%s [%s]\n", pts, DecodeRule(group_chars, group_length, rule_start, word_flags), decoded_phonemes);
				}
			}
		}

		// skip phoneme string to reach start of next template
		while (*rule++ != 0) ;
	}

	// advance input data pointer
	total_consumed += group_length;
	if (total_consumed == 0)
		total_consumed = 1; // always advance over 1st letter

	*word += total_consumed;

	if (best.points == 0)
		best.phonemes = "";
	memcpy(match_out, &best, sizeof(MatchRecord));
}

int TranslateRules(Translator *tr, char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, unsigned int *dict_flags)
{
	/* Translate a word bounded by space characters
	   Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */

	unsigned char c, c2;
	unsigned int c12;
	int wc = 0;
	int wc_bytes;
	char *p2;           // copy of p for use in double letter chain match
	int found;
	int g;              // group chain number
	int g1;             // first group for this letter
	int n;
	int letter;
	int any_alpha = 0;
	int ix;
	unsigned int digit_count = 0;
	char *p;
	ALPHABET *alphabet;
	int dict_flags0 = 0;
	MatchRecord match1;
	MatchRecord match2;
	char ph_buf[40];
	char word_copy[N_WORD_BYTES];
	static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };

	if (tr->data_dictrules == NULL)
		return 0;

	if (dict_flags != NULL)
		dict_flags0 = dict_flags[0];

	for (ix = 0; ix < (N_WORD_BYTES-1);) {
		c = p_start[ix];
		word_copy[ix++] = c;
		if (c == 0)
			break;
	}
	word_copy[ix] = 0;

	if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0)) {
		char wordbuf[120];
		unsigned int ix;

		for (ix = 0; ((c = p_start[ix]) != ' ') && (c != 0) && (ix < (sizeof(wordbuf)-1)); ix++)
			wordbuf[ix] = c;
		wordbuf[ix] = 0;
		if (word_flags & FLAG_UNPRON_TEST)
			fprintf(f_trans, "Unpronouncable? '%s'\n", wordbuf);
		else
			fprintf(f_trans, "Translate '%s'\n", wordbuf);
	}

	p = p_start;
	tr->word_vowel_count = 0;
	tr->word_stressed_count = 0;

	if (end_phonemes != NULL)
		end_phonemes[0] = 0;

	while (((c = *p) != ' ') && (c != 0)) {
		wc_bytes = utf8_in(&wc, p);
		if (IsAlpha(wc))
			any_alpha++;

		n = tr->groups2_count[c];
		if (IsDigit(wc) && ((tr->langopts.tone_numbers == 0) || !any_alpha)) {
			// lookup the number in *_list not *_rules
			char string[8];
			char buf[40];
			string[0] = '_';
			memcpy(&string[1], p, wc_bytes);
			string[1+wc_bytes] = 0;
			Lookup(tr, string, buf);
			if (++digit_count >= 2) {
				strcat(buf, str_pause);
				digit_count = 0;
			}
			AppendPhonemes(tr, phonemes, ph_size, buf);
			p += wc_bytes;
			continue;
		} else {
			digit_count = 0;
			found = 0;

			if (((ix = wc - tr->letter_bits_offset) >= 0) && (ix < 128)) {
				if (tr->groups3[ix] != NULL) {
					MatchRule(tr, &p, p_start, wc_bytes, tr->groups3[ix], &match1, word_flags, dict_flags0);
					found = 1;
				}
			}

			if (!found && (n > 0)) {
				// there are some 2 byte chains for this initial letter
				c2 = p[1];
				c12 = c + (c2 << 8); // 2 characters

				g1 = tr->groups2_start[c];
				for (g = g1; g < (g1+n); g++) {
					if (tr->groups2_name[g] == c12) {
						found = 1;

						p2 = p;
						MatchRule(tr, &p2, p_start, 2, tr->groups2[g], &match2, word_flags, dict_flags0);
						if (match2.points > 0)
							match2.points += 35; // to acount for 2 letters matching

						// now see whether single letter chain gives a better match ?
						MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);

						if (match2.points >= match1.points) {
							// use match from the 2-letter group
							memcpy(&match1, &match2, sizeof(MatchRecord));
							p = p2;
						}
					}
				}
			}

			if (!found) {
				// alphabetic, single letter chain
				if (tr->groups1[c] != NULL)
					MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
				else {
					// no group for this letter, use default group
					MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0);

					if ((match1.points == 0) && ((option_sayas & 0x10) == 0)) {
						n = utf8_in(&letter, p-1)-1;

						if (tr->letter_bits_offset > 0) {
							// not a Latin alphabet, switch to the default Latin alphabet language
							if ((letter <= 0x241) && iswalpha2(letter)) {
								sprintf(phonemes, "%c%s", phonSWITCH, tr->langopts.ascii_language);
								return 0;
							}
						}

						// is it a bracket ?
						if (letter == 0xe000+'(') {
							if (pre_pause < tr->langopts.param2[LOPT_BRACKET_PAUSE])
								pre_pause = tr->langopts.param2[LOPT_BRACKET_PAUSE]; // a bracket, aleady spoken by AnnouncePunctuation()
						}
						if (IsBracket(letter)) {
							if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE])
								pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE];
						}

						// no match, try removing the accent and re-translating the word
						if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0)) {
							// within range of the remove_accent table
							if ((p[-2] != ' ') || (p[n] != ' ')) {
								// not the only letter in the word
								p2 = p-1;
								p[-1] = ix;
								while ((p[0] = p[n]) != ' ')  p++;
								while (n-- > 0) *p++ = ' '; // replacement character must be no longer than original

								if (tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list, letter) > 0)) {
									// vowel with dieresis, replace and continue from this point
									p = p2;
									continue;
								}

								phonemes[0] = 0; // delete any phonemes which have been produced so far
								p = p_start;
								tr->word_vowel_count = 0;
								tr->word_stressed_count = 0;
								continue; // start again at the beginning of the word
							}
						}

						if (((alphabet = AlphabetFromChar(letter)) != NULL)  && (alphabet->offset != tr->letter_bits_offset)) {
							if (tr->langopts.alt_alphabet == alphabet->offset) {
								sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(tr->langopts.alt_alphabet_lang));
								return 0;
							}
							if (alphabet->flags & AL_WORDS) {
								// switch to the nominated language for this alphabet
								sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(alphabet->language));
								return 0;
							}
						}
					}
				}

				if (match1.points == 0) {
					if ((wc >= 0x300) && (wc <= 0x36f)) {
						// combining accent inside a word, ignore
					} else if (IsAlpha(wc)) {
						if ((any_alpha > 1) || (p[wc_bytes-1] > ' ')) {
							// an unrecognised character in a word, abort and then spell the word
							phonemes[0] = 0;
							if (dict_flags != NULL)
								dict_flags[0] |= FLAG_SPELLWORD;
							break;
						}
					} else {
						LookupLetter(tr, wc, -1, ph_buf, 0);
						if (ph_buf[0]) {
							match1.phonemes = ph_buf;
							match1.points = 1;
						}
					}
					p += (wc_bytes-1);
				} else
					tr->phonemes_repeat_count = 0;
			}
		}

		if (match1.phonemes == NULL)
			match1.phonemes = "";

		if (match1.points > 0) {
			if (word_flags & FLAG_UNPRON_TEST)
				return match1.end_type | 1;

			if ((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR) == 0)) {
				// an instruction to switch language, return immediately so we can re-translate
				strcpy(phonemes, match1.phonemes);
				return 0;
			}

			if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0))
				fprintf(f_trans, "\n");

			match1.end_type &= ~SUFX_UNPRON;

			if ((match1.end_type != 0) && (end_phonemes != NULL)) {
				// a standard ending has been found, re-translate the word without it
				if ((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX)) {
					// ignore the match on a prefix
				} else {
					if ((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0)) {
						// no prefix length specified
						match1.end_type |= p - p_start;
					}
					strcpy(end_phonemes, match1.phonemes);
					memcpy(p_start, word_copy, strlen(word_copy));
					return match1.end_type;
				}
			}
			if (match1.del_fwd != NULL)
				*match1.del_fwd = REPLACED_E;
			AppendPhonemes(tr, phonemes, ph_size, match1.phonemes);
		}
	}

	memcpy(p_start, word_copy, strlen(word_copy));

	return 0;
}

void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags)
{
	// apply after the translation is complete

	int ix;
	int len;
	char *p;

	len = strlen(phonemes);

	if (tr->langopts.param[LOPT_ALT] & 2) {
		for (ix = 0; ix < (len-1); ix++) {
			if (phonemes[ix] == phonSTRESS_P) {
				p = &phonemes[ix+1];
				if ((dict_flags & FLAG_ALT2_TRANS) != 0) {
					if (*p == PhonemeCode('E'))
						*p = PhonemeCode('e');
					if (*p == PhonemeCode('O'))
						*p = PhonemeCode('o');
				} else {
					if (*p == PhonemeCode('e'))
						*p = PhonemeCode('E');
					if (*p == PhonemeCode('o'))
						*p = PhonemeCode('O');
				}
				break;
			}
		}
	}
}

int TransposeAlphabet(Translator *tr, char *text)
{
	// transpose cyrillic alphabet (for example) into ascii (single byte) character codes
	// return: number of bytes, bit 6: 1=used compression

	int c;
	int c2;
	int ix;
	int offset;
	int min;
	int max;
	const char *map;
	char *p = text;
	char *p2;
	int all_alpha = 1;
	int bits;
	int acc;
	int pairs_start;
	const short *pairs_list;
	int bufix;
	char buf[N_WORD_BYTES+1];

	offset = tr->transpose_min - 1;
	min = tr->transpose_min;
	max = tr->transpose_max;
	map = tr->transpose_map;

	pairs_start = max - min + 2;

	bufix = 0;
	do {
		p += utf8_in(&c, p);
		if (c != 0) {
			if ((c >= min) && (c <= max)) {
				if (map == NULL)
					buf[bufix++] = c - offset;
				else {
					// get the code from the transpose map
					if (map[c - min] > 0)
						buf[bufix++] = map[c - min];
					else {
						all_alpha = 0;
						break;
					}
				}
			} else {
				all_alpha = 0;
				break;
			}
		}
	} while ((c != 0) && (bufix < N_WORD_BYTES));
	buf[bufix] = 0;

	if (all_alpha) {
		// compress to 6 bits per character
		acc = 0;
		bits = 0;

		p = buf;
		p2 = buf;
		while ((c = *p++) != 0) {
			if ((pairs_list = tr->frequent_pairs) != NULL) {
				c2 = c + (*p << 8);
				for (ix = 0; c2 >= pairs_list[ix]; ix++) {
					if (c2 == pairs_list[ix]) {
						// found an encoding for a 2-character pair
						c = ix + pairs_start; // 2-character codes start after the single letter codes
						p++;
						break;
					}
				}
			}
			acc = (acc << 6) + (c & 0x3f);
			bits += 6;

			if (bits >= 8) {
				bits -= 8;
				*p2++ = (acc >> bits);
			}
		}
		if (bits > 0)
			*p2++ = (acc << (8-bits));
		*p2 = 0;
		ix = p2 - buf;
		memcpy(text, buf, ix);
		return ix | 0x40; // bit 6 indicates compressed characters
	}
	return strlen(text);
}

/* Find an entry in the word_dict file for a specified word.
   Returns NULL if no match, else returns 'word_end'

    word   zero terminated word to match
    word2  pointer to next word(s) in the input text (terminated by space)

    flags:  returns dictionary flags which are associated with a matched word

    end_flags:  indicates whether this is a retranslation after removing a suffix
 */
static const char *LookupDict2(Translator *tr, const char *word, const char *word2,
                               char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
{
	char *p;
	char *next;
	int hash;
	int phoneme_len;
	int wlen;
	unsigned char flag;
	unsigned int dictionary_flags;
	unsigned int dictionary_flags2;
	int condition_failed = 0;
	int n_chars;
	int no_phonemes;
	int skipwords;
	int ix;
	int c;
	const char *word_end;
	const char *word1;
	int wflags = 0;
	int lookup_symbol;
	char word_buf[N_WORD_BYTES+1];
	char dict_flags_buf[80];

	if (wtab != NULL)
		wflags = wtab->flags;

	lookup_symbol = flags[1] & FLAG_LOOKUP_SYMBOL;
	word1 = word;
	if (tr->transpose_min > 0) {
		strncpy0(word_buf, word, N_WORD_BYTES);
		wlen = TransposeAlphabet(tr, word_buf); // bit 6 indicates compressed characters
		word = word_buf;
	} else
		wlen = strlen(word);

	hash = HashDictionary(word);
	p = tr->dict_hashtab[hash];

	if (p == NULL) {
		if (flags != NULL)
			*flags = 0;
		return 0;
	}

	// Find the first entry in the list for this hash value which matches.
	// This corresponds to the last matching entry in the *_list file.

	while (*p != 0) {
		next = p + p[0];

		if (((p[1] & 0x7f) != wlen) || (memcmp(word, &p[2], wlen & 0x3f) != 0)) {
			// bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
			p = next;
			continue;
		}

		// found matching entry. Decode the phonetic string
		word_end = word2;

		dictionary_flags = 0;
		dictionary_flags2 = 0;
		no_phonemes = p[1] & 0x80;

		p += ((p[1] & 0x3f) + 2);

		if (no_phonemes) {
			phonetic[0] = 0;
			phoneme_len = 0;
		} else {
			strcpy(phonetic, p);
			phoneme_len = strlen(p);
			p += (phoneme_len + 1);
		}

		while (p < next) {
			// examine the flags which follow the phoneme string

			flag = *p++;
			if (flag >= 100) {
				// conditional rule
				if (flag >= 132) {
					// fail if this condition is set
					if ((tr->dict_condition & (1 << (flag-132))) != 0)
						condition_failed = 1;
				} else {
					// allow only if this condition is set
					if ((tr->dict_condition & (1 << (flag-100))) == 0)
						condition_failed = 1;
				}
			} else if (flag > 80) {
				// flags 81 to 90  match more than one word
				// This comes after the other flags
				n_chars = next - p;
				skipwords = flag - 80;

				// don't use the contraction if any of the words are emphasized
				//  or has an embedded command, such as MARK
				if (wtab != NULL) {
					for (ix = 0; ix <= skipwords; ix++) {
						if (wtab[ix].flags & FLAG_EMPHASIZED2)
							condition_failed = 1;
					}
				}

				if (memcmp(word2, p, n_chars) != 0)
					condition_failed = 1;

				if (condition_failed) {
					p = next;
					break;
				}

				dictionary_flags |= FLAG_SKIPWORDS;
				dictionary_skipwords = skipwords;
				p = next;
				word_end = word2 + n_chars;
			} else if (flag > 64) {
				// stressed syllable information, put in bits 0-3
				dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
				if ((flag & 0xc) == 0xc)
					dictionary_flags |= FLAG_STRESS_END;
			} else if (flag >= 32)
				dictionary_flags2 |= (1L << (flag-32));
			else
				dictionary_flags |= (1L << flag);
		}

		if (condition_failed) {
			condition_failed = 0;
			continue;
		}

		if ((end_flags & FLAG_SUFX) == 0) {
			// no suffix has been removed
			if (dictionary_flags2 & FLAG_STEM)
				continue; // this word must have a suffix
		}

		if ((end_flags & SUFX_P) && (dictionary_flags2 & (FLAG_ONLY | FLAG_ONLY_S)))
			continue; // $only or $onlys, don't match if a prefix has been removed

		if (end_flags & FLAG_SUFX) {
			// a suffix was removed from the word
			if (dictionary_flags2 & FLAG_ONLY)
				continue; // no match if any suffix

			if ((dictionary_flags2 & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S) == 0)) {
				// only a 's' suffix allowed, but the suffix wasn't 's'
				continue;
			}
		}

		if (dictionary_flags2 & FLAG_HYPHENATED) {
			if (!(wflags & FLAG_HYPHEN_AFTER))
				continue;
		}
		if (dictionary_flags2 & FLAG_CAPITAL) {
			if (!(wflags & FLAG_FIRST_UPPER))
				continue;
		}
		if (dictionary_flags2 & FLAG_ALLCAPS) {
			if (!(wflags & FLAG_ALL_UPPER))
				continue;
		}
		if (dictionary_flags & FLAG_NEEDS_DOT) {
			if (!(wflags & FLAG_HAS_DOT))
				continue;
		}

		if ((dictionary_flags2 & FLAG_ATEND) && (word_end < translator->clause_end) && (lookup_symbol == 0)) {
			// only use this pronunciation if it's the last word of the clause, or called from Lookup()
			continue;
		}

		if ((dictionary_flags2 & FLAG_ATSTART) && !(wtab->flags & FLAG_FIRST_WORD)) {
			// only use this pronunciation if it's the first word of a clause
			continue;
		}

		if ((dictionary_flags2 & FLAG_SENTENCE) && !(translator->clause_terminator & CLAUSE_BIT_SENTENCE)) {
			// only if this clause is a sentence , i.e. terminator is {. ? !} not {, : :}
			continue;
		}

		if (dictionary_flags2 & FLAG_VERB) {
			// this is a verb-form pronunciation

			if (tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S))) {
				// OK, we are expecting a verb
				if ((tr->translator_name == L('e', 'n')) && (tr->prev_dict_flags[0] & FLAG_ALT7_TRANS) && (end_flags & FLAG_SUFX_S)) {
					// lang=en, don't use verb form after 'to' if the word has 's' suffix
					continue;
				}
			} else {
				// don't use the 'verb' pronunciation unless we are expecting a verb
				continue;
			}
		}
		if (dictionary_flags2 & FLAG_PAST) {
			if (!tr->expect_past) {
				// don't use the 'past' pronunciation unless we are expecting past tense
				continue;
			}
		}
		if (dictionary_flags2 & FLAG_NOUN) {
			if ((!tr->expect_noun) || (end_flags & SUFX_V)) {
				// don't use the 'noun' pronunciation unless we are expecting a noun
				continue;
			}
		}
		if (dictionary_flags2 & FLAG_NATIVE) {
			if (tr != translator)
				continue; // don't use if we've switched translators
		}
		if (dictionary_flags & FLAG_ALT2_TRANS) {
			// language specific
			if ((tr->translator_name == L('h', 'u')) && !(tr->prev_dict_flags[0] & FLAG_ALT_TRANS))
				continue;
		}

		if (flags != NULL) {
			flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES;
			flags[1] = dictionary_flags2;
		}

		if (phoneme_len == 0) {
			if (option_phonemes & espeakPHONEMES_TRACE) {
				print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
				fprintf(f_trans, "Flags:  %s  %s\n", word1, dict_flags_buf);
			}
			return 0; // no phoneme translation found here, only flags. So use rules
		}

		if (flags != NULL)
			flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary

		if (option_phonemes & espeakPHONEMES_TRACE) {
			char ph_decoded[N_WORD_PHONEMES];
			int textmode;

			DecodePhonemes(phonetic, ph_decoded);

			if ((dictionary_flags & FLAG_TEXTMODE) == 0)
				textmode = 0;
			else
				textmode = 1;

			if (textmode == translator->langopts.textmode) {
				// only show this line if the word translates to phonemes, not replacement text
				if ((dictionary_flags & FLAG_SKIPWORDS) && (wtab != NULL)) {
					// matched more than one word
					// (check for wtab prevents showing RULE_SPELLING byte when speaking individual letters)
					memcpy(word_buf, word2, word_end-word2);
					word_buf[word_end-word2-1] = 0;
					fprintf(f_trans, "Found: '%s %s\n", word1, word_buf);
				} else
					fprintf(f_trans, "Found: '%s", word1);
				print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
				fprintf(f_trans, "' [%s]  %s\n", ph_decoded, dict_flags_buf);
			}
		}

		ix = utf8_in(&c, word);
		if ((word[ix] == 0) && !IsAlpha(c))
			flags[0] |= FLAG_MAX3;
		return word_end;

	}
	return 0;
}

/* Lookup a specified word in the word dictionary.
   Returns phonetic data in 'phonetic' and bits in 'flags'

   end_flags:  indicates if a suffix has been removed
 */
int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
{
	int length;
	const char *found;
	const char *word1;
	const char *word2;
	unsigned char c;
	int nbytes;
	int len;
	char word[N_WORD_BYTES];
	static char word_replacement[N_WORD_BYTES];

	length = 0;
	word2 = word1 = *wordptr;

	while ((word2[nbytes = utf8_nbytes(word2)] == ' ') && (word2[nbytes+1] == '.')) {
		// look for an abbreviation of the form a.b.c
		// try removing the spaces between the dots and looking for a match
		memcpy(&word[length], word2, nbytes);
		length += nbytes;
		word[length++] = '.';
		word2 += nbytes+3;
	}
	if (length > 0) {
		// found an abbreviation containing dots
		nbytes = 0;
		while (((c = word2[nbytes]) != 0) && (c != ' '))
			nbytes++;
		memcpy(&word[length], word2, nbytes);
		word[length+nbytes] = 0;
		found =  LookupDict2(tr, word, word2, ph_out, flags, end_flags, wtab);
		if (found) {
			// set the skip words flag
			flags[0] |= FLAG_SKIPWORDS;
			dictionary_skipwords = length;
			return 1;
		}
	}

	for (length = 0; length < (N_WORD_BYTES-1); length++) {
		if (((c = *word1++) == 0) || (c == ' '))
			break;

		if ((c == '.') && (length > 0) && (IsDigit09(word[length-1])))
			break; // needed for lang=hu, eg. "december 2.-ig"

		word[length] = c;
	}
	word[length] = 0;

	found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);

	if (flags[0] & FLAG_MAX3) {
		if (strcmp(ph_out, tr->phonemes_repeat) == 0) {
			tr->phonemes_repeat_count++;
			if (tr->phonemes_repeat_count > 3)
				ph_out[0] = 0;
		} else {
			strncpy0(tr->phonemes_repeat, ph_out, sizeof(tr->phonemes_repeat));
			tr->phonemes_repeat_count = 1;
		}
	} else
		tr->phonemes_repeat_count = 0;

	if ((found == 0) && (flags[1] & FLAG_ACCENT)) {
		int letter;
		word2 = word;
		if (*word2 == '_') word2++;
		len = utf8_in(&letter, word2);
		LookupAccentedLetter(tr, letter, ph_out);
		found = word2 + len;
	}

	if (found == 0) {
		ph_out[0] = 0;

		// try modifications to find a recognised word

		if ((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e')) {
			// try removing an 'e' which has been added by RemoveEnding
			word[length-1] = 0;
			found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
		} else if ((end_flags & SUFX_D) && (word[length-1] == word[length-2])) {
			// try removing a double letter
			word[length-1] = 0;
			found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
		}
	}

	if (found) {
		// if textmode is the default, then words which have phonemes are marked.
		if (tr->langopts.textmode)
			*flags ^= FLAG_TEXTMODE;

		if (*flags & FLAG_TEXTMODE) {
			// the word translates to replacement text, not to phonemes

			if (end_flags & FLAG_ALLOW_TEXTMODE) {
				// only use replacement text if this is the original word, not if a prefix or suffix has been removed
				word_replacement[0] = 0;
				word_replacement[1] = ' ';
				sprintf(&word_replacement[2], "%s ", ph_out); // replacement word, preceded by zerochar and space

				word1 = *wordptr;
				*wordptr = &word_replacement[2];

				if (option_phonemes & espeakPHONEMES_TRACE) {
					len = found - word1;
					memcpy(word, word1, len); // include multiple matching words
					word[len] = 0;
					fprintf(f_trans, "Replace: %s  %s\n", word, *wordptr);
				}
			}

			ph_out[0] = 0;
			return 0;
		}

		return 1;
	}

	ph_out[0] = 0;
	return 0;
}

extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes

int Lookup(Translator *tr, const char *word, char *ph_out)
{
	// Look up in *_list, returns dictionary flags[0] and phonemes

	int flags0;
	unsigned int flags[2];
	int say_as;
	char *word1 = (char *)word;
	char text[80];

	flags[0] = 0;
	flags[1] = FLAG_LOOKUP_SYMBOL;
	if ((flags0 = LookupDictList(tr, &word1, ph_out, flags, FLAG_ALLOW_TEXTMODE, NULL)) != 0)
		flags0 = flags[0];

	if (flags[0] & FLAG_TEXTMODE) {
		say_as = option_sayas;
		option_sayas = 0; // don't speak replacement word as letter names
		strncpy0(text, word1, sizeof(text));
		flags0 = TranslateWord(tr, text, NULL, NULL);
		strcpy(ph_out, word_phonemes);
		option_sayas = say_as;
	}
	return flags0;
}

int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out)
{
	char buf[100];
	static unsigned int flags[2];
	char *word1 = (char *)word;

	flags[0] = flags[1] = 0;
	LookupDictList(tr, &word1, buf, flags, 0, NULL);
	*flags_out = flags;
	return flags[0];
}

int RemoveEnding(Translator *tr, char *word, int end_type, char *word_copy)
{
	/* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
	   end_type: bits 0-6  number of letters
	             bits 8-14  suffix flags

	    word_copy: make a copy of the original word
	    This routine is language specific.  In English it deals with reversing y->i and e-dropping
	    that were done when the suffix was added to the original word.
	 */

	int i;
	char *word_end;
	int len_ending;
	int end_flags;
	const char *p;
	int len;
	char ending[50];

	// these lists are language specific, but are only relevent if the 'e' suffix flag is used
	static const char *add_e_exceptions[] = {
		"ion", NULL
	};

	static const char *add_e_additions[] = {
		"c", "rs", "ir", "ur", "ath", "ns", "u", NULL
	};

	for (word_end = word; *word_end != ' '; word_end++) {
		// replace discarded 'e's
		if (*word_end == REPLACED_E)
			*word_end = 'e';
	}
	i = word_end - word;

	if (word_copy != NULL) {
		memcpy(word_copy, word, i);
		word_copy[i] = 0;
	}

	// look for multibyte characters to increase the number of bytes to remove
	for (len_ending = i = (end_type & 0x3f); i > 0; i--) { // num.of characters of the suffix
		word_end--;
		while ((*word_end & 0xc0) == 0x80) {
			word_end--; // for multibyte characters
			len_ending++;
		}
	}

	// remove bytes from the end of the word and replace them by spaces
	for (i = 0; (i < len_ending) && (i < (int)sizeof(ending)-1); i++) {
		ending[i] = word_end[i];
		word_end[i] = ' ';
	}
	ending[i] = 0;
	word_end--; // now pointing at last character of stem

	end_flags = (end_type & 0xfff0) | FLAG_SUFX;

	/* add an 'e' to the stem if appropriate,
	    if  stem ends in vowel+consonant
	    or  stem ends in 'c'  (add 'e' to soften it) */

	if (end_type & SUFX_I) {
		if (word_end[0] == 'i')
			word_end[0] = 'y';
	}

	if (end_type & SUFX_E) {
		if (tr->translator_name == L('n', 'l')) {
			if (((word_end[0] & 0x80) == 0) && ((word_end[-1] & 0x80) == 0) && IsVowel(tr, word_end[-1]) && IsLetter(tr, word_end[0], LETTERGP_C) && !IsVowel(tr, word_end[-2])) {
				// double the vowel before the (ascii) final consonant
				word_end[1] = word_end[0];
				word_end[0] = word_end[-1];
				word_end[2] = ' ';
			}
		} else if (tr->translator_name == L('e', 'n')) {
			// add 'e' to end of stem
			if (IsLetter(tr, word_end[-1], LETTERGP_VOWEL2) && IsLetter(tr, word_end[0], 1)) {
				// vowel(incl.'y') + hard.consonant

				for (i = 0; (p = add_e_exceptions[i]) != NULL; i++) {
					len = strlen(p);
					if (memcmp(p, &word_end[1-len], len) == 0)
						break;
				}
				if (p == NULL)
					end_flags |= FLAG_SUFX_E_ADDED; // no exception found
			} else {
				for (i = 0; (p = add_e_additions[i]) != NULL; i++) {
					len = strlen(p);
					if (memcmp(p, &word_end[1-len], len) == 0) {
						end_flags |= FLAG_SUFX_E_ADDED;
						break;
					}
				}
			}
		} else if (tr->langopts.suffix_add_e != 0)
			end_flags |= FLAG_SUFX_E_ADDED;

		if (end_flags & FLAG_SUFX_E_ADDED) {
			utf8_out(tr->langopts.suffix_add_e, &word_end[1]);

			if (option_phonemes & espeakPHONEMES_TRACE)
				fprintf(f_trans, "add e\n");
		}
	}

	if ((end_type & SUFX_V) && (tr->expect_verb == 0))
		tr->expect_verb = 1; // this suffix indicates the verb pronunciation


	if ((strcmp(ending, "s") == 0) || (strcmp(ending, "es") == 0))
		end_flags |= FLAG_SUFX_S;

	if (ending[0] == '\'')
		end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix

	return end_flags;
}