Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

ESpeakEngine - Objective-C speech synthesizer

, 23 Jan 2012
ESpeakEngine - Objective-C speech synthesizer
ESpeakTest.zip
ESpeakTest
.DS_Store
__MACOSX
ESpeakTest
._.DS_Store
.git
branches
COMMIT_EDITMSG
config
description
HEAD
hooks
applypatch-msg.sample
commit-msg.sample
post-commit.sample
post-receive.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-rebase.sample
prepare-commit-msg.sample
update.sample
index
info
exclude
.git
info
._exclude
logs
HEAD
refs
heads
master
objects
00
e88867c94b8f6728422a4ad099a53a9f392cb7
01
477be5465c04d4cfd46ab95003f17445731d74
02
37597b848a1890bb30ba0ff4102f8107cafdcb
6363f6a3bb9fe78b03b09f4d0fdce472b95e20
69a98a29eb1baf79dd1f6f2bbc9370b9c4c9cb
78ea2136d97866104e3789d7950d39632b5046
03
7b0f820ace631b85715dfb13cc21fe26daa44d
dac4f6baff6f5a2b06f5a68b6daa265c16e0a2
edde41daf54530ac1fe426e9349a5544b48ccc
05
b2d0d21b687b4190600f6ea0206972c960dd1c
06
23379b22e1417ed806ff4c48337afbe9457cdf
0c
2d13a65548266d9c8a6758599c9a86faf53b28
0d
51695daffab058d065e942018f5c56067942fe
6fa2a91273ee3271fb1d3202d7f6ad86379ef7
10
e91b22ea1e6c67962cd858e8d03de74e7abf6f
11
8095eb5859b9c2ee14be00adeb87ee66eaec94
12
83826f9250bed7326aed2542a0e3c5b47edbec
ce1096a4ef6c4538a43ebd951dcaf2b624b717
13
664a34fe6a3b585603e00df15b17bcef3591e0
93b3551c88468dccede7550726aae27a931555
14
1160885f6955ee4855933ce984bc117649d220
417c1fdfdb3af16ef717d90a078f5c9b8468c3
15
96e3c77205e1688ad5968359106b5ea92d322a
17
004519410f433308228397abc30d2b69e55b26
19
3700525eaa0aa02256a4211c820007bb005995
da34a5b9b6e585ece12d4a6ec24341fa98f50c
fd69d3bec6857b0d4b29f4f26c03ac0dc727e3
1a
61238c5c772724cb12490eb555630bcc0e7870
9e53b6e584bd1e8245f3fc69751211b7d4e86a
1c
2992dd5455746aef85905cc0293031e3e9ceaa
a6f6abadb2e86be2baf2f823a27d16c43d2528
1e
9a757f891fd7ece2ff350dc9225837cb7e70f7
1f
2eb929019174a9b4c2654c1398ed26aca50e2c
20
5e59c21f973ea13de6deab139efee2f171027a
21
4d6f9d708a7691c1abf13b0801ee9a54a44ae6
22
a3b80f6954fd663b59f66210e7b658c5db3a3f
a95d18a3ead5d0274abab7986121dccafc1842
23
8c69126e18387f9c2b3d059db6d7df1cd91a6f
25
55d5d8bec5ee0524034d96ea5aa95eeb1a90dd
5fe35ca3bf7c8743abf0f95518aae822aac4f0
bfdbf47ea384bd8118ed197a8842cb2104eb2a
d78c05ca841d2019f204184927282dc69ebd61
28
59e093f75f1f88ca412e0bde9345afc01f15ac
29
91e99d136fe8e13f7c0c99eaa27c8ca397186d
2a
736d4d3f3c3ffe36bea620d6c28b672efc5867
fb1adc71b7f4e90bd02355dd8cdb75e8d23870
2e
1534872410e5819a69396cdbb57225baa3a888
9dd1f58881b69e931f3b5a73fef461eb9b838b
30
441d7e239ccd9169d15b04efc59be7a158a4a5
dea89200864b8e84dc0fec67850491d460a78d
31
02c9a1a9a5db99cadf90502de610f69c994913
bd479a3d5873c8a970de821a84d2addc864839
32
49dd4f8012e94af03784f043478ebab38fd0b7
33
00c360065de4a18e093dd02df3f6419b656f74
430a22c186f11e5cf838e81dacb386a7e4c8d0
8b8c450f3027ea5d3751fc1e0b4bf969cc1afc
35
a4a3fc517a71ec8bad8f9d0c71bd9da960c494
36
665314753a98e11162485805608fe1aca7bc1e
669d3f0592eaeb8465b81341b486a4c9c1eb11
a4bff6503dfb82fcdc7a160cc36f4cdd9e333d
3a
f2aa1af75587b7a29685fa2dbbebbd2b3592c2
3b
105a7fb0f9b3bb28a87a7212dfcef08daf53c0
3d
d75260a68d0fb7b88fe70d0e3e73c916b963e5
3e
520ec5c8b88db8f95f6a230273278fd39bfbd6
41
22d96b39bf2bc1025a1c57ef01b8b03936773d
42
dbdc5586bc89d681d23851f4a5aafd79b414cb
de58882d0c80e94597b575893afc8e99bd431e
46
07dd079dd2748f8e9cde034268e6cd68278644
317618d912082d6070e4e3972809824bd35395
47
7b28ff8f86a3158a71c4934fbd3a2456717d7a
4b
6a9ae550599ea85d77fea7c0f71b4d5aaf1ba8
4c
6239268d21312d311a504ad0d8aeb0e4f1030b
4d
ec15997c4e00c7a764156687431ac8c47676a5
4e
2b9d23e84059b93883e1a0c0f7a859a23b087e
4f
1904e5164410f93689beb55d46901b214271fe
8f5e88f436d478b126c5c4eccf3568e398c26f
e4188e53b10cc21b50c3bf47e9ef3b2fc4c641
50
1b5a4a8620d5bd9a545c8941cdcbd565fca1c6
52
1be164ce6c87e1d6df58fd82ab160c8f6255db
692c38546eb82aee2a7550c93798f70f02dc9a
c5ac93561331143a9caea14d6c0f008216b4e9
53
6957cb8fe03bde580784e6f97537ec3444e9c3
7beb3ba82da8af147f028685e61fc839cad713
c2a70482993f53d6df321687b5cc4d9e95abc1
cb31446e077a5f159c831e126a0e3f9a2d0ed2
56
9f9d05432267a13b75ca2562beb85e1e1c17db
58
1cd883fed6aa3b84b580a543b6ec8998f4d327
5a
24e11ad4dc2842c79033ad323f02e2e6c2f566
85640ee385afef9b9dc9b7d2889d47b81aa1bd
5c
3583da4700ab6982766d187d1195b37f9a3fb5
5e
bb6a35716f489f249db8bb0e9df7dde150eba4
5f
3297d3a2009a6051a8ebc606bc674056ea03fc
61
124ff298a7d392b816cb1a71095ed1ec8ffe6b
65
3c3f5c4a2d2e44b8a188b88b64278085ebde27
68
1cad9627cb3af687a30507f05114c89ef9340f
69
cceefb779a5236074db6871d7523d92f8a709f
6a
8d5efd87553a3f0e977636c6b819cddf3a99e7
cadba6aea97cd920745428a1c4ccd998581cc7
6c
65e3c6851f204d9c4cd9b616b46a9ff425b3c2
6d
826477b6ee1ecb3e502ffe3c26387d1bce3961
6e
11c93121ab5d535e4f2d50253ee4a527694a9f
71
99341c34f93f5fa5219ff479e82edaee5d7936
ecab7197ec9646efdae05bb02b465f5b5e361a
73
ac62a4ab12374bbf6f72539b2e104d10d7d394
74
00c07a5c17fbbead0d252a22f1fcdb6e5f15c4
0601d129aa08fd59be839301c923b684361dbe
79
2d8a9f9ef248c4358c36000722ba0c53a76497
7c
bdab338114c51e83e0b54c67280b91872211d5
7d
276eb2b779d73c46d8ed97e4be287bec96c828
7e
6c16a2c28e97392d20d4f4c243ecd6f6f40a91
f93a5edd61fddc97d982242d7654e5ab07a09e
7f
4631899e208f50b855ad579b726eadea70f67d
fccbe6dd68c968e78da5b6265a13c62c1fc639
82
2c9a312addee71797811c17690f2be8746bea4
98f98722e5f5f405e9631eb4a5064d87114424
83
71a46410d32c3f000db4c7b11254f48a3d6055
84
79e658ebe74cdd0b9525a41dededf8b9839858
ccc3a6d4681e19dbe6b982ddcee2e760944d55
85
ebb03e3089c5055e4f76d272866738a90a7842
88
1634035cad7fbc213a9def0b5e5ef7f15dffa2
48d6820e826b907349234a642535725247f837
89
28f0ff69aa677f7c5f96053ca70589552e17b0
e6c82914aa9457a644d5a498fa643f98b9ade3
8f
c65d4bab0f132f34e70868a961188deabc55dc
d4a63a1a24c8b25eab5ed28c135457a8332e6b
92
3d517415d489cc9b3f91638c14264dd0df55a3
a1582817dc2f8256db5a02bed320a05f6e5e43
95
8799c61770d05ba341183cd2d6a107a1ec093c
96
35ac150af1804b398d67cf4703d718a16806a7
97
8ec49ed00c46862d89580efe68f7efd98c93c3
a337d34bb9ab89812b5e79c3bc2bb784d48953
98
9f9eab7b5ee98f4b6acf35fe8b4ef86db3a62e
9b
06e0bd24aa4658ed8009be3e2fe7e32ccee54e
280bf8bc106ca904c9b33a90d0822c4c9b03fd
2d891f12030afadc1e737914a1759ab59ef01f
a872a49ab896e3d7c6203bedfd2502d8cb6521
c60c7cbd38db3307551ae17ef2a8a5d623b3a0
9d
e1630d90e22bf6df53a0093c212e9e1d7da9b6
eba8432350a07d0ab15189bb124d48b836fb62
9e
9c4e7476f3dce3b6808b0c8ea917a4d8503d7f
a3
72fb170d6218ccc7298335f1ea55ff674fb3cc
a5
4cb02cd26c7ecc2bd08debb1cfe85c3546c039
504505ef08ce6040091e48eb5c1653a34574b0
a6
56d2c7f2e9509fef687b3174282f6ec9b61498
e0f46bb57f876255cdb5767729aefccbe3a33e
a7
a8223dbda4d4cd47ae8796ad2be9bc70e46754
a9
09e1af21f9dc9dccf095d02920266ea11fe7f5
0c7b72565da6e015014e343f10ae50197587c9
aa
80edaad05f0f16c6195e72a0130d803b58bcbc
ae
76a4c4deb77d53e7ee512c3e85f45ba802dd08
9247d41055f6721d3500645539ee00ae29d7e4
a3d895c09d5eae5411aec5fb5ecbe82451bd50
b0
d4979c857d151ef5cd27248926fb112c0c3cee
b1
a874be65ad7df189c933f4c576475f29d15f67
b2
7a8114a03ed27348a40f37428107e02f67acc8
fd9d084c6df70da314916ee674421872f9ef50
b3
2b6a6660b12a14fc5e8749eff9ba696b7f6061
b4
8b1788b979853cdd57bb6cec6b9cc4dd7925bc
b7
39a86e488eeec6f5693b7ee2590f84d6c95191
b8
519559d3db834193518d4f4c4fed777734c8ee
6f59306a576f4548ff3bd62e344570cc538932
f782946f4728a629eda8038e82dd165dadee1d
ba
7c42cc48ae095ccfffeb03cf36d519d7cbe5a3
bc
bb2a0058b7866064b779ae246244c5342a0ccb
bd
336a9884877889e2f85ee1849f8a068d2c6279
be
1b6246a01d3d9c01dac0f888454369e0b32892
c0
a5475ece89c3274e62f9132560ec8afdd569d4
c2
34f46877a761d5c98fda2cbbeb172dc2efc81a
76bec0dd56b68a8e0ed0062b38dd149422b45d
c3
2db3968df0779b3c3a47899085625a7b86339b
c6
32e263c2a639311be4ca1512aadee97a91bc9a
cb
c4fb3a5d3dcc07f26b5ca02eb53a52b897aa80
cd
02abedb89d8975a792d73de536d2ce82260553
ce
800f70be34d9fba11f7d09b0c329b3f259507b
cf
584b7dd3f005be3cffb47d27f1bfe980a6e53b
f13bac9dd46f2c2a2fb66db64ce78caffd5b5a
d0
2b035cae9b12c66a5e0d5ae9cf57433041ed40
864f3d6209ad6f3e8ab51dc880836e67c96fc5
b729579de2537a63cebdb809e8bdfb5a897a33
d2
5865608d7d973e77c77d13bbdbe3ae454d864d
d3
d7720074a2fc7e3bfbc18cc1bacad0f9ea61d2
d5
06e7f9f85667d054794f808f9098b2a1625372
9fe79529970f627e83857f9bd516f5474bf685
d6
737d787752a799349095b49cf81024dfd00fb6
811d3ae4f710d3eb8a16c690cd42bebc4faf4e
d7
59cfdf018f8814d995588d176dadde0820b5ca
d8
ecd252c04bc00a6dcc6d0b84d64285726b65d4
f35efaa2dc2293277365d811c9b6222cfe59b8
da
c1e4d0643b01e99052f575b5f6add2e10a2143
ef5160c9c1500d3a9f6b0eadf434ef702a009f
db
63d6ca3d87629e693327a2ba4a719e79847b6c
de212341a0b131224b3e123f91d154348070ba
dc
51396ce24aad46d1c761e2c96f8aee68d4b622
de
4786c94cfa90c84982ca06a2a9623035993dfc
df
70f4387ca97d30c66d7dc87d5abca9b0ffa820
7fa77bc23477530674f05f4f619803983d5eb0
e0
783ec3b1e38ce0f77b5b018d9b8641a7b924c3
ea6d6394ab8409463adfeb753f1632ef09a47f
e4
16c6dc5e784243ad8d33000139178fa178de87
1d3105c048e789fe605c780efe2881df833ea8
e7
178e4d5666a3b75aa48de33b966f0590665a8e
e8
5978c76abd7430436356cd2a126018642d6a2d
ea
dd707322e08d90e3a2208aef9060706c340799
eb
d92ffb3ec9499292d1faa7ca60adcc5edac74f
ed
05f4240da98a569e3c9f9a5b9e2301d7caa79e
ee
3bd233e2bc458aab63eb36f613f853f72e6bf7
f2
033dc11fee0a1db4a2cae358c1a808149a5aa2
130ba4f980783f8605eb50387ffc2e54f66b3c
f3
10f868efea6408fc49fd4e6d6c293db2098114
e97b52355ea6f5384790bb3cda677222be900c
f4
3ef84fa6caf08bc6e9e442e15d71eaa8c6962a
ede3296c4fb294a58715e61ecef26f74ed898a
f5
0e89c905a3e8dd519f415585283757fc85d2d6
f9
624b31623b500b57b74e9765ac7a2d9f039c65
fa
4eece0b1cc7dababed3d09d560f38e00ea8755
fc
377156b487a559efb384ec2b6e551d6c2085c6
60f41672c8fb6fc69c1548552988f046a2aa14
fe
7c4d8d002499fbaff530adfaa0543d626536dd
ff
a94206372e46914dcadeb10c212a9674daf62e
info
pack
refs
heads
master
tags
ESpeakTest
ESpeakTest
AppDelegate.m
._AppDelegate.m
en.lproj
InfoPlist.strings
en.lproj
._InfoPlist.strings
ViewController_iPad.xib
._ViewController_iPad.xib
ViewController_iPhone.xib
ESpeakTest-Info.plist
ESpeakTest-Prefix.pch
._ESpeakTest-Prefix.pch
main.m
._main.m
ViewController.m
._ViewController.m
ESpeakTest.xcodeproj
project.pbxproj
project.xcworkspace
contents.xcworkspacedata
xcuserdata
jozefbozek.xcuserdatad
UserInterfaceState.xcuserstate
xcuserdata
jozefbozek.xcuserdatad
xcdebugger
Breakpoints.xcbkptlist
xcschemes
ESpeakTest.xcscheme
xcschememanagement.plist
ESpeakTestTests
en.lproj
InfoPlist.strings
ESpeakTestTests
en.lproj
._InfoPlist.strings
ESpeakTestTests-Info.plist
ESpeakTestTests.m
._ESpeakTestTests.m
eSpeak_1.0.zip
eSpeak_1.0
Classes
eSpeak_1.0
Classes
ESpeakEngine.m
._ESpeakEngine.m
eSpeak.xcodeproj
jBozEk.mode1v3
jBozEk.pbxuser
project.pbxproj
project.xcworkspace
contents.xcworkspacedata
xcuserdata
jBozEk.xcuserdatad
UserInterfaceState.xcuserstate
jozefbozek.xcuserdatad
UserInterfaceState.xcuserstate
WorkspaceSettings.xcsettings
xcuserdata
jBozEk.xcuserdatad
xcschemes
xcschememanagement.plist
jozefbozek.xcuserdatad
xcdebugger
Breakpoints.xcbkptlist
xcschemes
eSpeak.xcscheme
TestApp.xcscheme
xcschememanagement.plist
eSpeak_Prefix.pch
._eSpeak_Prefix.pch
Libraries
Libraries
espeak-data
af_dict
espeak-data
._af_dict
ca_dict
._ca_dict
config
._config
cs_dict
._cs_dict
cy_dict
._cy_dict
da_dict
._da_dict
de_dict
._de_dict
el_dict
._el_dict
en_dict
._en_dict
eo_dict
._eo_dict
es_dict
._es_dict
fi_dict
._fi_dict
fr_dict
._fr_dict
grc_dict
._grc_dict
hbs_dict
._hbs_dict
hi_dict
._hi_dict
hu_dict
._hu_dict
hy_dict
._hy_dict
id_dict
._id_dict
intonations
._intonations
is_dict
._is_dict
it_dict
._it_dict
jbo_dict
._jbo_dict
ku_dict
._ku_dict
la_dict
._la_dict
lv_dict
._lv_dict
mbrola
._mbrola
mbrola_ph
af1_phtrans
mbrola_ph
._af1_phtrans
ca1_phtrans
._ca1_phtrans
cr1_phtrans
._cr1_phtrans
cs_phtrans
._cs_phtrans
de2_phtrans
._de2_phtrans
de4_phtrans
._de4_phtrans
de6_phtrans
._de6_phtrans
en1_phtrans
._en1_phtrans
es_phtrans
._es_phtrans
fr1_phtrans
._fr1_phtrans
gr2_phtrans
._gr2_phtrans
grc-de6_phtrans
._grc-de6_phtrans
hu1_phtrans
._hu1_phtrans
ic1_phtrans
._ic1_phtrans
id1_phtrans
._id1_phtrans
in1_phtrans
._in1_phtrans
it3_phtrans
._it3_phtrans
la1_phtrans
._la1_phtrans
nl_phtrans
._nl_phtrans
pl1_phtrans
._pl1_phtrans
pt1_phtrans
._pt1_phtrans
pt_phtrans
._pt_phtrans
ptbr4_phtrans
._ptbr4_phtrans
ptbr_phtrans
._ptbr_phtrans
ro1_phtrans
._ro1_phtrans
sv2_phtrans
._sv2_phtrans
sv_phtrans
._sv_phtrans
tr1_phtrans
._tr1_phtrans
us3_phtrans
._us3_phtrans
us_phtrans
._us_phtrans
._mbrola_ph
mk_dict
._mk_dict
nci_dict
._nci_dict
nl_dict
._nl_dict
no_dict
._no_dict
pap_dict
._pap_dict
phondata
._phondata
phonindex
._phonindex
phontab
._phontab
pl_dict
._pl_dict
pt_dict
._pt_dict
ro_dict
._ro_dict
ru_dict
._ru_dict
sk_dict
._sk_dict
soundicons
._soundicons
sq_dict
._sq_dict
sv_dict
._sv_dict
sw_dict
._sw_dict
ta_dict
._ta_dict
tr_dict
._tr_dict
vi_dict
._vi_dict
voices
!v
croak
voices
!v
._croak
f1
._f1
f2
._f2
f3
._f3
f4
._f4
f5
._f5
fast
._fast
klatt
._klatt
klatt2
._klatt2
klatt3
._klatt3
m1
._m1
m2
._m2
m3
._m3
m4
._m4
m5
._m5
m6
._m6
m7
._m7
whisper
._whisper
whisperf
._whisperf
._!v
af
._af
bs
._bs
ca
._ca
cs
._cs
cy
._cy
da
._da
de
._de
default
._default
el
._el
en
en
en
._en
en-n
._en-n
en-rp
._en-rp
en-sc
._en-sc
en-us
._en-us
en-wi
._en-wi
en-wm
._en-wm
._en
eo
._eo
es
._es
es-la
._es-la
fi
._fi
fr
._fr
fr-be
._fr-be
hi
._hi
hr
._hr
hu
._hu
hy
._hy
hy-west
._hy-west
id
._id
is
._is
it
._it
ku
._ku
la
._la
lv
._lv
mb
mb-af1
mb
._mb-af1
mb-af1-en
._mb-af1-en
mb-br1
._mb-br1
mb-br3
._mb-br3
mb-br4
._mb-br4
mb-cr1
._mb-cr1
mb-cz2
._mb-cz2
mb-de2
._mb-de2
mb-de4
._mb-de4
mb-de4-en
._mb-de4-en
mb-de5
._mb-de5
mb-de5-en
._mb-de5-en
mb-de6
._mb-de6
mb-de6-grc
._mb-de6-grc
mb-de7
._mb-de7
mb-en1
._mb-en1
mb-es1
._mb-es1
mb-es2
._mb-es2
mb-fr1
._mb-fr1
mb-fr1-en
._mb-fr1-en
mb-fr4
._mb-fr4
mb-fr4-en
._mb-fr4-en
mb-gr2
._mb-gr2
mb-gr2-en
._mb-gr2-en
mb-hu1
._mb-hu1
mb-hu1-en
._mb-hu1-en
mb-ic1
._mb-ic1
mb-id1
._mb-id1
mb-it3
._mb-it3
mb-it4
._mb-it4
mb-la1
._mb-la1
mb-nl2
._mb-nl2
mb-nl2-en
._mb-nl2-en
mb-pl1
._mb-pl1
mb-pl1-en
._mb-pl1-en
mb-pt1
._mb-pt1
mb-ro1
._mb-ro1
mb-ro1-en
._mb-ro1-en
mb-sw1
._mb-sw1
mb-sw1-en
._mb-sw1-en
mb-sw2
._mb-sw2
mb-sw2-en
._mb-sw2-en
mb-tr1
._mb-tr1
mb-tr2
._mb-tr2
mb-us1
._mb-us1
mb-us2
._mb-us2
mb-us3
._mb-us3
._mb
mk
._mk
nl
._nl
no
._no
pl
._pl
pt
._pt
pt-pt
._pt-pt
ro
._ro
ru
._ru
sk
._sk
sq
._sq
sr
._sr
sv
._sv
sw
._sw
ta
._ta
test
grc
test
._grc
jbo
._jbo
nci
._nci
pap
._pap
._test
tr
._tr
vi
._vi
zh
._zh
zh-yue
._zh-yue
._voices
zh_dict
._zh_dict
zhy_dict
._zhy_dict
._espeak-data
TestApp-Info.plist
._TestApp-Info.plist
Tests
FliteEngineTest-Info.plist
Tests
FliteEngineTestAppDelegate.m
._FliteEngineTestAppDelegate.m
FliteEngineTestViewController.m
._FliteEngineTestViewController.m
FliteEngineTestViewController.xib
main.m
._main.m
MainWindow.xib
/***************************************************************************
 *   Copyright (C) 2005 to 2010 by Jonathan Duddington                     *
 *   email: jonsd@users.sourceforge.net                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see:                                 *
 *               <http://www.gnu.org/licenses/>.                           *
 ***************************************************************************/

