eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

numbers.c 56KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. /*
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2016 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <ctype.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <wctype.h>
  27. #include <espeak-ng/espeak_ng.h>
  28. #include <espeak-ng/speak_lib.h>
  29. #include <espeak-ng/encoding.h>
  30. #include "speech.h"
  31. #include "phoneme.h"
  32. #include "voice.h"
  33. #include "synthesize.h"
  34. #include "translate.h"
  35. #define M_LIGATURE 0x8000
  36. #define M_NAME 0
  37. #define M_SMALLCAP 1
  38. #define M_TURNED 2
  39. #define M_REVERSED 3
  40. #define M_CURL 4
  41. #define M_ACUTE 5
  42. #define M_BREVE 6
  43. #define M_CARON 7
  44. #define M_CEDILLA 8
  45. #define M_CIRCUMFLEX 9
  46. #define M_DIAERESIS 10
  47. #define M_DOUBLE_ACUTE 11
  48. #define M_DOT_ABOVE 12
  49. #define M_GRAVE 13
  50. #define M_MACRON 14
  51. #define M_OGONEK 15
  52. #define M_RING 16
  53. #define M_STROKE 17
  54. #define M_TILDE 18
  55. #define M_BAR 19
  56. #define M_RETROFLEX 20
  57. #define M_HOOK 21
  58. #define M_MIDDLE_DOT M_DOT_ABOVE // duplicate of M_DOT_ABOVE
  59. #define M_IMPLOSIVE M_HOOK
  60. static int n_digit_lookup;
  61. static char *digit_lookup;
  62. static int speak_missing_thousands;
  63. static int number_control;
  64. typedef struct {
  65. const char *name;
  66. int accent_flags; // bit 0, say before the letter name
  67. } ACCENTS;
  68. // these are tokens to look up in the *_list file.
  69. static ACCENTS accents_tab[] = {
  70. { "_lig", 1 },
  71. { "_smc", 0 }, // smallcap
  72. { "_tur", 0 }, // turned
  73. { "_rev", 0 }, // reversed
  74. { "_crl", 0 }, // curl
  75. { "_acu", 0 }, // acute
  76. { "_brv", 0 }, // breve
  77. { "_hac", 0 }, // caron/hacek
  78. { "_ced", 0 }, // cedilla
  79. { "_cir", 0 }, // circumflex
  80. { "_dia", 0 }, // diaeresis
  81. { "_ac2", 0 }, // double acute
  82. { "_dot", 0 }, // dot
  83. { "_grv", 0 }, // grave
  84. { "_mcn", 0 }, // macron
  85. { "_ogo", 0 }, // ogonek
  86. { "_rng", 0 }, // ring
  87. { "_stk", 0 }, // stroke
  88. { "_tld", 0 }, // tilde
  89. { "_bar", 0 }, // bar
  90. { "_rfx", 0 }, // retroflex
  91. { "_hok", 0 }, // hook
  92. };
  93. #define CAPITAL 0
  94. #define LETTER(ch, mod1, mod2) (ch-59)+(mod1 << 6)+(mod2 << 11)
  95. #define LIGATURE(ch1, ch2, mod1) (ch1-59)+((ch2-59) << 6)+(mod1 << 12)+M_LIGATURE
  96. #define L_ALPHA 60 // U+3B1
  97. #define L_SCHWA 61 // U+259
  98. #define L_OPEN_E 62 // U+25B
  99. #define L_GAMMA 63 // U+3B3
  100. #define L_IOTA 64 // U+3B9
  101. #define L_PHI 67 // U+3C6
  102. #define L_ESH 68 // U+283
  103. #define L_UPSILON 69 // U+3C5
  104. #define L_EZH 70 // U+292
  105. #define L_GLOTTAL 71 // U+294
  106. #define L_RTAP 72 // U+27E
  107. #define L_RLONG 73 // U+27C
  108. static const short non_ascii_tab[] = {
  109. 0,
  110. 0x3b1, 0x259, 0x25b, 0x3b3, 0x3b9, 0x153, 0x3c9,
  111. 0x3c6, 0x283, 0x3c5, 0x292, 0x294, 0x27e, 0x27c
  112. };
  113. // characters U+00e0 to U+017f
  114. static const unsigned short letter_accents_0e0[] = {
  115. LETTER('a', M_GRAVE, 0), // U+00e0
  116. LETTER('a', M_ACUTE, 0),
  117. LETTER('a', M_CIRCUMFLEX, 0),
  118. LETTER('a', M_TILDE, 0),
  119. LETTER('a', M_DIAERESIS, 0),
  120. LETTER('a', M_RING, 0),
  121. LIGATURE('a', 'e', 0),
  122. LETTER('c', M_CEDILLA, 0),
  123. LETTER('e', M_GRAVE, 0),
  124. LETTER('e', M_ACUTE, 0),
  125. LETTER('e', M_CIRCUMFLEX, 0),
  126. LETTER('e', M_DIAERESIS, 0),
  127. LETTER('i', M_GRAVE, 0),
  128. LETTER('i', M_ACUTE, 0),
  129. LETTER('i', M_CIRCUMFLEX, 0),
  130. LETTER('i', M_DIAERESIS, 0),
  131. LETTER('d', M_NAME, 0), // eth U+00f0
  132. LETTER('n', M_TILDE, 0),
  133. LETTER('o', M_GRAVE, 0),
  134. LETTER('o', M_ACUTE, 0),
  135. LETTER('o', M_CIRCUMFLEX, 0),
  136. LETTER('o', M_TILDE, 0),
  137. LETTER('o', M_DIAERESIS, 0),
  138. 0, // division sign
  139. LETTER('o', M_STROKE, 0),
  140. LETTER('u', M_GRAVE, 0),
  141. LETTER('u', M_ACUTE, 0),
  142. LETTER('u', M_CIRCUMFLEX, 0),
  143. LETTER('u', M_DIAERESIS, 0),
  144. LETTER('y', M_ACUTE, 0),
  145. LETTER('t', M_NAME, 0), // thorn
  146. LETTER('y', M_DIAERESIS, 0),
  147. CAPITAL, // U+0100
  148. LETTER('a', M_MACRON, 0),
  149. CAPITAL,
  150. LETTER('a', M_BREVE, 0),
  151. CAPITAL,
  152. LETTER('a', M_OGONEK, 0),
  153. CAPITAL,
  154. LETTER('c', M_ACUTE, 0),
  155. CAPITAL,
  156. LETTER('c', M_CIRCUMFLEX, 0),
  157. CAPITAL,
  158. LETTER('c', M_DOT_ABOVE, 0),
  159. CAPITAL,
  160. LETTER('c', M_CARON, 0),
  161. CAPITAL,
  162. LETTER('d', M_CARON, 0),
  163. CAPITAL, // U+0110
  164. LETTER('d', M_STROKE, 0),
  165. CAPITAL,
  166. LETTER('e', M_MACRON, 0),
  167. CAPITAL,
  168. LETTER('e', M_BREVE, 0),
  169. CAPITAL,
  170. LETTER('e', M_DOT_ABOVE, 0),
  171. CAPITAL,
  172. LETTER('e', M_OGONEK, 0),
  173. CAPITAL,
  174. LETTER('e', M_CARON, 0),
  175. CAPITAL,
  176. LETTER('g', M_CIRCUMFLEX, 0),
  177. CAPITAL,
  178. LETTER('g', M_BREVE, 0),
  179. CAPITAL, // U+0120
  180. LETTER('g', M_DOT_ABOVE, 0),
  181. CAPITAL,
  182. LETTER('g', M_CEDILLA, 0),
  183. CAPITAL,
  184. LETTER('h', M_CIRCUMFLEX, 0),
  185. CAPITAL,
  186. LETTER('h', M_STROKE, 0),
  187. CAPITAL,
  188. LETTER('i', M_TILDE, 0),
  189. CAPITAL,
  190. LETTER('i', M_MACRON, 0),
  191. CAPITAL,
  192. LETTER('i', M_BREVE, 0),
  193. CAPITAL,
  194. LETTER('i', M_OGONEK, 0),
  195. CAPITAL, // U+0130
  196. LETTER('i', M_NAME, 0), // dotless i
  197. CAPITAL,
  198. LIGATURE('i', 'j', 0),
  199. CAPITAL,
  200. LETTER('j', M_CIRCUMFLEX, 0),
  201. CAPITAL,
  202. LETTER('k', M_CEDILLA, 0),
  203. LETTER('k', M_NAME, 0), // kra
  204. CAPITAL,
  205. LETTER('l', M_ACUTE, 0),
  206. CAPITAL,
  207. LETTER('l', M_CEDILLA, 0),
  208. CAPITAL,
  209. LETTER('l', M_CARON, 0),
  210. CAPITAL,
  211. LETTER('l', M_MIDDLE_DOT, 0), // U+0140
  212. CAPITAL,
  213. LETTER('l', M_STROKE, 0),
  214. CAPITAL,
  215. LETTER('n', M_ACUTE, 0),
  216. CAPITAL,
  217. LETTER('n', M_CEDILLA, 0),
  218. CAPITAL,
  219. LETTER('n', M_CARON, 0),
  220. LETTER('n', M_NAME, 0), // apostrophe n
  221. CAPITAL,
  222. LETTER('n', M_NAME, 0), // eng
  223. CAPITAL,
  224. LETTER('o', M_MACRON, 0),
  225. CAPITAL,
  226. LETTER('o', M_BREVE, 0),
  227. CAPITAL, // U+0150
  228. LETTER('o', M_DOUBLE_ACUTE, 0),
  229. CAPITAL,
  230. LIGATURE('o', 'e', 0),
  231. CAPITAL,
  232. LETTER('r', M_ACUTE, 0),
  233. CAPITAL,
  234. LETTER('r', M_CEDILLA, 0),
  235. CAPITAL,
  236. LETTER('r', M_CARON, 0),
  237. CAPITAL,
  238. LETTER('s', M_ACUTE, 0),
  239. CAPITAL,
  240. LETTER('s', M_CIRCUMFLEX, 0),
  241. CAPITAL,
  242. LETTER('s', M_CEDILLA, 0),
  243. CAPITAL, // U+0160
  244. LETTER('s', M_CARON, 0),
  245. CAPITAL,
  246. LETTER('t', M_CEDILLA, 0),
  247. CAPITAL,
  248. LETTER('t', M_CARON, 0),
  249. CAPITAL,
  250. LETTER('t', M_STROKE, 0),
  251. CAPITAL,
  252. LETTER('u', M_TILDE, 0),
  253. CAPITAL,
  254. LETTER('u', M_MACRON, 0),
  255. CAPITAL,
  256. LETTER('u', M_BREVE, 0),
  257. CAPITAL,
  258. LETTER('u', M_RING, 0),
  259. CAPITAL, // U+0170
  260. LETTER('u', M_DOUBLE_ACUTE, 0),
  261. CAPITAL,
  262. LETTER('u', M_OGONEK, 0),
  263. CAPITAL,
  264. LETTER('w', M_CIRCUMFLEX, 0),
  265. CAPITAL,
  266. LETTER('y', M_CIRCUMFLEX, 0),
  267. CAPITAL, // Y-DIAERESIS
  268. CAPITAL,
  269. LETTER('z', M_ACUTE, 0),
  270. CAPITAL,
  271. LETTER('z', M_DOT_ABOVE, 0),
  272. CAPITAL,
  273. LETTER('z', M_CARON, 0),
  274. LETTER('s', M_NAME, 0), // long-s U+17f
  275. };
  276. // characters U+0250 to U+029F
  277. static const unsigned short letter_accents_250[] = {
  278. LETTER('a', M_TURNED, 0), // U+250
  279. LETTER(L_ALPHA, 0, 0),
  280. LETTER(L_ALPHA, M_TURNED, 0),
  281. LETTER('b', M_IMPLOSIVE, 0),
  282. 0, // open-o
  283. LETTER('c', M_CURL, 0),
  284. LETTER('d', M_RETROFLEX, 0),
  285. LETTER('d', M_IMPLOSIVE, 0),
  286. LETTER('e', M_REVERSED, 0), // U+258
  287. 0, // schwa
  288. LETTER(L_SCHWA, M_HOOK, 0),
  289. 0, // open-e
  290. LETTER(L_OPEN_E, M_REVERSED, 0),
  291. LETTER(L_OPEN_E, M_HOOK, M_REVERSED),
  292. 0,
  293. LETTER('j', M_BAR, 0),
  294. LETTER('g', M_IMPLOSIVE, 0), // U+260
  295. LETTER('g', 0, 0),
  296. LETTER('g', M_SMALLCAP, 0),
  297. LETTER(L_GAMMA, 0, 0),
  298. 0, // ramshorn
  299. LETTER('h', M_TURNED, 0),
  300. LETTER('h', M_HOOK, 0),
  301. 0,
  302. LETTER('i', M_BAR, 0), // U+268
  303. LETTER(L_IOTA, 0, 0),
  304. LETTER('i', M_SMALLCAP, 0),
  305. LETTER('l', M_TILDE, 0),
  306. LETTER('l', M_BAR, 0),
  307. LETTER('l', M_RETROFLEX, 0),
  308. LIGATURE('l', 'z', 0),
  309. LETTER('m', M_TURNED, 0),
  310. 0,
  311. LETTER('m', M_HOOK, 0),
  312. 0,
  313. LETTER('n', M_RETROFLEX, 0),
  314. LETTER('n', M_SMALLCAP, 0),
  315. LETTER('o', M_BAR, 0),
  316. LIGATURE('o', 'e', M_SMALLCAP),
  317. 0,
  318. LETTER(L_PHI, 0, 0), // U+278
  319. LETTER('r', M_TURNED, 0),
  320. LETTER(L_RLONG, M_TURNED, 0),
  321. LETTER('r', M_RETROFLEX, M_TURNED),
  322. 0,
  323. LETTER('r', M_RETROFLEX, 0),
  324. 0, // r-tap
  325. LETTER(L_RTAP, M_REVERSED, 0),
  326. LETTER('r', M_SMALLCAP, 0), // U+280
  327. LETTER('r', M_TURNED, M_SMALLCAP),
  328. LETTER('s', M_RETROFLEX, 0),
  329. 0, // esh
  330. LETTER('j', M_HOOK, 0),
  331. LETTER(L_ESH, M_REVERSED, 0),
  332. LETTER(L_ESH, M_CURL, 0),
  333. LETTER('t', M_TURNED, 0),
  334. LETTER('t', M_RETROFLEX, 0), // U+288
  335. LETTER('u', M_BAR, 0),
  336. LETTER(L_UPSILON, 0, 0),
  337. LETTER('v', M_HOOK, 0),
  338. LETTER('v', M_TURNED, 0),
  339. LETTER('w', M_TURNED, 0),
  340. LETTER('y', M_TURNED, 0),
  341. LETTER('y', M_SMALLCAP, 0),
  342. LETTER('z', M_RETROFLEX, 0), // U+290
  343. LETTER('z', M_CURL, 0),
  344. 0, // ezh
  345. LETTER(L_EZH, M_CURL, 0),
  346. 0, // glottal stop
  347. LETTER(L_GLOTTAL, M_REVERSED, 0),
  348. LETTER(L_GLOTTAL, M_TURNED, 0),
  349. 0,
  350. 0, // bilabial click U+298
  351. LETTER('b', M_SMALLCAP, 0),
  352. 0,
  353. LETTER('g', M_IMPLOSIVE, M_SMALLCAP),
  354. LETTER('h', M_SMALLCAP, 0),
  355. LETTER('j', M_CURL, 0),
  356. LETTER('k', M_TURNED, 0),
  357. LETTER('l', M_SMALLCAP, 0),
  358. LETTER('q', M_HOOK, 0), // U+2a0
  359. LETTER(L_GLOTTAL, M_STROKE, 0),
  360. LETTER(L_GLOTTAL, M_STROKE, M_REVERSED),
  361. LIGATURE('d', 'z', 0),
  362. 0, // dezh
  363. LIGATURE('d', 'z', M_CURL),
  364. LIGATURE('t', 's', 0),
  365. 0, // tesh
  366. LIGATURE('t', 's', M_CURL),
  367. };
  368. static int LookupLetter2(Translator *tr, unsigned int letter, char *ph_buf)
  369. {
  370. int len;
  371. char single_letter[10];
  372. single_letter[0] = 0;
  373. single_letter[1] = '_';
  374. len = utf8_out(letter, &single_letter[2]);
  375. single_letter[len+2] = ' ';
  376. single_letter[len+3] = 0;
  377. if (Lookup(tr, &single_letter[1], ph_buf) == 0) {
  378. single_letter[1] = ' ';
  379. if (Lookup(tr, &single_letter[2], ph_buf) == 0)
  380. TranslateRules(tr, &single_letter[2], ph_buf, 20, NULL, 0, NULL);
  381. }
  382. return ph_buf[0];
  383. }
  384. void LookupAccentedLetter(Translator *tr, unsigned int letter, char *ph_buf)
  385. {
  386. // lookup the character in the accents table
  387. int accent_data = 0;
  388. int accent1 = 0;
  389. int accent2 = 0;
  390. int flags1, flags2;
  391. int basic_letter;
  392. int letter2 = 0;
  393. char ph_letter1[30];
  394. char ph_letter2[30];
  395. char ph_accent1[30];
  396. char ph_accent2[30];
  397. ph_accent2[0] = 0;
  398. if ((letter >= 0xe0) && (letter < 0x17f))
  399. accent_data = letter_accents_0e0[letter - 0xe0];
  400. else if ((letter >= 0x250) && (letter <= 0x2a8))
  401. accent_data = letter_accents_250[letter - 0x250];
  402. if (accent_data != 0) {
  403. basic_letter = (accent_data & 0x3f) + 59;
  404. if (basic_letter < 'a')
  405. basic_letter = non_ascii_tab[basic_letter-59];
  406. if (accent_data & M_LIGATURE) {
  407. letter2 = (accent_data >> 6) & 0x3f;
  408. letter2 += 59;
  409. accent2 = (accent_data >> 12) & 0x7;
  410. } else {
  411. accent1 = (accent_data >> 6) & 0x1f;
  412. accent2 = (accent_data >> 11) & 0xf;
  413. }
  414. if ((accent1 == 0) && !(accent_data & M_LIGATURE)) {
  415. // just a letter name, not an accented character or ligature
  416. return;
  417. }
  418. if ((flags1 = Lookup(tr, accents_tab[accent1].name, ph_accent1)) != 0) {
  419. if (LookupLetter2(tr, basic_letter, ph_letter1) != 0) {
  420. if (accent2 != 0) {
  421. flags2 = Lookup(tr, accents_tab[accent2].name, ph_accent2);
  422. if (flags2 & FLAG_ACCENT_BEFORE) {
  423. strcpy(ph_buf, ph_accent2);
  424. ph_buf += strlen(ph_buf);
  425. ph_accent2[0] = 0;
  426. }
  427. }
  428. if (letter2 != 0) {
  429. // ligature
  430. LookupLetter2(tr, letter2, ph_letter2);
  431. sprintf(ph_buf, "%s%c%s%c%s%s", ph_accent1, phonPAUSE_VSHORT, ph_letter1, phonSTRESS_P, ph_letter2, ph_accent2);
  432. } else {
  433. if (accent1 == 0)
  434. strcpy(ph_buf, ph_letter1);
  435. else if ((tr->langopts.accents & 1) || (flags1 & FLAG_ACCENT_BEFORE) || (accents_tab[accent1].accent_flags & 1))
  436. sprintf(ph_buf, "%s%c%c%s", ph_accent1, phonPAUSE_VSHORT, phonSTRESS_P, ph_letter1);
  437. else
  438. sprintf(ph_buf, "%c%s%c%s%c", phonSTRESS_2, ph_letter1, phonPAUSE_VSHORT, ph_accent1, phonPAUSE_VSHORT);
  439. }
  440. }
  441. }
  442. }
  443. }
  444. void LookupLetter(Translator *tr, unsigned int letter, int next_byte, char *ph_buf1, int control)
  445. {
  446. // control, bit 0: not the first letter of a word
  447. int len;
  448. static char single_letter[10] = { 0, 0 };
  449. unsigned int dict_flags[2];
  450. char ph_buf3[40];
  451. ph_buf1[0] = 0;
  452. len = utf8_out(letter, &single_letter[2]);
  453. single_letter[len+2] = ' ';
  454. if (next_byte == -1) {
  455. // speaking normal text, not individual characters
  456. if (Lookup(tr, &single_letter[2], ph_buf1) != 0)
  457. return;
  458. single_letter[1] = '_';
  459. if (Lookup(tr, &single_letter[1], ph_buf3) != 0)
  460. return; // the character is specified as _* so ignore it when speaking normal text
  461. // check whether this character is specified for English
  462. if (tr->translator_name == L('e', 'n'))
  463. return; // we are already using English
  464. SetTranslator2("en");
  465. if (Lookup(translator2, &single_letter[2], ph_buf3) != 0) {
  466. // yes, switch to English and re-translate the word
  467. sprintf(ph_buf1, "%c", phonSWITCH);
  468. }
  469. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  470. return;
  471. }
  472. if ((letter <= 32) || iswspace(letter)) {
  473. // lookup space as _&32 etc.
  474. sprintf(&single_letter[1], "_#%d ", letter);
  475. Lookup(tr, &single_letter[1], ph_buf1);
  476. return;
  477. }
  478. if (next_byte != ' ')
  479. next_byte = RULE_SPELLING;
  480. single_letter[3+len] = next_byte; // follow by space-space if the end of the word, or space-31
  481. single_letter[1] = '_';
  482. // if the $accent flag is set for this letter, use the accents table (below)
  483. dict_flags[1] = 0;
  484. if (Lookup(tr, &single_letter[1], ph_buf3) == 0) {
  485. single_letter[1] = ' ';
  486. if (Lookup(tr, &single_letter[2], ph_buf3) == 0)
  487. TranslateRules(tr, &single_letter[2], ph_buf3, sizeof(ph_buf3), NULL, FLAG_NO_TRACE, NULL);
  488. }
  489. if (ph_buf3[0] == 0)
  490. LookupAccentedLetter(tr, letter, ph_buf3);
  491. strcpy(ph_buf1, ph_buf3);
  492. if ((ph_buf1[0] == 0) || (ph_buf1[0] == phonSWITCH))
  493. return;
  494. dict_flags[0] = 0;
  495. dict_flags[1] = 0;
  496. SetWordStress(tr, ph_buf1, dict_flags, -1, control & 1);
  497. }
  498. // unicode ranges for non-ascii digits 0-9 (these must be in ascending order)
  499. static const int number_ranges[] = {
  500. 0x660, 0x6f0, // arabic
  501. 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xbe6, 0xc66, 0xce6, 0xd66, // indic
  502. 0xe50, 0xed0, 0xf20, 0x1040, 0x1090,
  503. 0
  504. };
  505. static int NonAsciiNumber(int letter)
  506. {
  507. // Change non-ascii digit into ascii digit '0' to '9', (or -1 if not)
  508. const int *p;
  509. int base;
  510. for (p = number_ranges; (base = *p) != 0; p++) {
  511. if (letter < base)
  512. break; // not found
  513. if (letter < (base+10))
  514. return letter-base+'0';
  515. }
  516. return -1;
  517. }
  518. #define L_SUB 0x4000 // subscript
  519. #define L_SUP 0x8000 // superscript
  520. static const char *modifiers[] = { NULL, "_sub", "_sup", NULL };
  521. // this list must be in ascending order
  522. static unsigned short derived_letters[] = {
  523. 0x00aa, 'a'+L_SUP,
  524. 0x00b2, '2'+L_SUP,
  525. 0x00b3, '3'+L_SUP,
  526. 0x00b9, '1'+L_SUP,
  527. 0x00ba, 'o'+L_SUP,
  528. 0x02b0, 'h'+L_SUP,
  529. 0x02b1, 0x266+L_SUP,
  530. 0x02b2, 'j'+L_SUP,
  531. 0x02b3, 'r'+L_SUP,
  532. 0x02b4, 0x279+L_SUP,
  533. 0x02b5, 0x27b+L_SUP,
  534. 0x02b6, 0x281+L_SUP,
  535. 0x02b7, 'w'+L_SUP,
  536. 0x02b8, 'y'+L_SUP,
  537. 0x02c0, 0x294+L_SUP,
  538. 0x02c1, 0x295+L_SUP,
  539. 0x02e0, 0x263+L_SUP,
  540. 0x02e1, 'l'+L_SUP,
  541. 0x02e2, 's'+L_SUP,
  542. 0x02e3, 'x'+L_SUP,
  543. 0x2070, '0'+L_SUP,
  544. 0x2071, 'i'+L_SUP,
  545. 0x2074, '4'+L_SUP,
  546. 0x2075, '5'+L_SUP,
  547. 0x2076, '6'+L_SUP,
  548. 0x2077, '7'+L_SUP,
  549. 0x2078, '8'+L_SUP,
  550. 0x2079, '9'+L_SUP,
  551. 0x207a, '+'+L_SUP,
  552. 0x207b, '-'+L_SUP,
  553. 0x207c, '='+L_SUP,
  554. 0x207d, '('+L_SUP,
  555. 0x207e, ')'+L_SUP,
  556. 0x207f, 'n'+L_SUP,
  557. 0x2080, '0'+L_SUB,
  558. 0x2081, '1'+L_SUB,
  559. 0x2082, '2'+L_SUB,
  560. 0x2083, '3'+L_SUB,
  561. 0x2084, '4'+L_SUB,
  562. 0x2085, '5'+L_SUB,
  563. 0x2086, '6'+L_SUB,
  564. 0x2087, '7'+L_SUB,
  565. 0x2088, '8'+L_SUB,
  566. 0x2089, '9'+L_SUB,
  567. 0x208a, '+'+L_SUB,
  568. 0x208b, '-'+L_SUB,
  569. 0x208c, '='+L_SUB,
  570. 0x208d, '('+L_SUB,
  571. 0x208e, ')'+L_SUB,
  572. 0x2090, 'a'+L_SUB,
  573. 0x2091, 'e'+L_SUB,
  574. 0x2092, 'o'+L_SUB,
  575. 0x2093, 'x'+L_SUB,
  576. 0x2094, 0x259+L_SUB,
  577. 0x2095, 'h'+L_SUB,
  578. 0x2096, 'k'+L_SUB,
  579. 0x2097, 'l'+L_SUB,
  580. 0x2098, 'm'+L_SUB,
  581. 0x2099, 'n'+L_SUB,
  582. 0x209a, 'p'+L_SUB,
  583. 0x209b, 's'+L_SUB,
  584. 0x209c, 't'+L_SUB,
  585. 0, 0
  586. };
  587. // names, using phonemes available to all languages
  588. static const char *hex_letters[] = {
  589. "'e:j",
  590. "b'i:",
  591. "s'i:",
  592. "d'i:",
  593. "'i:",
  594. "'ef"
  595. };
  596. int IsSuperscript(int letter)
  597. {
  598. // is this a subscript or superscript letter ?
  599. int ix;
  600. int c;
  601. for (ix = 0; (c = derived_letters[ix]) != 0; ix += 2) {
  602. if (c > letter)
  603. break;
  604. if (c == letter)
  605. return derived_letters[ix+1];
  606. }
  607. return 0;
  608. }
  609. int TranslateLetter(Translator *tr, char *word, char *phonemes, int control)
  610. {
  611. // get pronunciation for an isolated letter
  612. // return number of bytes used by the letter
  613. // control bit 0: a non-initial letter in a word
  614. // bit 1: say 'capital'
  615. // bit 2: say character code for unknown letters
  616. int n_bytes;
  617. int letter;
  618. int len;
  619. int ix;
  620. int c;
  621. char *p2;
  622. char *pbuf;
  623. const char *modifier;
  624. ALPHABET *alphabet;
  625. int al_offset;
  626. int al_flags;
  627. int language;
  628. int number;
  629. int phontab_1;
  630. int speak_letter_number;
  631. char capital[30];
  632. char ph_buf[80];
  633. char ph_buf2[80];
  634. char ph_alphabet[80];
  635. char hexbuf[12];
  636. static char pause_string[] = { phonPAUSE, 0 };
  637. ph_buf[0] = 0;
  638. ph_alphabet[0] = 0;
  639. capital[0] = 0;
  640. phontab_1 = translator->phoneme_tab_ix;
  641. n_bytes = utf8_in(&letter, word);
  642. if ((letter & 0xfff00) == 0x0e000)
  643. letter &= 0xff; // uncode private usage area
  644. if (control & 2) {
  645. // include CAPITAL information
  646. if (iswupper(letter))
  647. Lookup(tr, "_cap", capital);
  648. }
  649. letter = towlower2(letter);
  650. LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
  651. if (ph_buf[0] == 0) {
  652. // is this a subscript or superscript letter ?
  653. if ((c = IsSuperscript(letter)) != 0) {
  654. letter = c & 0x3fff;
  655. if ((control & 4 ) && ((modifier = modifiers[c >> 14]) != NULL)) {
  656. // don't say "superscript" during normal text reading
  657. Lookup(tr, modifier, capital);
  658. if (capital[0] == 0) {
  659. capital[2] = SetTranslator2("en"); // overwrites previous contents of translator2
  660. Lookup(translator2, modifier, &capital[3]);
  661. if (capital[3] != 0) {
  662. capital[0] = phonPAUSE;
  663. capital[1] = phonSWITCH;
  664. len = strlen(&capital[3]);
  665. capital[len+3] = phonSWITCH;
  666. capital[len+4] = phontab_1;
  667. capital[len+5] = 0;
  668. }
  669. }
  670. }
  671. }
  672. LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
  673. }
  674. if (ph_buf[0] == phonSWITCH) {
  675. strcpy(phonemes, ph_buf);
  676. return 0;
  677. }
  678. if ((ph_buf[0] == 0) && ((number = NonAsciiNumber(letter)) > 0)) {
  679. // convert a non-ascii number to 0-9
  680. LookupLetter(tr, number, 0, ph_buf, control & 1);
  681. }
  682. al_offset = 0;
  683. al_flags = 0;
  684. if ((alphabet = AlphabetFromChar(letter)) != NULL) {
  685. al_offset = alphabet->offset;
  686. al_flags = alphabet->flags;
  687. }
  688. if (alphabet != current_alphabet) {
  689. // speak the name of the alphabet
  690. current_alphabet = alphabet;
  691. if ((alphabet != NULL) && !(al_flags & AL_DONT_NAME) && (al_offset != translator->letter_bits_offset)) {
  692. if ((al_flags & AL_DONT_NAME) || (al_offset == translator->langopts.alt_alphabet) || (al_offset == translator->langopts.our_alphabet)) {
  693. // don't say the alphabet name
  694. } else {
  695. ph_buf2[0] = 0;
  696. if (Lookup(translator, alphabet->name, ph_alphabet) == 0) { // the original language for the current voice
  697. // Can't find the local name for this alphabet, use the English name
  698. ph_alphabet[2] = SetTranslator2("en"); // overwrites previous contents of translator2
  699. Lookup(translator2, alphabet->name, ph_buf2);
  700. } else if (translator != tr) {
  701. phontab_1 = tr->phoneme_tab_ix;
  702. strcpy(ph_buf2, ph_alphabet);
  703. ph_alphabet[2] = translator->phoneme_tab_ix;
  704. }
  705. if (ph_buf2[0] != 0) {
  706. // we used a different language for the alphabet name (now in ph_buf2)
  707. ph_alphabet[0] = phonPAUSE;
  708. ph_alphabet[1] = phonSWITCH;
  709. strcpy(&ph_alphabet[3], ph_buf2);
  710. len = strlen(ph_buf2) + 3;
  711. ph_alphabet[len] = phonSWITCH;
  712. ph_alphabet[len+1] = phontab_1;
  713. ph_alphabet[len+2] = 0;
  714. }
  715. }
  716. }
  717. }
  718. // caution: SetWordStress() etc don't expect phonSWITCH + phoneme table number
  719. if (ph_buf[0] == 0) {
  720. if ((al_offset != 0) && (al_offset == translator->langopts.alt_alphabet))
  721. language = translator->langopts.alt_alphabet_lang;
  722. else if ((alphabet != NULL) && (alphabet->language != 0) && !(al_flags & AL_NOT_LETTERS))
  723. language = alphabet->language;
  724. else
  725. language = L('e', 'n');
  726. if ((language != tr->translator_name) || (language == L('k', 'o'))) {
  727. char *p3;
  728. int initial, code;
  729. char hangul_buf[12];
  730. // speak in the language for this alphabet (or English)
  731. ph_buf[2] = SetTranslator2(WordToString2(language));
  732. if (translator2 != NULL) {
  733. if (((code = letter - 0xac00) >= 0) && (letter <= 0xd7af)) {
  734. // Special case for Korean letters.
  735. // break a syllable hangul into 2 or 3 individual jamo
  736. hangul_buf[0] = ' ';
  737. p3 = &hangul_buf[1];
  738. if ((initial = (code/28)/21) != 11) {
  739. p3 += utf8_out(initial + 0x1100, p3);
  740. }
  741. utf8_out(((code/28) % 21) + 0x1161, p3); // medial
  742. utf8_out((code % 28) + 0x11a7, &p3[3]); // final
  743. p3[6] = ' ';
  744. p3[7] = 0;
  745. ph_buf[3] = 0;
  746. TranslateRules(translator2, &hangul_buf[1], &ph_buf[3], sizeof(ph_buf)-3, NULL, 0, NULL);
  747. SetWordStress(translator2, &ph_buf[3], NULL, -1, 0);
  748. } else
  749. LookupLetter(translator2, letter, word[n_bytes], &ph_buf[3], control & 1);
  750. if (ph_buf[3] == phonSWITCH) {
  751. // another level of language change
  752. ph_buf[2] = SetTranslator2(&ph_buf[4]);
  753. LookupLetter(translator2, letter, word[n_bytes], &ph_buf[3], control & 1);
  754. }
  755. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  756. if (ph_buf[3] != 0) {
  757. ph_buf[0] = phonPAUSE;
  758. ph_buf[1] = phonSWITCH;
  759. len = strlen(&ph_buf[3]) + 3;
  760. ph_buf[len] = phonSWITCH; // switch back
  761. ph_buf[len+1] = tr->phoneme_tab_ix;
  762. ph_buf[len+2] = 0;
  763. }
  764. }
  765. }
  766. }
  767. if (ph_buf[0] == 0) {
  768. // character name not found
  769. if (ph_buf[0] == 0) {
  770. speak_letter_number = 1;
  771. if (!(al_flags & AL_NO_SYMBOL)) {
  772. if (iswalpha(letter))
  773. Lookup(translator, "_?A", ph_buf);
  774. if ((ph_buf[0] == 0) && !iswspace(letter))
  775. Lookup(translator, "_??", ph_buf);
  776. if (ph_buf[0] == 0)
  777. EncodePhonemes("l'et@", ph_buf, NULL);
  778. }
  779. if (!(control & 4) && (al_flags & AL_NOT_CODE)) {
  780. // don't speak the character code number, unless we want full details of this character
  781. speak_letter_number = 0;
  782. }
  783. if (speak_letter_number) {
  784. if (al_offset == 0x2800) {
  785. // braille dots symbol, list the numbered dots
  786. p2 = hexbuf;
  787. for (ix = 0; ix < 8; ix++) {
  788. if (letter & (1 << ix))
  789. *p2++ = '1'+ix;
  790. }
  791. *p2 = 0;
  792. } else {
  793. // speak the hexadecimal number of the character code
  794. sprintf(hexbuf, "%x", letter);
  795. }
  796. pbuf = ph_buf;
  797. for (p2 = hexbuf; *p2 != 0; p2++) {
  798. pbuf += strlen(pbuf);
  799. *pbuf++ = phonPAUSE_VSHORT;
  800. LookupLetter(translator, *p2, 0, pbuf, 1);
  801. if (((pbuf[0] == 0) || (pbuf[0] == phonSWITCH)) && (*p2 >= 'a')) {
  802. // This language has no translation for 'a' to 'f', speak English names using base phonemes
  803. EncodePhonemes(hex_letters[*p2 - 'a'], pbuf, NULL);
  804. }
  805. }
  806. strcat(pbuf, pause_string);
  807. }
  808. }
  809. }
  810. len = strlen(phonemes);
  811. if (tr->langopts.accents & 2) // 'capital' before or after the word ?
  812. sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, ph_buf, capital);
  813. else
  814. sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, capital, ph_buf); // the 0xff marker will be removed or replaced in SetSpellingStress()
  815. if ((len + strlen(ph_buf2)) < N_WORD_PHONEMES)
  816. strcpy(&phonemes[len], ph_buf2);
  817. return n_bytes;
  818. }
  819. void SetSpellingStress(Translator *tr, char *phonemes, int control, int n_chars)
  820. {
  821. // Individual letter names, reduce the stress of some.
  822. int ix;
  823. unsigned int c;
  824. int n_stress = 0;
  825. int prev = 0;
  826. int count;
  827. unsigned char buf[N_WORD_PHONEMES];
  828. for (ix = 0; (c = phonemes[ix]) != 0; ix++) {
  829. if ((c == phonSTRESS_P) && (prev != phonSWITCH))
  830. n_stress++;
  831. buf[ix] = prev = c;
  832. }
  833. buf[ix] = 0;
  834. count = 0;
  835. prev = 0;
  836. for (ix = 0; (c = buf[ix]) != 0; ix++) {
  837. if ((c == phonSTRESS_P) && (n_chars > 1) && (prev != phonSWITCH)) {
  838. count++;
  839. if (tr->langopts.spelling_stress == 1) {
  840. // stress on initial letter when spelling
  841. if (count > 1)
  842. c = phonSTRESS_3;
  843. } else {
  844. if (count != n_stress) {
  845. if (((count % 3) != 0) || (count == n_stress-1))
  846. c = phonSTRESS_3; // reduce to secondary stress
  847. }
  848. }
  849. } else if (c == 0xff) {
  850. if ((control < 2) || (ix == 0))
  851. continue; // don't insert pauses
  852. if (((count % 3) == 0) || (control > 2))
  853. c = phonPAUSE_NOLINK; // pause following a primary stress
  854. else
  855. c = phonPAUSE_VSHORT;
  856. }
  857. *phonemes++ = prev = c;
  858. }
  859. if (control >= 2)
  860. *phonemes++ = phonPAUSE_NOLINK;
  861. *phonemes = 0;
  862. }
  863. // Numbers
  864. static char ph_ordinal2[12];
  865. static char ph_ordinal2x[12];
  866. static int CheckDotOrdinal(Translator *tr, char *word, char *word_end, WORD_TAB *wtab, int roman)
  867. {
  868. int ordinal = 0;
  869. int c2;
  870. int nextflags;
  871. if ((tr->langopts.numbers & NUM_ORDINAL_DOT) && ((word_end[0] == '.') || (wtab[0].flags & FLAG_HAS_DOT)) && !(wtab[1].flags & FLAG_NOSPACE)) {
  872. if (roman || !(wtab[1].flags & FLAG_FIRST_UPPER)) {
  873. if (word_end[0] == '.')
  874. utf8_in(&c2, &word_end[2]);
  875. else
  876. utf8_in(&c2, &word_end[0]);
  877. if ((word_end[0] != 0) && (word_end[1] != 0) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || IsAlpha(c2))) {
  878. // ordinal number is indicated by dot after the number
  879. // but not if the next word starts with an upper-case letter
  880. // (c2 == 0) is for cases such as, "2.,"
  881. ordinal = 2;
  882. if (word_end[0] == '.')
  883. word_end[0] = ' ';
  884. if ((roman == 0) && (tr->translator_name == L('h', 'u'))) {
  885. // lang=hu don't treat dot as ordinal indicator if the next word is a month name ($alt). It may have a suffix.
  886. nextflags = 0;
  887. if (IsAlpha(c2))
  888. nextflags = TranslateWord(tr, &word_end[2], NULL, NULL);
  889. if ((tr->prev_dict_flags[0] & FLAG_ALT_TRANS) && ((c2 == 0) || (wtab[0].flags & FLAG_COMMA_AFTER) || iswdigit(c2)))
  890. ordinal = 0; // TEST 09.02.10
  891. if (nextflags & FLAG_ALT_TRANS)
  892. ordinal = 0;
  893. if (nextflags & FLAG_ALT3_TRANS) {
  894. if (word[-2] == '-')
  895. ordinal = 0; // e.g. december 2-5. között
  896. if (tr->prev_dict_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT3_TRANS))
  897. ordinal = 0x22;
  898. }
  899. }
  900. }
  901. }
  902. }
  903. return ordinal;
  904. }
  905. static int hu_number_e(const char *word, int thousandplex, int value)
  906. {
  907. // lang-hu: variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt, att. ett
  908. if ((word[0] == 'a') || (word[0] == 'e')) {
  909. if ((word[1] == ' ') || (word[1] == 'z') || ((word[1] == 't') && (word[2] == 't')))
  910. return 0;
  911. if (((thousandplex == 1) || ((value % 1000) == 0)) && (word[1] == 'l'))
  912. return 0; // 1000-el
  913. return 1;
  914. }
  915. return 0;
  916. }
  917. int TranslateRoman(Translator *tr, char *word, char *ph_out, WORD_TAB *wtab)
  918. {
  919. int c;
  920. char *p;
  921. const char *p2;
  922. int acc;
  923. int prev;
  924. int value;
  925. int subtract;
  926. int repeat = 0;
  927. int n_digits = 0;
  928. char *word_start;
  929. int num_control = 0;
  930. unsigned int flags[2];
  931. char ph_roman[30];
  932. char number_chars[N_WORD_BYTES];
  933. static const char *roman_numbers = "ixcmvld";
  934. static int roman_values[] = { 1, 10, 100, 1000, 5, 50, 500 };
  935. acc = 0;
  936. prev = 0;
  937. subtract = 0x7fff;
  938. ph_out[0] = 0;
  939. flags[0] = 0;
  940. flags[1] = 0;
  941. if (((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && !(wtab[0].flags & FLAG_ALL_UPPER)) || IsDigit09(word[-2]))
  942. return 0; // not '2xx'
  943. if (word[1] == ' ') {
  944. if ((tr->langopts.numbers & (NUM_ROMAN_CAPITALS | NUM_ROMAN_ORDINAL | NUM_ORDINAL_DOT)) && (wtab[0].flags & FLAG_HAS_DOT)) {
  945. // allow single letter Roman ordinal followed by dot.
  946. } else
  947. return 0; // only one letter, don't speak as a Roman Number
  948. }
  949. word_start = word;
  950. while ((c = *word++) != ' ') {
  951. if ((p2 = strchr(roman_numbers, c)) == NULL)
  952. return 0;
  953. value = roman_values[p2 - roman_numbers];
  954. if (value == prev) {
  955. repeat++;
  956. if (repeat >= 3)
  957. return 0;
  958. } else
  959. repeat = 0;
  960. if ((prev > 1) && (prev != 10) && (prev != 100)) {
  961. if (value >= prev)
  962. return 0;
  963. }
  964. if ((prev != 0) && (prev < value)) {
  965. if (((acc % 10) != 0) || ((prev*10) < value))
  966. return 0;
  967. subtract = prev;
  968. value -= subtract;
  969. } else if (value >= subtract)
  970. return 0;
  971. else
  972. acc += prev;
  973. prev = value;
  974. n_digits++;
  975. }
  976. if (IsDigit09(word[0]))
  977. return 0; // e.g. 'xx2'
  978. acc += prev;
  979. if (acc < tr->langopts.min_roman)
  980. return 0;
  981. if (acc > tr->langopts.max_roman)
  982. return 0;
  983. Lookup(tr, "_roman", ph_roman); // precede by "roman" if _rom is defined in *_list
  984. p = &ph_out[0];
  985. if ((tr->langopts.numbers & NUM_ROMAN_AFTER) == 0) {
  986. strcpy(ph_out, ph_roman);
  987. p = &ph_out[strlen(ph_roman)];
  988. }
  989. sprintf(number_chars, " %d %s ", acc, tr->langopts.roman_suffix);
  990. if (word[0] == '.') {
  991. // dot has not been removed. This implies that there was no space after it
  992. return 0;
  993. }
  994. if (CheckDotOrdinal(tr, word_start, word, wtab, 1))
  995. wtab[0].flags |= FLAG_ORDINAL;
  996. if (tr->langopts.numbers & NUM_ROMAN_ORDINAL) {
  997. if (tr->translator_name == L('h', 'u')) {
  998. if (!(wtab[0].flags & FLAG_ORDINAL)) {
  999. if ((wtab[0].flags & FLAG_HYPHEN_AFTER) && hu_number_e(word, 0, acc)) {
  1000. // should use the 'e' form of the number
  1001. num_control |= 1;
  1002. } else
  1003. return 0;
  1004. }
  1005. } else
  1006. wtab[0].flags |= FLAG_ORDINAL;
  1007. }
  1008. tr->prev_dict_flags[0] = 0;
  1009. tr->prev_dict_flags[1] = 0;
  1010. TranslateNumber(tr, &number_chars[2], p, flags, wtab, num_control);
  1011. if (tr->langopts.numbers & NUM_ROMAN_AFTER)
  1012. strcat(ph_out, ph_roman);
  1013. return 1;
  1014. }
  1015. static const char *M_Variant(int value)
  1016. {
  1017. // returns M, or perhaps MA or MB for some cases
  1018. bool teens = false;
  1019. if (((value % 100) > 10) && ((value % 100) < 20))
  1020. teens = true;
  1021. switch ((translator->langopts.numbers2 >> 6) & 0x7)
  1022. {
  1023. case 1: // lang=ru use singular for xx1 except for x11
  1024. if ((teens == false) && ((value % 10) == 1))
  1025. return "1M";
  1026. break;
  1027. case 2: // lang=cs,sk
  1028. if ((value >= 2) && (value <= 4))
  1029. return "0MA";
  1030. break;
  1031. case 3: // lang=pl
  1032. if ((teens == false) && (((value % 10) >= 2) && ((value % 10) <= 4)))
  1033. return "0MA";
  1034. break;
  1035. case 4: // lang=lt
  1036. if ((teens == true) || ((value % 10) == 0))
  1037. return "0MB";
  1038. if ((value % 10) == 1)
  1039. return "0MA";
  1040. break;
  1041. case 5: // lang=bs,hr,sr
  1042. if (teens == false) {
  1043. if ((value % 10) == 1)
  1044. return "1M";
  1045. if (((value % 10) >= 2) && ((value % 10) <= 4))
  1046. return "0MA";
  1047. }
  1048. break;
  1049. }
  1050. return "0M";
  1051. }
  1052. static int LookupThousands(Translator *tr, int value, int thousandplex, int thousands_exact, char *ph_out)
  1053. {
  1054. // thousands_exact: bit 0 no hundreds,tens,or units, bit 1 ordinal numberr
  1055. int found;
  1056. int found_value = 0;
  1057. char string[12];
  1058. char ph_of[12];
  1059. char ph_thousands[40];
  1060. char ph_buf[40];
  1061. ph_of[0] = 0;
  1062. // first look for a match with the exact value of thousands
  1063. if (value > 0) {
  1064. if (thousands_exact & 1) {
  1065. if (thousands_exact & 2) {
  1066. // ordinal number
  1067. sprintf(string, "_%dM%do", value, thousandplex);
  1068. found_value = Lookup(tr, string, ph_thousands);
  1069. }
  1070. if (!found_value && (number_control & 1)) {
  1071. // look for the 'e' variant
  1072. sprintf(string, "_%dM%de", value, thousandplex);
  1073. found_value = Lookup(tr, string, ph_thousands);
  1074. }
  1075. if (!found_value) {
  1076. // is there a different pronunciation if there are no hundreds,tens,or units ? (LANG=ta)
  1077. sprintf(string, "_%dM%dx", value, thousandplex);
  1078. found_value = Lookup(tr, string, ph_thousands);
  1079. }
  1080. }
  1081. if (found_value == 0) {
  1082. sprintf(string, "_%dM%d", value, thousandplex);
  1083. found_value = Lookup(tr, string, ph_thousands);
  1084. }
  1085. }
  1086. if (found_value == 0) {
  1087. if ((value % 100) >= 20)
  1088. Lookup(tr, "_0of", ph_of);
  1089. found = 0;
  1090. if (thousands_exact & 1) {
  1091. if (thousands_exact & 2) {
  1092. // ordinal number
  1093. sprintf(string, "_%s%do", M_Variant(value), thousandplex);
  1094. found = Lookup(tr, string, ph_thousands);
  1095. }
  1096. if (!found && (number_control & 1)) {
  1097. // look for the 'e' variant
  1098. sprintf(string, "_%s%de", M_Variant(value), thousandplex);
  1099. found = Lookup(tr, string, ph_thousands);
  1100. }
  1101. if (!found) {
  1102. // is there a different pronunciation if there are no hundreds,tens,or units ?
  1103. sprintf(string, "_%s%dx", M_Variant(value), thousandplex);
  1104. found = Lookup(tr, string, ph_thousands);
  1105. }
  1106. }
  1107. if (found == 0) {
  1108. sprintf(string, "_%s%d", M_Variant(value), thousandplex);
  1109. if (Lookup(tr, string, ph_thousands) == 0) {
  1110. if (thousandplex > 3) {
  1111. sprintf(string, "_0M%d", thousandplex-1);
  1112. if (Lookup(tr, string, ph_buf) == 0) {
  1113. // say "millions" if this name is not available and neither is the next lower
  1114. Lookup(tr, "_0M2", ph_thousands);
  1115. speak_missing_thousands = 3;
  1116. }
  1117. }
  1118. if (ph_thousands[0] == 0) {
  1119. // repeat "thousand" if higher order names are not available
  1120. sprintf(string, "_%dM1", value);
  1121. if ((found_value = Lookup(tr, string, ph_thousands)) == 0)
  1122. Lookup(tr, "_0M1", ph_thousands);
  1123. speak_missing_thousands = 2;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. sprintf(ph_out, "%s%s", ph_of, ph_thousands);
  1129. if ((value == 1) && (thousandplex == 1) && (tr->langopts.numbers & NUM_OMIT_1_THOUSAND))
  1130. return 1;
  1131. return found_value;
  1132. }
  1133. static int LookupNum2(Translator *tr, int value, int thousandplex, const int control, char *ph_out)
  1134. {
  1135. // Lookup a 2 digit number
  1136. // control bit 0: ordinal number
  1137. // control bit 1: final tens and units (not number of thousands) (use special form of '1', LANG=de "eins")
  1138. // control bit 2: tens and units only, no higher digits
  1139. // control bit 3: use feminine form of '2' (for thousands
  1140. // control bit 4: speak zero tens
  1141. // control bit 5: variant of ordinal number (lang=hu)
  1142. // bit 8 followed by decimal fraction
  1143. // bit 9: use #f form for both tens and units (lang=ml)
  1144. int found;
  1145. int ix;
  1146. int units;
  1147. int tens;
  1148. int is_ordinal;
  1149. int used_and = 0;
  1150. int found_ordinal = 0;
  1151. int next_phtype;
  1152. int ord_type = 'o';
  1153. char string[12]; // for looking up entries in *_list
  1154. char ph_ordinal[20];
  1155. char ph_tens[50];
  1156. char ph_digits[50];
  1157. char ph_and[12];
  1158. units = value % 10;
  1159. tens = value / 10;
  1160. found = 0;
  1161. ph_ordinal[0] = 0;
  1162. ph_tens[0] = 0;
  1163. ph_digits[0] = 0;
  1164. ph_and[0] = 0;
  1165. if (control & 0x20)
  1166. ord_type = 'q';
  1167. is_ordinal = control & 1;
  1168. if ((control & 2) && (n_digit_lookup == 2)) {
  1169. // pronunciation of the final 2 digits has already been found
  1170. strcpy(ph_out, digit_lookup);
  1171. } else {
  1172. if (digit_lookup[0] == 0) {
  1173. // is there a special pronunciation for this 2-digit number
  1174. if (control & 8) {
  1175. // is there a feminine or thousands-variant form?
  1176. sprintf(string, "_%dfx", value);
  1177. if ((found = Lookup(tr, string, ph_digits)) == 0) {
  1178. sprintf(string, "_%df", value);
  1179. found = Lookup(tr, string, ph_digits);
  1180. }
  1181. } else if (is_ordinal) {
  1182. strcpy(ph_ordinal, ph_ordinal2);
  1183. if (control & 4) {
  1184. sprintf(string, "_%d%cx", value, ord_type); // LANG=hu, special word for 1. 2. when there are no higher digits
  1185. if ((found = Lookup(tr, string, ph_digits)) != 0) {
  1186. if (ph_ordinal2x[0] != 0)
  1187. strcpy(ph_ordinal, ph_ordinal2x); // alternate pronunciation (lang=an)
  1188. }
  1189. }
  1190. if (found == 0) {
  1191. sprintf(string, "_%d%c", value, ord_type);
  1192. found = Lookup(tr, string, ph_digits);
  1193. }
  1194. found_ordinal = found;
  1195. }
  1196. if (found == 0) {
  1197. if (control & 2) {
  1198. // the final tens and units of a number
  1199. if (number_control & 1) {
  1200. // look for 'e' variant
  1201. sprintf(string, "_%de", value);
  1202. found = Lookup(tr, string, ph_digits);
  1203. }
  1204. } else {
  1205. // followed by hundreds or thousands etc
  1206. if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
  1207. sprintf(string, "_%do", value); // LANG=TA
  1208. else
  1209. sprintf(string, "_%da", value);
  1210. found = Lookup(tr, string, ph_digits);
  1211. }
  1212. if (!found) {
  1213. if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_NO_TEEN_ORDINALS)) {
  1214. // don't use numbers 10-99 to make ordinals, always use _1Xo etc (lang=pt)
  1215. } else {
  1216. sprintf(string, "_%d", value);
  1217. found = Lookup(tr, string, ph_digits);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. // no, speak as tens+units
  1223. if ((value < 10) && (control & 0x10)) {
  1224. // speak leading zero
  1225. Lookup(tr, "_0", ph_tens);
  1226. } else {
  1227. if (found)
  1228. ph_tens[0] = 0;
  1229. else {
  1230. if (is_ordinal) {
  1231. sprintf(string, "_%dX%c", tens, ord_type);
  1232. if (Lookup(tr, string, ph_tens) != 0) {
  1233. found_ordinal = 1;
  1234. if ((units != 0) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
  1235. // Use the ordinal form of tens as well as units. Add the ordinal ending
  1236. strcat(ph_tens, ph_ordinal2);
  1237. }
  1238. }
  1239. }
  1240. if (found_ordinal == 0) {
  1241. if (control & 0x200)
  1242. sprintf(string, "_%dXf", tens);
  1243. else
  1244. sprintf(string, "_%dX", tens);
  1245. Lookup(tr, string, ph_tens);
  1246. }
  1247. if ((ph_tens[0] == 0) && (tr->langopts.numbers & NUM_VIGESIMAL)) {
  1248. // tens not found, (for example) 73 is 60+13
  1249. units = (value % 20);
  1250. sprintf(string, "_%dX", tens & 0xfe);
  1251. Lookup(tr, string, ph_tens);
  1252. }
  1253. ph_digits[0] = 0;
  1254. if (units > 0) {
  1255. found = 0;
  1256. if ((control & 2) && (digit_lookup[0] != 0)) {
  1257. // we have an entry for this digit (possibly together with the next word)
  1258. strcpy(ph_digits, digit_lookup);
  1259. found_ordinal = 1;
  1260. ph_ordinal[0] = 0;
  1261. } else {
  1262. if (control & 8) {
  1263. // is there a variant form of this number?
  1264. sprintf(string, "_%df", units);
  1265. found = Lookup(tr, string, ph_digits);
  1266. }
  1267. if ((is_ordinal) && ((tr->langopts.numbers & NUM_SWAP_TENS) == 0)) {
  1268. // ordinal
  1269. sprintf(string, "_%d%c", units, ord_type);
  1270. if ((found = Lookup(tr, string, ph_digits)) != 0)
  1271. found_ordinal = 1;
  1272. }
  1273. if (found == 0) {
  1274. if ((number_control & 1) && (control & 2)) {
  1275. // look for 'e' variant
  1276. sprintf(string, "_%de", units);
  1277. found = Lookup(tr, string, ph_digits);
  1278. } else if (((control & 2) == 0) || ((tr->langopts.numbers & NUM_SWAP_TENS) != 0)) {
  1279. // followed by hundreds or thousands (or tens)
  1280. if ((tr->langopts.numbers2 & NUM2_ORDINAL_AND_THOUSANDS) && (thousandplex <= 1))
  1281. sprintf(string, "_%do", units); // LANG=TA, only for 100s, 1000s
  1282. else
  1283. sprintf(string, "_%da", units);
  1284. found = Lookup(tr, string, ph_digits);
  1285. }
  1286. }
  1287. if (found == 0) {
  1288. sprintf(string, "_%d", units);
  1289. Lookup(tr, string, ph_digits);
  1290. }
  1291. }
  1292. }
  1293. }
  1294. }
  1295. if ((is_ordinal) && (found_ordinal == 0) && (ph_ordinal[0] == 0)) {
  1296. if ((value >= 20) && (((value % 10) == 0) || (tr->langopts.numbers & NUM_SWAP_TENS)))
  1297. Lookup(tr, "_ord20", ph_ordinal);
  1298. if (ph_ordinal[0] == 0)
  1299. Lookup(tr, "_ord", ph_ordinal);
  1300. }
  1301. if ((tr->langopts.numbers & (NUM_SWAP_TENS | NUM_AND_UNITS)) && (ph_tens[0] != 0) && (ph_digits[0] != 0)) {
  1302. Lookup(tr, "_0and", ph_and);
  1303. if ((is_ordinal) && (tr->langopts.numbers2 & NUM2_ORDINAL_NO_AND))
  1304. ph_and[0] = 0;
  1305. if (tr->langopts.numbers & NUM_SWAP_TENS)
  1306. sprintf(ph_out, "%s%s%s%s", ph_digits, ph_and, ph_tens, ph_ordinal);
  1307. else
  1308. sprintf(ph_out, "%s%s%s%s", ph_tens, ph_and, ph_digits, ph_ordinal);
  1309. used_and = 1;
  1310. } else {
  1311. if (tr->langopts.numbers & NUM_SINGLE_VOWEL) {
  1312. // remove vowel from the end of tens if units starts with a vowel (LANG=Italian)
  1313. if (((ix = strlen(ph_tens)-1) >= 0) && (ph_digits[0] != 0)) {
  1314. if ((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS)
  1315. next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type;
  1316. if ((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL))
  1317. ph_tens[ix] = 0;
  1318. }
  1319. }
  1320. if ((tr->langopts.numbers2 & NUM2_ORDINAL_DROP_VOWEL) && (ph_ordinal[0] != 0)) {
  1321. ix = sprintf(ph_out, "%s%s", ph_tens, ph_digits);
  1322. if ((ix > 0) && (phoneme_tab[(unsigned char)(ph_out[ix-1])]->type == phVOWEL))
  1323. ix--;
  1324. sprintf(&ph_out[ix], "%s", ph_ordinal);
  1325. } else
  1326. sprintf(ph_out, "%s%s%s", ph_tens, ph_digits, ph_ordinal);
  1327. }
  1328. }
  1329. if (tr->langopts.numbers & NUM_SINGLE_STRESS_L) {
  1330. // only one primary stress, on the first part (tens)
  1331. found = 0;
  1332. for (ix = 0; ix < (signed)strlen(ph_out); ix++) {
  1333. if (ph_out[ix] == phonSTRESS_P) {
  1334. if (found)
  1335. ph_out[ix] = phonSTRESS_3;
  1336. else
  1337. found = 1;
  1338. }
  1339. }
  1340. } else if (tr->langopts.numbers & NUM_SINGLE_STRESS) {
  1341. // only one primary stress
  1342. found = 0;
  1343. for (ix = strlen(ph_out)-1; ix >= 0; ix--) {
  1344. if (ph_out[ix] == phonSTRESS_P) {
  1345. if (found)
  1346. ph_out[ix] = phonSTRESS_3;
  1347. else
  1348. found = 1;
  1349. }
  1350. }
  1351. }
  1352. return used_and;
  1353. }
  1354. static int LookupNum3(Translator *tr, int value, char *ph_out, bool suppress_null, int thousandplex, int control)
  1355. {
  1356. // Translate a 3 digit number
  1357. // control bit 0, previous thousands
  1358. // bit 1, ordinal number
  1359. // bit 5 variant form of ordinal number
  1360. // bit 8 followed by decimal fraction
  1361. int found;
  1362. int hundreds;
  1363. int tensunits;
  1364. int x;
  1365. int ix;
  1366. int exact;
  1367. int ordinal;
  1368. int tplex;
  1369. bool say_zero_hundred = false;
  1370. bool say_one_hundred;
  1371. char string[12]; // for looking up entries in **_list
  1372. char buf1[100];
  1373. char buf2[100];
  1374. char ph_100[20];
  1375. char ph_10T[20];
  1376. char ph_digits[50];
  1377. char ph_thousands[50];
  1378. char ph_hundred_and[12];
  1379. char ph_thousand_and[12];
  1380. ordinal = control & 0x22;
  1381. hundreds = value / 100;
  1382. tensunits = value % 100;
  1383. buf1[0] = 0;
  1384. ph_thousands[0] = 0;
  1385. ph_thousand_and[0] = 0;
  1386. if ((tr->langopts.numbers & NUM_ZERO_HUNDRED) && ((control & 1) || (hundreds >= 10)))
  1387. say_zero_hundred = true; // lang=vi
  1388. if ((hundreds > 0) || say_zero_hundred) {
  1389. found = 0;
  1390. if (ordinal && (tensunits == 0)) {
  1391. // ordinal number, with no tens or units
  1392. found = Lookup(tr, "_0Co", ph_100);
  1393. }
  1394. if (found == 0) {
  1395. if (tensunits == 0) {
  1396. // special form for exact hundreds?
  1397. found = Lookup(tr, "_0C0", ph_100);
  1398. }
  1399. if (!found)
  1400. Lookup(tr, "_0C", ph_100);
  1401. }
  1402. if (((tr->langopts.numbers & NUM_1900) != 0) && (hundreds == 19)) {
  1403. // speak numbers such as 1984 as years: nineteen-eighty-four
  1404. } else if (hundreds >= 10) {
  1405. ph_digits[0] = 0;
  1406. exact = 0;
  1407. if ((value % 1000) == 0)
  1408. exact = 1;
  1409. tplex = thousandplex+1;
  1410. if (tr->langopts.numbers2 & NUM2_MYRIADS)
  1411. tplex = 0;
  1412. if (LookupThousands(tr, hundreds / 10, tplex, exact | ordinal, ph_10T) == 0) {
  1413. x = 0;
  1414. if (tr->langopts.numbers2 & (1 << tplex))
  1415. x = 8; // use variant (feminine) for before thousands and millions
  1416. if (tr->translator_name == L('m', 'l'))
  1417. x = 0x208;
  1418. LookupNum2(tr, hundreds/10, thousandplex, x, ph_digits);
  1419. }
  1420. if (tr->langopts.numbers2 & 0x200)
  1421. sprintf(ph_thousands, "%s%c%s%c", ph_10T, phonEND_WORD, ph_digits, phonEND_WORD); // say "thousands" before its number, not after
  1422. else
  1423. sprintf(ph_thousands, "%s%c%s%c", ph_digits, phonEND_WORD, ph_10T, phonEND_WORD);
  1424. hundreds %= 10;
  1425. if ((hundreds == 0) && (say_zero_hundred == false))
  1426. ph_100[0] = 0;
  1427. suppress_null = true;
  1428. control |= 1;
  1429. }
  1430. ph_digits[0] = 0;
  1431. if ((hundreds > 0) || say_zero_hundred) {
  1432. if ((tr->langopts.numbers & NUM_AND_HUNDRED) && ((control & 1) || (ph_thousands[0] != 0)))
  1433. Lookup(tr, "_0and", ph_thousand_and);
  1434. suppress_null = true;
  1435. found = 0;
  1436. if ((ordinal)
  1437. && ((tensunits == 0) || (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL))) {
  1438. // ordinal number
  1439. sprintf(string, "_%dCo", hundreds);
  1440. found = Lookup(tr, string, ph_digits);
  1441. if ((tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL) && (tensunits > 0)) {
  1442. // Use ordinal form of hundreds, as well as for tens and units
  1443. // Add ordinal suffix to the hundreds
  1444. strcat(ph_digits, ph_ordinal2);
  1445. }
  1446. }
  1447. if ((hundreds == 0) && say_zero_hundred)
  1448. Lookup(tr, "_0", ph_digits);
  1449. else {
  1450. if ((hundreds == 1) && (tr->langopts.numbers2 & NUM2_OMIT_1_HUNDRED_ONLY) && ((control & 1) == 0)) {
  1451. // only look for special 100 if there are previous thousands
  1452. } else {
  1453. if ((!found) && (tensunits == 0)) {
  1454. // is there a special pronunciation for exactly n00 ?
  1455. sprintf(string, "_%dC0", hundreds);
  1456. found = Lookup(tr, string, ph_digits);
  1457. }
  1458. if (!found) {
  1459. sprintf(string, "_%dC", hundreds);
  1460. found = Lookup(tr, string, ph_digits); // is there a specific pronunciation for n-hundred ?
  1461. }
  1462. }
  1463. if (found)
  1464. ph_100[0] = 0;
  1465. else {
  1466. say_one_hundred = true;
  1467. if (hundreds == 1) {
  1468. if ((tr->langopts.numbers & NUM_OMIT_1_HUNDRED) != 0)
  1469. say_one_hundred = false;
  1470. }
  1471. if (say_one_hundred == true)
  1472. LookupNum2(tr, hundreds, thousandplex, 0, ph_digits);
  1473. }
  1474. }
  1475. }
  1476. sprintf(buf1, "%s%s%s%s", ph_thousands, ph_thousand_and, ph_digits, ph_100);
  1477. }
  1478. ph_hundred_and[0] = 0;
  1479. if (tensunits > 0) {
  1480. if ((control & 2) && (tr->langopts.numbers2 & NUM2_MULTIPLE_ORDINAL)) {
  1481. // Don't use "and" if we apply ordinal to both hundreds and units
  1482. } else {
  1483. if ((value > 100) || ((control & 1) && (thousandplex == 0))) {
  1484. if ((tr->langopts.numbers & NUM_HUNDRED_AND) || ((tr->langopts.numbers & NUM_HUNDRED_AND_DIGIT) && (tensunits < 10)))
  1485. Lookup(tr, "_0and", ph_hundred_and);
  1486. }
  1487. if ((tr->langopts.numbers & NUM_THOUSAND_AND) && (hundreds == 0) && ((control & 1) || (ph_thousands[0] != 0)))
  1488. Lookup(tr, "_0and", ph_hundred_and);
  1489. }
  1490. }
  1491. buf2[0] = 0;
  1492. if ((tensunits != 0) || (suppress_null == false)) {
  1493. x = 0;
  1494. if (thousandplex == 0) {
  1495. x = 2; // allow "eins" for 1 rather than "ein"
  1496. if (ordinal)
  1497. x = 3; // ordinal number
  1498. if ((value < 100) && !(control & 1))
  1499. x |= 4; // tens and units only, no higher digits
  1500. if (ordinal & 0x20)
  1501. x |= 0x20; // variant form of ordinal number
  1502. } else if (tr->langopts.numbers2 & (1 << thousandplex))
  1503. x = 8; // use variant (feminine) for before thousands and millions
  1504. if ((tr->translator_name == L('m', 'l')) && (thousandplex == 1))
  1505. x |= 0x208; // use #f form for both tens and units
  1506. if ((tr->langopts.numbers2 & NUM2_ZERO_TENS) && ((control & 1) || (hundreds > 0))) {
  1507. // LANG=zh,
  1508. x |= 0x10;
  1509. }
  1510. if (LookupNum2(tr, tensunits, thousandplex, x | (control & 0x100), buf2) != 0) {
  1511. if (tr->langopts.numbers & NUM_SINGLE_AND)
  1512. ph_hundred_and[0] = 0; // don't put 'and' after 'hundred' if there's 'and' between tens and units
  1513. }
  1514. } else {
  1515. if (ph_ordinal2[0] != 0) {
  1516. ix = strlen(buf1);
  1517. if ((ix > 0) && (buf1[ix-1] == phonPAUSE_SHORT))
  1518. buf1[ix-1] = 0; // remove pause before addding ordinal suffix
  1519. strcpy(buf2, ph_ordinal2);
  1520. }
  1521. }
  1522. sprintf(ph_out, "%s%s%c%s", buf1, ph_hundred_and, phonEND_WORD, buf2);
  1523. return 0;
  1524. }
  1525. static bool CheckThousandsGroup(char *word, int group_len)
  1526. {
  1527. // Is this a group of 3 digits which looks like a thousands group?
  1528. int ix;
  1529. if (IsDigit09(word[group_len]) || IsDigit09(-1))
  1530. return false;
  1531. for (ix = 0; ix < group_len; ix++) {
  1532. if (!IsDigit09(word[ix]))
  1533. return false;
  1534. }
  1535. return true;
  1536. }
  1537. static int TranslateNumber_1(Translator *tr, char *word, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
  1538. {
  1539. // Number translation with various options
  1540. // the "word" may be up to 4 digits
  1541. // "words" of 3 digits may be preceded by another number "word" for thousands or millions
  1542. int n_digits;
  1543. int value;
  1544. int ix;
  1545. int digix;
  1546. unsigned char c;
  1547. bool suppress_null = false;
  1548. int decimal_point = 0;
  1549. int thousandplex = 0;
  1550. int thousands_exact = 1;
  1551. int thousands_inc = 0;
  1552. int prev_thousands = 0;
  1553. int ordinal = 0;
  1554. int this_value;
  1555. int decimal_count;
  1556. int max_decimal_count;
  1557. int decimal_mode;
  1558. int suffix_ix;
  1559. int skipwords = 0;
  1560. int group_len;
  1561. int len;
  1562. char *p;
  1563. char string[32]; // for looking up entries in **_list
  1564. char buf1[100];
  1565. char ph_append[50];
  1566. char ph_buf[200];
  1567. char ph_buf2[50];
  1568. char ph_zeros[50];
  1569. char suffix[30]; // string[] must be long enough for sizeof(suffix)+2
  1570. char buf_digit_lookup[50];
  1571. static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };
  1572. *flags = 0;
  1573. n_digit_lookup = 0;
  1574. buf_digit_lookup[0] = 0;
  1575. digit_lookup = buf_digit_lookup;
  1576. number_control = control;
  1577. for (ix = 0; IsDigit09(word[ix]); ix++) ;
  1578. n_digits = ix;
  1579. value = this_value = atoi(word);
  1580. group_len = 3;
  1581. if (tr->langopts.numbers2 & NUM2_MYRIADS)
  1582. group_len = 4;
  1583. // is there a previous thousands part (as a previous "word") ?
  1584. if ((n_digits == group_len) && (word[-2] == tr->langopts.thousands_sep) && IsDigit09(word[-3]))
  1585. prev_thousands = 1;
  1586. else if ((tr->langopts.thousands_sep == ' ') || (tr->langopts.numbers & NUM_ALLOW_SPACE)) {
  1587. // thousands groups can be separated by spaces
  1588. if ((n_digits == 3) && !(wtab->flags & FLAG_MULTIPLE_SPACES) && IsDigit09(word[-2]))
  1589. prev_thousands = 1;
  1590. }
  1591. if (prev_thousands == 0)
  1592. speak_missing_thousands = 0;
  1593. ph_ordinal2[0] = 0;
  1594. ph_zeros[0] = 0;
  1595. if (prev_thousands || (word[0] != '0')) {
  1596. // don't check for ordinal if the number has a leading zero
  1597. ordinal = CheckDotOrdinal(tr, word, &word[ix], wtab, 0);
  1598. }
  1599. if ((word[ix] == '.') && !IsDigit09(word[ix+1]) && !IsDigit09(word[ix+2]) && !(wtab[1].flags & FLAG_NOSPACE)) {
  1600. // remove dot unless followed by another number
  1601. word[ix] = 0;
  1602. }
  1603. if ((ordinal == 0) || (tr->translator_name == L('h', 'u'))) {
  1604. // NOTE lang=hu, allow both dot and ordinal suffix, eg. "december 21.-én"
  1605. // look for an ordinal number suffix after the number
  1606. ix++;
  1607. p = suffix;
  1608. if (wtab[0].flags & FLAG_HYPHEN_AFTER) {
  1609. *p++ = '-';
  1610. ix++;
  1611. }
  1612. while ((word[ix] != 0) && (word[ix] != ' ') && (ix < (int)(sizeof(suffix)-1)))
  1613. *p++ = word[ix++];
  1614. *p = 0;
  1615. if (suffix[0] != 0) {
  1616. if ((tr->langopts.ordinal_indicator != NULL) && (strcmp(suffix, tr->langopts.ordinal_indicator) == 0))
  1617. ordinal = 2;
  1618. else if (!IsDigit09(suffix[0])) { // not _#9 (tab)
  1619. sprintf(string, "_#%s", suffix);
  1620. if (Lookup(tr, string, ph_ordinal2)) {
  1621. // this is an ordinal suffix
  1622. ordinal = 2;
  1623. flags[0] |= FLAG_SKIPWORDS;
  1624. skipwords = 1;
  1625. sprintf(string, "_x#%s", suffix);
  1626. Lookup(tr, string, ph_ordinal2x); // is there an alternate pronunciation?
  1627. }
  1628. }
  1629. }
  1630. }
  1631. if (wtab[0].flags & FLAG_ORDINAL)
  1632. ordinal = 2;
  1633. ph_append[0] = 0;
  1634. ph_buf2[0] = 0;
  1635. if ((word[0] == '0') && (prev_thousands == 0) && (word[1] != ' ') && (word[1] != tr->langopts.decimal_sep)) {
  1636. if ((n_digits == 2) && (word[3] == ':') && IsDigit09(word[5]) && isspace(word[7])) {
  1637. // looks like a time 02:30, omit the leading zero
  1638. } else {
  1639. if (n_digits > 3) {
  1640. flags[0] &= ~FLAG_SKIPWORDS;
  1641. return 0; // long number string with leading zero, speak as individual digits
  1642. }
  1643. // speak leading zeros
  1644. for (ix = 0; (word[ix] == '0') && (ix < (n_digits-1)); ix++)
  1645. Lookup(tr, "_0", &ph_zeros[strlen(ph_zeros)]);
  1646. }
  1647. }
  1648. if ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[n_digits] == ' '))
  1649. thousands_inc = 1;
  1650. else if (word[n_digits] == tr->langopts.thousands_sep)
  1651. thousands_inc = 2;
  1652. suffix_ix = n_digits+2;
  1653. if (thousands_inc > 0) {
  1654. // if the following "words" are three-digit groups, count them and add
  1655. // a "thousand"/"million" suffix to this one
  1656. digix = n_digits + thousands_inc;
  1657. while (((wtab[thousandplex+1].flags & FLAG_MULTIPLE_SPACES) == 0) && CheckThousandsGroup(&word[digix], group_len)) {
  1658. for (ix = 0; ix < group_len; ix++) {
  1659. if (word[digix+ix] != '0') {
  1660. thousands_exact = 0;
  1661. break;
  1662. }
  1663. }
  1664. thousandplex++;
  1665. digix += group_len;
  1666. if ((word[digix] == tr->langopts.thousands_sep) || ((tr->langopts.numbers & NUM_ALLOW_SPACE) && (word[digix] == ' '))) {
  1667. suffix_ix = digix+2;
  1668. digix += thousands_inc;
  1669. } else
  1670. break;
  1671. }
  1672. }
  1673. if ((value == 0) && prev_thousands)
  1674. suppress_null = true;
  1675. if (tr->translator_name == L('h', 'u')) {
  1676. // variant form of numbers when followed by hyphen and a suffix starting with 'a' or 'e' (but not a, e, az, ez, azt, ezt
  1677. if ((wtab[thousandplex].flags & FLAG_HYPHEN_AFTER) && (thousands_exact == 1) && hu_number_e(&word[suffix_ix], thousandplex, value))
  1678. number_control |= 1; // use _1e variant of number
  1679. }
  1680. if ((word[n_digits] == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
  1681. // this "word" ends with a decimal point
  1682. Lookup(tr, "_dpt", ph_append);
  1683. decimal_point = 0x100;
  1684. } else if (suppress_null == false) {
  1685. if (thousands_inc > 0) {
  1686. if (thousandplex > 0) {
  1687. if ((suppress_null == false) && (LookupThousands(tr, value, thousandplex, thousands_exact, ph_append))) {
  1688. // found an exact match for N thousand
  1689. value = 0;
  1690. suppress_null = true;
  1691. }
  1692. }
  1693. }
  1694. } else if (speak_missing_thousands == 1) {
  1695. // speak this thousandplex if there was no word for the previous thousandplex
  1696. sprintf(string, "_0M%d", thousandplex+1);
  1697. if (Lookup(tr, string, buf1) == 0) {
  1698. sprintf(string, "_0M%d", thousandplex);
  1699. Lookup(tr, string, ph_append);
  1700. }
  1701. }
  1702. if ((ph_append[0] == 0) && (word[n_digits] == '.') && (thousandplex == 0))
  1703. Lookup(tr, "_.", ph_append);
  1704. if (thousandplex == 0) {
  1705. char *p2;
  1706. // look for combinations of the number with the next word
  1707. p = word;
  1708. while (IsDigit09(p[1])) p++; // just use the last digit
  1709. if (IsDigit09(p[-1])) {
  1710. p2 = p - 1;
  1711. if (LookupDictList(tr, &p2, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // lookup 2 digits
  1712. n_digit_lookup = 2;
  1713. }
  1714. if ((buf_digit_lookup[0] == 0) && (*p != '0')) {
  1715. // LANG=hu ?
  1716. // not found, lookup only the last digit (?? but not if dot-ordinal has been found)
  1717. if (LookupDictList(tr, &p, buf_digit_lookup, flags, FLAG_SUFX, wtab)) // don't match '0', or entries with $only
  1718. n_digit_lookup = 1;
  1719. }
  1720. if (prev_thousands == 0) {
  1721. if ((decimal_point == 0) && (ordinal == 0)) {
  1722. // Look for special pronunciation for this number in isolation (LANG=kl)
  1723. sprintf(string, "_%dn", value);
  1724. if (Lookup(tr, string, ph_out))
  1725. return 1;
  1726. }
  1727. if (tr->langopts.numbers2 & NUM2_PERCENT_BEFORE) {
  1728. // LANG=si, say "percent" before the number
  1729. p2 = word;
  1730. while ((*p2 != ' ') && (*p2 != 0))
  1731. p2++;
  1732. if (p2[1] == '%') {
  1733. Lookup(tr, "%", ph_out);
  1734. ph_out += strlen(ph_out);
  1735. p2[1] = ' ';
  1736. }
  1737. }
  1738. }
  1739. }
  1740. LookupNum3(tr, value, ph_buf, suppress_null, thousandplex, prev_thousands | ordinal | decimal_point);
  1741. if ((thousandplex > 0) && (tr->langopts.numbers2 & 0x200))
  1742. sprintf(ph_out, "%s%s%c%s%s", ph_zeros, ph_append, phonEND_WORD, ph_buf2, ph_buf); // say "thousands" before its number
  1743. else
  1744. sprintf(ph_out, "%s%s%s%c%s", ph_zeros, ph_buf2, ph_buf, phonEND_WORD, ph_append);
  1745. while (decimal_point) {
  1746. n_digits++;
  1747. decimal_count = 0;
  1748. while (IsDigit09(word[n_digits+decimal_count]))
  1749. decimal_count++;
  1750. max_decimal_count = 2;
  1751. switch (decimal_mode = (tr->langopts.numbers & 0xe000))
  1752. {
  1753. case NUM_DFRACTION_4:
  1754. max_decimal_count = 5;
  1755. // fallthrough:
  1756. case NUM_DFRACTION_2:
  1757. // French/Polish decimal fraction
  1758. while (word[n_digits] == '0') {
  1759. Lookup(tr, "_0", buf1);
  1760. strcat(ph_out, buf1);
  1761. decimal_count--;
  1762. n_digits++;
  1763. }
  1764. if ((decimal_count <= max_decimal_count) && IsDigit09(word[n_digits])) {
  1765. LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
  1766. strcat(ph_out, buf1);
  1767. n_digits += decimal_count;
  1768. }
  1769. break;
  1770. case NUM_DFRACTION_1: // italian, say "hundredths" if leading zero
  1771. case NUM_DFRACTION_5: // hungarian, always say "tenths" etc.
  1772. case NUM_DFRACTION_6: // kazakh, always say "tenths" etc, before the decimal fraction
  1773. LookupNum3(tr, atoi(&word[n_digits]), ph_buf, false, 0, 0);
  1774. if ((word[n_digits] == '0') || (decimal_mode != NUM_DFRACTION_1)) {
  1775. // decimal part has leading zeros, so add a "hundredths" or "thousandths" suffix
  1776. sprintf(string, "_0Z%d", decimal_count);
  1777. if (Lookup(tr, string, buf1) == 0)
  1778. break; // revert to speaking single digits
  1779. if (decimal_mode == NUM_DFRACTION_6)
  1780. strcat(ph_out, buf1);
  1781. else
  1782. strcat(ph_buf, buf1);
  1783. }
  1784. strcat(ph_out, ph_buf);
  1785. n_digits += decimal_count;
  1786. break;
  1787. case NUM_DFRACTION_3:
  1788. // Romanian decimal fractions
  1789. if ((decimal_count <= 4) && (word[n_digits] != '0')) {
  1790. LookupNum3(tr, atoi(&word[n_digits]), buf1, false, 0, 0);
  1791. strcat(ph_out, buf1);
  1792. n_digits += decimal_count;
  1793. }
  1794. break;
  1795. case NUM_DFRACTION_7:
  1796. // alternative form of decimal fraction digits, except the final digit
  1797. while (decimal_count-- > 1) {
  1798. sprintf(string, "_%cd", word[n_digits]);
  1799. if (Lookup(tr, string, buf1) == 0)
  1800. break;
  1801. n_digits++;
  1802. strcat(ph_out, buf1);
  1803. }
  1804. }
  1805. while (IsDigit09(c = word[n_digits]) && (strlen(ph_out) < (N_WORD_PHONEMES - 10))) {
  1806. // speak any remaining decimal fraction digits individually
  1807. value = word[n_digits++] - '0';
  1808. LookupNum2(tr, value, 0, 2, buf1);
  1809. len = strlen(ph_out);
  1810. sprintf(&ph_out[len], "%c%s", phonEND_WORD, buf1);
  1811. }
  1812. // something after the decimal part ?
  1813. if (Lookup(tr, "_dpt2", buf1))
  1814. strcat(ph_out, buf1);
  1815. if ((c == tr->langopts.decimal_sep) && IsDigit09(word[n_digits+1])) {
  1816. Lookup(tr, "_dpt", buf1);
  1817. strcat(ph_out, buf1);
  1818. } else
  1819. decimal_point = 0;
  1820. }
  1821. if ((ph_out[0] != 0) && (ph_out[0] != phonSWITCH)) {
  1822. int next_char;
  1823. char *p;
  1824. p = &word[n_digits+1];
  1825. p += utf8_in(&next_char, p);
  1826. if ((tr->langopts.numbers & NUM_NOPAUSE) && (next_char == ' '))
  1827. utf8_in(&next_char, p);
  1828. if (!iswalpha(next_char) && (thousands_exact == 0))
  1829. strcat(ph_out, str_pause); // don't add pause for 100s, 6th, etc.
  1830. }
  1831. *flags |= FLAG_FOUND;
  1832. speak_missing_thousands--;
  1833. if (skipwords)
  1834. dictionary_skipwords = skipwords;
  1835. return 1;
  1836. }
  1837. int TranslateNumber(Translator *tr, char *word1, char *ph_out, unsigned int *flags, WORD_TAB *wtab, int control)
  1838. {
  1839. if ((option_sayas == SAYAS_DIGITS1) || (wtab[0].flags & FLAG_INDIVIDUAL_DIGITS))
  1840. return 0; // speak digits individually
  1841. if (tr->langopts.numbers != 0)
  1842. return TranslateNumber_1(tr, word1, ph_out, flags, wtab, control);
  1843. return 0;
  1844. }