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.

dictionary.c 89KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2017 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 <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <wctype.h>
  26. #include <wchar.h>
  27. #include <assert.h>
  28. #include <espeak-ng/espeak_ng.h>
  29. #include <espeak-ng/speak_lib.h>
  30. #include <espeak-ng/encoding.h>
  31. #include "dictionary.h"
  32. #include "numbers.h" // for LookupAccentedLetter, Look...
  33. #include "phoneme.h" // for PHONEME_TAB, phVOWEL, phon...
  34. #include "readclause.h" // for WordToString2, is_str_tota...
  35. #include "speech.h" // for GetFileLength, path_home
  36. #include "compiledict.h" // for DecodeRule
  37. #include "synthdata.h" // for PhonemeCode, InterpretPhoneme
  38. #include "synthesize.h" // for STRESS_IS_PRIMARY, phoneme...
  39. #include "translate.h" // for Translator, utf8_in, LANGU...
  40. typedef struct {
  41. int points;
  42. const char *phonemes;
  43. int end_type;
  44. char *del_fwd;
  45. } MatchRecord;
  46. int dictionary_skipwords;
  47. char dictionary_name[40];
  48. // accented characters which indicate (in some languages) the start of a separate syllable
  49. static const unsigned short diereses_list[7] = { 0xe4, 0xeb, 0xef, 0xf6, 0xfc, 0xff, 0 };
  50. // convert characters to an approximate 7 bit ascii equivalent
  51. // used for checking for vowels (up to 0x259=schwa)
  52. #define N_REMOVE_ACCENT 0x25e
  53. static unsigned char remove_accent[N_REMOVE_ACCENT] = {
  54. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', // 0c0
  55. 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 's', // 0d0
  56. 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', // 0e0
  57. 'd', 'n', 'o', 'o', 'o', 'o', 'o', 0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 'y', // 0f0
  58. 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'd', 'd', // 100
  59. 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', // 110
  60. 'g', 'g', 'g', 'g', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', // 120
  61. 'i', 'i', 'i', 'i', 'j', 'j', 'k', 'k', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l', // 130
  62. 'l', 'l', 'l', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', // 140
  63. 'o', 'o', 'o', 'o', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', // 150
  64. 's', 's', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 160
  65. 'u', 'u', 'u', 'u', 'w', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 's', // 170
  66. 'b', 'b', 'b', 'b', 0, 0, 'o', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'e', 'e', // 180
  67. 'e', 'f', 'f', 'g', 'g', 'h', 'i', 'i', 'k', 'k', 'l', 'l', 'm', 'n', 'n', 'o', // 190
  68. 'o', 'o', 'o', 'o', 'p', 'p', 'y', 0, 0, 's', 's', 't', 't', 't', 't', 'u', // 1a0
  69. 'u', 'u', 'v', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 0, 0, 0, 'w', // 1b0
  70. 't', 't', 't', 'k', 'd', 'd', 'd', 'l', 'l', 'l', 'n', 'n', 'n', 'a', 'a', 'i', // 1c0
  71. 'i', 'o', 'o', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'e', 'a', 'a', // 1d0
  72. 'a', 'a', 'a', 'a', 'g', 'g', 'g', 'g', 'k', 'k', 'o', 'o', 'o', 'o', 'z', 'z', // 1e0
  73. 'j', 'd', 'd', 'd', 'g', 'g', 'w', 'w', 'n', 'n', 'a', 'a', 'a', 'a', 'o', 'o', // 1f0
  74. 'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', // 200
  75. 'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 's', 's', 't', 't', 'y', 'y', 'h', 'h', // 210
  76. 'n', 'd', 'o', 'o', 'z', 'z', 'a', 'a', 'e', 'e', 'o', 'o', 'o', 'o', 'o', 'o', // 220
  77. 'o', 'o', 'y', 'y', 'l', 'n', 't', 'j', 'd', 'q', 'a', 'c', 'c', 'l', 't', 's', // 230
  78. 'z', 0, 0, 'b', 'u', 'v', 'e', 'e', 'j', 'j', 'q', 'q', 'r', 'r', 'y', 'y', // 240
  79. 'a', 'a', 'a', 'b', 'o', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e'
  80. };
  81. #pragma GCC visibility push(default)
  82. void strncpy0(char *to, const char *from, int size)
  83. {
  84. // strcpy with limit, ensures a zero terminator
  85. strncpy(to, from, size);
  86. to[size-1] = 0;
  87. }
  88. #pragma GCC visibility pop
  89. static int Reverse4Bytes(int word)
  90. {
  91. // reverse the order of bytes from little-endian to big-endian
  92. #ifdef ARCH_BIG
  93. int ix;
  94. int word2 = 0;
  95. for (ix = 0; ix <= 24; ix += 8) {
  96. word2 = word2 << 8;
  97. word2 |= (word >> ix) & 0xff;
  98. }
  99. return word2;
  100. #else
  101. return word;
  102. #endif
  103. }
  104. static void InitGroups(Translator *tr)
  105. {
  106. // Called after dictionary 1 is loaded, to set up table of entry points for translation rule chains
  107. // for single-letters and two-letter combinations
  108. int ix;
  109. char *p;
  110. char *p_name;
  111. unsigned char c, c2;
  112. int len;
  113. tr->n_groups2 = 0;
  114. for (ix = 0; ix < 256; ix++) {
  115. tr->groups1[ix] = NULL;
  116. tr->groups2_count[ix] = 0;
  117. tr->groups2_start[ix] = 255; // indicates "not set"
  118. }
  119. memset(tr->letterGroups, 0, sizeof(tr->letterGroups));
  120. memset(tr->groups3, 0, sizeof(tr->groups3));
  121. p = tr->data_dictrules;
  122. // If there are no rules in the dictionary, compile_dictrules will not
  123. // write a RULE_GROUP_START (written in the for loop), but will write
  124. // a RULE_GROUP_END.
  125. if (*p != RULE_GROUP_END) while (*p != 0) {
  126. if (*p != RULE_GROUP_START) {
  127. fprintf(stderr, "Bad rules data in '%s_dict' at 0x%x (%c)\n", dictionary_name, (unsigned int)(p - tr->data_dictrules), *p);
  128. break;
  129. }
  130. p++;
  131. if (p[0] == RULE_REPLACEMENTS) {
  132. p = (char *)(((intptr_t)p+4) & ~3); // advance to next word boundary
  133. tr->langopts.replace_chars = (unsigned char *)p;
  134. while ( !is_str_totally_null(p, 4) ) {
  135. p++;
  136. }
  137. while (*p != RULE_GROUP_END) p++;
  138. p++;
  139. continue;
  140. }
  141. if (p[0] == RULE_LETTERGP2) {
  142. ix = p[1] - 'A';
  143. if (ix < 0)
  144. ix += 256;
  145. p += 2;
  146. if ((ix >= 0) && (ix < N_LETTER_GROUPS))
  147. tr->letterGroups[ix] = p;
  148. } else {
  149. len = strlen(p);
  150. p_name = p;
  151. c = p_name[0];
  152. c2 = p_name[1];
  153. p += (len+1);
  154. if (len == 1)
  155. tr->groups1[c] = p;
  156. else if (len == 0)
  157. tr->groups1[0] = p;
  158. else if (c == 1) {
  159. // index by offset from letter base
  160. tr->groups3[c2 - 1] = p;
  161. } else {
  162. if (tr->groups2_start[c] == 255)
  163. tr->groups2_start[c] = tr->n_groups2;
  164. tr->groups2_count[c]++;
  165. tr->groups2[tr->n_groups2] = p;
  166. tr->groups2_name[tr->n_groups2++] = (c + (c2 << 8));
  167. }
  168. }
  169. // skip over all the rules in this group
  170. while (*p != RULE_GROUP_END)
  171. p += (strlen(p) + 1);
  172. p++;
  173. }
  174. }
  175. int LoadDictionary(Translator *tr, const char *name, int no_error)
  176. {
  177. int hash;
  178. char *p;
  179. int *pw;
  180. int length;
  181. FILE *f;
  182. int size;
  183. char fname[sizeof(path_home)+20];
  184. if (dictionary_name != name)
  185. strncpy(dictionary_name, name, 40); // currently loaded dictionary name
  186. if (tr->dictionary_name != name)
  187. strncpy(tr->dictionary_name, name, 40);
  188. // Load a pronunciation data file into memory
  189. // bytes 0-3: offset to rules data
  190. // bytes 4-7: number of hash table entries
  191. sprintf(fname, "%s%c%s_dict", path_home, PATHSEP, name);
  192. size = GetFileLength(fname);
  193. if (tr->data_dictlist != NULL) {
  194. free(tr->data_dictlist);
  195. tr->data_dictlist = NULL;
  196. }
  197. f = fopen(fname, "rb");
  198. if ((f == NULL) || (size <= 0)) {
  199. if (no_error == 0)
  200. fprintf(stderr, "Can't read dictionary file: '%s'\n", fname);
  201. if (f != NULL)
  202. fclose(f);
  203. return 1;
  204. }
  205. if ((tr->data_dictlist = malloc(size)) == NULL) {
  206. fclose(f);
  207. return 3;
  208. }
  209. size = fread(tr->data_dictlist, 1, size, f);
  210. fclose(f);
  211. pw = (int *)(tr->data_dictlist);
  212. length = Reverse4Bytes(pw[1]);
  213. if (size <= (N_HASH_DICT + sizeof(int)*2)) {
  214. fprintf(stderr, "Empty _dict file: '%s\n", fname);
  215. return 2;
  216. }
  217. if ((Reverse4Bytes(pw[0]) != N_HASH_DICT) ||
  218. (length <= 0) || (length > 0x8000000)) {
  219. fprintf(stderr, "Bad data: '%s' (%x length=%x)\n", fname, Reverse4Bytes(pw[0]), length);
  220. return 2;
  221. }
  222. tr->data_dictrules = &(tr->data_dictlist[length]);
  223. // set up indices into data_dictrules
  224. InitGroups(tr);
  225. // set up hash table for data_dictlist
  226. p = &(tr->data_dictlist[8]);
  227. for (hash = 0; hash < N_HASH_DICT; hash++) {
  228. tr->dict_hashtab[hash] = p;
  229. while ((length = *(uint8_t *)p) != 0)
  230. p += length;
  231. p++; // skip over the zero which terminates the list for this hash value
  232. }
  233. if ((tr->dict_min_size > 0) && (size < (unsigned int)tr->dict_min_size))
  234. fprintf(stderr, "Full dictionary is not installed for '%s'\n", name);
  235. return 0;
  236. }
  237. /* Generate a hash code from the specified string
  238. This is used to access the dictionary_2 word-lookup dictionary
  239. */
  240. int HashDictionary(const char *string)
  241. {
  242. int c;
  243. int chars = 0;
  244. int hash = 0;
  245. while ((c = (*string++ & 0xff)) != 0) {
  246. hash = hash * 8 + c;
  247. hash = (hash & 0x3ff) ^ (hash >> 8); // exclusive or
  248. chars++;
  249. }
  250. return (hash+chars) & 0x3ff; // a 10 bit hash code
  251. }
  252. /* Translate a phoneme string from ascii mnemonics to internal phoneme numbers,
  253. from 'p' up to next blank .
  254. Returns advanced 'p'
  255. outptr contains encoded phonemes, unrecognized phoneme stops the encoding
  256. bad_phoneme must point to char array of length 2 of more
  257. */
  258. const char *EncodePhonemes(const char *p, char *outptr, int *bad_phoneme)
  259. {
  260. int ix;
  261. unsigned char c;
  262. int count; // num. of matching characters
  263. int max; // highest num. of matching found so far
  264. int max_ph; // corresponding phoneme with highest matching
  265. int consumed;
  266. unsigned int mnemonic_word;
  267. if (bad_phoneme != NULL)
  268. *bad_phoneme = 0;
  269. // skip initial blanks
  270. while ((uint8_t)*p < 0x80 && isspace(*p))
  271. p++;
  272. while (((c = *p) != 0) && !isspace(c)) {
  273. consumed = 0;
  274. switch (c)
  275. {
  276. case '|':
  277. // used to separate phoneme mnemonics if needed, to prevent characters being treated
  278. // as a multi-letter mnemonic
  279. if ((c = p[1]) == '|') {
  280. // treat double || as a word-break symbol, drop through
  281. // to the default case with c = '|'
  282. } else {
  283. p++;
  284. break;
  285. }
  286. default:
  287. // lookup the phoneme mnemonic, find the phoneme with the highest number of
  288. // matching characters
  289. max = -1;
  290. max_ph = 0;
  291. for (ix = 1; ix < n_phoneme_tab; ix++) {
  292. if (phoneme_tab[ix] == NULL)
  293. continue;
  294. if (phoneme_tab[ix]->type == phINVALID)
  295. continue; // this phoneme is not defined for this language
  296. count = 0;
  297. mnemonic_word = phoneme_tab[ix]->mnemonic;
  298. while (((c = p[count]) > ' ') && (count < 4) &&
  299. (c == ((mnemonic_word >> (count*8)) & 0xff)))
  300. count++;
  301. if ((count > max) &&
  302. ((count == 4) || (((mnemonic_word >> (count*8)) & 0xff) == 0))) {
  303. max = count;
  304. max_ph = phoneme_tab[ix]->code;
  305. }
  306. }
  307. if (max_ph == 0) {
  308. // not recognised, report and ignore
  309. if (bad_phoneme != NULL)
  310. utf8_in(bad_phoneme, p);
  311. *outptr++ = 0;
  312. return p+1;
  313. }
  314. if (max <= 0)
  315. max = 1;
  316. p += (consumed + max);
  317. *outptr++ = (char)(max_ph);
  318. if (max_ph == phonSWITCH) {
  319. // Switch Language: this phoneme is followed by a text string
  320. char *p_lang = outptr;
  321. while (!isspace(c = *p) && (c != 0)) {
  322. p++;
  323. *outptr++ = tolower(c);
  324. }
  325. *outptr = 0;
  326. if (c == 0) {
  327. if (strcmp(p_lang, ESPEAKNG_DEFAULT_VOICE) == 0) {
  328. *p_lang = 0; // don't need ESPEAKNG_DEFAULT_VOICE, it's assumed by default
  329. return p;
  330. }
  331. } else
  332. *outptr++ = '|'; // more phonemes follow, terminate language string with separator
  333. }
  334. break;
  335. }
  336. }
  337. // terminate the encoded string
  338. *outptr = 0;
  339. return p;
  340. }
  341. void DecodePhonemes(const char *inptr, char *outptr)
  342. {
  343. // Translate from internal phoneme codes into phoneme mnemonics
  344. unsigned char phcode;
  345. unsigned char c;
  346. unsigned int mnem;
  347. PHONEME_TAB *ph;
  348. static const char *stress_chars = "==,,'* ";
  349. sprintf(outptr, "* ");
  350. while ((phcode = *inptr++) > 0) {
  351. if (phcode == 255)
  352. continue; // indicates unrecognised phoneme
  353. if ((ph = phoneme_tab[phcode]) == NULL)
  354. continue;
  355. if ((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0)) {
  356. if (ph->std_length > 1)
  357. *outptr++ = stress_chars[ph->std_length];
  358. } else {
  359. mnem = ph->mnemonic;
  360. while ((c = (mnem & 0xff)) != 0) {
  361. *outptr++ = c;
  362. mnem = mnem >> 8;
  363. }
  364. if (phcode == phonSWITCH) {
  365. while (isalpha(*inptr))
  366. *outptr++ = *inptr++;
  367. }
  368. }
  369. }
  370. *outptr = 0; // string terminator
  371. }
  372. // using Kirschenbaum to IPA translation, ascii 0x20 to 0x7f
  373. unsigned short ipa1[96] = {
  374. 0x20, 0x21, 0x22, 0x2b0, 0x24, 0x25, 0x0e6, 0x2c8, 0x28, 0x29, 0x27e, 0x2b, 0x2cc, 0x2d, 0x2e, 0x2f,
  375. 0x252, 0x31, 0x32, 0x25c, 0x34, 0x35, 0x36, 0x37, 0x275, 0x39, 0x2d0, 0x2b2, 0x3c, 0x3d, 0x3e, 0x294,
  376. 0x259, 0x251, 0x3b2, 0xe7, 0xf0, 0x25b, 0x46, 0x262, 0x127, 0x26a, 0x25f, 0x4b, 0x26b, 0x271, 0x14b, 0x254,
  377. 0x3a6, 0x263, 0x280, 0x283, 0x3b8, 0x28a, 0x28c, 0x153, 0x3c7, 0xf8, 0x292, 0x32a, 0x5c, 0x5d, 0x5e, 0x5f,
  378. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x261, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  379. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x303, 0x7f
  380. };
  381. #define N_PHON_OUT 500 // realloc increment
  382. static char *phon_out_buf = NULL; // passes the result of GetTranslatedPhonemeString()
  383. static unsigned int phon_out_size = 0;
  384. char *WritePhMnemonic(char *phon_out, PHONEME_TAB *ph, PHONEME_LIST *plist, int use_ipa, int *flags)
  385. {
  386. int c;
  387. int mnem;
  388. int len;
  389. bool first;
  390. int ix = 0;
  391. char *p;
  392. PHONEME_DATA phdata;
  393. if (ph->code == phonEND_WORD) {
  394. // ignore
  395. phon_out[0] = 0;
  396. return phon_out;
  397. }
  398. if (ph->code == phonSWITCH) {
  399. // the tone_ph field contains a phoneme table number
  400. p = phoneme_tab_list[plist->tone_ph].name;
  401. sprintf(phon_out, "(%s)", p);
  402. return phon_out + strlen(phon_out);
  403. }
  404. if (use_ipa) {
  405. // has an ipa name been defined for this phoneme ?
  406. phdata.ipa_string[0] = 0;
  407. if (plist == NULL)
  408. InterpretPhoneme2(ph->code, &phdata);
  409. else
  410. InterpretPhoneme(NULL, 0, plist, &phdata, NULL);
  411. p = phdata.ipa_string;
  412. if (*p == 0x20) {
  413. // indicates no name for this phoneme
  414. *phon_out = 0;
  415. return phon_out;
  416. }
  417. if ((*p != 0) && ((*p & 0xff) < 0x20)) {
  418. // name starts with a flags byte
  419. if (flags != NULL)
  420. *flags = *p;
  421. p++;
  422. }
  423. len = strlen(p);
  424. if (len > 0) {
  425. strcpy(phon_out, p);
  426. phon_out += len;
  427. *phon_out = 0;
  428. return phon_out;
  429. }
  430. }
  431. first = true;
  432. for (mnem = ph->mnemonic; (c = mnem & 0xff) != 0; mnem = mnem >> 8) {
  433. if (c == '/')
  434. break; // discard phoneme variant indicator
  435. if (use_ipa) {
  436. // convert from ascii to ipa
  437. if (first && (c == '_'))
  438. break; // don't show pause phonemes
  439. if ((c == '#') && (ph->type == phVOWEL))
  440. break; // # is subscript-h, but only for consonants
  441. // ignore digits after the first character
  442. if (!first && IsDigit09(c))
  443. continue;
  444. if ((c >= 0x20) && (c < 128))
  445. c = ipa1[c-0x20];
  446. ix += utf8_out(c, &phon_out[ix]);
  447. } else
  448. phon_out[ix++] = c;
  449. first = false;
  450. }
  451. phon_out = &phon_out[ix];
  452. *phon_out = 0;
  453. return phon_out;
  454. }
  455. const char *GetTranslatedPhonemeString(int phoneme_mode)
  456. {
  457. /* Called after a clause has been translated into phonemes, in order
  458. to display the clause in phoneme mnemonic form.
  459. phoneme_mode
  460. bit 1: use IPA phoneme names
  461. bit 7: use tie between letters in multi-character phoneme names
  462. bits 8-23 tie or separator character
  463. */
  464. int ix;
  465. unsigned int len;
  466. int phon_out_ix = 0;
  467. int stress;
  468. int c;
  469. char *p;
  470. char *buf;
  471. int count;
  472. int flags;
  473. int use_ipa;
  474. int use_tie;
  475. int separate_phonemes;
  476. char phon_buf[30];
  477. char phon_buf2[30];
  478. PHONEME_LIST *plist;
  479. static const char *stress_chars = "==,,''";
  480. if (phon_out_buf == NULL) {
  481. phon_out_size = N_PHON_OUT;
  482. if ((phon_out_buf = (char *)malloc(phon_out_size)) == NULL) {
  483. phon_out_size = 0;
  484. return "";
  485. }
  486. }
  487. use_ipa = phoneme_mode & espeakPHONEMES_IPA;
  488. if (phoneme_mode & espeakPHONEMES_TIE) {
  489. use_tie = phoneme_mode >> 8;
  490. separate_phonemes = 0;
  491. } else {
  492. separate_phonemes = phoneme_mode >> 8;
  493. use_tie = 0;
  494. }
  495. for (ix = 1; ix < (n_phoneme_list-2); ix++) {
  496. buf = phon_buf;
  497. plist = &phoneme_list[ix];
  498. WritePhMnemonic(phon_buf2, plist->ph, plist, use_ipa, &flags);
  499. if (plist->newword & PHLIST_START_OF_WORD && !(plist->newword & (PHLIST_START_OF_SENTENCE | PHLIST_START_OF_CLAUSE)))
  500. *buf++ = ' ';
  501. if ((!plist->newword) || (separate_phonemes == ' ')) {
  502. if ((separate_phonemes != 0) && (ix > 1)) {
  503. utf8_in(&c, phon_buf2);
  504. if ((c < 0x2b0) || (c > 0x36f)) // not if the phoneme starts with a superscript letter
  505. buf += utf8_out(separate_phonemes, buf);
  506. }
  507. }
  508. if (plist->synthflags & SFLAG_SYLLABLE) {
  509. if ((stress = plist->stresslevel) > 1) {
  510. c = 0;
  511. if (stress > STRESS_IS_PRIORITY) stress = STRESS_IS_PRIORITY;
  512. if (use_ipa) {
  513. c = 0x2cc; // ipa, secondary stress
  514. if (stress > STRESS_IS_SECONDARY)
  515. c = 0x02c8; // ipa, primary stress
  516. } else
  517. c = stress_chars[stress];
  518. if (c != 0)
  519. buf += utf8_out(c, buf);
  520. }
  521. }
  522. flags = 0;
  523. count = 0;
  524. for (p = phon_buf2; *p != 0;) {
  525. p += utf8_in(&c, p);
  526. if (use_tie != 0) {
  527. // look for non-initial alphabetic character, but not diacritic, superscript etc.
  528. if ((count > 0) && !(flags & (1 << (count-1))) && ((c < 0x2b0) || (c > 0x36f)) && iswalpha(c))
  529. buf += utf8_out(use_tie, buf);
  530. }
  531. buf += utf8_out(c, buf);
  532. count++;
  533. }
  534. if (plist->ph->code != phonSWITCH) {
  535. if (plist->synthflags & SFLAG_LENGTHEN)
  536. buf = WritePhMnemonic(buf, phoneme_tab[phonLENGTHEN], plist, use_ipa, NULL);
  537. if ((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL)) {
  538. // syllablic consonant
  539. buf = WritePhMnemonic(buf, phoneme_tab[phonSYLLABIC], plist, use_ipa, NULL);
  540. }
  541. if (plist->tone_ph > 0)
  542. buf = WritePhMnemonic(buf, phoneme_tab[plist->tone_ph], plist, use_ipa, NULL);
  543. }
  544. len = buf - phon_buf;
  545. if ((phon_out_ix + len) >= phon_out_size) {
  546. // enlarge the phoneme buffer
  547. phon_out_size = phon_out_ix + len + N_PHON_OUT;
  548. char *new_phon_out_buf = (char *)realloc(phon_out_buf, phon_out_size);
  549. if (new_phon_out_buf == NULL) {
  550. phon_out_size = 0;
  551. return "";
  552. } else
  553. phon_out_buf = new_phon_out_buf;
  554. }
  555. phon_buf[len] = 0;
  556. strcpy(&phon_out_buf[phon_out_ix], phon_buf);
  557. phon_out_ix += len;
  558. }
  559. if (!phon_out_buf)
  560. return "";
  561. phon_out_buf[phon_out_ix] = 0;
  562. return phon_out_buf;
  563. }
  564. static int LetterGroupNo(char *rule)
  565. {
  566. /*
  567. * Returns number of letter group
  568. */
  569. int groupNo = *rule;
  570. groupNo = groupNo - 'A'; // subtracting 'A' makes letter_group equal to number in .Lxx definition
  571. if (groupNo < 0) // fix sign if necessary
  572. groupNo += 256;
  573. return groupNo;
  574. }
  575. static int IsLetterGroup(Translator *tr, char *word, int group, int pre)
  576. {
  577. /* Match the word against a list of utf-8 strings.
  578. * returns length of matching letter group or -1
  579. *
  580. * How this works:
  581. *
  582. * +-+
  583. * |c|<-(tr->letterGroups[group])
  584. * |0|
  585. * *p->|c|<-len+ +-+
  586. * |s|<----+ |a|<-(Actual word to be tested)
  587. * |0| *word-> |t|<-*w=word-len+1 (for pre-rule)
  588. * |~| |a|<-*w=word (for post-rule)
  589. * |7| |s|
  590. * +-+ +-+
  591. *
  592. * 7=RULE_GROUP_END
  593. * 0=null terminator
  594. * pre==1 — pre-rule
  595. * pre==0 — post-rule
  596. */
  597. char *p; // group counter
  598. char *w; // word counter
  599. int len = 0, i;
  600. p = tr->letterGroups[group];
  601. if (p == NULL)
  602. return -1;
  603. while (*p != RULE_GROUP_END) {
  604. if (pre) {
  605. len = strlen(p);
  606. w = word;
  607. for (i = 0; i < len-1; i++)
  608. {
  609. w--;
  610. if (*w == 0)
  611. // Not found
  612. return -1;
  613. }
  614. } else
  615. w = word;
  616. // If '~' (no character) is allowed in group, return 0.
  617. if (*p == '~')
  618. return 0;
  619. // Check current group
  620. while ((*p == *w) && (*w != 0)) {
  621. w++;
  622. p++;
  623. }
  624. if (*p == 0) { // Matched the current group.
  625. if (pre)
  626. return len;
  627. return w - word;
  628. }
  629. // No match, so skip the rest of this group.
  630. while (*p++ != 0)
  631. ;
  632. }
  633. // Not found
  634. return -1;
  635. }
  636. static int IsLetter(Translator *tr, int letter, int group)
  637. {
  638. int letter2;
  639. if (tr->letter_groups[group] != NULL) {
  640. if (wcschr(tr->letter_groups[group], letter))
  641. return 1;
  642. return 0;
  643. }
  644. if (group > 7)
  645. return 0;
  646. if (tr->letter_bits_offset > 0) {
  647. if (((letter2 = (letter - tr->letter_bits_offset)) > 0) && (letter2 < 0x100))
  648. letter = letter2;
  649. else
  650. return 0;
  651. } else if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT))
  652. return tr->letter_bits[remove_accent[letter-0xc0]] & (1L << group);
  653. if ((letter >= 0) && (letter < 0x100))
  654. return tr->letter_bits[letter] & (1L << group);
  655. return 0;
  656. }
  657. int IsVowel(Translator *tr, int letter)
  658. {
  659. return IsLetter(tr, letter, LETTERGP_VOWEL2);
  660. }
  661. static int Unpronouncable2(Translator *tr, char *word)
  662. {
  663. int c;
  664. int end_flags;
  665. char ph_buf[N_WORD_PHONEMES];
  666. ph_buf[0] = 0;
  667. c = word[-1];
  668. word[-1] = ' '; // ensure there is a space before the "word"
  669. end_flags = TranslateRules(tr, word, ph_buf, sizeof(ph_buf), NULL, FLAG_UNPRON_TEST, NULL);
  670. word[-1] = c;
  671. if ((end_flags == 0) || (end_flags & SUFX_UNPRON))
  672. return 1;
  673. return 0;
  674. }
  675. int Unpronouncable(Translator *tr, char *word, int posn)
  676. {
  677. /* Determines whether a word in 'unpronouncable', i.e. whether it should
  678. be spoken as individual letters.
  679. This function may be language specific. This is a generic version.
  680. */
  681. int c;
  682. int c1 = 0;
  683. int vowel_posn = 9;
  684. int index;
  685. int count;
  686. ALPHABET *alphabet;
  687. utf8_in(&c, word);
  688. if ((tr->letter_bits_offset > 0) && (c < 0x241)) {
  689. // Latin characters for a language with a non-latin alphabet
  690. return 0; // so we can re-translate the word as English
  691. }
  692. if (((alphabet = AlphabetFromChar(c)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) {
  693. // Character is not in our alphabet
  694. return 0;
  695. }
  696. if (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 1)
  697. return 0;
  698. if (((c = *word) == ' ') || (c == 0) || (c == '\''))
  699. return 0;
  700. index = 0;
  701. count = 0;
  702. for (;;) {
  703. index += utf8_in(&c, &word[index]);
  704. if ((c == 0) || (c == ' '))
  705. break;
  706. if ((c == '\'') && ((count > 1) || (posn > 0)))
  707. break; // "tv'" but not "l'"
  708. if (count == 0)
  709. c1 = c;
  710. if ((c == '\'') && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 3)) {
  711. // don't count apostrophe
  712. } else
  713. count++;
  714. if (IsVowel(tr, c)) {
  715. vowel_posn = count; // position of the first vowel
  716. break;
  717. }
  718. if ((c != '\'') && !iswalpha(c))
  719. return 0;
  720. }
  721. if ((vowel_posn > 2) && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 2)) {
  722. // Lookup unpronounable rules in *_rules
  723. return Unpronouncable2(tr, word);
  724. }
  725. if (c1 == tr->langopts.param[LOPT_UNPRONOUNCABLE])
  726. vowel_posn--; // disregard this as the initial letter when counting
  727. if (vowel_posn > (tr->langopts.max_initial_consonants+1))
  728. return 1; // no vowel, or no vowel in first few letters
  729. return 0;
  730. }
  731. static int GetVowelStress(Translator *tr, unsigned char *phonemes, signed char *vowel_stress, int *vowel_count, int *stressed_syllable, int control)
  732. {
  733. // control = 1, set stress to 1 for forced unstressed vowels
  734. unsigned char phcode;
  735. PHONEME_TAB *ph;
  736. unsigned char *ph_out = phonemes;
  737. int count = 1;
  738. int max_stress = -1;
  739. int ix;
  740. int j;
  741. int stress = -1;
  742. int primary_posn = 0;
  743. vowel_stress[0] = STRESS_IS_UNSTRESSED;
  744. while (((phcode = *phonemes++) != 0) && (count < (N_WORD_PHONEMES/2)-1)) {
  745. if ((ph = phoneme_tab[phcode]) == NULL)
  746. continue;
  747. if ((ph->type == phSTRESS) && (ph->program == 0)) {
  748. // stress marker, use this for the following vowel
  749. if (phcode == phonSTRESS_PREV) {
  750. // primary stress on preceeding vowel
  751. j = count - 1;
  752. while ((j > 0) && (*stressed_syllable == 0) && (vowel_stress[j] < STRESS_IS_PRIMARY)) {
  753. if ((vowel_stress[j] != STRESS_IS_DIMINISHED) && (vowel_stress[j] != STRESS_IS_UNSTRESSED)) {
  754. // don't promote a phoneme which must be unstressed
  755. vowel_stress[j] = STRESS_IS_PRIMARY;
  756. if (max_stress < STRESS_IS_PRIMARY) {
  757. max_stress = STRESS_IS_PRIMARY;
  758. primary_posn = j;
  759. }
  760. /* reduce any preceding primary stress markers */
  761. for (ix = 1; ix < j; ix++) {
  762. if (vowel_stress[ix] == STRESS_IS_PRIMARY)
  763. vowel_stress[ix] = STRESS_IS_SECONDARY;
  764. }
  765. break;
  766. }
  767. j--;
  768. }
  769. } else {
  770. if ((ph->std_length < 4) || (*stressed_syllable == 0)) {
  771. stress = ph->std_length;
  772. if (stress > max_stress)
  773. max_stress = stress;
  774. }
  775. }
  776. continue;
  777. }
  778. if ((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) {
  779. vowel_stress[count] = (char)stress;
  780. if ((stress >= STRESS_IS_PRIMARY) && (stress >= max_stress)) {
  781. primary_posn = count;
  782. max_stress = stress;
  783. }
  784. if ((stress < 0) && (control & 1) && (ph->phflags & phUNSTRESSED))
  785. vowel_stress[count] = STRESS_IS_UNSTRESSED; // weak vowel, must be unstressed
  786. count++;
  787. stress = -1;
  788. } else if (phcode == phonSYLLABIC) {
  789. // previous consonant phoneme is syllablic
  790. vowel_stress[count] = (char)stress;
  791. if ((stress == 0) && (control & 1))
  792. vowel_stress[count++] = STRESS_IS_UNSTRESSED; // syllabic consonant, usually unstressed
  793. }
  794. *ph_out++ = phcode;
  795. }
  796. vowel_stress[count] = STRESS_IS_UNSTRESSED;
  797. *ph_out = 0;
  798. // has the position of the primary stress been specified by $1, $2, etc?
  799. if (*stressed_syllable > 0) {
  800. if (*stressed_syllable >= count)
  801. *stressed_syllable = count-1; // the final syllable
  802. vowel_stress[*stressed_syllable] = STRESS_IS_PRIMARY;
  803. max_stress = STRESS_IS_PRIMARY;
  804. primary_posn = *stressed_syllable;
  805. }
  806. if (max_stress == STRESS_IS_PRIORITY) {
  807. // priority stress, replaces any other primary stress marker
  808. for (ix = 1; ix < count; ix++) {
  809. if (vowel_stress[ix] == STRESS_IS_PRIMARY) {
  810. if (tr->langopts.stress_flags & S_PRIORITY_STRESS)
  811. vowel_stress[ix] = STRESS_IS_UNSTRESSED;
  812. else
  813. vowel_stress[ix] = STRESS_IS_SECONDARY;
  814. }
  815. if (vowel_stress[ix] == STRESS_IS_PRIORITY) {
  816. vowel_stress[ix] = STRESS_IS_PRIMARY;
  817. primary_posn = ix;
  818. }
  819. }
  820. max_stress = STRESS_IS_PRIMARY;
  821. }
  822. *stressed_syllable = primary_posn;
  823. *vowel_count = count;
  824. return max_stress;
  825. }
  826. static char stress_phonemes[] = {
  827. phonSTRESS_D, phonSTRESS_U, phonSTRESS_2, phonSTRESS_3,
  828. phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC
  829. };
  830. void ChangeWordStress(Translator *tr, char *word, int new_stress)
  831. {
  832. int ix;
  833. unsigned char *p;
  834. int max_stress;
  835. int vowel_count; // num of vowels + 1
  836. int stressed_syllable = 0; // position of stressed syllable
  837. unsigned char phonetic[N_WORD_PHONEMES];
  838. signed char vowel_stress[N_WORD_PHONEMES/2];
  839. strcpy((char *)phonetic, word);
  840. max_stress = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 0);
  841. if (new_stress >= STRESS_IS_PRIMARY) {
  842. // promote to primary stress
  843. for (ix = 1; ix < vowel_count; ix++) {
  844. if (vowel_stress[ix] >= max_stress) {
  845. vowel_stress[ix] = new_stress;
  846. break;
  847. }
  848. }
  849. } else {
  850. // remove primary stress
  851. for (ix = 1; ix < vowel_count; ix++) {
  852. if (vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
  853. vowel_stress[ix] = new_stress;
  854. }
  855. }
  856. // write out phonemes
  857. ix = 1;
  858. p = phonetic;
  859. while (*p != 0) {
  860. if ((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC)) {
  861. if ((vowel_stress[ix] == STRESS_IS_DIMINISHED) || (vowel_stress[ix] > STRESS_IS_UNSTRESSED))
  862. *word++ = stress_phonemes[(unsigned char)vowel_stress[ix]];
  863. ix++;
  864. }
  865. *word++ = *p++;
  866. }
  867. *word = 0;
  868. }
  869. void SetWordStress(Translator *tr, char *output, unsigned int *dictionary_flags, int tonic, int control)
  870. {
  871. /* Guess stress pattern of word. This is language specific
  872. 'output' is used for input and output
  873. 'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0)
  874. or unstressed (if == 7) or syllables 1 and 2 (if == 6)
  875. bits 8... dictionary flags
  876. If 'tonic' is set (>= 0), replace highest stress by this value.
  877. control: bit 0 This is an individual symbol, not a word
  878. bit 1 Suffix phonemes are still to be added
  879. */
  880. unsigned char phcode;
  881. unsigned char *p;
  882. PHONEME_TAB *ph;
  883. int stress;
  884. int max_stress;
  885. int max_stress_input; // any stress specified in the input?
  886. int vowel_count; // num of vowels + 1
  887. int ix;
  888. int v;
  889. int v_stress;
  890. int stressed_syllable; // position of stressed syllable
  891. int max_stress_posn;
  892. char *max_output;
  893. int final_ph;
  894. int final_ph2;
  895. int mnem;
  896. int opt_length;
  897. int stressflags;
  898. int dflags = 0;
  899. int first_primary;
  900. int long_vowel;
  901. signed char vowel_stress[N_WORD_PHONEMES/2];
  902. char syllable_weight[N_WORD_PHONEMES/2];
  903. char vowel_length[N_WORD_PHONEMES/2];
  904. unsigned char phonetic[N_WORD_PHONEMES];
  905. static const char consonant_types[16] = { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
  906. stressflags = tr->langopts.stress_flags;
  907. if (dictionary_flags != NULL)
  908. dflags = dictionary_flags[0];
  909. // copy input string into internal buffer
  910. for (ix = 0; ix < N_WORD_PHONEMES; ix++) {
  911. phonetic[ix] = output[ix];
  912. // check for unknown phoneme codes
  913. if (phonetic[ix] >= n_phoneme_tab)
  914. phonetic[ix] = phonSCHWA;
  915. if (phonetic[ix] == 0)
  916. break;
  917. }
  918. if (ix == 0) return;
  919. final_ph = phonetic[ix-1];
  920. final_ph2 = phonetic[(ix > 1) ? ix-2 : ix-1];
  921. max_output = output + (N_WORD_PHONEMES-3); // check for overrun
  922. // any stress position marked in the xx_list dictionary ?
  923. bool unstressed_word = false;
  924. stressed_syllable = dflags & 0x7;
  925. if (dflags & 0x8) {
  926. // this indicates a word without a primary stress
  927. stressed_syllable = dflags & 0x3;
  928. unstressed_word = true;
  929. }
  930. max_stress = max_stress_input = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 1);
  931. if ((max_stress < 0) && dictionary_flags)
  932. max_stress = STRESS_IS_DIMINISHED;
  933. // heavy or light syllables
  934. ix = 1;
  935. for (p = phonetic; *p != 0; p++) {
  936. if ((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC)) {
  937. int weight = 0;
  938. bool lengthened = false;
  939. if (phoneme_tab[p[1]]->code == phonLENGTHEN)
  940. lengthened = true;
  941. if (lengthened || (phoneme_tab[p[0]]->phflags & phLONG)) {
  942. // long vowel, increase syllable weight
  943. weight++;
  944. }
  945. vowel_length[ix] = weight;
  946. if (lengthened) p++; // advance over phonLENGTHEN
  947. if (consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG))) {
  948. // followed by two consonants, a long consonant, or consonant and end-of-word
  949. weight++;
  950. }
  951. syllable_weight[ix] = weight;
  952. ix++;
  953. }
  954. }
  955. switch (tr->langopts.stress_rule)
  956. {
  957. case STRESSPOSN_2LLH:
  958. // stress on first syllable, unless it is a light syllable followed by a heavy syllable
  959. if ((syllable_weight[1] > 0) || (syllable_weight[2] == 0))
  960. break;
  961. // fallthrough:
  962. case STRESSPOSN_2L:
  963. // stress on second syllable
  964. if ((stressed_syllable == 0) && (vowel_count > 2)) {
  965. stressed_syllable = 2;
  966. if (max_stress == STRESS_IS_DIMINISHED)
  967. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  968. max_stress = STRESS_IS_PRIMARY;
  969. }
  970. break;
  971. case STRESSPOSN_2R:
  972. // a language with stress on penultimate vowel
  973. if (stressed_syllable == 0) {
  974. // no explicit stress - stress the penultimate vowel
  975. max_stress = STRESS_IS_PRIMARY;
  976. if (vowel_count > 2) {
  977. stressed_syllable = vowel_count - 2;
  978. if (stressflags & S_FINAL_SPANISH) {
  979. // LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
  980. if (phoneme_tab[final_ph]->type != phVOWEL) {
  981. mnem = phoneme_tab[final_ph]->mnemonic;
  982. if (tr->translator_name == L('a', 'n')) {
  983. if (((mnem != 's') && (mnem != 'n')) || phoneme_tab[final_ph2]->type != phVOWEL)
  984. stressed_syllable = vowel_count - 1; // stress on last syllable
  985. } else if (tr->translator_name == L('i', 'a')) {
  986. if ((mnem != 's') || phoneme_tab[final_ph2]->type != phVOWEL)
  987. stressed_syllable = vowel_count - 1; // stress on last syllable
  988. } else {
  989. if ((mnem == 's') && (phoneme_tab[final_ph2]->type == phNASAL)) {
  990. // -ns stress remains on penultimate syllable
  991. } else if (((phoneme_tab[final_ph]->type != phNASAL) && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL))
  992. stressed_syllable = vowel_count - 1;
  993. }
  994. }
  995. }
  996. if (stressflags & S_FINAL_LONG) {
  997. // stress on last syllable if it has a long vowel, but previous syllable has a short vowel
  998. if (vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2])
  999. stressed_syllable = vowel_count - 1;
  1000. }
  1001. if ((vowel_stress[stressed_syllable] == STRESS_IS_DIMINISHED) || (vowel_stress[stressed_syllable] == STRESS_IS_UNSTRESSED)) {
  1002. // but this vowel is explicitly marked as unstressed
  1003. if (stressed_syllable > 1)
  1004. stressed_syllable--;
  1005. else
  1006. stressed_syllable++;
  1007. }
  1008. } else
  1009. stressed_syllable = 1;
  1010. // only set the stress if it's not already marked explicitly
  1011. if (vowel_stress[stressed_syllable] < 0) {
  1012. // don't stress if next and prev syllables are stressed
  1013. if ((vowel_stress[stressed_syllable-1] < STRESS_IS_PRIMARY) || (vowel_stress[stressed_syllable+1] < STRESS_IS_PRIMARY))
  1014. vowel_stress[stressed_syllable] = max_stress;
  1015. }
  1016. }
  1017. break;
  1018. case STRESSPOSN_1R:
  1019. // stress on last vowel
  1020. if (stressed_syllable == 0) {
  1021. // no explicit stress - stress the final vowel
  1022. stressed_syllable = vowel_count - 1;
  1023. while (stressed_syllable > 0) {
  1024. // find the last vowel which is not unstressed
  1025. if (vowel_stress[stressed_syllable] < STRESS_IS_DIMINISHED) {
  1026. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1027. break;
  1028. } else
  1029. stressed_syllable--;
  1030. }
  1031. max_stress = STRESS_IS_PRIMARY;
  1032. }
  1033. break;
  1034. case STRESSPOSN_3R: // stress on antipenultimate vowel
  1035. if (stressed_syllable == 0) {
  1036. stressed_syllable = vowel_count - 3;
  1037. if (stressed_syllable < 1)
  1038. stressed_syllable = 1;
  1039. if (max_stress == STRESS_IS_DIMINISHED)
  1040. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1041. max_stress = STRESS_IS_PRIMARY;
  1042. }
  1043. break;
  1044. case STRESSPOSN_SYLCOUNT:
  1045. // LANG=Russian
  1046. if (stressed_syllable == 0) {
  1047. // no explicit stress - guess the stress from the number of syllables
  1048. static const char guess_ru[16] = { 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11 };
  1049. static const char guess_ru_v[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10 }; // for final phoneme is a vowel
  1050. static const char guess_ru_t[16] = { 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 10 }; // for final phoneme is an unvoiced stop
  1051. stressed_syllable = vowel_count - 3;
  1052. if (vowel_count < 16) {
  1053. if (phoneme_tab[final_ph]->type == phVOWEL)
  1054. stressed_syllable = guess_ru_v[vowel_count];
  1055. else if (phoneme_tab[final_ph]->type == phSTOP)
  1056. stressed_syllable = guess_ru_t[vowel_count];
  1057. else
  1058. stressed_syllable = guess_ru[vowel_count];
  1059. }
  1060. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1061. max_stress = STRESS_IS_PRIMARY;
  1062. }
  1063. break;
  1064. case STRESSPOSN_1RH: // LANG=hi stress on the last heaviest syllable
  1065. if (stressed_syllable == 0) {
  1066. int wt;
  1067. int max_weight = -1;
  1068. // find the heaviest syllable, excluding the final syllable
  1069. for (ix = 1; ix < (vowel_count-1); ix++) {
  1070. if (vowel_stress[ix] < STRESS_IS_DIMINISHED) {
  1071. if ((wt = syllable_weight[ix]) >= max_weight) {
  1072. max_weight = wt;
  1073. stressed_syllable = ix;
  1074. }
  1075. }
  1076. }
  1077. if ((syllable_weight[vowel_count-1] == 2) && (max_weight < 2)) {
  1078. // the only double=heavy syllable is the final syllable, so stress this
  1079. stressed_syllable = vowel_count-1;
  1080. } else if (max_weight <= 0) {
  1081. // all syllables, exclusing the last, are light. Stress the first syllable
  1082. stressed_syllable = 1;
  1083. }
  1084. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1085. max_stress = STRESS_IS_PRIMARY;
  1086. }
  1087. break;
  1088. case STRESSPOSN_1RU : // LANG=tr, the last syllable for any vowel marked explicitly as unstressed
  1089. if (stressed_syllable == 0) {
  1090. stressed_syllable = vowel_count - 1;
  1091. for (ix = 1; ix < vowel_count; ix++) {
  1092. if (vowel_stress[ix] == STRESS_IS_UNSTRESSED) {
  1093. stressed_syllable = ix-1;
  1094. break;
  1095. }
  1096. }
  1097. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1098. max_stress = STRESS_IS_PRIMARY;
  1099. }
  1100. break;
  1101. case STRESSPOSN_ALL: // mark all as stressed
  1102. for (ix = 1; ix < vowel_count; ix++) {
  1103. if (vowel_stress[ix] < STRESS_IS_DIMINISHED)
  1104. vowel_stress[ix] = STRESS_IS_PRIMARY;
  1105. }
  1106. break;
  1107. case STRESSPOSN_GREENLANDIC: // LANG=kl (Greenlandic)
  1108. long_vowel = 0;
  1109. for (ix = 1; ix < vowel_count; ix++) {
  1110. if (vowel_stress[ix] == STRESS_IS_PRIMARY)
  1111. vowel_stress[ix] = STRESS_IS_SECONDARY; // change marked stress (consonant clusters) to secondary (except the last)
  1112. if (vowel_length[ix] > 0) {
  1113. long_vowel = ix;
  1114. vowel_stress[ix] = STRESS_IS_SECONDARY; // give secondary stress to all long vowels
  1115. }
  1116. }
  1117. // 'stressed_syllable' gives the last marked stress
  1118. if (stressed_syllable == 0) {
  1119. // no marked stress, choose the last long vowel
  1120. if (long_vowel > 0)
  1121. stressed_syllable = long_vowel;
  1122. else {
  1123. // no long vowels or consonant clusters
  1124. if (vowel_count > 5)
  1125. stressed_syllable = vowel_count - 3; // more than 4 syllables
  1126. else
  1127. stressed_syllable = vowel_count - 1;
  1128. }
  1129. }
  1130. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1131. max_stress = STRESS_IS_PRIMARY;
  1132. break;
  1133. case STRESSPOSN_1SL: // LANG=ml, 1st unless 1st vowel is short and 2nd is long
  1134. if (stressed_syllable == 0) {
  1135. stressed_syllable = 1;
  1136. if ((vowel_length[1] == 0) && (vowel_count > 2) && (vowel_length[2] > 0))
  1137. stressed_syllable = 2;
  1138. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1139. max_stress = STRESS_IS_PRIMARY;
  1140. }
  1141. break;
  1142. case STRESSPOSN_EU: // LANG=eu. If more than 2 syllables: primary stress in second syllable and secondary on last.
  1143. if ((stressed_syllable == 0) && (vowel_count > 2)) {
  1144. for (ix = 1; ix < vowel_count; ix++) {
  1145. vowel_stress[ix] = STRESS_IS_DIMINISHED;
  1146. }
  1147. stressed_syllable = 2;
  1148. if (max_stress == STRESS_IS_DIMINISHED)
  1149. vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY;
  1150. max_stress = STRESS_IS_PRIMARY;
  1151. if (vowel_count > 3) {
  1152. vowel_stress[vowel_count - 1] = STRESS_IS_SECONDARY;
  1153. }
  1154. }
  1155. break;
  1156. }
  1157. if ((stressflags & S_FINAL_VOWEL_UNSTRESSED) && ((control & 2) == 0) && (vowel_count > 2) && (max_stress_input < STRESS_IS_SECONDARY) && (vowel_stress[vowel_count - 1] == STRESS_IS_PRIMARY)) {
  1158. // Don't allow stress on a word-final vowel
  1159. // Only do this if there is no suffix phonemes to be added, and if a stress position was not given explicitly
  1160. if (phoneme_tab[final_ph]->type == phVOWEL) {
  1161. vowel_stress[vowel_count - 1] = STRESS_IS_UNSTRESSED;
  1162. vowel_stress[vowel_count - 2] = STRESS_IS_PRIMARY;
  1163. }
  1164. }
  1165. // now guess the complete stress pattern
  1166. if (max_stress < STRESS_IS_PRIMARY)
  1167. stress = STRESS_IS_PRIMARY; // no primary stress marked, use for 1st syllable
  1168. else
  1169. stress = STRESS_IS_SECONDARY;
  1170. if (unstressed_word == false) {
  1171. if ((stressflags & S_2_SYL_2) && (vowel_count == 3)) {
  1172. // Two syllable word, if one syllable has primary stress, then give the other secondary stress
  1173. if (vowel_stress[1] == STRESS_IS_PRIMARY)
  1174. vowel_stress[2] = STRESS_IS_SECONDARY;
  1175. if (vowel_stress[2] == STRESS_IS_PRIMARY)
  1176. vowel_stress[1] = STRESS_IS_SECONDARY;
  1177. }
  1178. if ((stressflags & S_INITIAL_2) && (vowel_stress[1] < STRESS_IS_DIMINISHED)) {
  1179. // If there is only one syllable before the primary stress, give it a secondary stress
  1180. if ((vowel_count > 3) && (vowel_stress[2] >= STRESS_IS_PRIMARY))
  1181. vowel_stress[1] = STRESS_IS_SECONDARY;
  1182. }
  1183. }
  1184. bool done = false;
  1185. first_primary = 0;
  1186. for (v = 1; v < vowel_count; v++) {
  1187. if (vowel_stress[v] < STRESS_IS_DIMINISHED) {
  1188. if ((stressflags & S_FINAL_NO_2) && (stress < STRESS_IS_PRIMARY) && (v == vowel_count-1)) {
  1189. // flag: don't give secondary stress to final vowel
  1190. } else if ((stressflags & 0x8000) && (done == false)) {
  1191. vowel_stress[v] = (char)stress;
  1192. done = true;
  1193. stress = STRESS_IS_SECONDARY; // use secondary stress for remaining syllables
  1194. } else if ((vowel_stress[v-1] <= STRESS_IS_UNSTRESSED) && ((vowel_stress[v+1] <= STRESS_IS_UNSTRESSED) || ((stress == STRESS_IS_PRIMARY) && (vowel_stress[v+1] <= STRESS_IS_NOT_STRESSED)))) {
  1195. // trochaic: give stress to vowel surrounded by unstressed vowels
  1196. if ((stress == STRESS_IS_SECONDARY) && (stressflags & S_NO_AUTO_2))
  1197. continue; // don't use secondary stress
  1198. // don't put secondary stress on a light syllable if the rest of the word (excluding last syllable) contains a heavy syllable
  1199. if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0)) {
  1200. bool skip = false;
  1201. for (int i = v; i < vowel_count - 1; i++) {
  1202. if (syllable_weight[i] > 0) {
  1203. skip = true;
  1204. break;
  1205. }
  1206. }
  1207. if (skip == true)
  1208. continue;
  1209. }
  1210. if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0) && (syllable_weight[v+1] > 0)) {
  1211. // don't put secondary stress on a light syllable which is followed by a heavy syllable
  1212. continue;
  1213. }
  1214. // should start with secondary stress on the first syllable, or should it count back from
  1215. // the primary stress and put secondary stress on alternate syllables?
  1216. vowel_stress[v] = (char)stress;
  1217. done = true;
  1218. stress = STRESS_IS_SECONDARY; // use secondary stress for remaining syllables
  1219. }
  1220. }
  1221. if (vowel_stress[v] >= STRESS_IS_PRIMARY) {
  1222. if (first_primary == 0)
  1223. first_primary = v;
  1224. else if (stressflags & S_FIRST_PRIMARY) {
  1225. // reduce primary stresses after the first to secondary
  1226. vowel_stress[v] = STRESS_IS_SECONDARY;
  1227. }
  1228. }
  1229. }
  1230. if ((unstressed_word) && (tonic < 0)) {
  1231. if (vowel_count <= 2)
  1232. tonic = tr->langopts.unstressed_wd1; // monosyllable - unstressed
  1233. else
  1234. tonic = tr->langopts.unstressed_wd2; // more than one syllable, used secondary stress as the main stress
  1235. }
  1236. max_stress = STRESS_IS_DIMINISHED;
  1237. max_stress_posn = 0;
  1238. for (v = 1; v < vowel_count; v++) {
  1239. if (vowel_stress[v] >= max_stress) {
  1240. max_stress = vowel_stress[v];
  1241. max_stress_posn = v;
  1242. }
  1243. }
  1244. if (tonic >= 0) {
  1245. // find position of highest stress, and replace it by 'tonic'
  1246. // don't disturb an explicitly set stress by 'unstress-at-end' flag
  1247. if ((tonic > max_stress) || (max_stress <= STRESS_IS_PRIMARY))
  1248. vowel_stress[max_stress_posn] = (char)tonic;
  1249. max_stress = tonic;
  1250. }
  1251. // produce output phoneme string
  1252. p = phonetic;
  1253. v = 1;
  1254. if (!(control & 1) && ((ph = phoneme_tab[*p]) != NULL)) {
  1255. while ((ph->type == phSTRESS) || (*p == phonEND_WORD)) {
  1256. p++;
  1257. ph = phoneme_tab[p[0]];
  1258. }
  1259. if ((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL)) {
  1260. // word starts with a vowel
  1261. if ((tr->langopts.vowel_pause & 0x20) && (vowel_stress[1] >= STRESS_IS_PRIMARY))
  1262. *output++ = phonPAUSE_NOLINK; // not to be replaced by link
  1263. else
  1264. *output++ = phonPAUSE_VSHORT; // break, but no pause
  1265. }
  1266. }
  1267. p = phonetic;
  1268. while (((phcode = *p++) != 0) && (output < max_output)) {
  1269. if ((ph = phoneme_tab[phcode]) == NULL)
  1270. continue;
  1271. if (ph->type == phPAUSE)
  1272. tr->prev_last_stress = 0;
  1273. else if (((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC)) {
  1274. // a vowel, or a consonant followed by a syllabic consonant marker
  1275. v_stress = vowel_stress[v];
  1276. tr->prev_last_stress = v_stress;
  1277. if (v_stress <= STRESS_IS_UNSTRESSED) {
  1278. if ((v > 1) && (max_stress >= 2) && (stressflags & S_FINAL_DIM) && (v == (vowel_count-1))) {
  1279. // option: mark unstressed final syllable as diminished
  1280. v_stress = STRESS_IS_DIMINISHED;
  1281. } else if ((stressflags & S_NO_DIM) || (v == 1) || (v == (vowel_count-1))) {
  1282. // first or last syllable, or option 'don't set diminished stress'
  1283. v_stress = STRESS_IS_UNSTRESSED;
  1284. } else if ((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= STRESS_IS_UNSTRESSED)) {
  1285. // penultimate syllable, followed by an unstressed final syllable
  1286. v_stress = STRESS_IS_UNSTRESSED;
  1287. } else {
  1288. // unstressed syllable within a word
  1289. if ((vowel_stress[v-1] < STRESS_IS_DIMINISHED) || ((stressflags & S_MID_DIM) == 0)) {
  1290. v_stress = STRESS_IS_DIMINISHED;
  1291. vowel_stress[v] = v_stress;
  1292. }
  1293. }
  1294. }
  1295. if ((v_stress == STRESS_IS_DIMINISHED) || (v_stress > STRESS_IS_UNSTRESSED))
  1296. *output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 1 (unstressed)
  1297. if (vowel_stress[v] > max_stress)
  1298. max_stress = vowel_stress[v];
  1299. if ((*p == phonLENGTHEN) && ((opt_length = tr->langopts.param[LOPT_IT_LENGTHEN]) & 1)) {
  1300. // remove lengthen indicator from non-stressed syllables
  1301. bool shorten = false;
  1302. if (opt_length & 0x10) {
  1303. // only allow lengthen indicator on the highest stress syllable in the word
  1304. if (v != max_stress_posn)
  1305. shorten = true;
  1306. } else if (v_stress < STRESS_IS_PRIMARY) {
  1307. // only allow lengthen indicator if stress >= STRESS_IS_PRIMARY.
  1308. shorten = true;
  1309. }
  1310. if (shorten)
  1311. p++;
  1312. }
  1313. v++;
  1314. }
  1315. if (phcode != 1)
  1316. *output++ = phcode;
  1317. }
  1318. *output++ = 0;
  1319. return;
  1320. }
  1321. void AppendPhonemes(Translator *tr, char *string, int size, const char *ph)
  1322. {
  1323. /* Add new phoneme string "ph" to "string"
  1324. Keeps count of the number of vowel phonemes in the word, and whether these
  1325. can be stressed syllables. These values can be used in translation rules
  1326. */
  1327. const char *p;
  1328. unsigned char c;
  1329. int length;
  1330. length = strlen(ph) + strlen(string);
  1331. if (length >= size)
  1332. return;
  1333. // any stressable vowel ?
  1334. bool unstress_mark = false;
  1335. p = ph;
  1336. while ((c = *p++) != 0) {
  1337. if (c >= n_phoneme_tab) continue;
  1338. if (phoneme_tab[c]->type == phSTRESS) {
  1339. if (phoneme_tab[c]->std_length < 4)
  1340. unstress_mark = true;
  1341. } else {
  1342. if (phoneme_tab[c]->type == phVOWEL) {
  1343. if (((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
  1344. (unstress_mark == false)) {
  1345. tr->word_stressed_count++;
  1346. }
  1347. unstress_mark = false;
  1348. tr->word_vowel_count++;
  1349. }
  1350. }
  1351. }
  1352. if (string != NULL)
  1353. strcat(string, ph);
  1354. }
  1355. static void MatchRule(Translator *tr, char *word[], char *word_start, int group_length, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
  1356. {
  1357. /* Checks a specified word against dictionary rules.
  1358. Returns with phoneme code string, or NULL if no match found.
  1359. word (indirect) points to current character group within the input word
  1360. This is advanced by this procedure as characters are consumed
  1361. group: the initial characters used to choose the rules group
  1362. rule: address of dictionary rule data for this character group
  1363. match_out: returns best points score
  1364. word_flags: indicates whether this is a retranslation after a suffix has been removed
  1365. */
  1366. unsigned char rb; // current instuction from rule
  1367. unsigned char letter; // current letter from input word, single byte
  1368. int letter_w; // current letter, wide character
  1369. int last_letter_w; // last letter, wide character
  1370. int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1)
  1371. char *pre_ptr;
  1372. char *post_ptr; // pointer to first character after group
  1373. char *rule_start; // start of current match template
  1374. char *p;
  1375. int ix;
  1376. int match_type; // left, right, or consume
  1377. int failed;
  1378. int unpron_ignore;
  1379. int consumed; // number of letters consumed from input
  1380. int syllable_count;
  1381. int vowel;
  1382. int letter_group;
  1383. int distance_right;
  1384. int distance_left;
  1385. int lg_pts;
  1386. int n_bytes;
  1387. int add_points;
  1388. int command;
  1389. bool check_atstart;
  1390. unsigned int *flags;
  1391. MatchRecord match;
  1392. static MatchRecord best;
  1393. int total_consumed; // letters consumed for best match
  1394. unsigned char condition_num;
  1395. char *common_phonemes; // common to a group of entries
  1396. char *group_chars;
  1397. char word_buf[N_WORD_BYTES];
  1398. group_chars = *word;
  1399. if (rule == NULL) {
  1400. match_out->points = 0;
  1401. (*word)++;
  1402. return;
  1403. }
  1404. total_consumed = 0;
  1405. common_phonemes = NULL;
  1406. best.points = 0;
  1407. best.phonemes = "";
  1408. best.end_type = 0;
  1409. best.del_fwd = NULL;
  1410. // search through dictionary rules
  1411. while (rule[0] != RULE_GROUP_END) {
  1412. unpron_ignore = word_flags & FLAG_UNPRON_TEST;
  1413. match_type = 0;
  1414. consumed = 0;
  1415. letter_w = 0;
  1416. distance_right = -6; // used to reduce points for matches further away the current letter
  1417. distance_left = -2;
  1418. check_atstart = false;
  1419. match.points = 1;
  1420. match.end_type = 0;
  1421. match.del_fwd = NULL;
  1422. pre_ptr = *word;
  1423. post_ptr = *word + group_length;
  1424. // work through next rule until end, or until no-match proved
  1425. rule_start = rule;
  1426. failed = 0;
  1427. while (!failed) {
  1428. rb = *rule++;
  1429. if (rb <= RULE_LINENUM) {
  1430. switch (rb)
  1431. {
  1432. case 0: // no phoneme string for this rule, use previous common rule
  1433. if (common_phonemes != NULL) {
  1434. match.phonemes = common_phonemes;
  1435. while (((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES)) {
  1436. if (rb == RULE_CONDITION)
  1437. match.phonemes++; // skip over condition number
  1438. if (rb == RULE_LINENUM)
  1439. match.phonemes += 2; // skip over line number
  1440. }
  1441. } else
  1442. match.phonemes = "";
  1443. rule--; // so we are still pointing at the 0
  1444. failed = 2; // matched OK
  1445. break;
  1446. case RULE_PRE_ATSTART: // pre rule with implied 'start of word'
  1447. check_atstart = true;
  1448. unpron_ignore = 0;
  1449. match_type = RULE_PRE;
  1450. break;
  1451. case RULE_PRE:
  1452. match_type = RULE_PRE;
  1453. if (word_flags & FLAG_UNPRON_TEST) {
  1454. // checking the start of the word for unpronouncable character sequences, only
  1455. // consider rules which explicitly match the start of a word
  1456. // Note: Those rules now use RULE_PRE_ATSTART
  1457. failed = 1;
  1458. }
  1459. break;
  1460. case RULE_POST:
  1461. match_type = RULE_POST;
  1462. break;
  1463. case RULE_PHONEMES:
  1464. match.phonemes = rule;
  1465. failed = 2; // matched OK
  1466. break;
  1467. case RULE_PH_COMMON:
  1468. common_phonemes = rule;
  1469. break;
  1470. case RULE_CONDITION:
  1471. // conditional rule, next byte gives condition number
  1472. condition_num = *rule++;
  1473. if (condition_num >= 32) {
  1474. // allow the rule only if the condition number is NOT set
  1475. if ((tr->dict_condition & (1L << (condition_num-32))) != 0)
  1476. failed = 1;
  1477. } else {
  1478. // allow the rule only if the condition number is set
  1479. if ((tr->dict_condition & (1L << condition_num)) == 0)
  1480. failed = 1;
  1481. }
  1482. if (!failed)
  1483. match.points++; // add one point for a matched conditional rule
  1484. break;
  1485. case RULE_LINENUM:
  1486. rule += 2;
  1487. break;
  1488. }
  1489. continue;
  1490. }
  1491. add_points = 0;
  1492. switch (match_type)
  1493. {
  1494. case 0:
  1495. // match and consume this letter
  1496. letter = *post_ptr++;
  1497. if ((letter == rb) || ((letter == (unsigned char)REPLACED_E) && (rb == 'e'))) {
  1498. if ((letter & 0xc0) != 0x80)
  1499. add_points = 21; // don't add point for non-initial UTF-8 bytes
  1500. consumed++;
  1501. } else
  1502. failed = 1;
  1503. break;
  1504. case RULE_POST:
  1505. // continue moving forwards
  1506. distance_right += 6;
  1507. if (distance_right > 18)
  1508. distance_right = 19;
  1509. last_letter_w = letter_w;
  1510. if (!post_ptr[-1]) {
  1511. // we had already reached the end of text!
  1512. // reading after that does not make sense, that cannot match
  1513. failed = 1;
  1514. break;
  1515. }
  1516. letter_xbytes = utf8_in(&letter_w, post_ptr)-1;
  1517. letter = *post_ptr++;
  1518. switch (rb)
  1519. {
  1520. case RULE_LETTERGP:
  1521. letter_group = LetterGroupNo(rule++);
  1522. if (IsLetter(tr, letter_w, letter_group)) {
  1523. lg_pts = 20;
  1524. if (letter_group == 2)
  1525. lg_pts = 19; // fewer points for C, general consonant
  1526. add_points = (lg_pts-distance_right);
  1527. post_ptr += letter_xbytes;
  1528. } else
  1529. failed = 1;
  1530. break;
  1531. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1532. letter_group = LetterGroupNo(rule++);
  1533. if ((n_bytes = IsLetterGroup(tr, post_ptr-1, letter_group, 0)) >= 0) {
  1534. add_points = (20-distance_right);
  1535. if (n_bytes >= 0) // move pointer, if group was found
  1536. post_ptr += (n_bytes-1);
  1537. } else
  1538. failed = 1;
  1539. break;
  1540. case RULE_NOTVOWEL:
  1541. if (IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL)))
  1542. failed = 1;
  1543. else {
  1544. add_points = (20-distance_right);
  1545. post_ptr += letter_xbytes;
  1546. }
  1547. break;
  1548. case RULE_DIGIT:
  1549. if (IsDigit(letter_w)) {
  1550. add_points = (20-distance_right);
  1551. post_ptr += letter_xbytes;
  1552. } else if (tr->langopts.tone_numbers) {
  1553. // also match if there is no digit
  1554. add_points = (20-distance_right);
  1555. post_ptr--;
  1556. } else
  1557. failed = 1;
  1558. break;
  1559. case RULE_NONALPHA:
  1560. if (!iswalpha(letter_w)) {
  1561. add_points = (21-distance_right);
  1562. post_ptr += letter_xbytes;
  1563. } else
  1564. failed = 1;
  1565. break;
  1566. case RULE_DOUBLE:
  1567. if (letter_w == last_letter_w) {
  1568. add_points = (21-distance_right);
  1569. post_ptr += letter_xbytes;
  1570. } else
  1571. failed = 1;
  1572. break;
  1573. case RULE_DOLLAR:
  1574. post_ptr--;
  1575. command = *rule++;
  1576. if (command == DOLLAR_UNPR)
  1577. match.end_type = SUFX_UNPRON; // $unpron
  1578. else if (command == DOLLAR_NOPREFIX) { // $noprefix
  1579. if (word_flags & FLAG_PREFIX_REMOVED)
  1580. failed = 1; // a prefix has been removed
  1581. else
  1582. add_points = 1;
  1583. } else if ((command & 0xf0) == 0x10) {
  1584. // $w_alt
  1585. if (dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1586. add_points = 23;
  1587. else
  1588. failed = 1;
  1589. } else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) {
  1590. // $list or $p_alt
  1591. // make a copy of the word up to the post-match characters
  1592. ix = *word - word_start + consumed + group_length + 1;
  1593. memcpy(word_buf, word_start-1, ix);
  1594. word_buf[ix] = ' ';
  1595. word_buf[ix+1] = 0;
  1596. LookupFlags(tr, &word_buf[1], &flags);
  1597. if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  1598. add_points = 23;
  1599. else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1600. add_points = 23;
  1601. else
  1602. failed = 1;
  1603. }
  1604. break;
  1605. case '-':
  1606. if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
  1607. add_points = (22-distance_right); // one point more than match against space
  1608. else
  1609. failed = 1;
  1610. break;
  1611. case RULE_SYLLABLE:
  1612. {
  1613. // more than specified number of vowel letters to the right
  1614. char *p = post_ptr + letter_xbytes;
  1615. int vowel_count = 0;
  1616. syllable_count = 1;
  1617. while (*rule == RULE_SYLLABLE) {
  1618. rule++;
  1619. syllable_count += 1; // number of syllables to match
  1620. }
  1621. vowel = 0;
  1622. while (letter_w != RULE_SPACE) {
  1623. if ((vowel == 0) && IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1624. // this is counting vowels which are separated by non-vowel letters
  1625. vowel_count++;
  1626. }
  1627. vowel = IsLetter(tr, letter_w, LETTERGP_VOWEL2);
  1628. p += utf8_in(&letter_w, p);
  1629. }
  1630. if (syllable_count <= vowel_count)
  1631. add_points = (18+syllable_count-distance_right);
  1632. else
  1633. failed = 1;
  1634. }
  1635. break;
  1636. case RULE_NOVOWELS:
  1637. {
  1638. char *p = post_ptr + letter_xbytes;
  1639. while (letter_w != RULE_SPACE) {
  1640. if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1641. failed = 1;
  1642. break;
  1643. }
  1644. p += utf8_in(&letter_w, p);
  1645. }
  1646. if (!failed)
  1647. add_points = (19-distance_right);
  1648. }
  1649. break;
  1650. case RULE_SKIPCHARS:
  1651. {
  1652. // '(Jxy' means 'skip characters until xy'
  1653. char *p = post_ptr - 1; // to allow empty jump (without letter between), go one back
  1654. char *p2 = p; // pointer to the previous character in the word
  1655. int rule_w; // first wide character of skip rule
  1656. utf8_in(&rule_w, rule);
  1657. int g_bytes = -1; // bytes of successfully found character group
  1658. while ((letter_w != rule_w) && (letter_w != RULE_SPACE) && (letter_w != 0) && (g_bytes == -1)) {
  1659. if (rule_w == RULE_LETTERGP2)
  1660. g_bytes = IsLetterGroup(tr, p, LetterGroupNo(rule + 1), 0);
  1661. p2 = p;
  1662. p += utf8_in(&letter_w, p);
  1663. }
  1664. if ((letter_w == rule_w) || (g_bytes >= 0))
  1665. post_ptr = p2;
  1666. }
  1667. break;
  1668. case RULE_INC_SCORE:
  1669. post_ptr--;
  1670. add_points = 20; // force an increase in points
  1671. break;
  1672. case RULE_DEC_SCORE:
  1673. post_ptr--;
  1674. add_points = -20; // force an decrease in points
  1675. break;
  1676. case RULE_DEL_FWD:
  1677. // find the next 'e' in the word and replace by 'E'
  1678. for (p = *word + group_length; p < post_ptr; p++) {
  1679. if (*p == 'e') {
  1680. match.del_fwd = p;
  1681. break;
  1682. }
  1683. }
  1684. break;
  1685. case RULE_ENDING:
  1686. {
  1687. int end_type;
  1688. // next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
  1689. end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
  1690. if ((tr->word_vowel_count == 0) && !(end_type & SUFX_P) && (tr->langopts.param[LOPT_SUFFIX] & 1))
  1691. failed = 1; // don't match a suffix rule if there are no previous syllables (needed for lang=tr).
  1692. else {
  1693. match.end_type = end_type;
  1694. rule += 3;
  1695. }
  1696. }
  1697. break;
  1698. case RULE_NO_SUFFIX:
  1699. if (word_flags & FLAG_SUFFIX_REMOVED)
  1700. failed = 1; // a suffix has been removed
  1701. else {
  1702. post_ptr--;
  1703. add_points = 1;
  1704. }
  1705. break;
  1706. default:
  1707. if (letter == rb) {
  1708. if ((letter & 0xc0) != 0x80) {
  1709. // not for non-initial UTF-8 bytes
  1710. add_points = (21-distance_right);
  1711. }
  1712. } else
  1713. failed = 1;
  1714. break;
  1715. }
  1716. break;
  1717. case RULE_PRE:
  1718. // match backwards from start of current group
  1719. distance_left += 2;
  1720. if (distance_left > 18)
  1721. distance_left = 19;
  1722. if (!*pre_ptr) {
  1723. // we had already reached the beginning of text!
  1724. // reading before this does not make sense, that cannot match
  1725. failed = 1;
  1726. break;
  1727. }
  1728. utf8_in(&last_letter_w, pre_ptr);
  1729. pre_ptr--;
  1730. letter_xbytes = utf8_in2(&letter_w, pre_ptr, 1)-1;
  1731. letter = *pre_ptr;
  1732. switch (rb)
  1733. {
  1734. case RULE_LETTERGP:
  1735. letter_group = LetterGroupNo(rule++);
  1736. if (IsLetter(tr, letter_w, letter_group)) {
  1737. lg_pts = 20;
  1738. if (letter_group == 2)
  1739. lg_pts = 19; // fewer points for C, general consonant
  1740. add_points = (lg_pts-distance_left);
  1741. pre_ptr -= letter_xbytes;
  1742. } else
  1743. failed = 1;
  1744. break;
  1745. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1746. letter_group = LetterGroupNo(rule++);
  1747. if ((n_bytes = IsLetterGroup(tr, pre_ptr, letter_group, 1)) >= 0) {
  1748. add_points = (20-distance_right);
  1749. if (n_bytes >= 0) // move pointer, if group was found
  1750. pre_ptr -= (n_bytes-1);
  1751. } else
  1752. failed = 1;
  1753. break;
  1754. case RULE_NOTVOWEL:
  1755. if (!IsLetter(tr, letter_w, 0)) {
  1756. add_points = (20-distance_left);
  1757. pre_ptr -= letter_xbytes;
  1758. } else
  1759. failed = 1;
  1760. break;
  1761. case RULE_DOUBLE:
  1762. if (letter_w == last_letter_w) {
  1763. add_points = (21-distance_left);
  1764. pre_ptr -= letter_xbytes;
  1765. } else
  1766. failed = 1;
  1767. break;
  1768. case RULE_DIGIT:
  1769. if (IsDigit(letter_w)) {
  1770. add_points = (21-distance_left);
  1771. pre_ptr -= letter_xbytes;
  1772. } else
  1773. failed = 1;
  1774. break;
  1775. case RULE_NONALPHA:
  1776. if (!iswalpha(letter_w)) {
  1777. add_points = (21-distance_right);
  1778. pre_ptr -= letter_xbytes;
  1779. } else
  1780. failed = 1;
  1781. break;
  1782. case RULE_DOLLAR:
  1783. pre_ptr++;
  1784. command = *rule++;
  1785. if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) {
  1786. // $list or $p_alt
  1787. // make a copy of the word up to the current character
  1788. ix = *word - word_start + 1;
  1789. memcpy(word_buf, word_start-1, ix);
  1790. word_buf[ix] = ' ';
  1791. word_buf[ix+1] = 0;
  1792. LookupFlags(tr, &word_buf[1], &flags);
  1793. if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  1794. add_points = 23;
  1795. else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1796. add_points = 23;
  1797. else
  1798. failed = 1;
  1799. }
  1800. break;
  1801. case RULE_SYLLABLE:
  1802. // more than specified number of vowels to the left
  1803. syllable_count = 1;
  1804. while (*rule == RULE_SYLLABLE) {
  1805. rule++;
  1806. syllable_count++; // number of syllables to match
  1807. }
  1808. if (syllable_count <= tr->word_vowel_count)
  1809. add_points = (18+syllable_count-distance_left);
  1810. else
  1811. failed = 1;
  1812. break;
  1813. case RULE_STRESSED:
  1814. pre_ptr++;
  1815. if (tr->word_stressed_count > 0)
  1816. add_points = 19;
  1817. else
  1818. failed = 1;
  1819. break;
  1820. case RULE_NOVOWELS:
  1821. {
  1822. char *p = pre_ptr - letter_xbytes;
  1823. while (letter_w != RULE_SPACE) {
  1824. if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1825. failed = 1;
  1826. break;
  1827. }
  1828. p -= utf8_in2(&letter_w, p, 1);
  1829. }
  1830. if (!failed)
  1831. add_points = 3;
  1832. }
  1833. break;
  1834. case RULE_IFVERB:
  1835. pre_ptr++;
  1836. if (tr->expect_verb)
  1837. add_points = 1;
  1838. else
  1839. failed = 1;
  1840. break;
  1841. case RULE_CAPITAL:
  1842. pre_ptr++;
  1843. if (word_flags & FLAG_FIRST_UPPER)
  1844. add_points = 1;
  1845. else
  1846. failed = 1;
  1847. break;
  1848. case '.':
  1849. // dot in pre- section, match on any dot before this point in the word
  1850. for (p = pre_ptr; *p != ' '; p--) {
  1851. if (*p == '.') {
  1852. add_points = 50;
  1853. break;
  1854. }
  1855. }
  1856. if (*p == ' ')
  1857. failed = 1;
  1858. break;
  1859. case '-':
  1860. if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
  1861. add_points = (22-distance_right); // one point more than match against space
  1862. else
  1863. failed = 1;
  1864. break;
  1865. case RULE_SKIPCHARS: {
  1866. // 'xyJ)' means 'skip characters backwards until xy'
  1867. char *p = pre_ptr + 1; // to allow empty jump (without letter between), go one forward
  1868. char *p2 = p; // pointer to previous character in word
  1869. int g_bytes = -1; // bytes of successfully found character group
  1870. while ((*p != *rule) && (*p != RULE_SPACE) && (*p != 0) && (g_bytes == -1)) {
  1871. p2 = p;
  1872. p--;
  1873. if (*rule == RULE_LETTERGP2)
  1874. g_bytes = IsLetterGroup(tr, p2, LetterGroupNo(rule + 1), 1);
  1875. }
  1876. // if succeed, set pre_ptr to next character after 'xy' and remaining
  1877. // 'xy' part is checked as usual in following cycles of PRE rule characters
  1878. if (*p == *rule)
  1879. pre_ptr = p2;
  1880. if (g_bytes >= 0)
  1881. pre_ptr = p2 + 1;
  1882. }
  1883. break;
  1884. default:
  1885. if (letter == rb) {
  1886. if (letter == RULE_SPACE)
  1887. add_points = 4;
  1888. else if ((letter & 0xc0) != 0x80) {
  1889. // not for non-initial UTF-8 bytes
  1890. add_points = (21-distance_left);
  1891. }
  1892. } else
  1893. failed = 1;
  1894. break;
  1895. }
  1896. break;
  1897. }
  1898. if (failed == 0)
  1899. match.points += add_points;
  1900. }
  1901. if ((failed == 2) && (unpron_ignore == 0)) {
  1902. // do we also need to check for 'start of word' ?
  1903. if ((check_atstart == false) || (pre_ptr[-1] == ' ')) {
  1904. if (check_atstart)
  1905. match.points += 4;
  1906. // matched OK, is this better than the last best match ?
  1907. if (match.points >= best.points) {
  1908. memcpy(&best, &match, sizeof(match));
  1909. total_consumed = consumed;
  1910. }
  1911. if ((option_phonemes & espeakPHONEMES_TRACE) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0)) {
  1912. // show each rule that matches, and it's points score
  1913. int pts;
  1914. char decoded_phonemes[80];
  1915. pts = match.points;
  1916. if (group_length > 1)
  1917. pts += 35; // to account for an extra letter matching
  1918. DecodePhonemes(match.phonemes, decoded_phonemes);
  1919. fprintf(f_trans, "%3d\t%s [%s]\n", pts, DecodeRule(group_chars, group_length, rule_start, word_flags), decoded_phonemes);
  1920. }
  1921. }
  1922. }
  1923. // skip phoneme string to reach start of next template
  1924. while (*rule++ != 0) ;
  1925. }
  1926. // advance input data pointer
  1927. total_consumed += group_length;
  1928. if (total_consumed == 0)
  1929. total_consumed = 1; // always advance over 1st letter
  1930. *word += total_consumed;
  1931. if (best.points == 0)
  1932. best.phonemes = "";
  1933. memcpy(match_out, &best, sizeof(MatchRecord));
  1934. }
  1935. int TranslateRules(Translator *tr, char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, unsigned int *dict_flags)
  1936. {
  1937. /* Translate a word bounded by space characters
  1938. Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */
  1939. unsigned char c, c2;
  1940. unsigned int c12;
  1941. int wc = 0;
  1942. int wc_bytes;
  1943. char *p2; // copy of p for use in double letter chain match
  1944. int found;
  1945. int g; // group chain number
  1946. int g1; // first group for this letter
  1947. int n;
  1948. int letter;
  1949. int any_alpha = 0;
  1950. int ix;
  1951. unsigned int digit_count = 0;
  1952. char *p;
  1953. ALPHABET *alphabet;
  1954. int dict_flags0 = 0;
  1955. MatchRecord match1;
  1956. MatchRecord match2;
  1957. char ph_buf[N_PHONEME_BYTES];
  1958. char word_copy[N_WORD_BYTES];
  1959. static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };
  1960. if (tr->data_dictrules == NULL)
  1961. return 0;
  1962. if (dict_flags != NULL)
  1963. dict_flags0 = dict_flags[0];
  1964. for (ix = 0; ix < (N_WORD_BYTES-1);) {
  1965. c = p_start[ix];
  1966. word_copy[ix++] = c;
  1967. if (c == 0)
  1968. break;
  1969. }
  1970. word_copy[ix] = 0;
  1971. if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0)) {
  1972. char wordbuf[120];
  1973. unsigned int ix;
  1974. for (ix = 0; ((c = p_start[ix]) != ' ') && (c != 0) && (ix < (sizeof(wordbuf)-1)); ix++)
  1975. wordbuf[ix] = c;
  1976. wordbuf[ix] = 0;
  1977. if (word_flags & FLAG_UNPRON_TEST)
  1978. fprintf(f_trans, "Unpronouncable? '%s'\n", wordbuf);
  1979. else
  1980. fprintf(f_trans, "Translate '%s'\n", wordbuf);
  1981. }
  1982. p = p_start;
  1983. tr->word_vowel_count = 0;
  1984. tr->word_stressed_count = 0;
  1985. if (end_phonemes != NULL)
  1986. end_phonemes[0] = 0;
  1987. while (((c = *p) != ' ') && (c != 0)) {
  1988. wc_bytes = utf8_in(&wc, p);
  1989. if (IsAlpha(wc))
  1990. any_alpha++;
  1991. n = tr->groups2_count[c];
  1992. if (IsDigit(wc) && ((tr->langopts.tone_numbers == 0) || !any_alpha)) {
  1993. // lookup the number in *_list not *_rules
  1994. char string[8];
  1995. char buf[40];
  1996. string[0] = '_';
  1997. memcpy(&string[1], p, wc_bytes);
  1998. string[1+wc_bytes] = 0;
  1999. Lookup(tr, string, buf);
  2000. if (++digit_count >= 2) {
  2001. strcat(buf, str_pause);
  2002. digit_count = 0;
  2003. }
  2004. AppendPhonemes(tr, phonemes, ph_size, buf);
  2005. p += wc_bytes;
  2006. continue;
  2007. } else {
  2008. digit_count = 0;
  2009. found = 0;
  2010. if (((ix = wc - tr->letter_bits_offset) >= 0) && (ix < 128)) {
  2011. if (tr->groups3[ix] != NULL) {
  2012. MatchRule(tr, &p, p_start, wc_bytes, tr->groups3[ix], &match1, word_flags, dict_flags0);
  2013. found = 1;
  2014. }
  2015. }
  2016. if (!found && (n > 0)) {
  2017. // there are some 2 byte chains for this initial letter
  2018. c2 = p[1];
  2019. c12 = c + (c2 << 8); // 2 characters
  2020. g1 = tr->groups2_start[c];
  2021. for (g = g1; g < (g1+n); g++) {
  2022. if (tr->groups2_name[g] == c12) {
  2023. found = 1;
  2024. p2 = p;
  2025. MatchRule(tr, &p2, p_start, 2, tr->groups2[g], &match2, word_flags, dict_flags0);
  2026. if (match2.points > 0)
  2027. match2.points += 35; // to acount for 2 letters matching
  2028. // now see whether single letter chain gives a better match ?
  2029. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  2030. if (match2.points >= match1.points) {
  2031. // use match from the 2-letter group
  2032. memcpy(&match1, &match2, sizeof(MatchRecord));
  2033. p = p2;
  2034. }
  2035. }
  2036. }
  2037. }
  2038. if (!found) {
  2039. // alphabetic, single letter chain
  2040. if (tr->groups1[c] != NULL)
  2041. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  2042. else {
  2043. // no group for this letter, use default group
  2044. MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0);
  2045. if ((match1.points == 0) && ((option_sayas & 0x10) == 0)) {
  2046. n = utf8_in(&letter, p-1)-1;
  2047. if (tr->letter_bits_offset > 0) {
  2048. // not a Latin alphabet, switch to the default Latin alphabet language
  2049. if ((letter <= 0x241) && iswalpha(letter)) {
  2050. sprintf(phonemes, "%cen", phonSWITCH);
  2051. return 0;
  2052. }
  2053. }
  2054. // is it a bracket ?
  2055. if (letter == 0xe000+'(') {
  2056. if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE_ANNOUNCED])
  2057. pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE_ANNOUNCED]; // a bracket, already spoken by AnnouncePunctuation()
  2058. }
  2059. if (IsBracket(letter)) {
  2060. if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE])
  2061. pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE];
  2062. }
  2063. // no match, try removing the accent and re-translating the word
  2064. if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0)) {
  2065. // within range of the remove_accent table
  2066. if ((p[-2] != ' ') || (p[n] != ' ')) {
  2067. // not the only letter in the word
  2068. p2 = p-1;
  2069. p[-1] = ix;
  2070. while ((p[0] = p[n]) != ' ') p++;
  2071. while (n-- > 0) *p++ = ' '; // replacement character must be no longer than original
  2072. if (tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list, letter) > 0)) {
  2073. // vowel with dieresis, replace and continue from this point
  2074. p = p2;
  2075. continue;
  2076. }
  2077. phonemes[0] = 0; // delete any phonemes which have been produced so far
  2078. p = p_start;
  2079. tr->word_vowel_count = 0;
  2080. tr->word_stressed_count = 0;
  2081. continue; // start again at the beginning of the word
  2082. }
  2083. }
  2084. if (((alphabet = AlphabetFromChar(letter)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) {
  2085. if (tr->langopts.alt_alphabet == alphabet->offset) {
  2086. sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(tr->langopts.alt_alphabet_lang));
  2087. return 0;
  2088. }
  2089. if (alphabet->flags & AL_WORDS) {
  2090. // switch to the nominated language for this alphabet
  2091. sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(alphabet->language));
  2092. return 0;
  2093. }
  2094. }
  2095. }
  2096. }
  2097. if (match1.points == 0) {
  2098. if ((wc >= 0x300) && (wc <= 0x36f)) {
  2099. // combining accent inside a word, ignore
  2100. } else if (IsAlpha(wc)) {
  2101. if ((any_alpha > 1) || (p[wc_bytes-1] > ' ')) {
  2102. // an unrecognised character in a word, abort and then spell the word
  2103. phonemes[0] = 0;
  2104. if (dict_flags != NULL)
  2105. dict_flags[0] |= FLAG_SPELLWORD;
  2106. break;
  2107. }
  2108. } else {
  2109. LookupLetter(tr, wc, -1, ph_buf, 0);
  2110. if (ph_buf[0]) {
  2111. match1.phonemes = ph_buf;
  2112. match1.points = 1;
  2113. }
  2114. }
  2115. p += (wc_bytes-1);
  2116. } else
  2117. tr->phonemes_repeat_count = 0;
  2118. }
  2119. }
  2120. if (match1.phonemes == NULL)
  2121. match1.phonemes = "";
  2122. if (match1.points > 0) {
  2123. if (word_flags & FLAG_UNPRON_TEST)
  2124. return match1.end_type | 1;
  2125. if ((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR) == 0)) {
  2126. // an instruction to switch language, return immediately so we can re-translate
  2127. strcpy(phonemes, match1.phonemes);
  2128. return 0;
  2129. }
  2130. if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0))
  2131. fprintf(f_trans, "\n");
  2132. match1.end_type &= ~SUFX_UNPRON;
  2133. if ((match1.end_type != 0) && (end_phonemes != NULL)) {
  2134. // a standard ending has been found, re-translate the word without it
  2135. if ((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX)) {
  2136. // ignore the match on a prefix
  2137. } else {
  2138. if ((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0)) {
  2139. // no prefix length specified
  2140. match1.end_type |= p - p_start;
  2141. }
  2142. strcpy(end_phonemes, match1.phonemes);
  2143. memcpy(p_start, word_copy, strlen(word_copy));
  2144. return match1.end_type;
  2145. }
  2146. }
  2147. if (match1.del_fwd != NULL)
  2148. *match1.del_fwd = REPLACED_E;
  2149. AppendPhonemes(tr, phonemes, ph_size, match1.phonemes);
  2150. }
  2151. }
  2152. memcpy(p_start, word_copy, strlen(word_copy));
  2153. return 0;
  2154. }
  2155. void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags)
  2156. {
  2157. // apply after the translation is complete
  2158. int ix;
  2159. int len;
  2160. char *p;
  2161. len = strlen(phonemes);
  2162. if (tr->langopts.param[LOPT_ALT] & 2) {
  2163. for (ix = 0; ix < (len-1); ix++) {
  2164. if (phonemes[ix] == phonSTRESS_P) {
  2165. p = &phonemes[ix+1];
  2166. if ((dict_flags & FLAG_ALT2_TRANS) != 0) {
  2167. if (*p == PhonemeCode('E'))
  2168. *p = PhonemeCode('e');
  2169. if (*p == PhonemeCode('O'))
  2170. *p = PhonemeCode('o');
  2171. } else {
  2172. if (*p == PhonemeCode('e'))
  2173. *p = PhonemeCode('E');
  2174. if (*p == PhonemeCode('o'))
  2175. *p = PhonemeCode('O');
  2176. }
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. int TransposeAlphabet(Translator *tr, char *text)
  2183. {
  2184. // transpose cyrillic alphabet (for example) into ascii (single byte) character codes
  2185. // return: number of bytes, bit 6: 1=used compression
  2186. int c;
  2187. int c2;
  2188. int ix;
  2189. int offset;
  2190. int min;
  2191. int max;
  2192. const char *map;
  2193. char *p = text;
  2194. char *p2;
  2195. bool all_alpha = true;
  2196. int bits;
  2197. int acc;
  2198. int pairs_start;
  2199. const short *pairs_list;
  2200. int bufix;
  2201. char buf[N_WORD_BYTES+1];
  2202. offset = tr->transpose_min - 1;
  2203. min = tr->transpose_min;
  2204. max = tr->transpose_max;
  2205. map = tr->transpose_map;
  2206. pairs_start = max - min + 2;
  2207. bufix = 0;
  2208. do {
  2209. p += utf8_in(&c, p);
  2210. if (c != 0) {
  2211. if ((c >= min) && (c <= max)) {
  2212. if (map == NULL)
  2213. buf[bufix++] = c - offset;
  2214. else {
  2215. // get the code from the transpose map
  2216. if (map[c - min] > 0)
  2217. buf[bufix++] = map[c - min];
  2218. else {
  2219. all_alpha = false;
  2220. break;
  2221. }
  2222. }
  2223. } else {
  2224. all_alpha = false;
  2225. break;
  2226. }
  2227. }
  2228. } while ((c != 0) && (bufix < N_WORD_BYTES));
  2229. buf[bufix] = 0;
  2230. if (all_alpha) {
  2231. // compress to 6 bits per character
  2232. acc = 0;
  2233. bits = 0;
  2234. p = buf;
  2235. p2 = buf;
  2236. while ((c = *p++) != 0) {
  2237. if ((pairs_list = tr->frequent_pairs) != NULL) {
  2238. c2 = c + (*p << 8);
  2239. for (ix = 0; c2 >= pairs_list[ix]; ix++) {
  2240. if (c2 == pairs_list[ix]) {
  2241. // found an encoding for a 2-character pair
  2242. c = ix + pairs_start; // 2-character codes start after the single letter codes
  2243. p++;
  2244. break;
  2245. }
  2246. }
  2247. }
  2248. acc = (acc << 6) + (c & 0x3f);
  2249. bits += 6;
  2250. if (bits >= 8) {
  2251. bits -= 8;
  2252. *p2++ = (acc >> bits);
  2253. }
  2254. }
  2255. if (bits > 0)
  2256. *p2++ = (acc << (8-bits));
  2257. *p2 = 0;
  2258. ix = p2 - buf;
  2259. memcpy(text, buf, ix);
  2260. return ix | 0x40; // bit 6 indicates compressed characters
  2261. }
  2262. return strlen(text);
  2263. }
  2264. /* Find an entry in the word_dict file for a specified word.
  2265. Returns NULL if no match, else returns 'word_end'
  2266. word zero terminated word to match
  2267. word2 pointer to next word(s) in the input text (terminated by space)
  2268. flags: returns dictionary flags which are associated with a matched word
  2269. end_flags: indicates whether this is a retranslation after removing a suffix
  2270. */
  2271. static const char *LookupDict2(Translator *tr, const char *word, const char *word2,
  2272. char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2273. {
  2274. char *p;
  2275. char *next;
  2276. int hash;
  2277. int phoneme_len;
  2278. int wlen;
  2279. unsigned char flag;
  2280. unsigned int dictionary_flags;
  2281. unsigned int dictionary_flags2;
  2282. int condition_failed = 0;
  2283. int n_chars;
  2284. int no_phonemes;
  2285. int skipwords;
  2286. int ix;
  2287. int c;
  2288. const char *word_end;
  2289. const char *word1;
  2290. int wflags = 0;
  2291. int lookup_symbol;
  2292. char word_buf[N_WORD_BYTES+1];
  2293. char dict_flags_buf[80];
  2294. if (wtab != NULL)
  2295. wflags = wtab->flags;
  2296. lookup_symbol = flags[1] & FLAG_LOOKUP_SYMBOL;
  2297. word1 = word;
  2298. if (tr->transpose_min > 0) {
  2299. strncpy0(word_buf, word, N_WORD_BYTES);
  2300. wlen = TransposeAlphabet(tr, word_buf); // bit 6 indicates compressed characters
  2301. word = word_buf;
  2302. } else
  2303. wlen = strlen(word);
  2304. hash = HashDictionary(word);
  2305. p = tr->dict_hashtab[hash];
  2306. if (p == NULL) {
  2307. if (flags != NULL)
  2308. *flags = 0;
  2309. return 0;
  2310. }
  2311. // Find the first entry in the list for this hash value which matches.
  2312. // This corresponds to the last matching entry in the *_list file.
  2313. while (*p != 0) {
  2314. next = p + (p[0] & 0xff);
  2315. if (((p[1] & 0x7f) != wlen) || (memcmp(word, &p[2], wlen & 0x3f) != 0)) {
  2316. // bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
  2317. p = next;
  2318. continue;
  2319. }
  2320. // found matching entry. Decode the phonetic string
  2321. word_end = word2;
  2322. dictionary_flags = 0;
  2323. dictionary_flags2 = 0;
  2324. no_phonemes = p[1] & 0x80;
  2325. p += ((p[1] & 0x3f) + 2);
  2326. if (no_phonemes) {
  2327. phonetic[0] = 0;
  2328. phoneme_len = 0;
  2329. } else {
  2330. phoneme_len = strlen(p);
  2331. assert(phoneme_len < N_PHONEME_BYTES);
  2332. strcpy(phonetic, p);
  2333. p += (phoneme_len + 1);
  2334. }
  2335. while (p < next) {
  2336. // examine the flags which follow the phoneme string
  2337. flag = *p++;
  2338. if (flag >= 100) {
  2339. // conditional rule
  2340. if (flag >= 132) {
  2341. // fail if this condition is set
  2342. if ((tr->dict_condition & (1 << (flag-132))) != 0)
  2343. condition_failed = 1;
  2344. } else {
  2345. // allow only if this condition is set
  2346. if ((tr->dict_condition & (1 << (flag-100))) == 0)
  2347. condition_failed = 1;
  2348. }
  2349. } else if (flag > 80) {
  2350. // flags 81 to 90 match more than one word
  2351. // This comes after the other flags
  2352. n_chars = next - p;
  2353. skipwords = flag - 80;
  2354. // don't use the contraction if any of the words are emphasized
  2355. // or has an embedded command, such as MARK
  2356. if (wtab != NULL) {
  2357. for (ix = 0; ix <= skipwords && wtab[ix].length; ix++) {
  2358. if (wtab[ix].flags & FLAG_EMPHASIZED2)
  2359. condition_failed = 1;
  2360. }
  2361. }
  2362. if (strncmp(word2, p, n_chars) != 0)
  2363. condition_failed = 1;
  2364. if (condition_failed) {
  2365. p = next;
  2366. break;
  2367. }
  2368. dictionary_flags |= FLAG_SKIPWORDS;
  2369. dictionary_skipwords = skipwords;
  2370. p = next;
  2371. word_end = word2 + n_chars;
  2372. } else if (flag > 64) {
  2373. // stressed syllable information, put in bits 0-3
  2374. dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
  2375. if ((flag & 0xc) == 0xc)
  2376. dictionary_flags |= FLAG_STRESS_END;
  2377. } else if (flag >= 32)
  2378. dictionary_flags2 |= (1L << (flag-32));
  2379. else
  2380. dictionary_flags |= (1L << flag);
  2381. }
  2382. if (condition_failed) {
  2383. condition_failed = 0;
  2384. continue;
  2385. }
  2386. if ((end_flags & FLAG_SUFX) == 0) {
  2387. // no suffix has been removed
  2388. if (dictionary_flags2 & FLAG_STEM)
  2389. continue; // this word must have a suffix
  2390. }
  2391. if ((end_flags & SUFX_P) && (dictionary_flags2 & (FLAG_ONLY | FLAG_ONLY_S)))
  2392. continue; // $only or $onlys, don't match if a prefix has been removed
  2393. if (end_flags & FLAG_SUFX) {
  2394. // a suffix was removed from the word
  2395. if (dictionary_flags2 & FLAG_ONLY)
  2396. continue; // no match if any suffix
  2397. if ((dictionary_flags2 & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S) == 0)) {
  2398. // only a 's' suffix allowed, but the suffix wasn't 's'
  2399. continue;
  2400. }
  2401. }
  2402. if (dictionary_flags2 & FLAG_CAPITAL) {
  2403. if (!(wflags & FLAG_FIRST_UPPER))
  2404. continue;
  2405. }
  2406. if (dictionary_flags2 & FLAG_ALLCAPS) {
  2407. if (!(wflags & FLAG_ALL_UPPER))
  2408. continue;
  2409. }
  2410. if (dictionary_flags & FLAG_NEEDS_DOT) {
  2411. if (!(wflags & FLAG_HAS_DOT))
  2412. continue;
  2413. }
  2414. if ((dictionary_flags2 & FLAG_ATEND) && (word_end < translator->clause_end) && (lookup_symbol == 0)) {
  2415. // only use this pronunciation if it's the last word of the clause, or called from Lookup()
  2416. continue;
  2417. }
  2418. if ((dictionary_flags2 & FLAG_ATSTART) && !(wflags & FLAG_FIRST_WORD)) {
  2419. // only use this pronunciation if it's the first word of a clause
  2420. continue;
  2421. }
  2422. if ((dictionary_flags2 & FLAG_SENTENCE) && !(translator->clause_terminator & CLAUSE_TYPE_SENTENCE)) {
  2423. // only if this clause is a sentence , i.e. terminator is {. ? !} not {, : :}
  2424. continue;
  2425. }
  2426. if (dictionary_flags2 & FLAG_VERB) {
  2427. // this is a verb-form pronunciation
  2428. if (tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S))) {
  2429. // OK, we are expecting a verb
  2430. if ((tr->translator_name == L('e', 'n')) && (tr->prev_dict_flags[0] & FLAG_ALT7_TRANS) && (end_flags & FLAG_SUFX_S)) {
  2431. // lang=en, don't use verb form after 'to' if the word has 's' suffix
  2432. continue;
  2433. }
  2434. } else {
  2435. // don't use the 'verb' pronunciation unless we are expecting a verb
  2436. continue;
  2437. }
  2438. }
  2439. if (dictionary_flags2 & FLAG_PAST) {
  2440. if (!tr->expect_past) {
  2441. // don't use the 'past' pronunciation unless we are expecting past tense
  2442. continue;
  2443. }
  2444. }
  2445. if (dictionary_flags2 & FLAG_NOUN) {
  2446. if ((!tr->expect_noun) || (end_flags & SUFX_V)) {
  2447. // don't use the 'noun' pronunciation unless we are expecting a noun
  2448. continue;
  2449. }
  2450. }
  2451. if (dictionary_flags2 & FLAG_NATIVE) {
  2452. if (tr != translator)
  2453. continue; // don't use if we've switched translators
  2454. }
  2455. if (dictionary_flags & FLAG_ALT2_TRANS) {
  2456. // language specific
  2457. if ((tr->translator_name == L('h', 'u')) && !(tr->prev_dict_flags[0] & FLAG_ALT_TRANS))
  2458. continue;
  2459. }
  2460. if (flags != NULL) {
  2461. flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES;
  2462. flags[1] = dictionary_flags2;
  2463. }
  2464. if (phoneme_len == 0) {
  2465. if (option_phonemes & espeakPHONEMES_TRACE) {
  2466. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2467. fprintf(f_trans, "Flags: %s %s\n", word1, dict_flags_buf);
  2468. }
  2469. return 0; // no phoneme translation found here, only flags. So use rules
  2470. }
  2471. if (flags != NULL)
  2472. flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary
  2473. if (option_phonemes & espeakPHONEMES_TRACE) {
  2474. char ph_decoded[N_WORD_PHONEMES];
  2475. bool textmode;
  2476. DecodePhonemes(phonetic, ph_decoded);
  2477. if ((dictionary_flags & FLAG_TEXTMODE) == 0)
  2478. textmode = false;
  2479. else
  2480. textmode = true;
  2481. if (textmode == translator->langopts.textmode) {
  2482. // only show this line if the word translates to phonemes, not replacement text
  2483. if ((dictionary_flags & FLAG_SKIPWORDS) && (wtab != NULL)) {
  2484. // matched more than one word
  2485. // (check for wtab prevents showing RULE_SPELLING byte when speaking individual letters)
  2486. memcpy(word_buf, word2, word_end-word2);
  2487. word_buf[word_end-word2-1] = 0;
  2488. fprintf(f_trans, "Found: '%s %s\n", word1, word_buf);
  2489. } else
  2490. fprintf(f_trans, "Found: '%s", word1);
  2491. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2492. fprintf(f_trans, "' [%s] %s\n", ph_decoded, dict_flags_buf);
  2493. }
  2494. }
  2495. ix = utf8_in(&c, word);
  2496. if (flags != NULL && (word[ix] == 0) && !IsAlpha(c))
  2497. flags[0] |= FLAG_MAX3;
  2498. return word_end;
  2499. }
  2500. return 0;
  2501. }
  2502. /* Lookup a specified word in the word dictionary.
  2503. Returns phonetic data in 'phonetic' and bits in 'flags'
  2504. end_flags: indicates if a suffix has been removed
  2505. */
  2506. int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2507. {
  2508. int length;
  2509. const char *found;
  2510. const char *word1;
  2511. const char *word2;
  2512. unsigned char c;
  2513. int nbytes;
  2514. int len;
  2515. char word[N_WORD_BYTES];
  2516. static char word_replacement[N_WORD_BYTES];
  2517. MAKE_MEM_UNDEFINED(&word_replacement, sizeof(word_replacement));
  2518. length = 0;
  2519. word2 = word1 = *wordptr;
  2520. while ((word2[nbytes = utf8_nbytes(word2)] == ' ') && (word2[nbytes+1] == '.')) {
  2521. // look for an abbreviation of the form a.b.c
  2522. // try removing the spaces between the dots and looking for a match
  2523. memcpy(&word[length], word2, nbytes);
  2524. length += nbytes;
  2525. word[length++] = '.';
  2526. word2 += nbytes+3;
  2527. }
  2528. if (length > 0) {
  2529. // found an abbreviation containing dots
  2530. nbytes = 0;
  2531. while (((c = word2[nbytes]) != 0) && (c != ' '))
  2532. nbytes++;
  2533. memcpy(&word[length], word2, nbytes);
  2534. word[length+nbytes] = 0;
  2535. found = LookupDict2(tr, word, word2, ph_out, flags, end_flags, wtab);
  2536. if (found) {
  2537. // set the skip words flag
  2538. flags[0] |= FLAG_SKIPWORDS;
  2539. dictionary_skipwords = length;
  2540. return 1;
  2541. }
  2542. }
  2543. for (length = 0; length < (N_WORD_BYTES-1); length++) {
  2544. if (((c = *word1++) == 0) || (c == ' '))
  2545. break;
  2546. if ((c == '.') && (length > 0) && (IsDigit09(word[length-1])))
  2547. break; // needed for lang=hu, eg. "december 2.-ig"
  2548. word[length] = c;
  2549. }
  2550. word[length] = 0;
  2551. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2552. if (flags[0] & FLAG_MAX3) {
  2553. if (strcmp(ph_out, tr->phonemes_repeat) == 0) {
  2554. tr->phonemes_repeat_count++;
  2555. if (tr->phonemes_repeat_count > 3)
  2556. ph_out[0] = 0;
  2557. } else {
  2558. strncpy0(tr->phonemes_repeat, ph_out, sizeof(tr->phonemes_repeat));
  2559. tr->phonemes_repeat_count = 1;
  2560. }
  2561. } else
  2562. tr->phonemes_repeat_count = 0;
  2563. if ((found == 0) && (flags[1] & FLAG_ACCENT)) {
  2564. int letter;
  2565. word2 = word;
  2566. if (*word2 == '_') word2++;
  2567. len = utf8_in(&letter, word2);
  2568. LookupAccentedLetter(tr, letter, ph_out);
  2569. found = word2 + len;
  2570. }
  2571. if (found == 0 && length >= 2) {
  2572. ph_out[0] = 0;
  2573. // try modifications to find a recognised word
  2574. if ((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e')) {
  2575. // try removing an 'e' which has been added by RemoveEnding
  2576. word[length-1] = 0;
  2577. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2578. } else if ((end_flags & SUFX_D) && (word[length-1] == word[length-2])) {
  2579. // try removing a double letter
  2580. word[length-1] = 0;
  2581. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2582. }
  2583. }
  2584. if (found) {
  2585. // if textmode is the default, then words which have phonemes are marked.
  2586. if (tr->langopts.textmode)
  2587. *flags ^= FLAG_TEXTMODE;
  2588. if (*flags & FLAG_TEXTMODE) {
  2589. // the word translates to replacement text, not to phonemes
  2590. if (end_flags & FLAG_ALLOW_TEXTMODE) {
  2591. // only use replacement text if this is the original word, not if a prefix or suffix has been removed
  2592. word_replacement[0] = 0;
  2593. word_replacement[1] = ' ';
  2594. sprintf(&word_replacement[2], "%s ", ph_out); // replacement word, preceded by zerochar and space
  2595. word1 = *wordptr;
  2596. *wordptr = &word_replacement[2];
  2597. if (option_phonemes & espeakPHONEMES_TRACE) {
  2598. len = found - word1;
  2599. memcpy(word, word1, len); // include multiple matching words
  2600. word[len] = 0;
  2601. fprintf(f_trans, "Replace: %s %s\n", word, *wordptr);
  2602. }
  2603. }
  2604. ph_out[0] = 0;
  2605. return 0;
  2606. }
  2607. return 1;
  2608. }
  2609. ph_out[0] = 0;
  2610. return 0;
  2611. }
  2612. extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  2613. int Lookup(Translator *tr, const char *word, char *ph_out)
  2614. {
  2615. // Look up in *_list, returns dictionary flags[0] and phonemes
  2616. int flags0;
  2617. unsigned int flags[2];
  2618. int say_as;
  2619. char *word1 = (char *)word;
  2620. char text[80];
  2621. flags[0] = 0;
  2622. flags[1] = FLAG_LOOKUP_SYMBOL;
  2623. if ((flags0 = LookupDictList(tr, &word1, ph_out, flags, FLAG_ALLOW_TEXTMODE, NULL)) != 0)
  2624. flags0 = flags[0];
  2625. if (flags[0] & FLAG_TEXTMODE) {
  2626. say_as = option_sayas;
  2627. option_sayas = 0; // don't speak replacement word as letter names
  2628. // NOTE: TranslateRoman checks text[-2] and IsLetterGroup looks
  2629. // for a heading \0, so pad the start of text to prevent
  2630. // it reading data on the stack.
  2631. text[0] = 0;
  2632. text[1] = ' ';
  2633. text[2] = ' ';
  2634. strncpy0(text+3, word1, sizeof(text)-3);
  2635. flags0 = TranslateWord(tr, text+3, NULL, NULL);
  2636. strcpy(ph_out, word_phonemes);
  2637. option_sayas = say_as;
  2638. }
  2639. return flags0;
  2640. }
  2641. int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out)
  2642. {
  2643. char buf[100];
  2644. static unsigned int flags[2];
  2645. char *word1 = (char *)word;
  2646. flags[0] = flags[1] = 0;
  2647. LookupDictList(tr, &word1, buf, flags, 0, NULL);
  2648. *flags_out = flags;
  2649. return flags[0];
  2650. }
  2651. int RemoveEnding(Translator *tr, char *word, int end_type, char *word_copy)
  2652. {
  2653. /* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
  2654. end_type: bits 0-6 number of letters
  2655. bits 8-14 suffix flags
  2656. word_copy: make a copy of the original word
  2657. This routine is language specific. In English it deals with reversing y->i and e-dropping
  2658. that were done when the suffix was added to the original word.
  2659. */
  2660. int i;
  2661. char *word_end;
  2662. int len_ending;
  2663. int end_flags;
  2664. const char *p;
  2665. int len;
  2666. char ending[50] = {0};
  2667. // these lists are language specific, but are only relevant if the 'e' suffix flag is used
  2668. static const char * const add_e_exceptions[] = {
  2669. "ion", NULL
  2670. };
  2671. static const char * const add_e_additions[] = {
  2672. "c", "rs", "ir", "ur", "ath", "ns", "u",
  2673. "spong", // sponge
  2674. "rang", // strange
  2675. "larg", // large
  2676. NULL
  2677. };
  2678. for (word_end = word; *word_end != ' '; word_end++) {
  2679. // replace discarded 'e's
  2680. if (*word_end == REPLACED_E)
  2681. *word_end = 'e';
  2682. }
  2683. i = word_end - word;
  2684. if (word_copy != NULL) {
  2685. memcpy(word_copy, word, i);
  2686. word_copy[i] = 0;
  2687. }
  2688. // look for multibyte characters to increase the number of bytes to remove
  2689. for (len_ending = i = (end_type & 0x3f); i > 0; i--) { // num.of characters of the suffix
  2690. word_end--;
  2691. while (word_end >= word && (*word_end & 0xc0) == 0x80) {
  2692. word_end--; // for multibyte characters
  2693. len_ending++;
  2694. }
  2695. }
  2696. // remove bytes from the end of the word and replace them by spaces
  2697. for (i = 0; (i < len_ending) && (i < (int)sizeof(ending)-1); i++) {
  2698. ending[i] = word_end[i];
  2699. word_end[i] = ' ';
  2700. }
  2701. ending[i] = 0;
  2702. word_end--; // now pointing at last character of stem
  2703. end_flags = (end_type & 0xfff0) | FLAG_SUFX;
  2704. /* add an 'e' to the stem if appropriate,
  2705. if stem ends in vowel+consonant
  2706. or stem ends in 'c' (add 'e' to soften it) */
  2707. if (end_type & SUFX_I) {
  2708. if (word_end[0] == 'i')
  2709. word_end[0] = 'y';
  2710. }
  2711. if (end_type & SUFX_E) {
  2712. if (tr->translator_name == L('n', 'l')) {
  2713. if (((word_end[0] & 0x80) == 0) && ((word_end[-1] & 0x80) == 0) && IsVowel(tr, word_end[-1]) && IsLetter(tr, word_end[0], LETTERGP_C) && !IsVowel(tr, word_end[-2])) {
  2714. // double the vowel before the (ascii) final consonant
  2715. word_end[1] = word_end[0];
  2716. word_end[0] = word_end[-1];
  2717. word_end[2] = ' ';
  2718. }
  2719. } else if (tr->translator_name == L('e', 'n')) {
  2720. // add 'e' to end of stem
  2721. if (IsLetter(tr, word_end[-1], LETTERGP_VOWEL2) && IsLetter(tr, word_end[0], 1)) {
  2722. // vowel(incl.'y') + hard.consonant
  2723. for (i = 0; (p = add_e_exceptions[i]) != NULL; i++) {
  2724. len = strlen(p);
  2725. if (memcmp(p, &word_end[1-len], len) == 0)
  2726. break;
  2727. }
  2728. if (p == NULL)
  2729. end_flags |= FLAG_SUFX_E_ADDED; // no exception found
  2730. } else {
  2731. for (i = 0; (p = add_e_additions[i]) != NULL; i++) {
  2732. len = strlen(p);
  2733. if (memcmp(p, &word_end[1-len], len) == 0) {
  2734. end_flags |= FLAG_SUFX_E_ADDED;
  2735. break;
  2736. }
  2737. }
  2738. }
  2739. } else if (tr->langopts.suffix_add_e != 0)
  2740. end_flags |= FLAG_SUFX_E_ADDED;
  2741. if (end_flags & FLAG_SUFX_E_ADDED) {
  2742. utf8_out(tr->langopts.suffix_add_e, &word_end[1]);
  2743. if (option_phonemes & espeakPHONEMES_TRACE)
  2744. fprintf(f_trans, "add e\n");
  2745. }
  2746. }
  2747. if ((end_type & SUFX_V) && (tr->expect_verb == 0))
  2748. tr->expect_verb = 1; // this suffix indicates the verb pronunciation
  2749. if ((strcmp(ending, "s") == 0) || (strcmp(ending, "es") == 0))
  2750. end_flags |= FLAG_SUFX_S;
  2751. if (ending[0] == '\'')
  2752. end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix
  2753. return end_flags;
  2754. }