#include "StdAfx.h"

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include <wctype.h>
#include <wchar.h>

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

#define WORD_STRESS_CHAR   '*'


Translator *translator = NULL;    // the main translator
Translator *translator2 = NULL;   // secondary translator for certain words
static char translator2_language[20] = {0};

FILE *f_trans = NULL;     // phoneme output text
int option_tone2 = 0;
int option_tone_flags = 0;   // bit 8=emphasize allcaps, bit 9=emphasize penultimate stress
int option_phonemes = 0;
int option_phoneme_events = 0;
int option_quiet = 0;
int option_endpause = 0;  // suppress pause after end of text
int option_capitals = 0;
int option_punctuation = 0;
int option_sayas = 0;
static int option_sayas2 = 0;  // used in translate_clause()
static int option_emphasis = 0;  // 0=normal, 1=normal, 2=weak, 3=moderate, 4=strong
int option_ssml = 0;
int option_phoneme_input = 0;  // allow [[phonemes]] in input
int option_phoneme_variants = 0;  // 0= don't display phoneme variant mnemonics
int option_wordgap = 0;

static int count_sayas_digits;
int skip_sentences;
int skip_words;
int skip_characters;
char skip_marker[N_MARKER_LENGTH];
int skipping_text;   // waiting until word count, sentence count, or named marker is reached
int end_character_position;
int count_sentences;
int count_words;
int clause_start_char;
int clause_start_word;
int new_sentence;
static int word_emphasis = 0;    // set if emphasis level 3 or 4
static int embedded_flag = 0;    // there are embedded commands to be applied to the next phoneme, used in TranslateWord2()

static int prev_clause_pause=0;
static int max_clause_pause = 0;
static int any_stressed_words;
int pre_pause;


// these were previously in translator class
#ifdef PLATFORM_WINDOWS
char word_phonemes[N_WORD_PHONEMES*2];    // longer, because snprint() is not available
#else
char word_phonemes[N_WORD_PHONEMES];    // a word translated into phoneme codes
#endif
int n_ph_list2;
PHONEME_LIST2 ph_list2[N_PHONEME_LIST];	// first stage of text->phonemes



wchar_t option_punctlist[N_PUNCTLIST]={0};
char ctrl_embedded = '\001';    // to allow an alternative CTRL for embedded commands
int option_multibyte=espeakCHARS_AUTO;   // 0=auto, 1=utf8, 2=8bit, 3=wchar, 4=16bit

// these are overridden by defaults set in the "speak" file
int option_linelength = 0;

#define N_EMBEDDED_LIST  250
static int embedded_ix;
static int embedded_read;
unsigned int embedded_list[N_EMBEDDED_LIST];

// the source text of a single clause (UTF8 bytes)
#define N_TR_SOURCE    700
static char source[N_TR_SOURCE+40];     // extra space for embedded command & voice change info at end

int n_replace_phonemes;
REPLACE_PHONEMES replace_phonemes[N_REPLACE_PHONEMES];


// brackets, also 0x2014 to 0x021f which don't need to be in this list
static const unsigned short brackets[] = {
'(',')','[',']','{','}','<','>','"','\'','`',
0xab,0xbb,  // double angle brackets
0x300a,0x300b,  // double angle brackets (ideograph)
0xe000+'<',  // private usage area
0};

// other characters which break a word, but don't produce a pause
static const unsigned short breaks[] = {'_', 0};

// treat these characters as spaces, in addition to iswspace()
static const wchar_t chars_space[] = {0x2500,0};  // box drawing horiz


// Translate character codes 0xA0 to 0xFF into their unicode values
// ISO_8859_1 is set as default
static const unsigned short ISO_8859_1[0x60] = {
   0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
   0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
   0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
   0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
   0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
   0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
   0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
   0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, // d8
   0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
   0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
   0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
   0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff, // f8
};

static const unsigned short ISO_8859_2[0x60] = {
   0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7, // a0
   0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b, // a8
   0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7, // b0
   0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, // b8
   0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7, // c0
   0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e, // c8
   0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7, // d0
   0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df, // d8
   0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7, // e0
   0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f, // e8
   0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7, // f0
   0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9, // f8
};

