PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/native/external/espeak/src/translate.cpp

http://eyes-free.googlecode.com/
C++ | 2355 lines | 2055 code | 178 blank | 122 comment | 356 complexity | 61c8975e020ed42b82768670e4c3515c MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 by Jonathan Duddington *
  3. * email: jonsd@users.sourceforge.net *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, see: *
  17. * <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include "StdAfx.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <wctype.h>
  25. ////#include <wchar.h>
  26. #include "speak_lib.h"
  27. #include "speech.h"
  28. #include "phoneme.h"
  29. #include "synthesize.h"
  30. #include "voice.h"
  31. #include "translate.h"
  32. #define WORD_STRESS_CHAR '*'
  33. Translator *translator = NULL; // the main translator
  34. Translator *translator2 = NULL; // secondary translator for certain words
  35. static char translator2_language[20] = {0};
  36. FILE *f_trans = NULL; // phoneme output text
  37. int option_tone1 = 0;
  38. int option_tone2 = 0;
  39. int option_tone_flags = 0; // bit 8=emphasize allcaps, bit 9=emphasize penultimate stress
  40. int option_phonemes = 0;
  41. int option_phoneme_events = 0;
  42. int option_quiet = 0;
  43. int option_endpause = 0; // suppress pause after end of text
  44. int option_capitals = 0;
  45. int option_punctuation = 0;
  46. int option_sayas = 0;
  47. int option_sayas2 = 0; // used in translate_clause()
  48. int option_emphasis = 0; // 0=normal, 1=normal, 2=weak, 3=moderate, 4=strong
  49. int option_ssml = 0;
  50. int option_phoneme_input = 1; // allow [[phonemes]] in input
  51. int option_phoneme_variants = 0; // 0= don't display phoneme variant mnemonics
  52. int option_wordgap = 0;
  53. int count_sayas_digits;
  54. int skip_sentences;
  55. int skip_words;
  56. int skip_characters;
  57. char skip_marker[N_MARKER_LENGTH];
  58. int skipping_text; // waiting until word count, sentence count, or named marker is reached
  59. int end_character_position;
  60. int count_sentences;
  61. int count_words;
  62. int clause_start_char;
  63. int clause_start_word;
  64. int new_sentence;
  65. int word_emphasis = 0; // set if emphasis level 3 or 4
  66. int prev_clause_pause=0;
  67. int max_clause_pause = 0;
  68. wchar_t option_punctlist[N_PUNCTLIST]={0};
  69. char ctrl_embedded = '\001'; // to allow an alternative CTRL for embedded commands
  70. int option_multibyte=espeakCHARS_AUTO; // 0=auto, 1=utf8, 2=8bit, 3=wchar
  71. // these are overridden by defaults set in the "speak" file
  72. int option_linelength = 0;
  73. #define N_EMBEDDED_LIST 250
  74. static int embedded_ix;
  75. static int embedded_read;
  76. unsigned int embedded_list[N_EMBEDDED_LIST];
  77. // the source text of a single clause (UTF8 bytes)
  78. #define N_TR_SOURCE 700
  79. static char source[N_TR_SOURCE+40]; // extra space for embedded command & voice change info at end
  80. int n_replace_phonemes;
  81. REPLACE_PHONEMES replace_phonemes[N_REPLACE_PHONEMES];
  82. // brackets, also 0x2014 to 0x021f which don't need to be in this list
  83. static const unsigned short brackets[] = {
  84. '(',')','[',']','{','}','<','>','"','\'','`',
  85. 0xab,0xbb, // double angle brackets
  86. 0x300a,0x300b, // double angle brackets (ideograph)
  87. 0};
  88. // other characters which break a word, but don't produce a pause
  89. static const unsigned short breaks[] = {'_', 0};
  90. // treat these characters as spaces, in addition to iswspace()
  91. static const wchar_t chars_space[] = {0x2500,0}; // box drawing horiz
  92. // Translate character codes 0xA0 to 0xFF into their unicode values
  93. // ISO_8859_1 is set as default
  94. static const unsigned short ISO_8859_1[0x60] = {
  95. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  96. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  97. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  98. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  99. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  100. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  101. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  102. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, // d8
  103. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  104. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  105. 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  106. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff, // f8
  107. };
  108. static const unsigned short ISO_8859_2[0x60] = {
  109. 0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7, // a0
  110. 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b, // a8
  111. 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7, // b0
  112. 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, // b8
  113. 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7, // c0
  114. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e, // c8
  115. 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7, // d0
  116. 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df, // d8
  117. 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7, // e0
  118. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f, // e8
  119. 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7, // f0
  120. 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9, // f8
  121. };
  122. static const unsigned short ISO_8859_3[0x60] = {
  123. 0x00a0, 0x0126, 0x02d8, 0x00a3, 0x00a4, 0x0000, 0x0124, 0x00a7, // a0
  124. 0x00a8, 0x0130, 0x015e, 0x011e, 0x0134, 0x00ad, 0x0000, 0x017b, // a8
  125. 0x00b0, 0x0127, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x0125, 0x00b7, // b0
  126. 0x00b8, 0x0131, 0x015f, 0x011f, 0x0135, 0x00bd, 0x0000, 0x017c, // b8
  127. 0x00c0, 0x00c1, 0x00c2, 0x0000, 0x00c4, 0x010a, 0x0108, 0x00c7, // c0
  128. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  129. 0x0000, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x0120, 0x00d6, 0x00d7, // d0
  130. 0x011c, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x016c, 0x015c, 0x00df, // d8
  131. 0x00e0, 0x00e1, 0x00e2, 0x0000, 0x00e4, 0x010b, 0x0109, 0x00e7, // e0
  132. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  133. 0x0000, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x0121, 0x00f6, 0x00f7, // f0
  134. 0x011d, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x016d, 0x015d, 0x02d9, // f8
  135. };
  136. static const unsigned short ISO_8859_4[0x60] = {
  137. 0x00a0, 0x0104, 0x0138, 0x0156, 0x00a4, 0x0128, 0x013b, 0x00a7, // a0
  138. 0x00a8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00ad, 0x017d, 0x00af, // a8
  139. 0x00b0, 0x0105, 0x02db, 0x0157, 0x00b4, 0x0129, 0x013c, 0x02c7, // b0
  140. 0x00b8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014a, 0x017e, 0x014b, // b8
  141. 0x0100, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x012e, // c0
  142. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x0116, 0x00cd, 0x00ce, 0x012a, // c8
  143. 0x0110, 0x0145, 0x014c, 0x0136, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  144. 0x00d8, 0x0172, 0x00da, 0x00db, 0x00dc, 0x0168, 0x016a, 0x00df, // d8
  145. 0x0101, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x012f, // e0
  146. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x0117, 0x00ed, 0x00ee, 0x012b, // e8
  147. 0x0111, 0x0146, 0x014d, 0x0137, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  148. 0x00f8, 0x0173, 0x00fa, 0x00fb, 0x00fc, 0x0169, 0x016b, 0x02d9, // f8
  149. };
  150. static const unsigned short ISO_8859_5[0x60] = {
  151. 0x00a0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, // a0 Cyrillic
  152. 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x00ad, 0x040e, 0x040f, // a8
  153. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, // b0
  154. 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, // b8
  155. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, // c0
  156. 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, // c8
  157. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, // d0
  158. 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, // d8
  159. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, // e0
  160. 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f, // e8
  161. 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, // f0
  162. 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x00a7, 0x045e, 0x045f, // f8
  163. };
  164. static const unsigned short ISO_8859_7[0x60] = {
  165. 0x00a0, 0x2018, 0x2019, 0x00a3, 0x20ac, 0x20af, 0x00a6, 0x00a7, // a0 Greek
  166. 0x00a8, 0x00a9, 0x037a, 0x00ab, 0x00ac, 0x00ad, 0x0000, 0x2015, // a8
  167. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x0385, 0x0386, 0x00b7, // b0
  168. 0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f, // b8
  169. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, // c0
  170. 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, // c8
  171. 0x03a0, 0x03a1, 0x0000, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, // d0
  172. 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, // d8
  173. 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, // e0
  174. 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, // e8
  175. 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7, // f0
  176. 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0x0000, // f8
  177. };
  178. static const unsigned short ISO_8859_9[0x60] = {
  179. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  180. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  181. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  182. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  183. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  184. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  185. 0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  186. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df, // d8
  187. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  188. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  189. 0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  190. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff, // f8
  191. };
  192. static const unsigned short ISO_8859_14[0x60] = {
  193. 0x00a0, 0x1e02, 0x1e03, 0x00a3, 0x010a, 0x010b, 0x1e0a, 0x00a7, // a0 Welsh
  194. 0x1e80, 0x00a9, 0x1e82, 0x1e0b, 0x1ef2, 0x00ad, 0x00ae, 0x0178, // a8
  195. 0x1e1e, 0x1e1f, 0x0120, 0x0121, 0x1e40, 0x1e41, 0x00b6, 0x1e56, // b0
  196. 0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, // b8
  197. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  198. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  199. 0x0174, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x1e6a, // d0
  200. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x0176, 0x00df, // d8
  201. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  202. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  203. 0x0175, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x1e6b, // f0
  204. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x0177, 0x00ff, // f8
  205. };
  206. static const unsigned short KOI8_R[0x60] = {
  207. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0 Russian
  208. 0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
  209. 0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
  210. 0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
  211. 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
  212. 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
  213. 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
  214. 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
  215. 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
  216. 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
  217. 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
  218. 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
  219. };
  220. static const unsigned short ISCII[0x60] = {
  221. 0x0020, 0x0901, 0x0902, 0x0903, 0x0905, 0x0906, 0x0907, 0x0908, // a0
  222. 0x0909, 0x090a, 0x090b, 0x090e, 0x090f, 0x0910, 0x090d, 0x0912, // a8
  223. 0x0913, 0x0914, 0x0911, 0x0915, 0x0916, 0x0917, 0x0918, 0x0919, // b0
  224. 0x091a, 0x091b, 0x091c, 0x091d, 0x091e, 0x091f, 0x0920, 0x0921, // b8
  225. 0x0922, 0x0923, 0x0924, 0x0925, 0x0926, 0x0927, 0x0928, 0x0929, // c0
  226. 0x092a, 0x092b, 0x092c, 0x092d, 0x092e, 0x092f, 0x095f, 0x0930, // c8
  227. 0x0931, 0x0932, 0x0933, 0x0934, 0x0935, 0x0936, 0x0937, 0x0938, // d0
  228. 0x0939, 0x0020, 0x093e, 0x093f, 0x0940, 0x0941, 0x0942, 0x0943, // d8
  229. 0x0946, 0x0947, 0x0948, 0x0945, 0x094a, 0x094b, 0x094c, 0x0949, // e0
  230. 0x094d, 0x093c, 0x0964, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, // e8
  231. 0x0020, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, // f0
  232. 0x0037, 0x0038, 0x0039, 0x20, 0x20, 0x20, 0x20, 0x20, // f8
  233. };
  234. const unsigned short *charsets[N_CHARSETS] = {
  235. ISO_8859_1,
  236. ISO_8859_1,
  237. ISO_8859_2,
  238. ISO_8859_3,
  239. ISO_8859_4,
  240. ISO_8859_5,
  241. ISO_8859_1,
  242. ISO_8859_7,
  243. ISO_8859_1,
  244. ISO_8859_9,
  245. ISO_8859_1,
  246. ISO_8859_1,
  247. ISO_8859_1,
  248. ISO_8859_1,
  249. ISO_8859_14,
  250. ISO_8859_1,
  251. ISO_8859_1,
  252. ISO_8859_1,
  253. KOI8_R, // 18
  254. ISCII };
  255. // Tables of the relative lengths of vowels, depending on the
  256. // type of the two phonemes that follow
  257. // indexes are the "length_mod" value for the following phonemes
  258. // use this table if vowel is not the last in the word
  259. static unsigned char length_mods_en[100] = {
  260. /* a , t s n d z r N <- next */
  261. 100,120,100,105,100,110,110,100, 95, 100, /* a <- next2 */
  262. 105,120,105,110,125,130,135,115,125, 100, /* , */
  263. 105,120, 75,100, 75,105,120, 85, 75, 100, /* t */
  264. 105,120, 85,105, 95,115,120,100, 95, 100, /* s */
  265. 110,120, 95,105,100,115,120,100,100, 100, /* n */
  266. 105,120,100,105, 95,115,120,110, 95, 100, /* d */
  267. 105,120,100,105,105,122,125,110,105, 100, /* z */
  268. 105,120,100,105,105,122,125,110,105, 100, /* r */
  269. 105,120, 95,105,100,115,120,110,100, 100, /* N */
  270. 100,120,100,100,100,100,100,100,100, 100 }; // SPARE
  271. // as above, but for the last syllable in a word
  272. static unsigned char length_mods_en0[100] = {
  273. /* a , t s n d z r N <- next */
  274. 100,150,100,105,110,115,110,110,110, 100, /* a <- next2 */
  275. 105,150,105,110,125,135,140,115,135, 100, /* , */
  276. 105,150, 90,105, 90,122,135,100, 90, 100, /* t */
  277. 105,150,100,105,100,122,135,100,100, 100, /* s */
  278. 105,150,100,105,105,115,135,110,105, 100, /* n */
  279. 105,150,100,105,105,122,130,120,125, 100, /* d */
  280. 105,150,100,105,110,122,125,115,110, 100, /* z */
  281. 105,150,100,105,105,122,135,120,105, 100, /* r */
  282. 105,150,100,105,105,115,135,110,105, 100, /* N */
  283. 100,100,100,100,100,100,100,100,100, 100 }; // SPARE
  284. static unsigned char length_mods_equal[100] = {
  285. /* a , t s n d z r N <- next */
  286. 110,110,110,110,110,110,110,110,110, 110, /* a <- next2 */
  287. 110,110,110,110,110,110,110,110,110, 110, /* , */
  288. 110,110,110,110,110,110,110,110,110, 110, /* t */
  289. 110,110,110,110,110,110,110,110,110, 110, /* s */
  290. 110,110,110,110,110,110,110,110,110, 110, /* n */
  291. 110,110,110,110,110,110,110,110,110, 110, /* d */
  292. 110,110,110,110,110,110,110,110,110, 110, /* z */
  293. 110,110,110,110,110,110,110,110,110, 110, /* r */
  294. 110,110,110,110,110,110,110,110,110, 110, /* N */
  295. 110,110,110,110,110,110,110,110,110, 110 }; // SPARE
  296. unsigned char *length_mod_tabs[6] = {
  297. length_mods_en,
  298. length_mods_en, // 1
  299. length_mods_en0, // 2
  300. length_mods_equal, // 3
  301. length_mods_equal, // 4
  302. length_mods_equal // 5
  303. };
  304. void SetLengthMods(Translator *tr, int value)
  305. {//==========================================
  306. int value2;
  307. tr->langopts.length_mods0 = tr->langopts.length_mods = length_mod_tabs[value % 100];
  308. if((value2 = value / 100) != 0)
  309. {
  310. tr->langopts.length_mods0 = length_mod_tabs[value2];
  311. }
  312. }
  313. int IsAlpha(unsigned int c)
  314. {//========================
  315. // Replacement for iswalph() which also checks for some in-word symbols
  316. if(iswalpha(c))
  317. return(1);
  318. if((c >= 0x901) && (c <= 0x957))
  319. return(1); // Devanagari vowel signs and other signs
  320. if((c >= 0xb81) && (c <= 0xbe5))
  321. return(1); // Tamil vowel signs and other signs
  322. if((c >= 0x300) && (c <= 0x36f))
  323. return(1); // combining accents
  324. if((c >= 0x1100) && (c <= 0x11ff))
  325. return(1); //Korean jamo
  326. if((c > 0x3040) && (c <= 0xa700))
  327. return(1); // Chinese/Japanese. Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure
  328. return(0);
  329. }
  330. int IsDigit09(unsigned int c)
  331. {//=========================
  332. if((c >= '0') && (c <= '9'))
  333. return(1);
  334. return(0);
  335. }
  336. int IsDigit(unsigned int c)
  337. {//========================
  338. if(iswdigit(c))
  339. return(1);
  340. if((c >= 0x966) && (c <= 0x96f))
  341. return(1);
  342. return(0);
  343. }
  344. int IsSpace(unsigned int c)
  345. {//========================
  346. if(c == 0)
  347. return(0);
  348. if(wcschr(chars_space,c))
  349. return(1);
  350. return(iswspace(c));
  351. }
  352. Translator::Translator()
  353. {//=====================
  354. int ix;
  355. static const unsigned char stress_amps2[] = {16,16, 20,20, 20,24, 24,21 };
  356. static const short stress_lengths2[8] = {182,140, 220,220, 220,240, 260,280};
  357. static const wchar_t empty_wstring[1] = {0};
  358. static const wchar_t punct_in_word[2] = {'\'', 0}; // allow hyphen within words
  359. charset_a0 = charsets[1]; // ISO-8859-1, this is for when the input is not utf8
  360. dictionary_name[0] = 0;
  361. dict_condition=0;
  362. data_dictrules = NULL; // language_1 translation rules file
  363. data_dictlist = NULL; // language_2 dictionary lookup file
  364. transpose_offset = 0;
  365. // only need lower case
  366. letter_bits_offset = 0;
  367. memset(letter_bits,0,sizeof(letter_bits));
  368. memset(letter_groups,0,sizeof(letter_groups));
  369. // 0-5 sets of characters matched by A B C E F G in pronunciation rules
  370. // these may be set differently for different languages
  371. SetLetterBits(this,0,"aeiou"); // A vowels, except y
  372. SetLetterBits(this,1,"bcdfgjklmnpqstvxz"); // B hard consonants, excluding h,r,w
  373. SetLetterBits(this,2,"bcdfghjklmnpqrstvwxz"); // C all consonants
  374. SetLetterBits(this,3,"hlmnr"); // H 'soft' consonants
  375. SetLetterBits(this,4,"cfhkpqstx"); // F voiceless consonants
  376. SetLetterBits(this,5,"bdgjlmnrvwyz"); // G voiced
  377. SetLetterBits(this,6,"eiy"); // Letter group Y, front vowels
  378. SetLetterBits(this,7,"aeiouy"); // vowels, including y
  379. char_plus_apostrophe = empty_wstring;
  380. punct_within_word = punct_in_word;
  381. for(ix=0; ix<8; ix++)
  382. {
  383. stress_amps[ix] = stress_amps2[ix];
  384. stress_amps_r[ix] = stress_amps2[ix] - 1;
  385. stress_lengths[ix] = stress_lengths2[ix];
  386. }
  387. memset(&langopts,0,sizeof(langopts));
  388. langopts.stress_rule = 2;
  389. langopts.unstressed_wd1 = 1;
  390. langopts.unstressed_wd2 = 3;
  391. langopts.param[LOPT_SONORANT_MIN] = 95;
  392. langopts.param[LOPT_MAXAMP_EOC] = 19;
  393. langopts.param[LOPT_UNPRONOUNCABLE] = 's'; // don't count this character at start of word
  394. langopts.max_initial_consonants = 3;
  395. langopts.replace_chars = NULL;
  396. langopts.length_mods = length_mods_en;
  397. langopts.length_mods0 = length_mods_en0;
  398. langopts.long_stop = 100;
  399. langopts.max_roman = 49;
  400. langopts.thousands_sep = ',';
  401. langopts.decimal_sep = '.';
  402. memcpy(punct_to_tone,punctuation_to_tone,sizeof(punct_to_tone));
  403. }
  404. Translator::~Translator(void)
  405. {//==========================
  406. if(data_dictlist != NULL)
  407. Free(data_dictlist);
  408. }
  409. int lookupwchar(const unsigned short *list,int c)
  410. {//==============================================
  411. // Is the character c in the list ?
  412. int ix;
  413. for(ix=0; list[ix] != 0; ix++)
  414. {
  415. if(list[ix] == c)
  416. return(ix+1);
  417. }
  418. return(0);
  419. }
  420. int IsBracket(int c)
  421. {//=================
  422. if((c >= 0x2014) && (c <= 0x201f))
  423. return(1);
  424. return(lookupwchar(brackets,c));
  425. }
  426. int utf8_out(unsigned int c, char *buf)
  427. {//====================================
  428. // write a unicode character into a buffer as utf8
  429. // returns the number of bytes written
  430. int n_bytes;
  431. int j;
  432. int shift;
  433. static char unsigned code[4] = {0,0xc0,0xe0,0xf0};
  434. if(c < 0x80)
  435. {
  436. buf[0] = c;
  437. return(1);
  438. }
  439. if(c >= 0x110000)
  440. {
  441. buf[0] = ' '; // out of range character code
  442. return(1);
  443. }
  444. if(c < 0x0800)
  445. n_bytes = 1;
  446. else
  447. if(c < 0x10000)
  448. n_bytes = 2;
  449. else
  450. n_bytes = 3;
  451. shift = 6*n_bytes;
  452. buf[0] = code[n_bytes] | (c >> shift);
  453. for(j=0; j<n_bytes; j++)
  454. {
  455. shift -= 6;
  456. buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
  457. }
  458. return(n_bytes+1);
  459. } // end of utf8_out
  460. int utf8_nbytes(const char *buf)
  461. {//=============================
  462. // Returns the number of bytes for the first UTF-8 character in buf
  463. unsigned char c = (unsigned char)buf[0];
  464. if(c < 0x80)
  465. return(1);
  466. if(c < 0xe0)
  467. return(2);
  468. if(c < 0xf0)
  469. return(3);
  470. return(4);
  471. }
  472. int utf8_in(int *c, const char *buf, int backwards)
  473. {//================================================
  474. // Read a unicode characater from a UTF8 string
  475. // Returns the number of UTF8 bytes used.
  476. // backwards: set if we are moving backwards through the UTF8 string
  477. int c1;
  478. int n_bytes;
  479. int ix;
  480. static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};
  481. // find the start of the next/previous character
  482. while((*buf & 0xc0) == 0x80)
  483. {
  484. // skip over non-initial bytes of a multi-byte utf8 character
  485. if(backwards)
  486. buf--;
  487. else
  488. buf++;
  489. }
  490. n_bytes = 0;
  491. if((c1 = *buf++) & 0x80)
  492. {
  493. if((c1 & 0xe0) == 0xc0)
  494. n_bytes = 1;
  495. else
  496. if((c1 & 0xf0) == 0xe0)
  497. n_bytes = 2;
  498. else
  499. if((c1 & 0xf8) == 0xf0)
  500. n_bytes = 3;
  501. c1 &= mask[n_bytes];
  502. for(ix=0; ix<n_bytes; ix++)
  503. {
  504. c1 = (c1 << 6) + (*buf++ & 0x3f);
  505. }
  506. }
  507. *c = c1;
  508. return(n_bytes+1);
  509. }
  510. char *strchr_w(const char *s, int c)
  511. {//=================================
  512. // return NULL for any non-ascii character
  513. if(c >= 0x80)
  514. return(NULL);
  515. return(strchr((char *)s,c)); // (char *) is needed for Borland compiler
  516. }
  517. int PhonemeCode(unsigned int mnem)
  518. {//===============================
  519. int ix;
  520. for(ix=0; ix<n_phoneme_tab; ix++)
  521. {
  522. if(phoneme_tab[ix]->mnemonic == mnem)
  523. return(phoneme_tab[ix]->code);
  524. }
  525. return(phonSCHWA);
  526. }
  527. int Translator::TranslateWord(char *word1, int next_pause, WORD_TAB *wtab)
  528. {//=======================================================================
  529. // word1 is terminated by space (0x20) character
  530. int length;
  531. int word_length;
  532. int ix;
  533. int posn;
  534. int pfix;
  535. int n_chars;
  536. unsigned int dictionary_flags[2];
  537. unsigned int dictionary_flags2[2];
  538. int end_type=0;
  539. int prefix_type=0;
  540. char *wordx;
  541. char phonemes[N_WORD_PHONEMES];
  542. char *ph_limit;
  543. char *phonemes_ptr;
  544. char prefix_phonemes[N_WORD_PHONEMES];
  545. char end_phonemes[N_WORD_PHONEMES];
  546. char word_copy[N_WORD_BYTES];
  547. char prefix_chars[N_WORD_BYTES];
  548. int found=0;
  549. int end_flags;
  550. char c_temp; // save a character byte while we temporarily replace it with space
  551. int first_char;
  552. int last_char = 0;
  553. int unpron_length;
  554. int add_plural_suffix = 0;
  555. int prefix_flags = 0;
  556. int confirm_prefix;
  557. int spell_word;
  558. int stress_bits;
  559. int emphasize_allcaps = 0;
  560. int wflags = wtab->flags;
  561. int wmark = wtab->wmark;
  562. // translate these to get pronunciations of plural 's' suffix (different forms depending on
  563. // the preceding letter
  564. static char word_zz[4] = {0,'z','z',0};
  565. static char word_iz[4] = {0,'i','z',0};
  566. static char word_ss[4] = {0,'s','s',0};
  567. dictionary_flags[0] = 0;
  568. dictionary_flags[1] = 0;
  569. dictionary_flags2[0] = 0;
  570. dictionary_flags2[1] = 0;
  571. dictionary_skipwords = 0;
  572. prefix_phonemes[0] = 0;
  573. end_phonemes[0] = 0;
  574. ph_limit = &phonemes[N_WORD_PHONEMES];
  575. // count the length of the word
  576. wordx = word1;
  577. utf8_in(&first_char,wordx,0);
  578. word_length = 0;
  579. while((*wordx != 0) && (*wordx != ' '))
  580. {
  581. wordx += utf8_in(&last_char,wordx,0);
  582. word_length++;
  583. }
  584. // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
  585. // we may just find some flags
  586. spell_word = 0;
  587. if(option_sayas == SAYAS_KEY)
  588. {
  589. if(word_length == 1)
  590. spell_word = 4;
  591. }
  592. if(option_sayas & 0x10)
  593. {
  594. // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
  595. spell_word = option_sayas & 0xf; // 2,3,4
  596. }
  597. else
  598. {
  599. found = LookupDictList(&word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab); // the original word
  600. if(dictionary_flags[0] & FLAG_TEXTMODE)
  601. {
  602. stress_bits = dictionary_flags[0] & 0x7f;
  603. found = LookupDictList(&word1, phonemes, dictionary_flags2, 0, wtab); // the text replacement
  604. if(dictionary_flags2[0]!=0)
  605. {
  606. dictionary_flags[0] = dictionary_flags2[0];
  607. dictionary_flags[1] = dictionary_flags2[1];
  608. if(stress_bits != 0)
  609. {
  610. // keep any stress information from the original word
  611. dictionary_flags[0] = (dictionary_flags[0] & ~0x7f) | stress_bits;
  612. }
  613. }
  614. }
  615. else
  616. if((found==0) && (dictionary_flags[0] & FLAG_SKIPWORDS))
  617. {
  618. // grouped words, but no translation. Join the words with hyphens.
  619. wordx = word1;
  620. ix = 0;
  621. while(ix < dictionary_skipwords)
  622. {
  623. if(*wordx == ' ')
  624. {
  625. *wordx = '-';
  626. ix++;
  627. }
  628. wordx++;
  629. }
  630. }
  631. // if textmode, LookupDictList() replaces word1 by the new text and returns found=0
  632. if(phonemes[0] == phonSWITCH)
  633. {
  634. // change to another language in order to translate this word
  635. strcpy(word_phonemes,phonemes);
  636. return(0);
  637. }
  638. if((wmark > 0) && (wmark < 8))
  639. {
  640. // the stressed syllable has been specified in the text (TESTING)
  641. dictionary_flags[0] = (dictionary_flags[0] & ~0xf) | wmark;
  642. }
  643. if(!found && (dictionary_flags[0] & FLAG_ABBREV))
  644. {
  645. // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
  646. spell_word = 1;
  647. }
  648. if(!found && iswdigit(first_char))
  649. {
  650. Lookup("_0lang",word_phonemes);
  651. if(word_phonemes[0] == phonSWITCH)
  652. return(0);
  653. found = TranslateNumber(word1,phonemes,dictionary_flags,wflags);
  654. }
  655. if(!found & ((word_flags & FLAG_UPPERS) != FLAG_FIRST_UPPER))
  656. {
  657. // either all upper or all lower case
  658. if((langopts.numbers & NUM_ROMAN) || ((langopts.numbers & NUM_ROMAN_UC) && (word_flags & FLAG_ALL_UPPER)))
  659. {
  660. if((found = TranslateRoman(word1,phonemes)) != 0)
  661. dictionary_flags[0] |= FLAG_ABBREV; // prevent emphasis if capitals
  662. }
  663. }
  664. if((wflags & FLAG_ALL_UPPER) && (word_length > 1)&& iswalpha(first_char))
  665. {
  666. if((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV))
  667. {
  668. // emphasize words which are in capitals
  669. emphasize_allcaps = FLAG_EMPHASIZED;
  670. }
  671. else
  672. if(!found && !(dictionary_flags[0] & FLAG_SKIPWORDS) && (word_length<4) && (clause_lower_count > 3) && (clause_upper_count <= clause_lower_count))
  673. {
  674. // An upper case word in a lower case clause. This could be an abbreviation.
  675. spell_word = 1;
  676. }
  677. }
  678. }
  679. if(spell_word > 0)
  680. {
  681. // Speak as individual letters
  682. wordx = word1;
  683. posn = 0;
  684. phonemes[0] = 0;
  685. end_type = 0;
  686. while(*wordx != ' ')
  687. {
  688. wordx += TranslateLetter(wordx, phonemes,spell_word, word_length);
  689. posn++;
  690. if(phonemes[0] == phonSWITCH)
  691. {
  692. // change to another language in order to translate this word
  693. strcpy(word_phonemes,phonemes);
  694. if(word_length > 1)
  695. return(FLAG_SPELLWORD); // a mixture of languages, retranslate as individual letters, separated by spaces
  696. return(0);
  697. }
  698. }
  699. SetSpellingStress(phonemes,spell_word,posn);
  700. }
  701. else
  702. if(found == 0)
  703. {
  704. // word's pronunciation is not given in the dictionary list, although
  705. // dictionary_flags may have ben set there
  706. posn = 0;
  707. length = 999;
  708. wordx = word1;
  709. while(((length < 3) && (length > 0))|| (word_length > 1 && Unpronouncable(wordx)))
  710. {
  711. char *p;
  712. // This word looks "unpronouncable", so speak letters individually until we
  713. // find a remainder that we can pronounce.
  714. emphasize_allcaps = 0;
  715. wordx += TranslateLetter(wordx,phonemes,0, word_length);
  716. posn++;
  717. if(phonemes[0] == phonSWITCH)
  718. {
  719. // change to another language in order to translate this word
  720. strcpy(word_phonemes,phonemes);
  721. if(strcmp(&phonemes[1],"en")==0)
  722. return(FLAG_SPELLWORD); // _^_en must have been set in TranslateLetter(), not *_rules
  723. return(0);
  724. }
  725. p = &wordx[word_length-3]; // this looks wrong. Doesn't consider multi-byte chars.
  726. if(memcmp(p,"'s ",3) == 0)
  727. {
  728. // remove a 's suffix and pronounce this separately (not as an individual letter)
  729. add_plural_suffix = 1;
  730. p[0] = ' ';
  731. p[1] = ' ';
  732. last_char = p[-1];
  733. }
  734. length=0;
  735. while(wordx[length] != ' ') length++;
  736. if(length > 0)
  737. wordx[-1] = ' '; // prevent this affecting the pronunciation of the pronuncable part
  738. }
  739. SetSpellingStress(phonemes,0,posn);
  740. // anything left ?
  741. if(*wordx != ' ')
  742. {
  743. // Translate the stem
  744. unpron_length = strlen(phonemes);
  745. end_type = TranslateRules(wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  746. if(phonemes[0] == phonSWITCH)
  747. {
  748. // change to another language in order to translate this word
  749. strcpy(word_phonemes,phonemes);
  750. return(0);
  751. }
  752. if((phonemes[0] == 0) && (end_phonemes[0] == 0))
  753. {
  754. int wc;
  755. // characters not recognised, speak them individually
  756. utf8_in(&wc, wordx, 0);
  757. if((word_length == 1) && IsAlpha(wc))
  758. {
  759. posn = 0;
  760. while((*wordx != ' ') && (*wordx != 0))
  761. {
  762. wordx += TranslateLetter(wordx, phonemes, 4, word_length);
  763. posn++;
  764. if(phonemes[0] == phonSWITCH)
  765. {
  766. // change to another language in order to translate this word
  767. strcpy(word_phonemes,phonemes);
  768. return(0);
  769. }
  770. }
  771. SetSpellingStress(phonemes,spell_word,posn);
  772. }
  773. }
  774. c_temp = wordx[-1];
  775. found = 0;
  776. confirm_prefix = 1;
  777. while(end_type & SUFX_P)
  778. {
  779. // Found a standard prefix, remove it and retranslate
  780. if(confirm_prefix && !(end_type & SUFX_B))
  781. {
  782. int end2;
  783. char phonemes2[N_WORD_PHONEMES];
  784. char end_phonemes2[N_WORD_PHONEMES];
  785. // remove any standard suffix and confirm that the prefix is still recognised
  786. phonemes2[0] = 0;
  787. end2 = TranslateRules(wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
  788. if(end2)
  789. {
  790. RemoveEnding(wordx,end2,word_copy);
  791. end_type = TranslateRules(wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
  792. memcpy(wordx,word_copy,strlen(word_copy));
  793. if((end_type & SUFX_P) == 0)
  794. {
  795. // after removing the suffix, the prefix is no longer recognised.
  796. // Keep the suffix, but don't use the prefix
  797. end_type = end2;
  798. strcpy(phonemes,phonemes2);
  799. strcpy(end_phonemes,end_phonemes2);
  800. if(option_phonemes == 2)
  801. {
  802. DecodePhonemes(end_phonemes,end_phonemes2);
  803. fprintf(f_trans," suffix [%s]\n\n",end_phonemes2);
  804. }
  805. }
  806. confirm_prefix = 0;
  807. continue;
  808. }
  809. }
  810. prefix_type = end_type;
  811. if(prefix_type & SUFX_V)
  812. {
  813. expect_verb = 1; // use the verb form of the word
  814. }
  815. wordx[-1] = c_temp;
  816. if((prefix_type & SUFX_B) == 0)
  817. {
  818. for(ix=(prefix_type & 0xf); ix>0; ix--) // num. of characters to remove
  819. {
  820. wordx++;
  821. while((*wordx & 0xc0) == 0x80) wordx++; // for multibyte characters
  822. }
  823. }
  824. else
  825. {
  826. pfix = 1;
  827. prefix_chars[0] = 0;
  828. n_chars = prefix_type & 0x3f;
  829. for(ix=0; ix < n_chars; ix++) // num. of bytes to remove
  830. {
  831. prefix_chars[pfix++] = *wordx++;
  832. if((prefix_type & SUFX_B) && (ix == (n_chars-1)))
  833. {
  834. prefix_chars[pfix-1] = 0; // discard the last character of the prefix, this is the separator character
  835. }
  836. }
  837. prefix_chars[pfix] = 0;
  838. }
  839. c_temp = wordx[-1];
  840. wordx[-1] = ' ';
  841. confirm_prefix = 1;
  842. if(prefix_type & SUFX_B)
  843. {
  844. // SUFX_B is used for Turkish, tr_rules contains "(PbÂ?
  845. // retranslate the prefix part
  846. char *wordpf;
  847. char prefix_phonemes2[12];
  848. strncpy0(prefix_phonemes2,end_phonemes,sizeof(prefix_phonemes2));
  849. wordpf = &prefix_chars[1];
  850. found = LookupDictList(&wordpf, phonemes, dictionary_flags, SUFX_P, wtab); // without prefix
  851. if(found == 0)
  852. {
  853. end_type = TranslateRules(wordpf, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);
  854. sprintf(prefix_phonemes,"%s%s%s",phonemes,end_phonemes,prefix_phonemes2);
  855. }
  856. prefix_flags = 1;
  857. }
  858. else
  859. {
  860. strcat(prefix_phonemes,end_phonemes);
  861. }
  862. end_phonemes[0] = 0;
  863. end_type = 0;
  864. found = LookupDictList(&wordx, phonemes, dictionary_flags2, SUFX_P, wtab); // without prefix
  865. if(dictionary_flags[0]==0)
  866. {
  867. dictionary_flags[0] = dictionary_flags2[0];
  868. dictionary_flags[1] = dictionary_flags2[1];
  869. }
  870. else
  871. prefix_flags = 1;
  872. if(found == 0)
  873. {
  874. end_type = TranslateRules(wordx, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);
  875. if(phonemes[0] == phonSWITCH)
  876. {
  877. // change to another language in order to translate this word
  878. wordx[-1] = c_temp;
  879. strcpy(word_phonemes,phonemes);
  880. return(0);
  881. }
  882. }
  883. }
  884. if((end_type != 0) && !(end_type & SUFX_P))
  885. {
  886. char phonemes2[N_WORD_PHONEMES];
  887. strcpy(phonemes2,phonemes);
  888. // The word has a standard ending, re-translate without this ending
  889. end_flags = RemoveEnding(wordx,end_type,word_copy);
  890. phonemes_ptr = &phonemes[unpron_length];
  891. phonemes_ptr[0] = 0;
  892. if(prefix_phonemes[0] != 0)
  893. {
  894. // lookup the stem without the prefix removed
  895. wordx[-1] = c_temp;
  896. found = LookupDictList(&word1, phonemes_ptr, dictionary_flags2, end_flags, wtab); // include prefix, but not suffix
  897. wordx[-1] = ' ';
  898. if(dictionary_flags[0]==0)
  899. {
  900. dictionary_flags[0] = dictionary_flags2[0];
  901. dictionary_flags[1] = dictionary_flags2[1];
  902. }
  903. if(found)
  904. prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
  905. if((found==0) && (dictionary_flags2[0] != 0))
  906. prefix_flags = 1;
  907. }
  908. if(found == 0)
  909. {
  910. found = LookupDictList(&wordx, phonemes_ptr, dictionary_flags2, end_flags, wtab); // without prefix and suffix
  911. if(phonemes_ptr[0] == phonSWITCH)
  912. {
  913. // change to another language in order to translate this word
  914. memcpy(wordx,word_copy,strlen(word_copy));
  915. strcpy(word_phonemes,phonemes_ptr);
  916. return(0);
  917. }
  918. if(dictionary_flags[0]==0)
  919. {
  920. dictionary_flags[0] = dictionary_flags2[0];
  921. dictionary_flags[1] = dictionary_flags2[1];
  922. }
  923. }
  924. if(found == 0)
  925. {
  926. if(end_type & SUFX_Q)
  927. {
  928. // don't retranslate, use the original lookup result
  929. strcpy(phonemes,phonemes2);
  930. // language specific changes
  931. ApplySpecialAttribute(phonemes,dictionary_flags[0]);
  932. }
  933. else
  934. {
  935. if(end_flags & FLAG_SUFX)
  936. TranslateRules(wordx, phonemes, N_WORD_PHONEMES, NULL,wflags | FLAG_SUFFIX_REMOVED, dictionary_flags);
  937. else
  938. TranslateRules(wordx, phonemes, N_WORD_PHONEMES, NULL,wflags,dictionary_flags);
  939. if(phonemes[0] == phonSWITCH)
  940. {
  941. // change to another language in order to translate this word
  942. strcpy(word_phonemes,phonemes);
  943. memcpy(wordx,word_copy,strlen(word_copy));
  944. wordx[-1] = c_temp;
  945. return(0);
  946. }
  947. }
  948. }
  949. if((end_type & SUFX_T) == 0)
  950. {
  951. // the default is to add the suffix and then determine the word's stress pattern
  952. AppendPhonemes(phonemes, N_WORD_PHONEMES, end_phonemes);
  953. end_phonemes[0] = 0;
  954. }
  955. }
  956. wordx[-1] = c_temp;
  957. }
  958. }
  959. if((add_plural_suffix) || (wflags & FLAG_HAS_PLURAL))
  960. {
  961. // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
  962. if(last_char == 'f')
  963. TranslateRules(&word_ss[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  964. else
  965. if((last_char==0) || (strchr_w("hsx",last_char)==NULL))
  966. TranslateRules(&word_zz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  967. else
  968. TranslateRules(&word_iz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  969. }
  970. wflags |= emphasize_allcaps;
  971. /* determine stress pattern for this word */
  972. /******************************************/
  973. /* NOTE: this also adds a single PAUSE if the previous word ended
  974. in a primary stress, and this one starts with one */
  975. if(prefix_flags || (strchr(prefix_phonemes,phonSTRESS_P)!=NULL))
  976. {
  977. if((langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T))
  978. {
  979. char *p;
  980. // German, keep a secondary stress on the stem
  981. SetWordStress(phonemes,dictionary_flags[0],3,0);
  982. // reduce all but the first primary stress
  983. ix=0;
  984. for(p=prefix_phonemes; *p != 0; p++)
  985. {
  986. if(*p == phonSTRESS_P)
  987. {
  988. if(ix==0)
  989. ix=1;
  990. else
  991. *p = phonSTRESS_3;
  992. }
  993. }
  994. strcpy(word_phonemes,prefix_phonemes);
  995. strcat(word_phonemes,phonemes);
  996. SetWordStress(word_phonemes,dictionary_flags[0],-1,0);
  997. }
  998. else
  999. {
  1000. // stress position affects the whole word, including prefix
  1001. strcpy(word_phonemes,prefix_phonemes);
  1002. strcat(word_phonemes,phonemes);
  1003. SetWordStress(word_phonemes,dictionary_flags[0],-1,prev_last_stress);
  1004. }
  1005. }
  1006. else
  1007. {
  1008. if(prefix_phonemes[0] == 0)
  1009. SetWordStress(phonemes,dictionary_flags[0],-1,prev_last_stress);
  1010. else
  1011. SetWordStress(phonemes,dictionary_flags[0],-1,0);
  1012. strcpy(word_phonemes,prefix_phonemes);
  1013. strcat(word_phonemes,phonemes);
  1014. }
  1015. if(end_phonemes[0] != 0)
  1016. {
  1017. // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
  1018. strcat(word_phonemes,end_phonemes);
  1019. }
  1020. if(wflags & FLAG_LAST_WORD)
  1021. {
  1022. // don't use $brk pause before the last word of a sentence
  1023. // (but allow it for emphasis, see below
  1024. dictionary_flags[0] &= ~FLAG_PAUSE1;
  1025. }
  1026. if(wflags & FLAG_EMPHASIZED)
  1027. {
  1028. // A word is indicated in the source text as stressed
  1029. // Give it stress level 6 (for the intonation module)
  1030. ChangeWordStress(this,word_phonemes,6);
  1031. // if(!(wflags & FLAG_LAST_WORD)) // ?? omit pre-pause if it's the last word in the sentence?
  1032. dictionary_flags[0] |= FLAG_PAUSE1; // precede by short pause
  1033. }
  1034. else
  1035. if(wtab[dictionary_skipwords].flags & FLAG_LAST_WORD)
  1036. {
  1037. // the word has attribute to stress or unstress when at end of clause
  1038. if(dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
  1039. ChangeWordStress(this,word_phonemes,4);
  1040. else
  1041. if(dictionary_flags[0] & FLAG_UNSTRESS_END)
  1042. ChangeWordStress(this,word_phonemes,3);
  1043. }
  1044. // dictionary flags for this word give a clue about which alternative pronunciations of
  1045. // following words to use.
  1046. if(end_type & SUFX_F)
  1047. {
  1048. // expect a verb form, with or without -s suffix
  1049. expect_verb = 2;
  1050. expect_verb_s = 2;
  1051. }
  1052. if(dictionary_flags[1] & FLAG_PASTF)
  1053. {
  1054. /* expect perfect tense in next two words */
  1055. expect_past = 3;
  1056. expect_verb = 0;
  1057. expect_noun = 0;
  1058. }
  1059. else
  1060. if(dictionary_flags[1] & FLAG_VERBF)
  1061. {
  1062. /* expect a verb in the next word */
  1063. expect_verb = 2;
  1064. expect_verb_s = 0; /* verb won't have -s suffix */
  1065. expect_noun = 0;
  1066. }
  1067. else
  1068. if(dictionary_flags[1] & FLAG_VERBSF)
  1069. {
  1070. // expect a verb, must have a -s suffix
  1071. expect_verb = 0;
  1072. expect_verb_s = 2;
  1073. expect_past = 0;
  1074. expect_noun = 0;
  1075. }
  1076. else
  1077. if(dictionary_flags[1] & FLAG_NOUNF)
  1078. {
  1079. /* not expecting a verb next */
  1080. expect_noun = 3;
  1081. expect_verb = 0;
  1082. expect_verb_s = 0;
  1083. expect_past = 0;
  1084. }
  1085. if((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT)))
  1086. {
  1087. if(expect_verb > 0)
  1088. expect_verb--;
  1089. if(expect_verb_s > 0)
  1090. expect_verb_s--;
  1091. if(expect_noun >0)
  1092. expect_noun--;
  1093. if(expect_past > 0)
  1094. expect_past--;
  1095. }
  1096. if((word_length == 1) && iswalpha(first_char) && (first_char != 'i'))
  1097. {
  1098. // English Specific !!!!
  1099. // any single letter before a dot is an abbreviation, except 'I'
  1100. dictionary_flags[0] |= FLAG_DOT;
  1101. }
  1102. return(dictionary_flags[0]);
  1103. } // end of TranslateWord
  1104. static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
  1105. {//==========================================================
  1106. p->phcode = phcode;
  1107. p->stress = 0;
  1108. p->tone_number = 0;
  1109. p->synthflags = 0;
  1110. p->sourceix = 0;
  1111. }
  1112. static int CountSyllables(unsigned char *phonemes)
  1113. {//===============================================
  1114. int count = 0;
  1115. int phon;
  1116. while((phon = *phonemes++) != 0)
  1117. {
  1118. if(phoneme_tab[phon]->type == phVOWEL)
  1119. count++;
  1120. }
  1121. return(count);
  1122. }
  1123. int SetTranslator2(const char *new_language)
  1124. {//=========================================
  1125. // Set translator2 to a second language
  1126. int new_phoneme_tab;
  1127. if((new_phoneme_tab = SelectPhonemeTableName(new_language)) >= 0)
  1128. {
  1129. if((translator2 != NULL) && (strcmp(new_language,translator2_language) != 0))
  1130. {
  1131. // we already have an alternative translator, but not for the required language, delete it
  1132. delete translator2;
  1133. translator2 = NULL;
  1134. }
  1135. if(translator2 == NULL)
  1136. {
  1137. translator2 = SelectTranslator(new_language);
  1138. strcpy(translator2_language,new_language);
  1139. if(translator2->LoadDictionary(new_language,0) != 0)
  1140. {
  1141. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1142. new_phoneme_tab = -1;
  1143. translator2_language[0] = 0;
  1144. }
  1145. }
  1146. }
  1147. return(new_phoneme_tab);
  1148. } // end of SetTranslator2
  1149. int Translator::TranslateWord2(char *word, WORD_TAB *wtab, int pre_pause, int next_pause)
  1150. {//======================================================================================
  1151. int flags=0;
  1152. int stress;
  1153. int next_stress;
  1154. int next_tone=0;
  1155. unsigned char *p;
  1156. int srcix;
  1157. int embedded_flag=0;
  1158. int embedded_cmd;
  1159. int value;
  1160. int found_dict_flag;
  1161. unsigned char ph_code;
  1162. PHONEME_LIST2 *plist2;
  1163. PHONEME_TAB *ph;
  1164. int max_stress;
  1165. int max_stress_ix=0;
  1166. int prev_vowel = -1;
  1167. int pitch_raised = 0;
  1168. int switch_phonemes = -1;
  1169. int first_phoneme = 1;
  1170. int source_ix;
  1171. int len;
  1172. int ix;
  1173. int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
  1174. const char *new_language;
  1175. unsigned char bad_phoneme[4];
  1176. int word_copy_len;
  1177. char word_copy[N_WORD_BYTES+1];
  1178. len = wtab->length;
  1179. if(len > 31) len = 31;
  1180. source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
  1181. word_flags = wtab[0].flags;
  1182. if(word_flags & FLAG_EMBEDDED)
  1183. {
  1184. embedded_flag = SFLAG_EMBEDDED;
  1185. do
  1186. {
  1187. embedded_cmd = embedded_list[embedded_read++];
  1188. value = embedded_cmd >> 8;
  1189. switch(embedded_cmd & 0x1f)
  1190. {
  1191. case EMBED_Y:
  1192. option_sayas = value;
  1193. break;
  1194. case EMBED_F:
  1195. option_emphasis = value;
  1196. break;
  1197. case EMBED_B:
  1198. // break command
  1199. if(value == 0)
  1200. pre_pause = 0; // break=none
  1201. else
  1202. pre_pause += value;
  1203. break;
  1204. }
  1205. } while((embedded_cmd & 0x80) == 0);
  1206. }
  1207. if(word[0] == 0)
  1208. {
  1209. // nothing to translate
  1210. word_phonemes[0] = 0;
  1211. return(0);
  1212. }
  1213. // after a $pause word attribute, ignore a $pause attribute on the next two words
  1214. if(prepause_timeout > 0)
  1215. prepause_timeout--;
  1216. if((option_sayas & 0xf0) == 0x10)
  1217. {
  1218. if(!(word_flags & FLAG_FIRST_WORD))
  1219. {
  1220. // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
  1221. pre_pause += 4;
  1222. }
  1223. }
  1224. if(word_flags & FLAG_FIRST_UPPER)
  1225. {
  1226. if((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6))
  1227. {
  1228. // indicate capital letter by raising pitch
  1229. if(embedded_flag)
  1230. embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
  1231. if((pitch_raised = option_capitals) == 3)
  1232. pitch_raised = 20; // default pitch raise for capitals
  1233. embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
  1234. embedded_flag = SFLAG_EMBEDDED;
  1235. }
  1236. }
  1237. p = (unsigned char *)translator->word_phonemes;
  1238. if(word_flags & FLAG_PHONEMES)
  1239. {
  1240. // The input is in phoneme mnemonics, not language text
  1241. int c1;
  1242. char lang_name[12];
  1243. if(memcmp(word,"_^_",3)==0)
  1244. {
  1245. // switch languages
  1246. word+=3;
  1247. for(ix=0;;)
  1248. {
  1249. c1 = *word++;
  1250. if((c1==' ') || (c1==0))
  1251. break;
  1252. lang_name[ix++] = tolower(c1);
  1253. }
  1254. lang_name[ix] = 0;
  1255. if((ix = LookupPhonemeTable(lang_name)) > 0)
  1256. {
  1257. SelectPhonemeTable(ix);
  1258. word_phonemes[0] = phonSWITCH;
  1259. word_phonemes[1] = ix;
  1260. word_phonemes[2] = 0;
  1261. }
  1262. }
  1263. else
  1264. {
  1265. EncodePhonemes(word,word_phonemes,bad_phoneme);
  1266. }
  1267. flags = FLAG_FOUND;
  1268. }
  1269. else
  1270. {
  1271. int c2;
  1272. ix = 0;
  1273. while(((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
  1274. word_copy_len = ix;
  1275. flags = translator->TranslateWord(word, next_pause, wtab);
  1276. if(flags & FLAG_SPELLWORD)
  1277. {
  1278. // re-translate the word as individual letters, separated by spaces
  1279. memcpy(word, word_copy, word_copy_len);
  1280. return(flags);
  1281. }
  1282. if((flags & FLAG_ALT2_TRANS) && ((sylimit = langopts.param[LOPT_COMBINE_WORDS]) > 0))
  1283. {
  1284. char *p2;
  1285. int ok = 1;
  1286. int flags2;
  1287. int c_word2;
  1288. char ph_buf[N_WORD_PHONEMES];
  1289. // LANG=cs,sk
  1290. // combine a preposition with the following word
  1291. p2 = word;
  1292. while(*p2 != ' ') p2++;
  1293. utf8_in(&c_word2, p2+1, 0); // first character of the next word;
  1294. if(!iswalpha(c_word2))
  1295. {
  1296. ok =0;
  1297. }
  1298. if(ok != 0)
  1299. {
  1300. if(sylimit & 0x100)
  1301. {
  1302. // only if the second word has $alt attribute
  1303. strcpy(ph_buf,word_phonemes);
  1304. flags2 = translator->TranslateWord(p2+1, 0, wtab+1);
  1305. if((flags2 & FLAG_ALT_TRANS) == 0)
  1306. {
  1307. ok = 0;
  1308. strcpy(word_phonemes,ph_buf);
  1309. }
  1310. }
  1311. if((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD))
  1312. {
  1313. // not if the next word is end-of-sentence
  1314. ok = 0;
  1315. }
  1316. }
  1317. if(ok)
  1318. {
  1319. *p2 = '-'; // replace next space by hyphen
  1320. flags = translator->TranslateWord(word, next_pause, wtab); // translate the combined word
  1321. if(CountSyllables(p) > (sylimit & 0xf))
  1322. {
  1323. // revert to separate words
  1324. *p2 = ' ';
  1325. flags = translator->TranslateWord(word, next_pause, wtab);
  1326. }
  1327. else
  1328. {
  1329. flags |= FLAG_SKIPWORDS;
  1330. dictionary_skipwords = 1;
  1331. }
  1332. }
  1333. }
  1334. if(p[0] == phonSWITCH)
  1335. {
  1336. // this word uses a different language
  1337. memcpy(word, word_copy, word_copy_len);
  1338. new_language = (char *)(&p[1]);
  1339. if(new_language[0]==0)
  1340. new_language = "en";
  1341. switch_phonemes = SetTranslator2(new_language);
  1342. if(switch_phonemes >= 0)
  1343. {
  1344. // re-translate the word using the new translator
  1345. flags = translator2->TranslateWord(word, next_pause, wtab);
  1346. strcpy((char *)p,translator2->word_phonemes);
  1347. if(p[0] == phonSWITCH)
  1348. {
  1349. // the second translator doesn't want to process this word
  1350. switch_phonemes = -1;
  1351. }
  1352. }
  1353. if(switch_phonemes < 0)
  1354. {
  1355. // language code is not recognised or 2nd translator won't translate it
  1356. p[0] = phonSCHWA; // just say something
  1357. p[1] = phonSCHWA;
  1358. p[2] = 0;
  1359. }
  1360. }
  1361. if(!(word_flags & FLAG_HYPHEN))
  1362. {
  1363. if(flags & FLAG_PAUSE1)
  1364. {
  1365. if(pre_pause < 1)
  1366. pre_pause = 1;
  1367. }
  1368. if((flags & FLAG_PREPAUSE) && (prepause_timeout == 0))
  1369. {
  1370. // the word is marked in the dictionary list with $pause
  1371. if(pre_pause < 4) pre_pause = 4;
  1372. prepause_timeout = 3;
  1373. }
  1374. }
  1375. if((option_emphasis >= 3) && (pre_pause < 1))
  1376. pre_pause = 1;
  1377. }
  1378. plist2 = &ph_list2[n_ph_list2];
  1379. stress = 0;
  1380. next_stress = 0;
  1381. srcix = 0;
  1382. max_stress = -1;
  1383. found_dict_flag = 0;
  1384. if(flags & FLAG_FOUND)
  1385. found_dict_flag = SFLAG_DICTIONARY;
  1386. while((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1387. {
  1388. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1389. // text, or because the word is marked in the dictionary lookup as a conjunction
  1390. if(pre_pause > 1)
  1391. {
  1392. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE);
  1393. pre_pause -= 2;
  1394. }
  1395. else
  1396. {
  1397. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_NOLINK);
  1398. pre_pause--;
  1399. }
  1400. end_stressed_vowel = 0; // forget about the previous word
  1401. prev_dict_flags = 0;
  1402. }
  1403. if((option_capitals==1) && (word_flags & FLAG_FIRST_UPPER))
  1404. {
  1405. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1406. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1407. if((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1]))
  1408. {
  1409. // word > 1 letter and all capitals
  1410. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1411. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1412. }
  1413. }
  1414. if(switch_phonemes >= 0)
  1415. {
  1416. // this word uses a different phoneme table
  1417. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1418. ph_list2[n_ph_list2++].tone_number = switch_phonemes; // temporary phoneme table number
  1419. }
  1420. // remove initial pause from a word if it follows a hyphen
  1421. if((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1422. p++;
  1423. while(((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1424. {
  1425. if(ph_code == 255)
  1426. continue; // unknown phoneme
  1427. // Add the phonemes to the first stage phoneme list (ph_list2)
  1428. ph = phoneme_tab[ph_code];
  1429. if(ph_code == phonSWITCH)
  1430. {
  1431. ph_list2[n_ph_list2].phcode = ph_code;
  1432. ph_list2[n_ph_list2].sourceix = 0;
  1433. ph_list2[n_ph_list2].synthflags = embedded_flag;
  1434. ph_list2[n_ph_list2++].tone_number = *p++;
  1435. }
  1436. else
  1437. if(ph->type == phSTRESS)
  1438. {
  1439. // don't add stress phonemes codes to the list, but give their stress
  1440. // value to the next vowel phoneme
  1441. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1442. if(ph->spect == 0)
  1443. next_stress = ph->std_length;
  1444. else
  1445. {
  1446. // for tone languages, the tone number for a syllable follows the vowel
  1447. if(prev_vowel >= 0)
  1448. {
  1449. ph_list2[prev_vowel].tone_number = ph_code;
  1450. }
  1451. else
  1452. {
  1453. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1454. }
  1455. }
  1456. }
  1457. else
  1458. if(ph_code == phonSYLLABIC)
  1459. {
  1460. // mark the previous phoneme as a syllabic consonant
  1461. prev_vowel = n_ph_list2-1;
  1462. ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
  1463. ph_list2[prev_vowel].stress = next_stress;
  1464. }
  1465. else
  1466. if(ph_code == phonLENGTHEN)
  1467. {
  1468. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1469. }
  1470. else
  1471. if(ph_code == phonEND_WORD)
  1472. {
  1473. // a || symbol in a phoneme string was used to indicate a word boundary
  1474. // Don't add this phoneme to the list, but make sure the next phoneme has
  1475. // a newword indication
  1476. srcix = source_ix+1;
  1477. }
  1478. else
  1479. if(ph_code == phonX1)
  1480. {
  1481. // a language specific action
  1482. if(langopts.param[LOPT_IT_DOUBLING])
  1483. {
  1484. flags |= FLAG_DOUBLING;
  1485. }
  1486. }
  1487. else
  1488. {
  1489. ph_list2[n_ph_list2].phcode = ph_code;
  1490. ph_list2[n_ph_list2].tone_number = 0;
  1491. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1492. embedded_flag = 0;
  1493. ph_list2[n_ph_list2].sourceix = srcix;
  1494. srcix = 0;
  1495. if(ph->type == phVOWEL)
  1496. {
  1497. stress = next_stress;
  1498. next_stress = 0;
  1499. if((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1500. ph_list2[n_ph_list2-1].stress = stress; // set stress for previous consonant
  1501. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1502. prev_vowel = n_ph_list2;
  1503. if(stress > max_stress)
  1504. {
  1505. max_stress = stress;
  1506. max_stress_ix = n_ph_list2;
  1507. }
  1508. if(next_tone != 0)
  1509. {
  1510. ph_list2[n_ph_list2].tone_number = next_tone;
  1511. next_tone=0;
  1512. }
  1513. }
  1514. else
  1515. {
  1516. if(first_phoneme && langopts.param[LOPT_IT_DOUBLING])
  1517. {
  1518. if(((prev_dict_flags & FLAG_DOUBLING) && (langopts.param[LOPT_IT_DOUBLING] & 1)) ||
  1519. (end_stressed_vowel && (langopts.param[LOPT_IT_DOUBLING] & 2)))
  1520. {
  1521. // italian, double the initial consonant if the previous word ends with a
  1522. // stressed vowel, or is marked with a flag
  1523. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1524. }
  1525. }
  1526. }
  1527. ph_list2[n_ph_list2].stress = stress;
  1528. n_ph_list2++;
  1529. first_phoneme = 0;
  1530. }
  1531. }
  1532. plist2->sourceix = source_ix; // ????
  1533. end_stressed_vowel = 0;
  1534. if((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
  1535. {
  1536. end_stressed_vowel = 1; // word ends with a stressed vowel
  1537. }
  1538. if(switch_phonemes >= 0)
  1539. {
  1540. // this word uses a different phoneme table, now switch back
  1541. SelectPhonemeTable(voice->phoneme_tab_ix);
  1542. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1543. ph_list2[n_ph_list2++].tone_number = voice->phoneme_tab_ix; // original phoneme table number
  1544. }
  1545. if(pitch_raised > 0)
  1546. {
  1547. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1548. SetPlist2(&ph_list2[n_ph_list2],phonPAUSE_SHORT);
  1549. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1550. }
  1551. if(flags & FLAG_STRESS_END2)
  1552. {
  1553. // this's word's stress could be increased later
  1554. ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
  1555. }
  1556. prev_dict_flags = flags;
  1557. return(flags);
  1558. } // end of TranslateWord2
  1559. static int EmbeddedCommand(unsigned int &source_index)
  1560. {//===================================================
  1561. // An embedded command to change the pitch, volume, etc.
  1562. // returns number of commands added to embedded_list
  1563. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1564. const char *commands = "PSARHTIVYMUBF";
  1565. int value = -1;
  1566. int sign = 0;
  1567. unsigned char c;
  1568. char *p;
  1569. int cmd;
  1570. c = source[source_index];
  1571. if(c == '+')
  1572. {
  1573. sign = 0x40;
  1574. source_index++;
  1575. }
  1576. else
  1577. if(c == '-')
  1578. {
  1579. sign = 0x60;
  1580. source_index++;
  1581. }
  1582. if(isdigit(source[source_index]))
  1583. {
  1584. value = atoi(&source[source_index]);
  1585. while(isdigit(source[source_index]))
  1586. source_index++;
  1587. }
  1588. c = source[source_index++];
  1589. if(embedded_ix >= (N_EMBEDDED_LIST - 2))
  1590. return(0); // list is full
  1591. if((p = strchr_w(commands,c)) == NULL)
  1592. return(0);
  1593. cmd = (p - commands)+1;
  1594. if(value == -1)
  1595. {
  1596. value = embedded_default[cmd];
  1597. sign = 0;
  1598. }
  1599. if(cmd == EMBED_Y)
  1600. {
  1601. option_sayas2 = value;
  1602. count_sayas_digits = 0;
  1603. }
  1604. if(cmd == EMBED_F)
  1605. {
  1606. if(value >= 3)
  1607. word_emphasis = FLAG_EMPHASIZED;
  1608. else
  1609. word_emphasis = 0;
  1610. }
  1611. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1612. return(1);
  1613. } // end of EmbeddedCommand
  1614. int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, int *insert)
  1615. {//==================================================================================
  1616. int ix;
  1617. unsigned int word;
  1618. unsigned int new_c, c2, c_lower;
  1619. int upper_case = 0;
  1620. static int ignore_next = 0;
  1621. const unsigned int *replace_chars;
  1622. if(ignore_next)
  1623. {
  1624. ignore_next = 0;
  1625. return(8);
  1626. }
  1627. if(c == 0) return(0);
  1628. if((replace_chars = tr->langopts.replace_chars) == NULL)
  1629. return(c);
  1630. // there is a list of character codes to be substituted with alternative codes
  1631. if(iswupper(c_lower = c))
  1632. {
  1633. c_lower = towlower(c);
  1634. upper_case = 1;
  1635. }
  1636. new_c = 0;
  1637. for(ix=0; (word = replace_chars[ix]) != 0; ix+=2)
  1638. {
  1639. if(c_lower == (word & 0xffff))
  1640. {
  1641. if((word >> 16) == 0)
  1642. {
  1643. new_c = replace_chars[ix+1];
  1644. break;
  1645. }
  1646. if((word >> 16) == (unsigned int)towlower(next_in))
  1647. {
  1648. new_c = replace_chars[ix+1];
  1649. ignore_next = 1;
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. if(new_c == 0)
  1655. return(c); // no substitution
  1656. if(new_c & 0xffe00000)
  1657. {
  1658. // there is a second character to be inserted
  1659. // don't convert the case of the second character unless the next letter is also upper case
  1660. c2 = new_c >> 16;
  1661. if(upper_case && iswupper(next_in))
  1662. c2 = towupper(c2);
  1663. *insert = c2;
  1664. new_c &= 0xffff;
  1665. }
  1666. if(upper_case)
  1667. new_c = towupper(new_c);
  1668. return(new_c);
  1669. }
  1670. int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert)
  1671. {//=====================================================================================================
  1672. // To allow language specific examination and replacement of characters
  1673. int code;
  1674. int initial;
  1675. int medial;
  1676. int final;
  1677. static const unsigned char hangul_compatibility[0x34] = {
  1678. 0, 0x00,0x01,0xaa,0x02,0xac,0xad,0x03,
  1679. 0x04,0x05,0xb0,0xb1,0xb2,0xb3,0xb4,0xb4,
  1680. 0xb6,0x06,0x07,0x08,0xb9,0x09,0x0a,0xbc,
  1681. 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x61,
  1682. 0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
  1683. 0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,
  1684. 0x72,0x73,0x74,0x75 };
  1685. switch(translator_name)
  1686. {
  1687. case L('a','f'):
  1688. // look for 'n and replace by a special character (unicode: schwa)
  1689. if(!iswalpha(prev_in))
  1690. {
  1691. if((c == '\'') && (next_in == 'n'))
  1692. {
  1693. // n preceded by either apostrophe or U2019 "right single quotation mark"
  1694. ptr[0] = ' '; // delete the n
  1695. return(0x0259); // replace ' by unicode schwa character
  1696. }
  1697. }
  1698. break;
  1699. case L('k','o'):
  1700. if(((code = c - 0xac00) >= 0) && (c <= 0xd7af))
  1701. {
  1702. // break a syllable hangul into 2 or 3 individual jamo
  1703. initial = (code/28)/21;
  1704. medial = (code/28) % 21;
  1705. final = code % 28;
  1706. if(initial == 11)
  1707. {
  1708. // null initial
  1709. c = medial + 0x1161;
  1710. if(final > 0)
  1711. *insert = final + 0x11a7;
  1712. }
  1713. else
  1714. {
  1715. // extact the initial and insert the remainder with a null initial
  1716. c = initial + 0x1100;
  1717. *insert = (11*28*21) + (medial*28) + final + 0xac00;
  1718. }
  1719. return(c);
  1720. }
  1721. else
  1722. if(((code = c - 0x3130) >= 0) && (code < 0x34))
  1723. {
  1724. // Hangul compatibility jamo
  1725. return(hangul_compatibility[code] + 0x1100);
  1726. }
  1727. break;
  1728. }
  1729. return(SubstituteChar(this,c,next_in,insert));
  1730. }
  1731. void *Translator::TranslateClause(FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
  1732. {//======================================================================================================
  1733. int ix;
  1734. int c;
  1735. int cc;
  1736. unsigned int source_index=0;
  1737. unsigned int prev_source_index=0;
  1738. int prev_in;
  1739. int prev_out=' ';
  1740. int prev_out2;
  1741. int prev_in2=0;
  1742. int next_in;
  1743. int char_inserted=0;
  1744. int clause_pause;
  1745. int pre_pause=0;
  1746. int pre_pause_add=0;
  1747. int word_mark = 0;
  1748. int all_upper_case=FLAG_ALL_UPPER;
  1749. int finished;
  1750. int single_quoted;
  1751. int phoneme_mode = 0;
  1752. int dict_flags; // returned from dictionary lookup
  1753. int word_flags; // set here
  1754. int next_word_flags;
  1755. int embedded_count = 0;
  1756. int letter_count = 0;
  1757. int space_inserted = 0;
  1758. int syllable_marked = 0;
  1759. char *word;
  1760. char *p;
  1761. int j, k;
  1762. int n_digits;
  1763. short charix[N_TR_SOURCE+1];
  1764. WORD_TAB words[N_CLAUSE_WORDS];
  1765. int word_count=0; // index into words
  1766. char sbuf[N_TR_SOURCE];
  1767. int terminator;
  1768. int tone;
  1769. p_textinput = (char *)vp_input;
  1770. p_wchar_input = (wchar_t *)vp_input;
  1771. embedded_ix = 0;
  1772. embedded_read = 0;
  1773. if((clause_start_char = count_characters) < 0)
  1774. clause_start_char = 0;
  1775. clause_start_word = count_words + 1;
  1776. for(ix=0; ix<N_TR_SOURCE; ix++)
  1777. charix[ix] = 0;
  1778. terminator = translator->ReadClause(f_text,source,charix,N_TR_SOURCE);
  1779. charix[N_TR_SOURCE] = count_characters;
  1780. clause_pause = (terminator & 0xfff) * 10; // mS
  1781. tone = (terminator >> 12) & 0xf;
  1782. for(p=source; *p != 0; p++)
  1783. {
  1784. if(!isspace2(*p))
  1785. {
  1786. break;
  1787. }
  1788. }
  1789. if(*p == 0)
  1790. {
  1791. // No characters except spaces. This is not a sentence.
  1792. // Don't add this pause, just make up the previous pause to this value;
  1793. clause_pause -= max_clause_pause;
  1794. if(clause_pause < 0)
  1795. clause_pause = 0;
  1796. terminator &= ~CLAUSE_BIT_SENTENCE; // clear sentence bit
  1797. max_clause_pause += clause_pause;
  1798. }
  1799. else
  1800. {
  1801. max_clause_pause = clause_pause;
  1802. }
  1803. if(new_sentence)
  1804. {
  1805. count_sentences++;
  1806. if(skip_sentences > 0)
  1807. {
  1808. skip_sentences--;
  1809. if(skip_sentences == 0)
  1810. skipping_text = 0;
  1811. }
  1812. }
  1813. memset(&ph_list2[0],0,sizeof(ph_list2[0]));
  1814. ph_list2[0].phcode = phonPAUSE_SHORT;
  1815. n_ph_list2 = 1;
  1816. prev_last_stress = 0;
  1817. prepause_timeout = 0;
  1818. word_count = 0;
  1819. single_quoted = 0;
  1820. word_flags = 0;
  1821. next_word_flags = 0;
  1822. expect_verb=0;
  1823. expect_noun=0;
  1824. expect_past=0;
  1825. expect_verb_s=0;
  1826. end_stressed_vowel=0;
  1827. prev_dict_flags = 0;
  1828. sbuf[0] = 0;
  1829. sbuf[1] = ' ';
  1830. sbuf[2] = ' ';
  1831. ix = 3;
  1832. prev_in = ' ';
  1833. words[0].start = ix;
  1834. words[0].flags = 0;
  1835. finished = 0;
  1836. for(j=0; charix[j]==0; j++);
  1837. words[0].sourceix = charix[j];
  1838. k = 0;
  1839. while(charix[j] != 0)
  1840. {
  1841. // count the number of characters (excluding multibyte continuation bytes)
  1842. if(charix[j++] != -1)
  1843. k++;
  1844. }
  1845. words[0].length = k;
  1846. while(!finished && (ix < (int)sizeof(sbuf))&& (n_ph_list2 < N_PHONEME_LIST-4))
  1847. {
  1848. prev_out2 = prev_out;
  1849. utf8_in(&prev_out,&sbuf[ix-1],1); // prev_out = sbuf[ix-1];
  1850. if(langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2))
  1851. {
  1852. // tone numbers can be part of a word, consider them as alphabetic
  1853. prev_out = 'a';
  1854. }
  1855. if(prev_in2 != 0)
  1856. {
  1857. prev_in = prev_in2;
  1858. prev_in2 = 0;
  1859. }
  1860. else
  1861. if(source_index > 0)
  1862. {
  1863. utf8_in(&prev_in,&source[source_index-1],1); // prev_in = source[source_index-1];
  1864. }
  1865. prev_source_index = source_index;
  1866. if(char_inserted)
  1867. {
  1868. c = char_inserted;
  1869. char_inserted = 0;
  1870. }
  1871. else
  1872. {
  1873. source_index += utf8_in(&cc,&source[source_index],0); // cc = source[source_index++];
  1874. c = cc;
  1875. }
  1876. utf8_in(&next_in,&source[source_index],0);
  1877. if((c == CTRL_EMBEDDED) || (c == ctrl_embedded))
  1878. {
  1879. // start of embedded command in the text
  1880. int srcix = source_index-1;
  1881. if(prev_in != ' ')
  1882. {
  1883. c = ' ';
  1884. prev_in2 = c;
  1885. source_index--;
  1886. }
  1887. else
  1888. {
  1889. embedded_count += EmbeddedCommand(source_index);
  1890. prev_in2 = prev_in;
  1891. // replace the embedded command by spaces
  1892. memset(&source[srcix],' ',source_index-srcix);
  1893. source_index = srcix;
  1894. continue;
  1895. }
  1896. }
  1897. if(option_sayas2 == SAYAS_KEY)
  1898. {
  1899. if(((c == '_') || (c == '-')) && IsAlpha(prev_in))
  1900. {
  1901. c = ' ';
  1902. }
  1903. c = towlower2(c);
  1904. }
  1905. if(phoneme_mode)
  1906. {
  1907. all_upper_case = FLAG_PHONEMES;
  1908. if((c == ']') && (next_in == ']'))
  1909. {
  1910. phoneme_mode = 0;
  1911. source_index++;
  1912. c = ' ';
  1913. }
  1914. }
  1915. else
  1916. if((option_sayas2 & 0xf0) == SAYAS_DIGITS)
  1917. {
  1918. if(iswdigit(c))
  1919. {
  1920. count_sayas_digits++;
  1921. if(count_sayas_digits > (option_sayas2 & 0xf))
  1922. {
  1923. // break after the specified number of digits
  1924. c = ' ';
  1925. space_inserted = 1;
  1926. count_sayas_digits = 0;
  1927. }
  1928. }
  1929. else
  1930. {
  1931. count_sayas_digits = 0;
  1932. if(iswdigit(prev_out))
  1933. {
  1934. c = ' ';
  1935. space_inserted = 1;
  1936. }
  1937. }
  1938. }
  1939. else
  1940. if((option_sayas2 & 0x30) == 0)
  1941. {
  1942. // speak as words
  1943. #ifdef deleted
  1944. if((c == '/') && (langopts.testing & 2) && IsDigit09(next_in) && IsAlpha(prev_out))
  1945. {
  1946. // TESTING, explicit indication of stressed syllable by /2 after the word
  1947. word_mark = next_in-'0';
  1948. source_index++;
  1949. c = ' ';
  1950. }
  1951. #endif
  1952. if((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
  1953. c = '\''; // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe
  1954. if((c == '?') && IsAlpha(prev_out) && IsAlpha(next_in))
  1955. {
  1956. // ? between two letters may be a smart-quote replaced by ?
  1957. c = '\'';
  1958. }
  1959. c = TranslateChar(&source[source_index], prev_in,c, next_in, &char_inserted); // optional language specific function
  1960. if(c == 8)
  1961. continue; // ignore this character
  1962. if(char_inserted)
  1963. next_in = char_inserted;
  1964. // allow certain punctuation within a word (usually only apostrophe)
  1965. if(!IsAlpha(c) && !IsSpace(c) && (wcschr(punct_within_word,c) == 0))
  1966. {
  1967. if(IsAlpha(prev_out))
  1968. {
  1969. if(langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in))
  1970. {
  1971. // allow a tone number as part of the word
  1972. }
  1973. else
  1974. {
  1975. c = ' '; // ensure we have an end-of-word terminator
  1976. space_inserted = 1;
  1977. }
  1978. }
  1979. }
  1980. if(iswdigit(prev_out))
  1981. {
  1982. if(!iswdigit(c) && (c != '.') && (c != ','))
  1983. {
  1984. c = ' '; // terminate digit string with a space
  1985. space_inserted = 1;
  1986. }
  1987. }
  1988. if((c == '[') && (next_in == '[') && option_phoneme_input)
  1989. {
  1990. phoneme_mode = FLAG_PHONEMES;
  1991. source_index++;
  1992. continue;
  1993. }
  1994. if(c == 0)
  1995. {
  1996. finished = 1;
  1997. c = ' ';
  1998. }
  1999. else
  2000. if(IsAlpha(c))
  2001. {
  2002. if(!IsAlpha(prev_out) || (langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040))))
  2003. {
  2004. if(wcschr(punct_within_word,prev_out) == 0)
  2005. letter_count = 0; // don't reset count for an apostrophy within a word
  2006. if((prev_out != ' ') && (wcschr(punct_within_word,prev_out) == 0))
  2007. {
  2008. // start of word, insert space if not one there already
  2009. c = ' ';
  2010. space_inserted = 1;
  2011. }
  2012. else
  2013. {
  2014. if(iswupper(c))
  2015. word_flags |= FLAG_FIRST_UPPER;
  2016. if((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in))
  2017. {
  2018. // word, following a number, but with a space between
  2019. // Add an extra space, to distinguish "2 a" from "2a"
  2020. sbuf[ix++] = ' ';
  2021. words[word_count].start++;
  2022. }
  2023. }
  2024. }
  2025. letter_count++;
  2026. if(iswupper(c))
  2027. {
  2028. c = towlower2(c);
  2029. if(langopts.param[LOPT_SYLLABLE_CAPS])
  2030. {
  2031. if(syllable_marked == 0)
  2032. {
  2033. char_inserted = c;
  2034. c = 0x2c8; // stress marker
  2035. syllable_marked = 1;
  2036. }
  2037. }
  2038. else
  2039. {
  2040. if(iswlower(prev_in))
  2041. {
  2042. c = ' '; // lower case followed by upper case, treat as new word
  2043. space_inserted = 1;
  2044. prev_in2 = c;
  2045. }
  2046. else
  2047. if((c != ' ') && iswupper(prev_in) && iswlower(next_in) &&
  2048. (memcmp(&source[source_index],"s ",2) != 0)) // ENGLISH specific plural
  2049. {
  2050. c = ' '; // change from upper to lower case, start new word at the last uppercase
  2051. space_inserted = 1;
  2052. prev_in2 = c;
  2053. next_word_flags |= FLAG_NOSPACE;
  2054. }
  2055. }
  2056. }
  2057. else
  2058. {
  2059. if((all_upper_case) && (letter_count > 2))
  2060. {