static const unsigned short ISO_8859_3[0x60] = {
   0x00a0, 0x0126, 0x02d8, 0x00a3, 0x00a4, 0x0000, 0x0124, 0x00a7, // a0
   0x00a8, 0x0130, 0x015e, 0x011e, 0x0134, 0x00ad, 0x0000, 0x017b, // a8
   0x00b0, 0x0127, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x0125, 0x00b7, // b0
   0x00b8, 0x0131, 0x015f, 0x011f, 0x0135, 0x00bd, 0x0000, 0x017c, // b8
   0x00c0, 0x00c1, 0x00c2, 0x0000, 0x00c4, 0x010a, 0x0108, 0x00c7, // c0
   0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
   0x0000, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x0120, 0x00d6, 0x00d7, // d0
   0x011c, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x016c, 0x015c, 0x00df, // d8
   0x00e0, 0x00e1, 0x00e2, 0x0000, 0x00e4, 0x010b, 0x0109, 0x00e7, // e0
   0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
   0x0000, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x0121, 0x00f6, 0x00f7, // f0
   0x011d, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x016d, 0x015d, 0x02d9, // f8
};

static const unsigned short ISO_8859_4[0x60] = {
   0x00a0, 0x0104, 0x0138, 0x0156, 0x00a4, 0x0128, 0x013b, 0x00a7, // a0
   0x00a8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00ad, 0x017d, 0x00af, // a8
   0x00b0, 0x0105, 0x02db, 0x0157, 0x00b4, 0x0129, 0x013c, 0x02c7, // b0
   0x00b8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014a, 0x017e, 0x014b, // b8
   0x0100, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x012e, // c0
   0x010c, 0x00c9, 0x0118, 0x00cb, 0x0116, 0x00cd, 0x00ce, 0x012a, // c8
   0x0110, 0x0145, 0x014c, 0x0136, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
   0x00d8, 0x0172, 0x00da, 0x00db, 0x00dc, 0x0168, 0x016a, 0x00df, // d8
   0x0101, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x012f, // e0
   0x010d, 0x00e9, 0x0119, 0x00eb, 0x0117, 0x00ed, 0x00ee, 0x012b, // e8
   0x0111, 0x0146, 0x014d, 0x0137, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
   0x00f8, 0x0173, 0x00fa, 0x00fb, 0x00fc, 0x0169, 0x016b, 0x02d9, // f8
};

static const unsigned short ISO_8859_5[0x60] = {
   0x00a0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, // a0  Cyrillic
   0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x00ad, 0x040e, 0x040f, // a8
   0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, // b0
   0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, // b8
   0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, // c0
   0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, // c8
   0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, // d0
   0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, // d8
   0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, // e0
   0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f, // e8
   0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, // f0
   0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x00a7, 0x045e, 0x045f, // f8
};

static const unsigned short ISO_8859_7[0x60] = {
   0x00a0, 0x2018, 0x2019, 0x00a3, 0x20ac, 0x20af, 0x00a6, 0x00a7, // a0  Greek
   0x00a8, 0x00a9, 0x037a, 0x00ab, 0x00ac, 0x00ad, 0x0000, 0x2015, // a8
   0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x0385, 0x0386, 0x00b7, // b0
   0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f, // b8
   0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, // c0
   0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, // c8
   0x03a0, 0x03a1, 0x0000, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, // d0
   0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, // d8
   0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, // e0
   0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, // e8
   0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7, // f0
   0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0x0000, // f8
};

static const unsigned short ISO_8859_9[0x60] = {
   0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
   0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
   0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
   0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
   0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
   0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
   0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
   0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df, // d8
   0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
   0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
   0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
   0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff, // f8
};

static const unsigned short ISO_8859_14[0x60] = {
   0x00a0, 0x1e02, 0x1e03, 0x00a3, 0x010a, 0x010b, 0x1e0a, 0x00a7, // a0  Welsh
   0x1e80, 0x00a9, 0x1e82, 0x1e0b, 0x1ef2, 0x00ad, 0x00ae, 0x0178, // a8
   0x1e1e, 0x1e1f, 0x0120, 0x0121, 0x1e40, 0x1e41, 0x00b6, 0x1e56, // b0
   0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, // b8
   0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
   0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
   0x0174, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x1e6a, // d0
   0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x0176, 0x00df, // d8
   0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
   0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
   0x0175, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x1e6b, // f0
   0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x0177, 0x00ff, // f8
};

static const unsigned short KOI8_R[0x60] = {
   0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0  Russian
   0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
   0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
   0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
   0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
   0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
   0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
   0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
   0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
   0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
   0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
   0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
};

static const unsigned short ISCII[0x60] = {
   0x0020, 0x0901, 0x0902, 0x0903, 0x0905, 0x0906, 0x0907, 0x0908, // a0
   0x0909, 0x090a, 0x090b, 0x090e, 0x090f, 0x0910, 0x090d, 0x0912, // a8
   0x0913, 0x0914, 0x0911, 0x0915, 0x0916, 0x0917, 0x0918, 0x0919, // b0
   0x091a, 0x091b, 0x091c, 0x091d, 0x091e, 0x091f, 0x0920, 0x0921, // b8
   0x0922, 0x0923, 0x0924, 0x0925, 0x0926, 0x0927, 0x0928, 0x0929, // c0
   0x092a, 0x092b, 0x092c, 0x092d, 0x092e, 0x092f, 0x095f, 0x0930, // c8
   0x0931, 0x0932, 0x0933, 0x0934, 0x0935, 0x0936, 0x0937, 0x0938, // d0
   0x0939, 0x0020, 0x093e, 0x093f, 0x0940, 0x0941, 0x0942, 0x0943, // d8
   0x0946, 0x0947, 0x0948, 0x0945, 0x094a, 0x094b, 0x094c, 0x0949, // e0
   0x094d, 0x093c, 0x0964, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, // e8
   0x0020, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, // f0
   0x0037, 0x0038, 0x0039, 0x20,   0x20,   0x20,   0x20,   0x20,   // f8
};

const unsigned short *charsets[N_CHARSETS] = {
	ISO_8859_1,
	ISO_8859_1,
	ISO_8859_2,
	ISO_8859_3,
	ISO_8859_4,
	ISO_8859_5,
	ISO_8859_1,
	ISO_8859_7,
	ISO_8859_1,
	ISO_8859_9,
	ISO_8859_1,
	ISO_8859_1,
	ISO_8859_1,
	ISO_8859_1,
	ISO_8859_14,
	ISO_8859_1,
	ISO_8859_1,
	ISO_8859_1,
	KOI8_R,          // 18
	ISCII };

// Tables of the relative lengths of vowels, depending on the
// type of the two phonemes that follow
// indexes are the "length_mod" value for the following phonemes

// use this table if vowel is not the last in the word
static unsigned char length_mods_en[100] = {
/*  a   ,   t   s   n   d   z   r   N   <- next */
	100,120,100,105,100,110,110,100, 95, 100,  /* a  <- next2 */
	105,120,105,110,125,130,135,115,125, 100,  /* , */
	105,120, 75,100, 75,105,120, 85, 75, 100,  /* t */
	105,120, 85,105, 95,115,120,100, 95, 100,  /* s */
	110,120, 95,105,100,115,120,100,100, 100,  /* n */
	105,120,100,105, 95,115,120,110, 95, 100,  /* d */
	105,120,100,105,105,122,125,110,105, 100,  /* z */
	105,120,100,105,105,122,125,110,105, 100,  /* r */
	105,120, 95,105,100,115,120,110,100, 100,  /* N */
	100,120,100,100,100,100,100,100,100, 100 }; // SPARE

// as above, but for the last syllable in a word
static unsigned char length_mods_en0[100] = {
/*  a   ,   t   s   n   d   z   r    N  <- next */
	100,150,100,105,110,115,110,110,110, 100,  /* a  <- next2 */
	105,150,105,110,125,135,140,115,135, 100,  /* , */
	105,150, 90,105, 90,122,135,100, 90, 100,  /* t */
	105,150,100,105,100,122,135,100,100, 100,  /* s */
	105,150,100,105,105,115,135,110,105, 100,  /* n */
	105,150,100,105,105,122,130,120,125, 100,  /* d */
	105,150,100,105,110,122,125,115,110, 100,  /* z */
	105,150,100,105,105,122,135,120,105, 100,  /* r */
	105,150,100,105,105,115,135,110,105, 100,  /* N */
	100,100,100,100,100,100,100,100,100, 100 }; // SPARE


static unsigned char length_mods_equal[100] = {
/*  a   ,   t   s   n   d   z   r   N   <- next */
	110,110,110,110,110,110,110,110,110, 110,  /* a  <- next2 */
	110,110,110,110,110,110,110,110,110, 110,  /* , */
	110,110,110,110,110,110,110,110,110, 110,  /* t */
	110,110,110,110,110,110,110,110,110, 110,  /* s */
	110,110,110,110,110,110,110,110,110, 110,  /* n */
	110,110,110,110,110,110,110,110,110, 110,  /* d */
	110,110,110,110,110,110,110,110,110, 110,  /* z */
	110,110,110,110,110,110,110,110,110, 110,  /* r */
	110,110,110,110,110,110,110,110,110, 110,  /* N */
	110,110,110,110,110,110,110,110,110, 110 }; // SPARE


static unsigned char *length_mod_tabs[6] = {
  length_mods_en,
  length_mods_en,     // 1
  length_mods_en0,    // 2
  length_mods_equal,  // 3
  length_mods_equal,  // 4
  length_mods_equal   // 5
 };


void SetLengthMods(Translator *tr, int value)
{//==========================================
	int value2;

	tr->langopts.length_mods0 = tr->langopts.length_mods = length_mod_tabs[value % 100];
	if((value2 = value / 100) != 0)
	{
		tr->langopts.length_mods0 = length_mod_tabs[value2];
	}
}


int IsAlpha(unsigned int c)
{//========================
// Replacement for iswalph() which also checks for some in-word symbols

	const unsigned short extra_indic_alphas[] = {
	0xa70,0xa71,	// Gurmukhi: tippi, addak
	0 };

	if(iswalpha(c))
		return(1);

	if((c >= 0x901) && (c <= 0xdf7))
	{
		// Indic scripts: Devanagari, Tamil, etc
		if((c & 0x7f) < 0x64)
			return(1);
		if(lookupwchar(extra_indic_alphas, c) != 0)
			return(1);
		return(0);
	}

	if((c >= 0x64b)  && (c <= 0x65e))
		return(1);   // arabic vowel marks

	if((c >= 0x300) && (c <= 0x36f))
		return(1);   // combining accents

	if((c >= 0x1100) && (c <= 0x11ff))
		return(1);  //Korean jamo

	if((c > 0x3040) && (c <= 0xa700))
		return(1); // Chinese/Japanese.  Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure

	return(0);
}

static int IsDigit09(unsigned int c)
{//=========================
	if((c >= '0') && (c <= '9'))
		return(1);
	return(0);
}

int IsDigit(unsigned int c)
{//========================
	if(iswdigit(c))
		return(1);

	if((c >= 0x966) && (c <= 0x96f))
		return(1);

	return(0);
}

int IsSpace(unsigned int c)
{//========================
	if(c == 0)
		return(0);
	if(wcschr(chars_space,c))
		return(1);
	return(iswspace(c));
}


void DeleteTranslator(Translator *tr)
{//==================================
	if(tr->data_dictlist != NULL)
		Free(tr->data_dictlist);
	Free(tr);
}


int lookupwchar(const unsigned short *list,int c)
{//==============================================
// Is the character c in the list ?
	int ix;

	for(ix=0; list[ix] != 0; ix++)
	{
		if(list[ix] == c)
			return(ix+1);
	}
	return(0);
}

int IsBracket(int c)
{//=================
	if((c >= 0x2014) && (c <= 0x201f))
		return(1);
	return(lookupwchar(brackets,c));
}


int utf8_out(unsigned int c, char *buf)
{//====================================
// write a unicode character into a buffer as utf8
// returns the number of bytes written
	int n_bytes;
	int j;
	int shift;
	static char unsigned code[4] = {0,0xc0,0xe0,0xf0};

	if(c < 0x80)
	{
		buf[0] = c;
		return(1);
	}
	if(c >= 0x110000)
	{
		buf[0] = ' ';      // out of range character code
		return(1);
	}
	if(c < 0x0800)
		n_bytes = 1;
	else
	if(c < 0x10000)
		n_bytes = 2;
	else
		n_bytes = 3;

	shift = 6*n_bytes;
	buf[0] = code[n_bytes] | (c >> shift);
	for(j=0; j<n_bytes; j++)
	{
		shift -= 6;
		buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
	}
	return(n_bytes+1);
}  // end of utf8_out


int utf8_nbytes(const char *buf)
{//=============================
// Returns the number of bytes for the first UTF-8 character in buf
	unsigned char c = (unsigned char)buf[0];
	if(c < 0x80)
		return(1);
	if(c < 0xe0)
		return(2);
	if(c < 0xf0)
		return(3);
	return(4);
}


int utf8_in2(int *c, const char *buf, int backwards)
{//=================================================
// Read a unicode characater from a UTF8 string 
// Returns the number of UTF8 bytes used.
// backwards: set if we are moving backwards through the UTF8 string
	int c1;
	int n_bytes;
	int ix;
	static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};

	// find the start of the next/previous character
	while((*buf & 0xc0) == 0x80)
	{
		// skip over non-initial bytes of a multi-byte utf8 character
		if(backwards)
			buf--;
		else
			buf++;
	}

	n_bytes = 0;

	if((c1 = *buf++) & 0x80)
	{
		if((c1 & 0xe0) == 0xc0)
			n_bytes = 1;
		else
		if((c1 & 0xf0) == 0xe0)
			n_bytes = 2;
		else
		if((c1 & 0xf8) == 0xf0)
			n_bytes = 3;

		c1 &= mask[n_bytes];
		for(ix=0; ix<n_bytes; ix++)
		{
			c1 = (c1 << 6) + (*buf++ & 0x3f);
		}
	}
	*c = c1;
	return(n_bytes+1);
}


int utf8_in(int *c, const char *buf)
{//=================================
// Read a unicode characater from a UTF8 string 
// Returns the number of UTF8 bytes used.
	return(utf8_in2(c,buf,0));
}


char *strchr_w(const char *s, int c)
{//=================================
// return NULL for any non-ascii character
	if(c >= 0x80)
		return(NULL);
	return(strchr((char *)s,c));    // (char *) is needed for Borland compiler
}


static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word)
{//============================================================================================
	int posn = 0;
	int capitals = 0;
	int non_initial = 0;

	if (spell_word > 2)
		capitals = 2;

	while((*word != ' ') && (*word != 0))
	{
		word += TranslateLetter(tr, word, phonemes, capitals | non_initial);
		posn++;
		non_initial = 1;
		if(phonemes[0] == phonSWITCH)
		{
			// change to another language in order to translate this word
			strcpy(word_phonemes,phonemes);
			return(NULL);
		}
	}
	SetSpellingStress(tr,phonemes,spell_word,posn);
	return(word);
}  // end of SpeakIndividualLetters



static int CheckDottedAbbrev(char *word1, WORD_TAB *wtab)
{//=====================================================
	int wc;
	int count = 0;
	int nbytes;
	int ok;
	int ix;
	char *word;
	char *wbuf;
	char word_buf[80];

	word = word1;
	wbuf = word_buf;
	ix = 0;

	for(;;)
	{
		ok = 0;
		nbytes = utf8_in(&wc, word);
		if((word[nbytes] == ' ') && IsAlpha(wc))
		{
		 	if(word[nbytes+1] == '.')
			{
				if(word[nbytes+2] == ' ')
					ok = 1;
				else
				if(word[nbytes+2] =='\'')
				{
					nbytes += 2;   // delete the final dot (eg. u.s.a.'s)
					ok = 2;
				}
			}
			else
			if((count > 0) && (word[nbytes] == ' '))
				ok = 2;
		}

		if(ok == 0)
			break;

		for(ix=0; ix < nbytes; ix++)
			*wbuf++ = word[ix];

		count++;

		if(ok == 2)
		{
			word += nbytes;
			break;
		}

		word += (nbytes + 3);
	}

	if(count > 1)
	{
		ix = wbuf - word_buf;
		memcpy(word1, word_buf, ix);
		while(&word1[ix] < word)
			word1[ix++] = ' ';
		dictionary_skipwords = (count - 1)*2;
	}
	return(count);
}  // end of CheckDottedAbbrev


extern char *phondata_ptr;

int ChangeEquivalentPhonemes(Translator *tr, int lang2, char *phonemes)
{//====================================================================
// tr:  the original language
// lang2:  phoneme table number for the temporary language
// phonemes: the phonemes to be replaced

	int ix;
	int len;
	char  phon;
	char *p;
	unsigned char *pb;
	char *eqlist;
	char *p_out;
	char *p_in;
	int  remove_stress = 0;
	char phonbuf[N_WORD_PHONEMES];

	// has a phoneme equivalence table been specified for thus language pair?
	if((ix = phoneme_tab_list[tr->phoneme_tab_ix].equivalence_tables) == 0)
		return(0);

	pb = (unsigned char *)&phondata_ptr[ix];

	for(;;)
	{
		if(pb[0] == 0)
			return(0);   // table not found

		if(pb[0] == lang2)
			break;

		len = (pb[2] << 8) + pb[3];   // size of this table in words
		pb += (len * 4);
	}
	remove_stress = pb[1];

	if(option_phonemes == 2)
	{
		DecodePhonemes(phonemes, phonbuf);
		fprintf(f_trans,"(%s) %s  -> (%s) ", phoneme_tab_list[lang2].name, phonbuf, phoneme_tab_list[tr->phoneme_tab_ix].name);
	}

	p_in = phonemes;
	eqlist = (char *)&pb[8];
	p_out = phonbuf;

	while((phon = *p_in++) != 0)
	{
		if(remove_stress && ((phon & 0xff) < phonSTRESS_PREV))
			continue;   // remove stress marks

		// is there a translation for this phoneme code?
		p = eqlist;
		while(*p != 0)
		{
			len = strlen(&p[1]);
			if(*p == phon)
			{
				strcpy(p_out, &p[1]);
				p_out += len;
				break;
			}
			p += (len + 2);
		}
		if(*p == 0)
		{
			// no translation found
			*p_out++ = phon;
		}
	}
	*p_out = 0;

	if(remove_stress)
	{
		SetWordStress(tr, phonbuf, NULL, -1, 0);
	}

	strcpy(phonemes, phonbuf);

	if(option_phonemes == 2)
	{
		SelectPhonemeTable(tr->phoneme_tab_ix);
		DecodePhonemes(phonemes, phonbuf);
		fprintf(f_trans,"%s\n\n", phonbuf);
	}
	return(1);
}  // end of ChangeEquivalentPhonemes




int TranslateWord(Translator *tr, char *word1, int next_pause, WORD_TAB *wtab)
{//===========================================================================
// word1 is terminated by space (0x20) character

	int word_length;
	int ix;
	char *p;
	int pfix;
	int n_chars;
	unsigned int dictionary_flags[2];
	unsigned int dictionary_flags2[2];
	int end_type=0;
	int prefix_type=0;
	int prefix_stress;
	char *wordx;
	char phonemes[N_WORD_PHONEMES];
	char *ph_limit;
	char prefix_phonemes[N_WORD_PHONEMES];
	char unpron_phonemes[N_WORD_PHONEMES];
	char end_phonemes[N_WORD_PHONEMES];
	char word_copy[N_WORD_BYTES];
	char prefix_chars[41];
	int found=0;
   int end_flags;
	char c_temp;   // save a character byte while we temporarily replace it with space
	int first_char;
	int last_char = 0;
	int add_plural_suffix = 0;
	int prefix_flags = 0;
	int confirm_prefix;
	int spell_word;
	int stress_bits;
	int emphasize_allcaps = 0;
	int wflags;
	int wmark;
	WORD_TAB wtab_null[8];

	// translate these to get pronunciations of plural 's' suffix (different forms depending on
	// the preceding letter
	static char word_zz[4] = {0,'z','z',0};
	static char word_iz[4] = {0,'i','z',0};
	static char word_ss[4] = {0,'s','s',0};

	if(wtab == NULL)
	{
		memset(wtab_null, 0, sizeof(wtab_null));
		wtab = wtab_null;
	}
	wflags = wtab->flags;
	wmark = wtab->wmark;

	dictionary_flags[0] = 0;
	dictionary_flags[1] = 0;
	dictionary_flags2[0] = 0;
	dictionary_flags2[1] = 0;
	dictionary_skipwords = 0;

	phonemes[0] = 0;
	unpron_phonemes[0] = 0;
	prefix_phonemes[0] = 0;
	end_phonemes[0] = 0;
	ph_limit = &phonemes[N_WORD_PHONEMES];

	if(tr->data_dictlist == NULL)
	{
		// dictionary is not loaded
		word_phonemes[0] = 0;
		return(0);
	}

	// count the length of the word
	if(*word1 == ' ') word1++;   // possibly a dot was replaced by space:  $dot
	wordx = word1;

	utf8_in(&first_char,wordx);
	word_length = 0;
	while((*wordx != 0) && (*wordx != ' '))
	{
		wordx += utf8_in(&last_char,wordx);
		word_length++;
	}

	spell_word = 0;

	if(option_sayas == SAYAS_KEY)
	{
		if(word_length == 1)
			spell_word = 4;
		else
		{
			// is there a translation for this keyname ?
			word1--;
			*word1 = '_';   // prefix keyname with '_'
			found = LookupDictList(tr, &word1, phonemes, dictionary_flags, 0, wtab);
		}
	}

	// try an initial lookup in the dictionary list, we may find a pronunciation specified, or
	// we may just find some flags
	if(option_sayas & 0x10)
	{
		// SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
		spell_word = option_sayas & 0xf;    // 2,3,4
	}
	else
	{
		if(!found)
			found = LookupDictList(tr, &word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab);   // the original word


		if((dictionary_flags[0] & (FLAG_ALLOW_DOT || FLAG_NEEDS_DOT)) && (wordx[1] == '.'))
		{
			wordx[1] = ' ';   // remove a Dot after this word
		}

		if(dictionary_flags[0] & FLAG_TEXTMODE)
		{
			first_char = word1[0];
			stress_bits = dictionary_flags[0] & 0x7f;
			found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, 0, wtab);   // the text replacement
			if(dictionary_flags2[0]!=0)
			{
				dictionary_flags[0] = dictionary_flags2[0];
				dictionary_flags[1] = dictionary_flags2[1];
				if(stress_bits != 0)
				{
					// keep any stress information from the original word
					dictionary_flags[0] = (dictionary_flags[0] & ~0x7f) | stress_bits;
				}
			}
		}
		else
		if((found==0) && (dictionary_flags[0] & FLAG_SKIPWORDS))
		{
			// grouped words, but no translation.  Join the words with hyphens.
			wordx = word1;
			ix = 0;
			while(ix < dictionary_skipwords)
			{
				if(*wordx == ' ')
				{
					*wordx = '-';
					ix++;
				}
				wordx++;
			}
		}

		if((word_length == 1) && (dictionary_skipwords == 0))
		{
			// is this a series of single letters separated by dots?
			if(CheckDottedAbbrev(word1, wtab))
			{
				dictionary_flags[0] = 0;
				dictionary_flags[1] = 0;
				spell_word = 1;
				if(dictionary_skipwords)
					dictionary_flags[0] = FLAG_SKIPWORDS;
			}
		}


		// if textmode, LookupDictList() replaces word1 by the new text and returns found=0

		if(phonemes[0] == phonSWITCH)
		{
			// change to another language in order to translate this word
			strcpy(word_phonemes,phonemes);
			return(0);
		}

if((wmark > 0) && (wmark < 8))
{
	// the stressed syllable has been specified in the text  (TESTING)
	dictionary_flags[0] = (dictionary_flags[0] & ~0xf) | wmark;
}

		if(!found && (dictionary_flags[0] & FLAG_ABBREV))
		{
			// the word has $abbrev flag, but no pronunciation specified.  Speak as individual letters
			spell_word = 1;
		}
 
		if(!found && iswdigit(first_char))
		{
			Lookup(tr,"_0lang",word_phonemes);
			if(word_phonemes[0] == phonSWITCH)
				return(0);

			found = TranslateNumber(tr, word1, phonemes, dictionary_flags, wtab, 0);
		}

		if(!found & ((wflags & FLAG_UPPERS) != FLAG_FIRST_UPPER))
		{
			// either all upper or all lower case

			if((tr->langopts.numbers & NUM_ROMAN) || ((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && (wflags & FLAG_ALL_UPPER)))
			{
				if((found = TranslateRoman(tr, word1, phonemes, wtab)) != 0)
					dictionary_flags[0] |= FLAG_ABBREV;   // prevent emphasis if capitals
			}
		}

		if((wflags & FLAG_ALL_UPPER) && (word_length > 1)&& iswalpha(first_char))
		{
			if((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV))
			{
				// emphasize words which are in capitals
				emphasize_allcaps = FLAG_EMPHASIZED;
			}
			else
			if(!found && !(dictionary_flags[0] &  FLAG_SKIPWORDS) && (word_length<4) && (tr->clause_lower_count > 3)
				&& (tr->clause_upper_count <= tr->clause_lower_count))
			{
				// An upper case word in a lower case clause. This could be an abbreviation.
				spell_word = 1;
			}
		}
	}

	if(spell_word > 0)
	{
		// Speak as individual letters
		phonemes[0] = 0;
		end_type = 0;

		if(SpeakIndividualLetters(tr, word1, phonemes, spell_word) == NULL)
		{
			if(word_length > 1)
				return(FLAG_SPELLWORD);  // a mixture of languages, retranslate as individual letters, separated by spaces
			return(0);
		}
	}
	else
	if(found == 0)
	{
		int posn;
		int non_initial;
		int length;
		// word's pronunciation is not given in the dictionary list, although
		// dictionary_flags may have ben set there

		posn = 0;
		non_initial = 0;
		length = 999;
		wordx = word1;

		while(((length < 3) && (length > 0))|| (word_length > 1 && Unpronouncable(tr,wordx)))
		{
			// This word looks "unpronouncable", so speak letters individually until we
			// find a remainder that we can pronounce.
			emphasize_allcaps = 0;

			if(wordx[0] == '\'')
				break;

			if(posn > 0)
				non_initial = 1;

			wordx += TranslateLetter(tr, wordx, unpron_phonemes, non_initial);
			posn++;
			if(unpron_phonemes[0] == phonSWITCH)
			{
				// change to another language in order to translate this word
				strcpy(word_phonemes,unpron_phonemes);
				if(strcmp(&unpron_phonemes[1],"en")==0)
					return(FLAG_SPELLWORD);   // _^_en must have been set in TranslateLetter(), not *_rules
				return(0);
			}

#ifdef deleted
			p = &wordx[word_length-3];    // this looks wrong.  Doesn't consider multi-byte chars.
			if(memcmp(p,"'s ",3) == 0)
			{
				// remove a 's suffix and pronounce this separately (not as an individual letter)
				add_plural_suffix = 1;
				p[0] = ' ';
				p[1] = ' ';
				last_char = p[-1];
			}
#endif
			length=0;
			while(wordx[length] != ' ') length++;
		}
		SetSpellingStress(tr,unpron_phonemes,0,posn);

		// anything left ?
		if(*wordx != ' ')
		{
			if((unpron_phonemes[0] != 0) && (wordx[0] != '\''))
			{
				// letters which have been spoken individually from affecting the pronunciation of the pronuncable part
				wordx[-1] = ' ';
			}

			// Translate the stem
			end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);

			if(phonemes[0] == phonSWITCH)
			{
				// change to another language in order to translate this word
				strcpy(word_phonemes,phonemes);
				return(0);
			}

			if((phonemes[0] == 0) && (end_phonemes[0] == 0))
			{
				int wc;
				// characters not recognised, speak them individually

				utf8_in(&wc, wordx);
				if((word_length == 1) && IsAlpha(wc))
				{
					if((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word)) == NULL)
					{
						return(0);
					}
				}
			}

			c_temp = wordx[-1];

			found = 0;
			confirm_prefix = 1;
			while(end_type & SUFX_P)
			{
				// Found a standard prefix, remove it and retranslate

				if(confirm_prefix && !(end_type & SUFX_B))
				{
					int end2;
					char phonemes2[N_WORD_PHONEMES];
					char end_phonemes2[N_WORD_PHONEMES];

					// remove any standard suffix and confirm that the prefix is still recognised
					phonemes2[0] = 0;
					end2 = TranslateRules(tr, wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
					if(end2)
					{
						RemoveEnding(tr, wordx, end2, word_copy);
						end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
						memcpy(wordx,word_copy,strlen(word_copy));
						if((end_type & SUFX_P) == 0)
						{
							// after removing the suffix, the prefix is no longer recognised.
							// Keep the suffix, but don't use the prefix
							end_type = end2;
							strcpy(phonemes,phonemes2);
							strcpy(end_phonemes,end_phonemes2);
							if(option_phonemes == 2)
							{
								DecodePhonemes(end_phonemes,end_phonemes2);
								fprintf(f_trans,"  suffix [%s]\n\n",end_phonemes2);
							}
						}
						confirm_prefix = 0;
						continue;
					}
				}

				prefix_type = end_type;

				if(prefix_type & SUFX_V)
				{
					tr->expect_verb = 1;      // use the verb form of the word
				}

				wordx[-1] = c_temp;

				if((prefix_type & SUFX_B) == 0)
				{
					for(ix=(prefix_type & 0xf); ix>0; ix--)    // num. of characters to remove
					{
						wordx++;
						while((*wordx & 0xc0) == 0x80) wordx++;  // for multibyte characters
					}
				}
				else
				{
					pfix = 1;
					prefix_chars[0] = 0;
					n_chars = prefix_type & 0x3f;

					for(ix=0; ix < n_chars; ix++)    // num. of bytes to remove
					{
						prefix_chars[pfix++] = *wordx++;
	
						if((prefix_type & SUFX_B) && (ix == (n_chars-1)))
						{
							prefix_chars[pfix-1] = 0;  // discard the last character of the prefix, this is the separator character
						}
					}
					prefix_chars[pfix] = 0;
				}
				c_temp = wordx[-1];
				wordx[-1] = ' ';
				confirm_prefix = 1;

				if(prefix_type & SUFX_B)
				{
// SUFX_B is used for Turkish, tr_rules contains "(Pb£
					// retranslate the prefix part
					char *wordpf;
					char prefix_phonemes2[12];

					strncpy0(prefix_phonemes2,end_phonemes,sizeof(prefix_phonemes2));
					wordpf = &prefix_chars[1];
					found = LookupDictList(tr, &wordpf, phonemes, dictionary_flags, SUFX_P, wtab);   // without prefix
					if(found == 0)
					{
						end_type = TranslateRules(tr, wordpf, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);
						sprintf(prefix_phonemes,"%s%s%s",phonemes,end_phonemes,prefix_phonemes2);
					}
					prefix_flags = 1;
				}
				else
				{
					strcat(prefix_phonemes,end_phonemes);
				}
				end_phonemes[0] = 0;

				end_type = 0;
				found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, SUFX_P, wtab);   // without prefix
				if(dictionary_flags[0]==0)
				{
					dictionary_flags[0] = dictionary_flags2[0];
					dictionary_flags[1] = dictionary_flags2[1];
				}
				else
					prefix_flags = 1;
				if(found == 0)
				{
					end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);

					if(phonemes[0] == phonSWITCH)
					{
						// change to another language in order to translate this word
						wordx[-1] = c_temp;
						strcpy(word_phonemes,phonemes);
						return(0);
					}
				}
			}

			if((end_type != 0) && !(end_type & SUFX_P))
			{
char phonemes2[N_WORD_PHONEMES];
strcpy(phonemes2,phonemes);

				// The word has a standard ending, re-translate without this ending
				end_flags = RemoveEnding(tr, wordx, end_type, word_copy);

				phonemes[0] = 0;

				if(prefix_phonemes[0] != 0)
				{
					// lookup the stem without the prefix removed
					wordx[-1] = c_temp;
					found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, end_flags, wtab);  // include prefix, but not suffix
					wordx[-1] = ' ';
					if(dictionary_flags[0]==0)
					{
						dictionary_flags[0] = dictionary_flags2[0];
						dictionary_flags[1] = dictionary_flags2[1];
					}
					if(found)
						prefix_phonemes[0] = 0;  // matched whole word, don't need prefix now

					if((found==0) && (dictionary_flags2[0] != 0))
						prefix_flags = 1;
				}
				if(found == 0)
				{
					found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, end_flags, wtab);  // without prefix and suffix
					if(phonemes[0] == phonSWITCH)
					{
						// change to another language in order to translate this word
						memcpy(wordx,word_copy,strlen(word_copy));
						strcpy(word_phonemes,phonemes);
						return(0);
					}

if(dictionary_flags2[0] & FLAG_ABBREV)
{
	// Removing the suffix leaves a word which should be spoken as individual letters
	// Not yet implemented
}
					if(dictionary_flags[0]==0)
					{
						dictionary_flags[0] = dictionary_flags2[0];
						dictionary_flags[1] = dictionary_flags2[1];
					}
				}
				if(found == 0)
				{
					if(end_type & SUFX_Q)
					{
						// don't retranslate, use the original lookup result
						strcpy(phonemes,phonemes2);

						// language specific changes
						ApplySpecialAttribute(tr,phonemes,dictionary_flags[0]);
					}
					else
					{
						if(end_flags & FLAG_SUFX)
							wflags |= FLAG_SUFFIX_REMOVED;
						if(end_type & SUFX_A)
							wflags |= FLAG_SUFFIX_VOWEL;

						TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, NULL, wflags, dictionary_flags);

						if(phonemes[0] == phonSWITCH)
						{
							// change to another language in order to translate this word
							strcpy(word_phonemes,phonemes);
							memcpy(wordx,word_copy,strlen(word_copy));
							wordx[-1] = c_temp;
							return(0);
						}
					}
				}

				if((end_type & SUFX_T) == 0)
				{
					// the default is to add the suffix and then determine the word's stress pattern
					AppendPhonemes(tr,phonemes, N_WORD_PHONEMES, end_phonemes);
					end_phonemes[0] = 0;
				}
				memcpy(wordx,word_copy,strlen(word_copy));
			}
			wordx[-1] = c_temp;
		}
	}

	if((add_plural_suffix) || (wflags & FLAG_HAS_PLURAL))
	{
		// s or 's suffix, append [s], [z] or [Iz] depending on previous letter
		if(last_char == 'f')
			TranslateRules(tr, &word_ss[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
		else
		if((last_char==0) || (strchr_w("hsx",last_char)==NULL))
			TranslateRules(tr, &word_zz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
		else
			TranslateRules(tr, &word_iz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
	}

	wflags |= emphasize_allcaps;


	/* determine stress pattern for this word */
	/******************************************/
	prefix_stress = 0;
	for(p = prefix_phonemes; *p != 0; p++)
	{
		if((*p == phonSTRESS_P) || (*p == phonSTRESS_P2))
		{
			prefix_stress = *p;
		}
	}
	if(prefix_flags || (prefix_stress != 0))
	{
		if((tr->langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T))
		{
			char *p;
			// German, keep a secondary stress on the stem
			SetWordStress(tr, phonemes, dictionary_flags, 3, 0);

			// reduce all but the first primary stress
			ix=0;
			for(p=prefix_phonemes; *p != 0; p++)
			{
				if(*p == phonSTRESS_P)
				{
					if(ix==0)
						ix=1;
					else
						*p = phonSTRESS_3;
				}
			}
#ifdef PLATFORM_WINDOWS
			sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#else
			snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#endif
			word_phonemes[N_WORD_PHONEMES-1] = 0;
			SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
		}
		else
		{
			// stress position affects the whole word, including prefix
#ifdef PLATFORM_WINDOWS
			sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#else
			snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#endif
			word_phonemes[N_WORD_PHONEMES-1] = 0;
			SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
		}
	}
	else
	{
		if(prefix_phonemes[0] == 0)
			SetWordStress(tr, phonemes, dictionary_flags, -1, 0);
		else
			SetWordStress(tr, phonemes, dictionary_flags, -1, 0);
#ifdef PLATFORM_WINDOWS
		sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#else
		snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
#endif
		word_phonemes[N_WORD_PHONEMES-1] = 0;
	}

	if(end_phonemes[0] != 0)
	{
		// a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
		ix = strlen(word_phonemes);
		end_phonemes[N_WORD_PHONEMES-1-ix] = 0;   // ensure no buffer overflow
		strcpy(&word_phonemes[ix], end_phonemes);
	}

	if(wflags & FLAG_LAST_WORD)
	{
		// don't use $brk pause before the last word of a sentence
		// (but allow it for emphasis, see below
		dictionary_flags[0] &= ~FLAG_PAUSE1;
	}

	if((wflags & FLAG_HYPHEN) && (tr->langopts.stress_flags & S_HYPEN_UNSTRESS))
	{
		ChangeWordStress(tr,word_phonemes,3);
	}
	else
	if(wflags & FLAG_EMPHASIZED2)
	{
		// A word is indicated in the source text as stressed
		// Give it stress level 6 (for the intonation module)
		ChangeWordStress(tr,word_phonemes,6);

		if(wflags & FLAG_EMPHASIZED)
			dictionary_flags[0] |= FLAG_PAUSE1;   // precede by short pause
	}
	else
	if(wtab[dictionary_skipwords].flags & FLAG_LAST_WORD)
	{
		// the word has attribute to stress or unstress when at end of clause
		if(dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
			ChangeWordStress(tr,word_phonemes,4);
		else
		if((dictionary_flags[0] & FLAG_UNSTRESS_END) && (any_stressed_words))
			ChangeWordStress(tr,word_phonemes,3);
	}

	// dictionary flags for this word give a clue about which alternative pronunciations of
	// following words to use.
	if(end_type & SUFX_F)
	{
		// expect a verb form, with or without -s suffix
		tr->expect_verb = 2;
		tr->expect_verb_s = 2;
	}

	if(dictionary_flags[1] & FLAG_PASTF)
	{
		/* expect perfect tense in next two words */
		tr->expect_past = 3;
		tr->expect_verb = 0;
		tr->expect_noun = 0;
	}
	else
	if(dictionary_flags[1] & FLAG_VERBF)
	{
		/* expect a verb in the next word */
		tr->expect_verb = 2;
		tr->expect_verb_s = 0;   /* verb won't have -s suffix */
		tr->expect_noun = 0;
	}
	else
	if(dictionary_flags[1] & FLAG_VERBSF)
	{
		// expect a verb, must have a -s suffix
		tr->expect_verb = 0;
		tr->expect_verb_s = 2;
		tr->expect_past = 0;
		tr->expect_noun = 0;
	}
	else
	if(dictionary_flags[1] & FLAG_NOUNF)
	{
		/* not expecting a verb next */
		tr->expect_noun = 2;
		tr->expect_verb = 0;
		tr->expect_verb_s = 0;
		tr->expect_past = 0;
	}

	if((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT)))
	{
		if(tr->expect_verb > 0)
			tr->expect_verb--;

		if(tr->expect_verb_s > 0)
			tr->expect_verb_s--;

		if(tr->expect_noun >0)
			tr->expect_noun--;

		if(tr->expect_past > 0)
			tr->expect_past--;
	}

	if((word_length == 1) && (tr->translator_name == L('e','n')) && iswalpha(first_char) && (first_char != 'i'))
	{
// English Specific !!!!
		// any single letter before a dot is an abbreviation, except 'I'
		dictionary_flags[0] |= FLAG_ALLOW_DOT;
	}

	if((tr->langopts.param[LOPT_ALT] & 2) && ((dictionary_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) != 0))
	{
		ApplySpecialAttribute2(tr,word_phonemes,dictionary_flags[0]);
	}

	return(dictionary_flags[0]);
}  //  end of TranslateWord



static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
{//==========================================================
	p->phcode = phcode;
	p->stresslevel = 0;
	p->tone_ph = 0;
	p->synthflags = embedded_flag;
	p->sourceix = 0;
	embedded_flag = 0;
}

static int CountSyllables(unsigned char *phonemes)
{//===============================================
	int count = 0;
	int phon;
	while((phon = *phonemes++) != 0)
	{
		if(phoneme_tab[phon]->type == phVOWEL)
			count++;
	}
	return(count);
}


int SetTranslator2(const char *new_language)
{//=========================================
// Set translator2 to a second language
	int new_phoneme_tab;

	if((new_phoneme_tab = SelectPhonemeTableName(new_language)) >= 0)
	{
		if((translator2 != NULL) && (strcmp(new_language,translator2_language) != 0))
		{
			// we already have an alternative translator, but not for the required language, delete it
			DeleteTranslator(translator2);
			translator2 = NULL;
		}

		if(translator2 == NULL)
		{
			translator2 = SelectTranslator(new_language);
			strcpy(translator2_language,new_language);

			if(LoadDictionary(translator2, new_language, 0) != 0)
			{
				SelectPhonemeTable(voice->phoneme_tab_ix);  // revert to original phoneme table
				new_phoneme_tab = -1;
				translator2_language[0] = 0;
			}
		}
	}
	return(new_phoneme_tab);
}  // end of SetTranslator2



static int TranslateWord2(Translator *tr, char *word, WORD_TAB *wtab, int pre_pause, int next_pause)
{//=================================================================================================
	int flags=0;
	int stress;
	int next_stress;
	int next_tone=0;
	unsigned char *p;
	int srcix;
	int embedded_cmd;
	int value;
	int found_dict_flag;
	unsigned char ph_code;
	PHONEME_LIST2 *plist2;
	PHONEME_TAB *ph;
	int max_stress;
	int max_stress_ix=0;
	int prev_vowel = -1;
	int pitch_raised = 0;
	int switch_phonemes = -1;
	int first_phoneme = 1;
	int source_ix;
	int len;
	int ix;
	int sylimit;        // max. number of syllables in a word to be combined with a preceding preposition
	const char *new_language;
	unsigned char bad_phoneme[4];
	int word_flags;
	int word_copy_len;
	char word_copy[N_WORD_BYTES+1];
	char old_dictionary_name[40];

	if((f_logespeak != NULL) && (logging_type & 8))
	{
		fprintf(f_logespeak,"WORD: flg=%.5x len=%d  '",wtab->flags,wtab->length);
		for(ix=0; ix<40; ix++)
		{
			if(word[ix]==0) break;
			fputc(word[ix], f_logespeak);
		}
		fprintf(f_logespeak,"'\n");
	}

	len = wtab->length;
	if(len > 31) len = 31;
	source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length

	word_flags = wtab[0].flags;
	if(word_flags & FLAG_EMBEDDED)
	{
		embedded_flag = SFLAG_EMBEDDED;

		do
		{
			embedded_cmd = embedded_list[embedded_read++];
			value = embedded_cmd >> 8;

			switch(embedded_cmd & 0x1f)
			{
			case EMBED_Y:
				option_sayas = value;
				break;
	
			case EMBED_F:
				option_emphasis = value;
				break;
	
			case EMBED_B:
				// break command
				if(value == 0)
					pre_pause = 0;  // break=none
				else
					pre_pause += value;
				break;
			}
		} while((embedded_cmd & 0x80) == 0);
	}

	if(word[0] == 0)
	{
		// nothing to translate
		word_phonemes[0] = 0;
		return(0);
	}

	// after a $pause word attribute, ignore a $pause attribute on the next two words
	if(tr->prepause_timeout > 0)
		tr->prepause_timeout--;

	if((option_sayas & 0xf0) == 0x10)
	{
		if(!(word_flags & FLAG_FIRST_WORD))
		{
			// SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS.  Pause between each word.
			pre_pause += 4;
		}
	}

	if(word_flags & FLAG_FIRST_UPPER)
	{
		if((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6))
		{
			// indicate capital letter by raising pitch
			if(embedded_flag)
				embedded_list[embedded_ix-1] &= ~0x80;   // already embedded command before this word, remove terminator
			if((pitch_raised = option_capitals) == 3)
				pitch_raised = 20;  // default pitch raise for capitals
			embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8);  // raise pitch
			embedded_flag = SFLAG_EMBEDDED;
		}
	}

	p = (unsigned char *)word_phonemes;
	if(word_flags & FLAG_PHONEMES)
	{
		// The input is in phoneme mnemonics, not language text
		int c1;
		char lang_name[12];

		if(memcmp(word,"_^_",3)==0)
		{
			// switch languages
			word+=3;
			for(ix=0;;)
			{
				c1 = *word++;
				if((c1==' ') || (c1==0))
					break;
				lang_name[ix++] = tolower(c1);
			}
			lang_name[ix] = 0;

			if((ix = LookupPhonemeTable(lang_name)) > 0)
			{
				SelectPhonemeTable(ix);
				word_phonemes[0] = phonSWITCH;
				word_phonemes[1] = ix;
				word_phonemes[2] = 0;
			}
		}
		else
		{
			EncodePhonemes(word,word_phonemes,bad_phoneme);
		}
		flags = FLAG_FOUND;
	}
	else
	{
		int c2;
		ix = 0;
		while(((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
		word_copy_len = ix;

		flags = TranslateWord(translator, word, next_pause, wtab);

		if(flags & FLAG_SPELLWORD)
		{
			// re-translate the word as individual letters, separated by spaces
			memcpy(word, word_copy, word_copy_len);
			return(flags);
		}

		if((flags & FLAG_COMBINE) && !(wtab[1].flags & FLAG_PHONEMES))
		{
			char *p2;
			int ok = 1;
			int flags2 = 0;
			int c_word2;
			char ph_buf[N_WORD_PHONEMES];

			sylimit = tr->langopts.param[LOPT_COMBINE_WORDS];

			// LANG=cs,sk
			// combine a preposition with the following word
			p2 = word;
			while(*p2 != ' ') p2++;

			utf8_in(&c_word2, p2+1);   // first character of the next word;
			if(!iswalpha(c_word2))
			{
				ok =0;
			}

			if(ok != 0)
			{
				strcpy(ph_buf,word_phonemes);
				flags2 = TranslateWord(translator, p2+1, 0, wtab+1);

				if(sylimit & 0x100)
				{
					// only if the second word has $alt attribute
					if((flags2 & FLAG_ALT_TRANS) == 0)
					{
						ok = 0;
					}
				}
	
				if((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD))
				{
					// not if the next word is end-of-sentence
					ok = 0;
				}

				if(ok == 0)
				{
					strcpy(word_phonemes,ph_buf);
				}
			}

			if(ok)
			{
				*p2 = '-'; // replace next space by hyphen
				flags = TranslateWord(translator, word, next_pause, wtab);  // translate the combined word
				if((sylimit > 0) && (CountSyllables(p) > (sylimit & 0x1f)))
				{
					// revert to separate words
					*p2 = ' ';
					flags = TranslateWord(translator, word, next_pause, wtab);
				}
				else
				{
					if(flags == 0)
						flags = flags2;   // no flags for the combined word, so use flags from the second word eg. lang-hu "nem december 7-e"
					flags |= FLAG_SKIPWORDS;
					dictionary_skipwords = 1;
				}
			}
		}

		if(p[0] == phonSWITCH)
		{
			// this word uses a different language
			memcpy(word, word_copy, word_copy_len);

			new_language = (char *)(&p[1]);
			if(new_language[0]==0)
				new_language = "en";

			strcpy(old_dictionary_name, dictionary_name);
			switch_phonemes = SetTranslator2(new_language);

			if(switch_phonemes >= 0)
			{
				// re-translate the word using the new translator
				flags = TranslateWord(translator2, word, next_pause, wtab);
//				strcpy((char *)p,translator2->word_phonemes);
				if(p[0] == phonSWITCH)
				{
					// the second translator doesn't want to process this word
					switch_phonemes = -1;
				}
			}
			if(switch_phonemes < 0)
			{
				// language code is not recognised or 2nd translator won't translate it
				p[0] = phonSCHWA;  // just say something
				p[1] = phonSCHWA;
				p[2] = 0;
			}

// ?? Option to convert from language2 phonemes to the equivalent language1 phonemes
// ?? Option to set the word-stress according to language1 rules eg. lang=fr)
			if(ChangeEquivalentPhonemes(tr, switch_phonemes, (char *)p))
			{
				// Phonemes have been converted from the foreign language to the native language
				switch_phonemes = -1;
			}

			if(switch_phonemes == -1)
			{
				strcpy(dictionary_name, old_dictionary_name);
				SelectPhonemeTable(voice->phoneme_tab_ix);

				// leave switch_phonemes set, but use the original phoneme table number.
				// This will suppress LOPT_REGRESSIVE_VOICING
				switch_phonemes = voice->phoneme_tab_ix;   // original phoneme table
			}
		}

		if(!(word_flags & FLAG_HYPHEN))
		{
			if(flags & FLAG_PAUSE1)
			{
				if(pre_pause < 1)
					pre_pause = 1;
			}
			if((flags & FLAG_PREPAUSE) && !(word_flags && (FLAG_LAST_WORD | FLAG_FIRST_WORD)) && !(wtab[-1].flags & FLAG_FIRST_WORD) && (tr->prepause_timeout == 0))
			{
				// the word is marked in the dictionary list with $pause
				if(pre_pause < 4) pre_pause = 4;
				tr->prepause_timeout = 3;
			}
		}

		if((option_emphasis >= 3) && (pre_pause < 1))
			pre_pause = 1;
	}

	plist2 = &ph_list2[n_ph_list2];
	stress = 0;
	next_stress = 1;
	srcix = 0;
	max_stress = -1;

	found_dict_flag = 0;
	if((flags & FLAG_FOUND) && !(flags & FLAG_TEXTMODE))
		found_dict_flag = SFLAG_DICTIONARY;

	while((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4))
	{
		// add pause phonemes here. Either because of punctuation (brackets or quotes) in the
		// text, or because the word is marked in the dictionary lookup as a conjunction
		if(pre_pause > 1)
		{
			SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE);
			pre_pause -= 2;
		}
		else
		{
			SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_NOLINK);
			pre_pause--;
		}
		tr->end_stressed_vowel = 0;   // forget about the previous word
		tr->prev_dict_flags = 0;
	}

	if((option_capitals==1) && (word_flags & FLAG_FIRST_UPPER))
	{
		SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
		SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
		if((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1]))
		{
			// word > 1 letter and all capitals
			SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
			SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
		}
	}

	if(switch_phonemes >= 0)
	{
		// this word uses a different phoneme table
		SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
		ph_list2[n_ph_list2++].tone_ph = switch_phonemes;  // temporary phoneme table number
	}

	// remove initial pause from a word if it follows a hyphen
	if((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
		p++;

	if((p[0] == 0) && (embedded_flag))
	{
		// no phonemes.  Insert a very short pause to carry an embedded command
		p[0] = phonPAUSE_VSHORT;
		p[1] = 0;
	}

	while(((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4))
	{
		if(ph_code == 255)
			continue;      // unknown phoneme

		// Add the phonemes to the first stage phoneme list (ph_list2)
		ph = phoneme_tab[ph_code];

		if(ph_code == phonSWITCH)
		{
			ph_list2[n_ph_list2].phcode = ph_code;
			ph_list2[n_ph_list2].sourceix = 0;
			ph_list2[n_ph_list2].synthflags = embedded_flag;
			ph_list2[n_ph_list2++].tone_ph = *p++;
		}
		else
		if(ph->type == phSTRESS)
		{
			// don't add stress phonemes codes to the list, but give their stress
			// value to the next vowel phoneme
			// std_length is used to hold stress number or (if >10) a tone number for a tone language
			if(ph->program == 0)
				next_stress = ph->std_length;
			else
			{
				// for tone languages, the tone number for a syllable follows the vowel
				if(prev_vowel >= 0)
				{
					ph_list2[prev_vowel].tone_ph = ph_code;
				}
				else
				{
					next_tone = ph_code;       // no previous vowel, apply to the next vowel
				}
			}
		}
		else
		if(ph_code == phonSYLLABIC)
		{
			// mark the previous phoneme as a syllabic consonant
			prev_vowel = n_ph_list2-1;
			ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
			ph_list2[prev_vowel].stresslevel = next_stress;
		}
		else
		if(ph_code == phonLENGTHEN)
		{
			ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
		}
		else
		if(ph_code == phonEND_WORD)
		{
			// a || symbol in a phoneme string was used to indicate a word boundary
			// Don't add this phoneme to the list, but make sure the next phoneme has
			// a newword indication
			srcix = source_ix+1;
		}
		else
		if(ph_code == phonX1)
		{
			// a language specific action 
			if(tr->langopts.param[LOPT_IT_DOUBLING])
			{
				flags |= FLAG_DOUBLING;
			}
		}
		else
		{
			ph_list2[n_ph_list2].phcode = ph_code;
			ph_list2[n_ph_list2].tone_ph = 0;
			ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
			embedded_flag = 0;
			ph_list2[n_ph_list2].sourceix = srcix;
			srcix = 0;

			if(ph->type == phVOWEL)
			{
				stress = next_stress;
				next_stress = 1;  // default is 'unstressed'

				if(stress >= 4)
				{
					any_stressed_words = 1;
				}

				if((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
					ph_list2[n_ph_list2-1].stresslevel = stress;  // set stress for previous consonant

				ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
				prev_vowel = n_ph_list2;

				if(stress > max_stress)
				{
					max_stress = stress;
					max_stress_ix = n_ph_list2;
				}
				if(next_tone != 0)
				{
					ph_list2[n_ph_list2].tone_ph = next_tone;
					next_tone=0;
				}
			}
			else
			{
				if(first_phoneme && tr->langopts.param[LOPT_IT_DOUBLING])
				{
					if(((tr->prev_dict_flags & FLAG_DOUBLING) && (tr->langopts.param[LOPT_IT_DOUBLING] & 1)) || 
						(tr->end_stressed_vowel && (tr->langopts.param[LOPT_IT_DOUBLING] & 2)))
					{
						// italian, double the initial consonant if the previous word ends with a
						// stressed vowel, or is marked with a flag
						ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
					}
				}
			}

			ph_list2[n_ph_list2].stresslevel = stress;
			n_ph_list2++;
			first_phoneme = 0;
		}
	}

	if(word_flags & FLAG_COMMA_AFTER)
	{
		SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_CLAUSE);
	}

	// don't set new-word if there is a hyphen before it
	if((word_flags & FLAG_HYPHEN) == 0)
	{
		plist2->sourceix = source_ix;
	}

	tr->end_stressed_vowel = 0;
	if((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
	{
		tr->end_stressed_vowel = 1;   // word ends with a stressed vowel
	}

	if(switch_phonemes >= 0)
	{
		// this word uses a different phoneme table, now switch back
		strcpy(dictionary_name, old_dictionary_name);
		SelectPhonemeTable(voice->phoneme_tab_ix);
		SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
		ph_list2[n_ph_list2++].tone_ph = voice->phoneme_tab_ix;  // original phoneme table number
	}


	if(pitch_raised > 0)
	{
		embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8);  // lower pitch
		SetPlist2(&ph_list2[n_ph_list2],phonPAUSE_SHORT);
		ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
	}

	if(flags & FLAG_STRESS_END2)
	{
		// this's word's stress could be increased later
		ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
	}

	tr->prev_dict_flags = flags;
	return(flags);
}  //  end of TranslateWord2



static int EmbeddedCommand(unsigned int &source_index)
{//===================================================
	// An embedded command to change the pitch, volume, etc.
	// returns number of commands added to embedded_list

	// pitch,speed,amplitude,expression,reverb,tone,voice,sayas
	const char *commands = "PSARHTIVYMUBF";
	int value = -1;
	int sign = 0;
	unsigned char c;
	char *p;
	int cmd;

	c = source[source_index];
	if(c == '+')
	{
		sign = 0x40;
		source_index++;
	}
	else
	if(c == '-')
	{
		sign = 0x60;
		source_index++;
	}

	if(isdigit(source[source_index]))
	{
		value = atoi(&source[source_index]);
		while(isdigit(source[source_index]))
			source_index++;
	}

	c = source[source_index++];
	if(embedded_ix >= (N_EMBEDDED_LIST - 2))
		return(0);  // list is full

	if((p = strchr_w(commands,c)) == NULL)
		return(0);
	cmd = (p - commands)+1;
	if(value == -1)
	{
		value = embedded_default[cmd];
		sign = 0;
	}

	if(cmd == EMBED_Y)
	{
		option_sayas2 = value;
		count_sayas_digits = 0;
	}
	if(cmd == EMBED_F)
	{
		if(value >= 3)
			word_emphasis = FLAG_EMPHASIZED;
		else
			word_emphasis = 0;
	}

	embedded_list[embedded_ix++] = cmd + sign + (value << 8);
	return(1);
}  //  end of EmbeddedCommand



static int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, int *insert)
{//=========================================================================================
	int ix;
	unsigned int word;
	unsigned int new_c, c2, c_lower;
	int upper_case = 0;
	static int ignore_next = 0;
	const unsigned int *replace_chars;

	if(ignore_next)
	{
		ignore_next = 0;
		return(8);
	}
	if(c == 0) return(0);

	if((replace_chars = tr->langopts.replace_chars) == NULL)
		return(c);

	// there is a list of character codes to be substituted with alternative codes

	if(iswupper(c_lower = c))
	{
		c_lower = towlower(c);
		upper_case = 1;
	}

	new_c = 0;
	for(ix=0; (word = replace_chars[ix]) != 0; ix+=2)
	{
		if(c_lower == (word & 0xffff))
		{
			if((word >> 16) == 0)
			{
				new_c = replace_chars[ix+1];
				break;
			}
			if((word >> 16) == (unsigned int)towlower(next_in))
			{
				new_c = replace_chars[ix+1];
				ignore_next = 1;
				break;
			}
		}
	}

	if(new_c == 0)
		return(c);    // no substitution

	if(new_c & 0xffe00000)
	{
		// there is a second character to be inserted
		// don't convert the case of the second character unless the next letter is also upper case
		c2 = new_c >> 16;
		if(upper_case && iswupper(next_in))
			c2 = towupper(c2);
		*insert = c2;
		new_c &= 0xffff;
	}

	if(upper_case)
		new_c = towupper(new_c);
	return(new_c);

}


static int TranslateChar(Translator *tr, char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert)
{//================================================================================================================
	// To allow language specific examination and replacement of characters

	int code;
	int initial;
	int medial;
	int final;
	int next2;

	static const unsigned char hangul_compatibility[0x34] = {
	 0,  0x00,0x01,0xaa,0x02,0xac,0xad,0x03,
	0x04,0x05,0xb0,0xb1,0xb2,0xb3,0xb4,0xb4,
	0xb6,0x06,0x07,0x08,0xb9,0x09,0x0a,0xbc,
	0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x61,
	0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
	0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,
	0x72,0x73,0x74,0x75 };

	switch(tr->translator_name)
	{
	case L('a','f'):
	case L('n','l'):
	// look for 'n  and replace by a special character (unicode: schwa)


		if(!iswalpha(prev_in))
		{
			utf8_in(&next2, &ptr[1]);

			if((c == '\'') && IsSpace(next2))
			{
				if((next_in == 'n') && (tr->translator_name == L('a','f')))
				{
					// n preceded by either apostrophe or U2019 "right single quotation mark"
					ptr[0] = ' ';  // delete the  n
					return(0x0259); // replace  '  by  unicode schwa character
				}
				if((next_in == 'n') || (next_in == 't'))
				{
					// Dutch, [@n] and [@t]
					return(0x0259); // replace  '  by  unicode schwa character
				}
			}
		}
		break;

	case L('k','o'):
		if(((code = c - 0xac00) >= 0) && (c <= 0xd7af))
		{
			// break a syllable hangul into 2 or 3 individual jamo
			initial = (code/28)/21;
			medial = (code/28) % 21;
			final = code % 28;

			if(initial == 11)
			{
				// null initial
				c = medial + 0x1161;
				if(final > 0)
					*insert = final + 0x11a7;
			}
			else
			{
				// extact the initial and insert the remainder with a null initial
				c = initial + 0x1100;
				*insert = (11*28*21) + (medial*28) + final + 0xac00;
			}
			return(c);
		}
		else
		if(((code = c - 0x3130) >= 0) && (code < 0x34))
		{
			// Hangul compatibility jamo
			return(hangul_compatibility[code] + 0x1100);
		}
		break;
	}
	return(SubstituteChar(tr,c,next_in,insert));
}


void *TranslateClause(Translator *tr, FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
{//==========================================================================================================
	int ix;
	int c;
	int cc;
	unsigned int source_index=0;
	unsigned int prev_source_index=0;
	int prev_in;
	int prev_out=' ';
	int prev_out2;
	int prev_in2=0;
	int prev_in_save=0;
	int next_in;
	int next_in_nbytes;
	int char_inserted=0;
	int clause_pause;
	int pre_pause_add=0;
	int word_mark = 0;
	int all_upper_case=FLAG_ALL_UPPER;
	int finished;
	int single_quoted;
	int phoneme_mode = 0;
	int dict_flags = 0;        // returned from dictionary lookup
	int word_flags;        // set here
	int next_word_flags;
	int embedded_count = 0;
	int letter_count = 0;
	int space_inserted = 0;
	int syllable_marked = 0;
	int decimal_sep_count = 0;
	char *word;
	char *p;
	int j, k;
	int n_digits;
	int individual_digits;
	int charix_top=0;

	short charix[N_TR_SOURCE+4];
	WORD_TAB words[N_CLAUSE_WORDS];
	static char voice_change_name[40];
	int word_count=0;      // index into words

	char sbuf[N_TR_SOURCE];

	int terminator;
	int tone;
	int tone2;

	p_textinput = (unsigned char *)vp_input;
	p_wchar_input = (wchar_t *)vp_input;

	embedded_ix = 0;
	embedded_read = 0;
	pre_pause = 0;
	any_stressed_words = 0;

	if((clause_start_char = count_characters) < 0)
		clause_start_char = 0;
	clause_start_word = count_words + 1;

	for(ix=0; ix<N_TR_SOURCE; ix++)
		charix[ix] = 0;
	terminator = ReadClause(tr, f_text, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name);

	if((f_logespeak != NULL) && (logging_type & 4))
	{
		fprintf(f_logespeak,"CLAUSE %x:\n",terminator);
		for(p=source; *p != 0; p++)
			fputc(*p, f_logespeak);
		fprintf(f_logespeak,"ENDCLAUSE\n");
		fflush(f_logespeak);
	}
p = source;

	charix[charix_top+1] = 0;
	charix[charix_top+2] = 0x7fff;
	charix[charix_top+3] = 0;

	clause_pause = (terminator & 0xfff) * 10;  // mS
	tone = (terminator >> 12) & 0x7;
	if(tone2 != 0)
	{
		// override the tone type
		tone = tone2;
	}

	for(p=source; *p != 0; p++)
	{
		if(!isspace2(*p))
		{
			break;
		}
	}
	if(*p == 0)
	{
		// No characters except spaces. This is not a sentence.
		// Don't add this pause, just make up the previous pause to this value;
		clause_pause -= max_clause_pause;
		if(clause_pause < 0)
			clause_pause = 0;

		terminator &= ~CLAUSE_BIT_SENTENCE;  // clear sentence bit
		max_clause_pause += clause_pause;
	}
	else
	{
		max_clause_pause = clause_pause;
	}
	tr->clause_terminator = terminator;

	if(new_sentence)
	{
		count_sentences++;
		if(skip_sentences > 0)
		{
			skip_sentences--;
			if(skip_sentences == 0)
				skipping_text = 0;
		}
	}

	memset(&ph_list2[0],0,sizeof(ph_list2[0]));
	ph_list2[0].phcode = phonPAUSE_SHORT;

	n_ph_list2 = 1;
	tr->prev_last_stress = 0;
	tr->prepause_timeout = 0;
	tr->expect_verb=0;
	tr->expect_noun=0;
	tr->expect_past=0;
	tr->expect_verb_s=0;
	tr->phonemes_repeat_count = 0;
	tr->end_stressed_vowel=0;
	tr->prev_dict_flags = 0;

	word_count = 0;
	single_quoted = 0;
	word_flags = 0;
	next_word_flags = 0;

	sbuf[0] = 0;
	sbuf[1] = ' ';
	sbuf[2] = ' ';
	ix = 3;
	prev_in = ' ';

	words[0].start = ix;
	words[0].flags = 0;
	finished = 0;

	for(j=0; charix[j]==0; j++);
	words[0].sourceix = charix[j];
	k = 0;
	while(charix[j] != 0)
	{
		// count the number of characters (excluding multibyte continuation bytes)
		if(charix[j++] != -1)
			k++;
	}
	words[0].length = k;

	while(!finished && (ix < (int)sizeof(sbuf))&& (n_ph_list2 < N_PHONEME_LIST-4))
	{
		prev_out2 = prev_out;
		utf8_in2(&prev_out,&sbuf[ix-1],1);   // prev_out = sbuf[ix-1];

		if(tr->langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2))
		{
			// tone numbers can be part of a word, consider them as alphabetic
			prev_out = 'a';
		}

		if(prev_in_save != 0)
		{
			prev_in = prev_in_save;
			prev_in_save = 0;
		}
		else
		if(source_index > 0)
		{
			prev_in2 = prev_in;
			utf8_in2(&prev_in,&source[source_index-1],1);  //  prev_in = source[source_index-1];
		}

		prev_source_index = source_index;

		if(char_inserted)
		{
			c = char_inserted;
			char_inserted = 0;
		}
		else
		{
			source_index += utf8_in(&cc,&source[source_index]);   // cc = source[source_index++];
			c = cc;
		}
		next_in_nbytes = utf8_in(&next_in,&source[source_index]);

		if(c == 0)
		{
			finished = 1;
			c = ' ';
		}

		if((c == CTRL_EMBEDDED) || (c == ctrl_embedded))
		{
			// start of embedded command in the text
			int srcix = source_index-1;

			if(prev_in != ' ')
			{
				c = ' ';
				prev_in_save = c;
				source_index--;
			}
			else
			{
				embedded_count += EmbeddedCommand(source_index);
				prev_in_save = prev_in;
				// replace the embedded command by spaces
				memset(&source[srcix],' ',source_index-srcix);
				source_index = srcix;
				continue;
			}
		}

		if((option_sayas2 == SAYAS_KEY) && (c != ' '))
		{
			if((prev_in == ' ') && (next_in == ' '))
				option_sayas2 = SAYAS_SINGLE_CHARS;   // single character, speak its name
			c = towlower2(c);
		}


		if(phoneme_mode)
		{
			all_upper_case = FLAG_PHONEMES;

			if((c == ']') && (next_in == ']'))
			{
				phoneme_mode = 0;
				source_index++;
				c = ' ';
			}
		}
		else
		if((option_sayas2 & 0xf0) == SAYAS_DIGITS)
		{
			if(iswdigit(c))
			{
				count_sayas_digits++;
				if(count_sayas_digits > (option_sayas2 & 0xf))
				{
					// break after the specified number of digits
					c = ' ';
					space_inserted = 1;
					count_sayas_digits = 0;
				}
			}
			else
			{
				count_sayas_digits = 0;
				if(iswdigit(prev_out))
				{
					c = ' ';
					space_inserted = 1;
				}
			}
		}
		else
		if((option_sayas2 & 0x10) == 0)
		{
			// speak as words

#ifdef deleted
if((c == '/') && (tr->langopts.testing & 2) && IsDigit09(next_in) && IsAlpha(prev_out))
{
	// TESTING, explicit indication of stressed syllable by /2 after the word
	word_mark = next_in-'0';
	source_index++;
	c = ' ';
}
#endif
			if((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
				c = '\'';    // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe 

			if(((c == 0x2018) || (c == '?')) && IsAlpha(prev_out) && IsAlpha(next_in))
			{
				// ? between two letters may be a smart-quote replaced by ?
				c = '\'';
			}

			if(c == CHAR_EMPHASIS)
			{
				// this character is a marker that the previous word is the focus of the clause
				c = ' ';
				word_flags |= FLAG_FOCUS;
			}

			if(c == CHAR_COMMA_BREAK)
			{
				c = ' ';
				word_flags |= FLAG_COMMA_AFTER;
			}

			c = TranslateChar(tr, &source[source_index], prev_in,c, next_in, &char_inserted);  // optional language specific function
			if(c == 8)
				continue;  // ignore this character

			if(char_inserted)
				next_in = char_inserted;

			// allow certain punctuation within a word (usually only apostrophe)
			if(!IsAlpha(c) && !IsSpace(c) && (wcschr(tr->punct_within_word,c) == 0))
			{
				if(IsAlpha(prev_out))
				{
					if(tr->langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in))
					{
						// allow a tone number as part of the word
					}
					else
					{
						c = ' ';   // ensure we have an end-of-word terminator
						space_inserted = 1;
					}
				}
			}

			if(iswdigit(prev_out))
			{
				if(!iswdigit(c) && (c != '.') && (c != ',') && (c != ' '))
				{
					c = ' ';   // terminate digit string with a space
					space_inserted = 1;
				}
			}
			else
			{
				if(prev_in != ',')
				{
					decimal_sep_count = 0;
				}
			}

			if(c == '[')
			{
				if((next_in == '\002') || ((next_in == '[') && option_phoneme_input))
				{
					//  "[\002" is used internally to start phoneme mode  
					phoneme_mode = FLAG_PHONEMES;
					source_index++;
					continue;
				}
			}

			if(IsAlpha(c))
			{
				if(!IsAlpha(prev_out) || (tr->langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040))))
				{
					if(wcschr(tr->punct_within_word,prev_out) == 0)
						letter_count = 0;    // don't reset count for an apostrophy within a word

					if((prev_out != ' ') && (wcschr(tr->punct_within_word,prev_out) == 0))
					{
						// start of word, insert space if not one there already
						c = ' ';
						space_inserted = 1;

						if(!IsBracket(prev_out))    // ?? perhaps only set FLAG_NOSPACE for . - /  (hyphenated words, URLs, etc)
						{
							next_word_flags |= FLAG_NOSPACE;
						}
					}
					else
					{
						if(iswupper(c))
							word_flags |= FLAG_FIRST_UPPER;

						if((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in))
						{
							// word, following a number, but with a space between
							// Add an extra space, to distinguish "2 a" from "2a"
							sbuf[ix++] = ' ';
							words[word_count].start++;
						}
					}
				}

				letter_count++;

				if(iswupper(c))
				{
					c = towlower2(c);

					if((j = tr->langopts.param[LOPT_CAPS_IN_WORD]) > 0)
					{
						if((j == 2) && (syllable_marked == 0))
						{
							char_inserted = c;
							c = 0x2c8;   // stress marker
							syllable_marked = 1;
						}
					}
					else
					{
						if(iswlower(prev_in))
						{
							c = ' ';      // lower case followed by upper case, treat as new word
							space_inserted = 1;
							prev_in_save = c;
//							next_word_flags |= FLAG_NOSPACE;  // problem: prevents FLAG_HAS_DOT being set
						}
						else
						if((c != ' ') && iswupper(prev_in) && iswlower(next_in))
						{
							int next2_in;
							utf8_in(&next2_in,&source[source_index + next_in_nbytes]);
							if(IsAlpha(next2_in))
							{
								// changing from upper to lower case, start new word at the last uppercase, if 3 or more letters
								c = ' ';
								space_inserted = 1;
								prev_in_save = c;
								next_word_flags |= FLAG_NOSPACE;
							}
						}
					}
				}
				else
				{
					if((all_upper_case) && (letter_count > 2))
					{
						if((c == 's') && (next_in==' '))
						{
							c = ' ';
							all_upper_case |= FLAG_HAS_PLURAL;

							if(sbuf[ix-1] == '\'')
								sbuf[ix-1] = ' ';
						}
						else
							all_upper_case = 0;  // current word contains lower case letters, not "'s"
					}
					else
						all_upper_case = 0;
				}
			}
			else
			if(c=='-')
			{
				if(!IsSpace(prev_in) && IsAlpha(next_in))
				{
					if(prev_out != ' ')
					{	
						// previous 'word' not yet ended (not alpha or numeric), start new word now.
						c = ' ';
						space_inserted = 1;
					}
					else
					{
						// '-' between two letters is a hyphen, treat as a space
						word_flags |= FLAG_HYPHEN;
						words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
						c = ' ';
					}
				}
				else
				if((prev_in==' ') && (next_in==' '))
				{
					// ' - ' dash between two spaces, treat as pause
					c = ' ';
					pre_pause_add = 4;
				}
				else
				if(next_in=='-')
				{
					// double hyphen, treat as pause
					source_index++;
					c = ' ';
					pre_pause_add = 4;
				}
				else
				if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
				{
					// insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
					sbuf[ix++] = ' ';
					words[word_count].start++;
				}
			}
			else
			if(c == '.')
			{
				if(!(words[word_count-1].flags & FLAG_NOSPACE) && IsAlpha(prev_in))
				{
					// dot after a word, with space following, probably an abbreviation
					words[word_count-1].flags |= FLAG_HAS_DOT;

					if(IsSpace(next_in))
						c = ' ';   // remove the dot if it's followed by a space, so that it's not pronounced
				}
			}
			else
			if(c == '\'')
			{
				if(((prev_in == '.') || iswalnum(prev_in)) && IsAlpha(next_in))
				{
					// between two letters, or in an abbreviation (eg. u.s.a.'s). Consider the apostrophe as part of the word
					single_quoted = 0;
				}
				else
				if((wcschr(tr->char_plus_apostrophe,prev_in) != 0) && (prev_out2 == ' '))
				{
					// consider single character plus apostrophe as a word
					single_quoted = 0;
					if(next_in == ' ')
					{
						source_index++;  // skip following space
					}
				}
				else
				{
					if((prev_out == 's') && (single_quoted==0))
					{
						// looks like apostrophe after an 's'
						c = ' ';
					}
					else
					{
						if(IsSpace(prev_out))
							single_quoted = 1;
						else
							single_quoted = 0;

						pre_pause_add = 4;   // single quote
						c = ' ';
					}
				}
			}
			else
#ifdef deleted
// Brackets are now recognised in TranslateRules()
			if(IsBracket(c))
			{
				pre_pause_add = 4;
				c = ' ';
			}
			else
#endif
			if(lookupwchar(breaks,c) != 0)
			{
				c = ' ';  // various characters to treat as space
			}
			else
			if(iswdigit(c))
			{
				if(tr->langopts.tone_numbers && IsAlpha(prev_out) && !IsDigit(next_in))
				{
				}
				else
				if((prev_out != ' ') && !iswdigit(prev_out))
				{
					if((prev_out != tr->langopts.decimal_sep) || ((decimal_sep_count > 0) && (tr->langopts.decimal_sep == ',')))
					{
						c = ' ';
						space_inserted = 1;
					}
					else
					{
						decimal_sep_count = 1;
					}
				}
				else
				if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
				{
					// insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
					sbuf[ix++] = ' ';
					words[word_count].start++;
				}
			}
		}

		if(IsSpace(c))
		{
			if(prev_out == ' ')
			{
				word_flags |= FLAG_MULTIPLE_SPACES;
				continue;   // multiple spaces
			}

			if((cc == 0x09) || (cc == 0x0a))
			{
				next_word_flags |= FLAG_MULTIPLE_SPACES;   // tab or newline, not a simple space
			}

			if(space_inserted)
			{
				words[word_count].length = source_index - words[word_count].sourceix;
			}

			// end of 'word'
			sbuf[ix++] = ' ';

			if((ix > words[word_count].start) && (word_count < N_CLAUSE_WORDS-1))
			{
				if(embedded_count > 0)
				{
					// there are embedded commands before this word
					embedded_list[embedded_ix-1] |= 0x80;   // terminate list of commands for this word
					words[word_count].flags |= FLAG_EMBEDDED;
					embedded_count = 0;
				}
				words[word_count].pre_pause = pre_pause;
				words[word_count].flags |= (all_upper_case | word_flags | word_emphasis);
				words[word_count].wmark = word_mark;

				if(pre_pause > 0)
				{
					// insert an extra space before the word, to prevent influence from previous word across the pause
					for(j=ix; j>words[word_count].start; j--)
					{
						sbuf[j] = sbuf[j-1];
					}
					sbuf[j] = ' ';
					words[word_count].start++;
					ix++;
				}

				word_count++;
				words[word_count].start = ix;
				words[word_count].flags = 0;

				for(j=source_index; charix[j] <= 0; j++);   // skip blanks
				words[word_count].sourceix = charix[j];
				k = 0;
				while(charix[j] != 0)
				{
					// count the number of characters (excluding multibyte continuation bytes)
					if(charix[j++] != -1)
						k++;
				}
				words[word_count].length = k;

				word_flags = next_word_flags;
				next_word_flags = 0;
				pre_pause = 0;
				word_mark = 0;
				all_upper_case = FLAG_ALL_UPPER;
				syllable_marked = 0;
			}

			if(space_inserted)
			{
				source_index = prev_source_index;    // rewind to the previous character
				char_inserted = 0;
				space_inserted = 0;
			}
		}
		else
		{
			ix += utf8_out(c,&sbuf[ix]);   // sbuf[ix++] = c;
		}
		if(pre_pause_add > pre_pause)
			pre_pause = pre_pause_add;
		pre_pause_add = 0;
	}

	if((word_count==0) && (embedded_count > 0))
	{
		// add a null 'word' to carry the embedded command flag
		embedded_list[embedded_ix-1] |= 0x80; 
		words[word_count].flags |= FLAG_EMBEDDED;
		word_count = 1;
	}

	tr->clause_end = &sbuf[ix-1];
	sbuf[ix] = 0;
	words[0].pre_pause = 0;  // don't add extra pause at beginning of clause
	words[word_count].pre_pause = 8;
	if(word_count > 0)
	{
		ix = word_count-1;
		while((ix > 0) && (IsBracket(sbuf[words[ix].start])))
			ix--;  // the last word is a bracket, mark the previous word as last
		words[ix].flags |= FLAG_LAST_WORD;

		// FLAG_NOSPACE check to avoid recognizing  .mr  -mr 
		if((terminator & CLAUSE_DOT) && !(words[word_count-1].flags & FLAG_NOSPACE))
			words[word_count-1].flags |= FLAG_HAS_DOT;
	}
	words[0].flags |= FLAG_FIRST_WORD;


	for(ix=0; ix < word_count; ix++)
	{
		int nx;
		int c_temp;
		char *pn;
		char *pw;
		int nw;
		char number_buf[150];
		WORD_TAB num_wtab[50];  // copy of 'words', when splitting numbers into parts

		// start speaking at a specified word position in the text?
		count_words++;
		if(skip_words > 0)
		{
			skip_words--;
			if(skip_words == 0)
				skipping_text = 0;
		}
		if(skipping_text)
			continue;


		// digits should have been converted to Latin alphabet ('0' to '9')
		word = pw = &sbuf[words[ix].start];

		if(iswdigit(word[0]) && (tr->langopts.break_numbers != BREAK_THOUSANDS))
		{
			// Languages with 100000 numbers.  Remove thousands separators so that we can insert them again later
			pn = number_buf;
			while(pn < &number_buf[sizeof(number_buf)-20])
			{
				if(iswdigit(*pw))
				{
					*pn++ = *pw++;
				}
				else
				if((*pw == tr->langopts.thousands_sep) && (pw[1] == ' ') && iswdigit(pw[2]))
				{
					pw += 2;
					ix++;  // skip "word"
				}
				else
				{
					nx = pw - word;
					memset(word,' ',nx);
					nx = pn - number_buf;
					memcpy(word,number_buf,nx);
					break;
				}
			}
			pw = word;
		}

		for(n_digits=0; iswdigit(word[n_digits]); n_digits++);  // count consecutive digits

		if(n_digits > 4)
		{
			// word is entirely digits, insert commas and break into 3 digit "words"
			number_buf[0] = ' ';
			pn = &number_buf[1];
			nx = n_digits;
			nw = 0;
			individual_digits = 0;

			if((n_digits > tr->langopts.max_digits) || (word[0] == '0'))
				words[ix].flags |= FLAG_INDIVIDUAL_DIGITS;

			while(pn < &number_buf[sizeof(number_buf)-20])
			{
				if(!isdigit(c = *pw++) && (c != tr->langopts.decimal_sep))
					break;

				*pn++ = c;
				if((--nx > 0) && (tr->langopts.break_numbers & (1 << nx)))
				{
					memcpy(&num_wtab[nw++], &words[ix], sizeof(WORD_TAB));   // copy the 'words' entry for each word of numbers

					if(tr->langopts.thousands_sep != ' ')
					{
						*pn++ = tr->langopts.thousands_sep;
					}
					*pn++ = ' ';
					if(tr->langopts.break_numbers & (1 << (nx-1)))
					{
						// the next group only has 1 digits (i.e. NUM2_10000), make it three
						*pn++ = '0';
						*pn++ = '0';
					}
					if(tr->langopts.break_numbers & (1 << (nx-2)))
					{
						// the next group only has 2 digits (i.e. NUM2_10000), make it three
						*pn++ = '0';
					}
				}
			}
			pw--;
			memcpy(&num_wtab[nw], &words[ix], sizeof(WORD_TAB)*2);    // the original number word, and the word after it

			for(j=1; j<=nw; j++)
			{
					num_wtab[j].flags &= ~(FLAG_MULTIPLE_SPACES | FLAG_EMBEDDED);     // don't use these flags for subsequent parts when splitting a number
			}

			// include the next few characters, in case there are an ordinal indicator or other suffix
			memcpy(pn, pw, 16);
			pn[16] = 0;
			nw = 0;

			for(pw = &number_buf[1]; pw < pn;)
			{
				// keep wflags for each part, for FLAG_HYPHEN_AFTER
				dict_flags = TranslateWord2(tr, pw, &num_wtab[nw++], words[ix].pre_pause,0 );
				while(*pw++ != ' ');
				words[ix].pre_pause = 0;
			}
		}
		else
		{
			pre_pause = 0;
			dict_flags = TranslateWord2(tr, word, &words[ix], words[ix].pre_pause, words[ix+1].pre_pause);

			if(pre_pause > words[ix+1].pre_pause)
			{
				words[ix+1].pre_pause = pre_pause;
				pre_pause = 0;
			}

			if(dict_flags & FLAG_SPELLWORD)
			{
				// redo the word, speaking single letters
				for(pw = word; *pw != ' ';)
				{
					memset(number_buf,' ',9);
					nx = utf8_in(&c_temp, pw);
					memcpy(&number_buf[2],pw,nx);
					TranslateWord2(tr, &number_buf[2], &words[ix], 0, 0 );
					pw += nx;
				}
			}

			if((dict_flags & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (ix == word_count-1) && (terminator & CLAUSE_DOT))
			{
				// probably an abbreviation such as Mr. or B. rather than end of sentence
				clause_pause = 10;
				tone = 4;
			}
		}

		if(dict_flags & FLAG_SKIPWORDS)
		{
			ix += dictionary_skipwords;  // dictionary indicates skip next word(s)
		}
	}

	for(ix=0; ix<2; ix++)
	{
		// terminate the clause with 2 PAUSE phonemes
		PHONEME_LIST2 *p2;
		p2 = &ph_list2[n_ph_list2 + ix];
		p2->phcode = phonPAUSE;
		p2->stresslevel = 0;
		p2->sourceix = source_index;
		p2->synthflags = 0;
	}
	n_ph_list2 += 2;

	if(count_words == 0)
	{
		clause_pause = 0;
	}
	if(Eof() && ((word_count == 0) || (option_endpause==0)))
	{
		clause_pause = 10;
	}

	MakePhonemeList(tr, clause_pause, new_sentence);

	if(embedded_count)   // ???? is this needed
	{
		phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
		embedded_list[embedded_ix-1] |= 0x80;
	}


	prev_clause_pause = clause_pause;

	*tone_out = tone;

	new_sentence = 0;
	if(terminator & CLAUSE_BIT_SENTENCE)
	{
		new_sentence = 1;  // next clause is a new sentence
	}


	if(voice_change != NULL)
	{
		// return new voice name if an embedded voice change command terminated the clause
		if(terminator & CLAUSE_BIT_VOICE)
			*voice_change = voice_change_name;
		else
			*voice_change = NULL;
	}

	if(Eof() || (vp_input==NULL))
		return(NULL);

	if(option_multibyte == espeakCHARS_WCHAR)
		return((void *)p_wchar_input);
	else
		return((void *)p_textinput);
}  //  end of TranslateClause





void InitText(int control)
{//=======================
	count_sentences = 0;
	count_words = 0;
	end_character_position = 0;
	skip_sentences = 0;
	skip_marker[0] = 0;
	skip_words = 0;
	skip_characters = 0;
	skipping_text = 0;
	new_sentence = 1;

	prev_clause_pause = 0;

	option_sayas = 0;
	option_sayas2 = 0;
	option_emphasis = 0;
	word_emphasis = 0;
	embedded_flag = 0;

	InitText2();

	if((control & espeakKEEP_NAMEDATA) == 0)
	{
		InitNamedata();
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

Jozef Bozek
CEO bring-it-together s.r.o.
Slovakia Slovakia
Jozef Božek is currently a software engineer at bring-it-together s.r.o. in area of large scale infomation systems and mobile applications development.
He has been developing in C++ nearly full time since 2000, in Java since 2004 and in Objective-C since 2009. He is programming using Java EE SDK, iOS SDK, COM/DCOM, MFC, ATL, STL and so on Smile | :)
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Jozef Bozek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid