Click here to Skip to main content
11,416,572 members (68,438 online)
Click here to Skip to main content
Add your own
alternative version

ESpeakEngine - Objective-C speech synthesizer

, 24 Jan 2012 BSD
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, write 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 "translate.h"


int dictionary_skipwords;
char dictionary_name[40];

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

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

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

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

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





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


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

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


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


//=============================================================================================
//   Read pronunciation rules and pronunciation lookup dictionary
//
//=============================================================================================


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

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

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

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

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

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

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

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

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

}  //  end of InitGroups



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

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

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

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

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

	tr->data_dictlist = Alloc(size);
	size = fread(tr->data_dictlist,1,size,f);
	fclose(f);


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

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

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

	// set up indices into data_dictrules
	InitGroups(tr);
	if(tr->groups1[0] == NULL)
	{
		fprintf(stderr,"Error in %s_rules, no default rule group\n",name);
	}

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

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

	return(0);
}  //  end of LoadDictionary


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

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

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



//=============================================================================================
//   Translate between internal representation of phonemes and a mnemonic form for display
//
//=============================================================================================



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

	bad_phoneme[0] = 0;

	// skip initial blanks
	while(isspace(*p))
	{
		p++;
	}

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

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

			if((c = p[1]) == '|')
			{
				// treat double || as a word-break symbol, drop through
            // to the default case with c = '|'
			}
			else
			{
				p++;
				break;
			}

		default:
			// lookup the phoneme mnemonic, find the phoneme with the highest number of
			// matching characters
			max= -1;
			max_ph= 0;

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

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

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

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

			if(max_ph == 0)
			{
				max_ph = 255;   /* not recognised */
				bad_phoneme[0] = *p;
				bad_phoneme[1] = 0;
			}

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

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



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

	while((phcode = *inptr++) > 0)
	{
		if(phcode == 255)
			continue;     /* indicates unrecognised phoneme */
		if((ph = phoneme_tab[phcode]) == NULL)
			continue;
	
		if((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0))
		{
			if(ph->std_length > 1)
				*outptr++ = stress_chars[ph->std_length];
		}
		else
		{
			mnem = ph->mnemonic;

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


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


static void WritePhMnemonic(char *phon_out, int *ix, PHONEME_TAB *ph, PHONEME_LIST *plist)
{//=======================================================================================
	int c;
	int mnem;
	int len;
	int first;
	unsigned int ipa_control=0;  // first byte of ipa string may control the phoneme name interpretation. 0x20 = ignore this phoneme
	PHONEME_DATA phdata;

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

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

		len = strlen(phdata.ipa_string);
		if(len > 0)
		{
			if((ipa_control = phdata.ipa_string[0]) > 0x20)
			{
				strcpy(&phon_out[*ix], phdata.ipa_string);
				*ix += len;
			}
			if(ipa_control >= 0x20)
				return;  // 0x20 = ignore phoneme
		}
	}

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

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

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

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

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

			*ix += utf8_out(c, &phon_out[*ix]);
		}
		else
		{
			phon_out[(*ix)++]= c;
		}
		first = 0;
	}
}  // end of WritePhMnemonic



void GetTranslatedPhonemeString(char *phon_out, int n_phon_out)
{//============================================================
/* Can be called after a clause has been translated into phonemes, in order
   to display the clause in phoneme mnemonic form.
*/

	int  ix;
	int  phon_out_ix=0;
	int  stress;
	unsigned int c;
	char *p;
	PHONEME_LIST *plist;
	
	static const char *stress_chars = "==,,''";

	if(phon_out != NULL)
	{
		for(ix=1; ix<(n_phoneme_list-2) && (phon_out_ix < (n_phon_out - 6)); ix++)
		{
			plist = &phoneme_list[ix];
			if(plist->newword)
				phon_out[phon_out_ix++] = ' ';

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

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

					if(c != 0)
					{
						phon_out_ix += utf8_out(c, &phon_out[phon_out_ix]);
					}
				}
			}

			if(plist->ph->code == phonSWITCH)
			{
				// the tone_ph field contains a phoneme table number
				p = phoneme_tab_list[plist->tone_ph].name;

				sprintf(&phon_out[phon_out_ix], "(%s)", p);
				phon_out_ix += (strlen(p) + 2);
			}
			else
			{
				WritePhMnemonic(phon_out, &phon_out_ix, plist->ph, plist);
	
				if(plist->synthflags & SFLAG_LENGTHEN)
				{
					WritePhMnemonic(phon_out, &phon_out_ix, phoneme_tab[phonLENGTHEN], NULL);
				}
				if((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL))
				{
					// syllablic consonant
					WritePhMnemonic(phon_out, &phon_out_ix, phoneme_tab[phonSYLLABIC], NULL);
				}
				if(plist->tone_ph > 0)
				{
					WritePhMnemonic(phon_out, &phon_out_ix, phoneme_tab[plist->tone_ph], NULL);
				}
			}
		}
	
		if(phon_out_ix >= n_phon_out)
			phon_out_ix = n_phon_out - 1;
		phon_out[phon_out_ix] = 0;
	}
}  // end of GetTranslatedPhonemeString



//=============================================================================================
//   Is a word Unpronouncable - and so should be spoken as individual letters
//
//=============================================================================================



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

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

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

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


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

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

	if(group > 7)
		return(0);

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

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

	return(0);
}


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




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

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


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

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

	int  c;
	int  c1=0;
	int  vowel_posn=9;
	int  index;
	int  count;
	int  apostrophe=0;

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

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

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

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

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

		if(count==0)
			c1 = c;
		count++;

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

		if(c == '\'')
			apostrophe = 1;
		else
		if(!iswalpha(c))
			return(0);
	}

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

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

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

return(0);

}   /* end of Unpronounceable */



//=============================================================================================
//   Determine the stress pattern of a word
//
//=============================================================================================



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

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

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

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

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

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

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

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

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

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

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

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

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

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

	stressed_syllable = primary_posn;
	vowel_count = count;
	return(max_stress);
}  // end of GetVowelStress



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


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

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

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

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

			ix++;
		}
		*word++ = *p++;
	}
	*word = 0;
}  // end of ChangeWordStress



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

   'output' is used for input and output

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

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

  control:  bit 0   This is an individual symbol, not a word
*/

	unsigned char phcode;
	unsigned char *p;
	PHONEME_TAB *ph;
	int  stress;
	int  max_stress;
	int  vowel_count;      // num of vowels + 1
	int  ix;
	int  v;
	int  v_stress;
	int  stressed_syllable;      // position of stressed syllable
	int  max_stress_posn;
	int  unstressed_word = 0;
	char *max_output;
	int final_ph;
	int final_ph2;
	int mnem;
	int mnem2;
	int post_tonic;
	int opt_length;
	int done;
	int stressflags;
	int dflags = 0;

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

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


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

	stressflags = tr->langopts.stress_flags;

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

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

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

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

	max_stress = GetVowelStress(tr, phonetic, vowel_stress, vowel_count, stressed_syllable, 1);
	if((max_stress < 0) && dictionary_flags)
	{
		if((tr->langopts.stress_flags & 1) && (vowel_count == 2))
		{
			// lang=fr: don't stress monosyllables except at end-of-clause
			vowel_stress[1] = 0;
			dictionary_flags[0] |= FLAG_STRESS_END2;
		}
		max_stress = 0;
	}

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

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

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

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

			if(consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG)))
			{
				// followed by two consonants, a long consonant, or consonant and end-of-word
				weight++;
			}
			syllable_weight[ix] = weight;
			ix++;
		}
	}
	
	switch(tr->langopts.stress_rule)
	{
	case 8:
		// stress on first syllable, unless it is a light syllable
		if(syllable_weight[1] > 0)
			break;
		// else drop through to case 1
	case 1:
		// stress on second syllable
		if((stressed_syllable == 0) && (vowel_count > 2))
		{
			stressed_syllable = 2;
			if(max_stress == 0)
			{
				vowel_stress[stressed_syllable] = 4;
			}
			max_stress = 4;
		}
		break;

	case 10:  // penultimate, but final if only 1 or 2 syllables
		if(stressed_syllable == 0)
		{
			if(vowel_count < 4)
			{
				vowel_stress[vowel_count - 1] = 4;
				max_stress = 4;
				break;
			}
		}
		// drop through to next case
	case 2:
		// a language with stress on penultimate vowel

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

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

				if(stressflags & 0x300)
				{
					// LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
					if(phoneme_tab[final_ph]->type != phVOWEL)
					{
						if(stressflags & 0x100)
						{
							stressed_syllable = vowel_count - 1;
						}
						else
						{
							mnem = phoneme_tab[final_ph]->mnemonic;
							mnem2 = phoneme_tab[final_ph2]->mnemonic;

							if((mnem == 's') && (mnem2 == 'n'))
							{
								// -ns  stress remains on penultimate syllable
							}
							else
							if(((mnem != 'n') && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL))
							{
								stressed_syllable = vowel_count - 1;
							}
						}
					}
				}
				if(stressflags & 0x80000)
				{
					// stress on last syllable if it has a long vowel, but previous syllable has a short vowel
					if(vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2])
					{
						stressed_syllable = vowel_count - 1;
					}
				}

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

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

   case 3:
		// stress on last vowel
		if(stressed_syllable == 0)
		{
			/* no explicit stress - stress the final vowel */
			stressed_syllable = vowel_count - 1;

			while(stressed_syllable > 0)
			{
				// find the last vowel which is not unstressed
				if(vowel_stress[stressed_syllable] < 0)
				{
					vowel_stress[stressed_syllable] = 4;
					break;
				}
				else
					stressed_syllable--;
			}
			max_stress = 4;
		}
		break;

	case 4:   // stress on antipenultimate vowel
		if(stressed_syllable == 0)
		{
			stressed_syllable = vowel_count - 3;
			if(stressed_syllable < 1)
				stressed_syllable = 1;

			if(max_stress == 0)
			{
				vowel_stress[stressed_syllable] = 4;
			}
			max_stress = 4;
		}
		break;

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

			stressed_syllable = vowel_count - 3;
			if(vowel_count < 16)
			{
				if(phoneme_tab[final_ph]->type == phVOWEL)
					stressed_syllable = guess_ru_v[vowel_count];
				else
				if(phoneme_tab[final_ph]->type == phSTOP)
					stressed_syllable = guess_ru_t[vowel_count];
				else
					stressed_syllable = guess_ru[vowel_count];
			}
			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;

	case 6:    // LANG=hi stress on the last heaviest syllable
		if(stressed_syllable == 0)
		{
			int wt;
			int max_weight = -1;
			int prev_stressed;

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

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

			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;

	case 7:  // LANG=tr, the last syllable for any vowel marked explicitly as unstressed
		if(stressed_syllable == 0)
		{
			stressed_syllable = vowel_count - 1;
			for(ix=1; ix < vowel_count; ix++)
			{
				if(vowel_stress[ix] == 1)
				{
					stressed_syllable = ix-1;
					break;
				}
			}
			vowel_stress[stressed_syllable] = 4;
			max_stress = 4;
		}
		break;

	case 9:  // mark all as stressed
		for(ix=1; ix<vowel_count; ix++)
		{
			if(vowel_stress[ix] < 0)
				vowel_stress[ix] = 4;
		}
		break;
	}

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


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

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

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

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

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

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

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

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

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

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


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

	if(!(control & 1) && ((ph = phoneme_tab[*p]) != NULL))
	{

		if(ph->type == phSTRESS)
			ph = phoneme_tab[p[1]];

#ifdef deleted
		int gap = tr->langopts.word_gap & 0x700;
		if((gap) && (vowel_stress[1] >= 4) && (prev_stress >= 4))
		{
			/* two primary stresses together, insert a short pause */
			*output++ = pause_phonemes[gap >> 8];
		}
		else
#endif
		if((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL))
		{
			// word starts with a vowel

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

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

//		if(ph->type == phSTRESS)
//			continue;

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

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

			if(vowel_stress[v-1] >= max_stress)
				post_tonic = 1;

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

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


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

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

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

				if(shorten)
					p++;
			}

			if((v_stress >= 4) && (tr->langopts.param[LOPT_IT_LENGTHEN] == 2))
			{
				// LANG=Italian, lengthen penultimate stressed vowels, unless followed by 2 consonants
				if((v == (vowel_count - 2)) && (syllable_weight[v] == 0))
				{
					*output++ = phcode;
					phcode = phonLENGTHEN;
				}
			}

			v++;
		}

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

	return;
}  /* end of SetWordStress */




//=============================================================================================
//   Look up a word in the pronunciation rules
//
//=============================================================================================




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

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

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

		if(phoneme_tab[c]->type == phSTRESS)
		{
			if(phoneme_tab[c]->std_length < 4)
				unstress_mark = 1;
		}
		else
		{
			if(phoneme_tab[c]->type == phVOWEL)
			{
				if(((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
					(unstress_mark == 0))
				{
					tr->word_stressed_count++;
				}
				unstress_mark = 0;
				tr->word_vowel_count++;
			}
		}
	}
	
	if(string != NULL)
		strcat(string,ph);
}   /* end of AppendPhonemes */



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

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

	group:  the initial characters used to choose the rules group

	rule:  address of dictionary rule data for this character group

	match_out:  returns best points score

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

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

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

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

	int  match_type;      /* left, right, or consume */
	int  failed;
	int  unpron_ignore;
	int  consumed;        /* number of letters consumed from input */
	int  count;           /* count through rules in the group */
	int  syllable_count;
	int  vowel;
	int  letter_group;
	int  distance_right;
	int  distance_left;
	int  lg_pts;
	int  n_bytes;
	int add_points;
	int command;

	MatchRecord match;
	static MatchRecord best;

	int  total_consumed;  /* letters consumed for best match */

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

	group_chars = *word;

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


	total_consumed = 0;
	count = 0;
	common_phonemes = NULL;
	match_type = 0;

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

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

		match.points = 1;
		match.end_type = 0;
		match.del_fwd = NULL;
		
		pre_ptr = *word;
		post_ptr = *word + group_length;

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

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

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

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

			add_points = 0;

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

				if((letter == rb) || ((letter==(unsigned char)REPLACED_E) && (rb=='e')))
				{
					if((letter & 0xc0) != 0x80)
						add_points = 21;    // don't add point for non-initial UTF-8 bytes
					consumed++;
				}
				else
					failed = 1;
				break;


			case RULE_POST:
				/* continue moving fowards */
				distance_right += 6;
				if(distance_right > 18)
					distance_right = 19;
				last_letter = letter;
				letter_xbytes = utf8_in(&letter_w,post_ptr)-1;
				letter = *post_ptr++;

				switch(rb)
				{
				case RULE_LETTERGP:
					letter_group = *rule++ - 'A';
					if(IsLetter(tr, letter_w, letter_group))
					{
						lg_pts = 20;
						if(letter_group==2)
							lg_pts = 19;  // fewer points for C, general consonant
						add_points = (lg_pts-distance_right);
						post_ptr += letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_LETTERGP2:   // match against a list of utf-8 strings
					letter_group = *rule++ - 'A';
					if((n_bytes = IsLetterGroup(tr, post_ptr-1,letter_group,0)) >0)
					{
						add_points = (20-distance_right);
						post_ptr += (n_bytes-1);
					}
					else
						failed =1;
					break;

				case RULE_NOTVOWEL:
					if(IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL)))
					{
						failed = 1;
					}
					else
					{
						add_points = (20-distance_right);
						post_ptr += letter_xbytes;
					}
					break;

				case RULE_DIGIT:
					if(IsDigit(letter_w))
					{
						add_points = (20-distance_right);
						post_ptr += letter_xbytes;
					}
					else
					if(tr->langopts.tone_numbers)
					{
						// also match if there is no digit
						add_points = (20-distance_right);
						post_ptr--;
					}
					else
						failed = 1;
					break;
					
				case RULE_NONALPHA:
					if(!iswalpha(letter_w))
					{
						add_points = (21-distance_right);
						post_ptr += letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_DOUBLE:
					if(letter == last_letter)
						add_points = (21-distance_right);
					else
						failed = 1;
					break;

				case RULE_DOLLAR:
					command = *rule++;
					if(command == 0x01)
					{
						match.end_type = SUFX_UNPRON;    // $unpron
					}
					else
					if((command & 0xf0) == 0x10)
					{
						// $w_alt
						if(dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
							add_points = 23;
						else
							failed = 1;
					}
					else
					if((command & 0xf0) == 0x20)
					{
						// $p_alt
						// make a copy of the word up to the post-match characters
						ix = *word - word_start + consumed + group_length + 1;
						memcpy(word_buf, word_start-1, ix);
						word_buf[ix] = ' ';
						word_buf[ix+1] = 0;

						if(LookupFlags(tr, &word_buf[1]) & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
							add_points = 23;
						else
							failed = 1;
					}
					break;

				case '-':
					if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
					{
						add_points = (22-distance_right);    // one point more than match against space
					}
					else
						failed = 1;
					break;

				case RULE_SYLLABLE:
					{
						/* more than specified number of vowel letters to the right */
						char *p = post_ptr + letter_xbytes;
						int vowel_count=0;

						syllable_count = 1;
						while(*rule == RULE_SYLLABLE)
						{
							rule++;
							syllable_count+=1;   /* number of syllables to match */
						}
						vowel = 0;
						while(letter_w != RULE_SPACE)
						{
							if((vowel==0) && IsLetter(tr, letter_w,LETTERGP_VOWEL2))
							{
								// this is counting vowels which are separated by non-vowel letters
								vowel_count++;
							}
							vowel = IsLetter(tr, letter_w,LETTERGP_VOWEL2);
							p += utf8_in(&letter_w,p);
						}
						if(syllable_count <= vowel_count)
							add_points = (18+syllable_count-distance_right);
						else
							failed = 1;
					}
					break;

				case RULE_NOVOWELS:
					{
						char *p = post_ptr + letter_xbytes;
						while(letter_w != RULE_SPACE)
						{
							if(IsLetter(tr, letter_w,LETTERGP_VOWEL2))
							{
								failed = 1;
								break;
							}
							p += utf8_in(&letter_w,p);
						}
						if(!failed)
							add_points = (19-distance_right);
					}
					break;

				case RULE_SKIPCHARS:
					{
						// Used for lang=Tamil, used to match on the next word after an unknown word ending
						// only look until the end of the word (including the end-of-word marker)
						// Jx  means 'skip characters until x', where 'x' may be '_' for 'end of word'
						char *p = post_ptr + letter_xbytes;
						char *p2 = p;
						int rule_w;            // skip characters until this
						utf8_in(&rule_w,rule);
						while((letter_w != rule_w) && (letter_w != RULE_SPACE))
						{
							p2 = p;
							p += utf8_in(&letter_w,p);
						}
						if(letter_w == rule_w)
						{
							post_ptr = p2;
						}
					}
					break;

				case RULE_INC_SCORE:
					add_points = 20;      // force an increase in points
					break;

				case RULE_DEL_FWD:
					// find the next 'e' in the word and replace by 'E'
					for(p = *word + group_length; p < post_ptr; p++)
					{
						if(*p == 'e')
						{
							match.del_fwd = p;
							break;
						}
					}
					break;

				case RULE_ENDING:
					// next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
					match.end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
					rule += 3;
					break;

				case RULE_NO_SUFFIX:
					if(word_flags & FLAG_SUFFIX_REMOVED)
						failed = 1;             // a suffix has been removed
					else
						add_points = 1;
					break;

				default:
					if(letter == rb)
					{
						if((letter & 0xc0) != 0x80)
						{
							// not for non-initial UTF-8 bytes
							add_points = (21-distance_right);
						}
					}
					else
						failed = 1;
					break;
				}
				break;


			case RULE_PRE:
				/* match backwards from start of current group */
				distance_left += 2;
				if(distance_left > 18)
					distance_left = 19;

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

				switch(rb)
				{
				case RULE_LETTERGP:
					letter_group = *rule++ - 'A';
					if(IsLetter(tr, letter_w,letter_group))
					{
						lg_pts = 20;
						if(letter_group==2)
							lg_pts = 19;  // fewer points for C, general consonant
						add_points = (lg_pts-distance_left);
						pre_ptr -= letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_LETTERGP2:   // match against a list of utf-8 strings
					letter_group = *rule++ - 'A';
					if((n_bytes = IsLetterGroup(tr, pre_ptr,letter_group,1)) >0)
					{
						add_points = (20-distance_right);
						pre_ptr -= (n_bytes-1);
					}
					else
						failed =1;
					break;

				case RULE_NOTVOWEL:
					if(!IsLetter(tr, letter_w,0))
					{
						add_points = (20-distance_left);
						pre_ptr -= letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_DOUBLE:
					if(letter == last_letter)
						add_points = (21-distance_left);
					else
						failed = 1;
					break;

				case RULE_DIGIT:
					if(IsDigit(letter_w))
					{
						add_points = (21-distance_left);
						pre_ptr -= letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_NONALPHA:
					if(!iswalpha(letter_w))
					{
						add_points = (21-distance_right);
						pre_ptr -= letter_xbytes;
					}
					else
						failed = 1;
					break;

				case RULE_SYLLABLE:
					/* more than specified number of vowels to the left */
					syllable_count = 1;
					while(*rule == RULE_SYLLABLE)
					{
						rule++;
						syllable_count++;   /* number of syllables to match */
					}
					if(syllable_count <= tr->word_vowel_count)
						add_points = (18+syllable_count-distance_left);
					else
						failed = 1;
					break;

				case RULE_STRESSED:
					if(tr->word_stressed_count > 0)
						add_points = 19;
					else
						failed = 1;
					break;

				case RULE_NOVOWELS:
					{
						char *p = pre_ptr - letter_xbytes - 1;
						while(letter_w != RULE_SPACE)
						{
							if(IsLetter(tr, letter_w,LETTERGP_VOWEL2))
							{
								failed = 1;
								break;
							}
							p -= utf8_in2(&letter_w,p,1);
						}
						if(!failed)
							add_points = 3;
					}
					break;

				case RULE_IFVERB:
					if(tr->expect_verb)
						add_points = 1;
					else
						failed = 1;
					break;

				case RULE_CAPITAL:
					if(word_flags & FLAG_FIRST_UPPER)
						add_points = 1;
					else
						failed = 1;
					break;

				case '.':
					// dot in pre- section, match on any dot before this point in the word
					for(p=pre_ptr; *p != ' '; p--)
					{
						if(*p == '.')
						{
							add_points = 50;
							break;
						}
					}
					if(*p == ' ')
						failed = 1;
					break;

				case '-':
					if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
					{
						add_points = (22-distance_right);       // one point more than match against space
					}
					else
						failed = 1;
					break;

				default:
					if(letter == rb)
					{
						if(letter == RULE_SPACE)
							add_points = 4;
						else
						{
							if((letter & 0xc0) != 0x80)
							{
								// not for non-initial UTF-8 bytes
								add_points = (21-distance_left);
							}
						}
					}
					else
						failed = 1;
					break;
				}
				break;
			}

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


		if((failed == 2) && (unpron_ignore == 0))
		{
			/* matched OK, is this better than the last best match ? */

			if(match.points >= best.points)
			{
				memcpy(&best,&match,sizeof(match));
				total_consumed = consumed;
			}

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

				// note: 'count' contains the rule number, if we want to include it
				pts = match.points;
				if(group_length > 1)
					pts += 35;    // to account for an extra letter matching
				DecodePhonemes(match.phonemes,decoded_phonemes);
				fprintf(f_trans,"%3d\t%s [%s]\n",pts,DecodeRule(group_chars, group_length, rule_start, word_flags), decoded_phonemes);
			}

		}

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

	if((option_phonemes == 2) && ((word_flags & FLAG_NO_TRACE)==0))
	{
		if(group_length <= 1)
			fprintf(f_trans,"\n");
	}

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

	*word += total_consumed;

	if(best.points == 0)
		best.phonemes = "";
	memcpy(match_out,&best,sizeof(MatchRecord));
}   /* end of MatchRule */




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

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

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

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

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


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

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

	p = p_start;
	tr->word_vowel_count = 0;
	tr->word_stressed_count = 0;
	
	if(end_phonemes != NULL)
		end_phonemes[0] = 0;
	
	while(((c = *p) != ' ') && (c != 0))
	{
		wc_prev = wc;
		wc_bytes = utf8_in(&wc,p);
		if(IsAlpha(wc))
			any_alpha++;

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

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

			if(!found && (n > 0))
			{
				/* there are some 2 byte chains for this initial letter */
				c2 = p[1];
				c12 = c + (c2 << 8);   /* 2 characters */
				c123 = c12 + (p[2] << 16);
	
				g1 = tr->groups2_start[c];
				for(g=g1; g < (g1+n); g++)
				{
					if(tr->groups2_name[g] == c12)
					{
						found = 1;

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

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

						if(match2.points >= match1.points)
						{
							// use match from the 2-letter group
							memcpy(&match1,&match2,sizeof(MatchRecord));
							p = p2;
						}
					}
				}
			}
	
			if(!found)
			{
				/* alphabetic, single letter chain */
				if(tr->groups1[c] != NULL)
					MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
				else
				{
					// no group for this letter, use default group
					MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0);

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

						if(tr->letter_bits_offset > 0)
						{
							// not a Latin alphabet, switch to the default Latin alphabet language
							if((letter <= 0x241) && iswalpha(letter))
							{
								sprintf(phonemes,"%c%s",phonSWITCH,tr->langopts.ascii_language);
								return(0);
							}
						}
#ifdef deleted
// can't switch to a tone language, because the tone-phoneme numbers are not valid for the original language
						if((letter >= 0x4e00) && (letter < 0xa000) && (tr->langopts.ideographs != 1))
						{
							// Chinese ideogram
							sprintf(phonemes,"%czh",phonSWITCH);
							return(0);
						}
#endif

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

						// no match, try removing the accent and re-translating the word
						if((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0))
						{
							// within range of the remove_accent table
							if((p[-2] != ' ') || (p[n] != ' '))
							{
								// not the only letter in the word
								p2 = p-1;
								p[-1] = ix;
								while((p[0] = p[n]) != ' ')  p++;
								while(n-- > 0) *p++ = ' ';  // replacement character must be no longer than original
	
								if(tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list,letter) > 0))
								{
									// vowel with dieresis, replace and continue from this point
									p = p2;
									continue;
								}
	
								phonemes[0] = 0;  // delete any phonemes which have been produced so far
								p = p_start;
								tr->word_vowel_count = 0;
								tr->word_stressed_count = 0;
								continue;  // start again at the beginning of the word
							}
						}
						else
						if((letter >= 0x3200) && (letter < 0xa700) && (end_phonemes != NULL))
						{
							// ideograms
							// outside the range of the accent table, speak the unknown symbol sound
							Lookup(tr, "_??", ph_buf);
							match1.phonemes = ph_buf;
							match1.points = 1;
							p += (wc_bytes-1);
						}
					}
				}

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

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

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

			match1.end_type &= ~SUFX_UNPRON;

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

	// any language specific changes ?
	ApplySpecialAttribute(tr,phonemes,dict_flags0);
	memcpy(p_start,word_copy,strlen(word_copy));

	return(0);
}   /* end of TranslateRules */


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

	len = strlen(phonemes);

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


void ApplySpecialAttribute(Translator *tr, char *phonemes, int dict_flags)
{//=======================================================================
// Amend the translated phonemes according to an attribute which is specific for the language.
	int len;
	char *p_end;

	if((dict_flags & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) == 0)
		return;

	len = strlen(phonemes);
	p_end = &phonemes[len-1];

	switch(tr->translator_name)
	{
	case L('d','e'):
		if(p_end[0] == PhonemeCode2('i',':'))
		{
			// words ends in ['i:], change to [=I@]
			p_end[-1] = phonSTRESS_PREV;
			p_end[0] = PhonemeCode('I');
			p_end[1] = phonSCHWA;
			p_end[2] = 0;
		}
		break;

	case L('r','o'):
		if(p_end[0] == PhonemeCode('j'))
		{
			// word end in [j], change to ['i]
			p_end[0] = phonSTRESS_P;
			p_end[1] = PhonemeCode('i');
			p_end[2] = 0;
		}
		break;
	}
}  // end of ApplySpecialAttribute



//=============================================================================================
//   Look up a word in the pronunciation dictionary list
//   - exceptions which override the usual pronunciation rules, or which give a word
//     special properties, such as pronounce as unstressed
//=============================================================================================


int TransposeAlphabet(Translator *tr, char *text)
{//==============================================
// transpose cyrillic alphabet (for example) into ascii (single byte) character codes
// return: number of bytes, bit 6: 1=used compression
	int c;
	int c2;
	int ix;
	int offset;
	int min;
	int max;
	char *p = text;
	char *p2 = text;
	int all_alpha=1;
	int bits;
	int acc;
	int pairs_start;
	const short *pairs_list;

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

	pairs_start = max - min + 2;

	do {
		p += utf8_in(&c,p);
		if((c >= min) && (c <= max))
		{
			*p2++ = c - offset;
		}
		else
		if(c != 0)
		{
			p2 += utf8_out(c,p2);
			all_alpha=0;
		}
	} while (c != 0);
	*p2 = 0;

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

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

			if(bits >= 8)
			{
				bits -= 8;
				*p2++ = (acc >> bits); 
			}
		}
		if(bits > 0)
		{
			*p2++ = (acc << (8-bits));
		}
		*p2 = 0;
		return((p2 - text) | 0x40);  // bit 6 indicates compressed characters
	}
	return(p2 - text);
}  // end of TransposeAlphabet




static const char *LookupDict2(Translator *tr, const char *word, const char *word2,
		char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
//=====================================================================================
/* Find an entry in the word_dict file for a specified word.
   Returns NULL if no match, else returns 'word_end'

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

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

	end_flags:  indicates whether this is a retranslation after removing a suffix
*/
{
	char *p;
	char *next;
	int  hash;
	int  phoneme_len;
	int  wlen;
	unsigned char flag;
	unsigned int  dictionary_flags;
	unsigned int  dictionary_flags2;
	int  condition_failed=0;
	int  n_chars;
	int  no_phonemes;
	int  skipwords;
	int  ix;
	const char *word_end;
	const char *word1;
	int wflags = 0;
	char word_buf[N_WORD_BYTES];

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

	word1 = word;
	if(tr->transpose_min > 0)
	{
		strcpy(word_buf,word);
		wlen = TransposeAlphabet(tr, word_buf);
		word = word_buf;
	}
	else
	{
		wlen = strlen(word);
	}

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

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

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

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

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

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

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

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

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

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

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

				// don't use the contraction if any of the words are emphasized
				for(ix=0; ix <= skipwords; ix++)
				{
					if(wflags & FLAG_EMPHASIZED2)
					{
						condition_failed = 1;
					}
				}

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

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

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

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

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

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

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

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

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

		if((dictionary_flags & FLAG_ATEND) && (word_end < tr->clause_end))
		{
			// only use this pronunciation if it's the last word of the clause
			continue;
		}

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

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

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

			if(tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S)))
			{
				// OK, we are expecting a verb
			}
			else
			{
				/* don't use the 'verb' pronunciation unless we are
					expecting a verb */
				continue;
			}
		}
		if(dictionary_flags2 & FLAG_PAST)
		{
			if(!tr->expect_past)
			{
				/* don't use the 'past' pronunciation unless we are
					expecting past tense */
				continue;
			}
		}
		if(dictionary_flags2 & FLAG_NOUN)
		{
			if(!tr->expect_noun)
			{
				/* don't use the 'noun' pronunciation unless we are
					expecting a noun */
				continue;
			}
		}
		if(dictionary_flags & FLAG_ALT2_TRANS)
		{
			// language specific
			if((tr->translator_name == L('h','u')) && !(tr->prev_dict_flags & FLAG_ALT_TRANS))
				continue;
		}

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

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

		if(flags != NULL)
			flags[0] |= FLAG_FOUND;   // this flag indicates word was found in dictionary
		
		if(option_phonemes == 2)
		{
			unsigned int flags1 = 0;
			char ph_decoded[N_WORD_PHONEMES];
			int textmode;

			DecodePhonemes(phonetic,ph_decoded);
			if(flags != NULL)
				flags1 = flags[0];

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

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

	}
	return(0);
}   //  end of LookupDict2



int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
//==================================================================================================================
/* Lookup a specified word in the word dictionary.
   Returns phonetic data in 'phonetic' and bits in 'flags'

   end_flags:  indicates if a suffix has been removed
*/
{
	int  length;
	const char *found;
	const char *word1;
	const char *word2;
	unsigned char c;
	int  nbytes;
	int  len;
	char word[N_WORD_BYTES];
	static char word_replacement[N_WORD_BYTES];

	length = 0;
	word2 = word1 = *wordptr;

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

	for(length=0; length<N_WORD_BYTES; length++)
	{
		if(((c = *word1++)==0) || (c == ' '))
			break;
		word[length] = c;
	}
	word[length] = 0;

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

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


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

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

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

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

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

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

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

			ph_out[0] = 0;
			return(0);
		}

		return(1);
	}

	ph_out[0] = 0;
	return(0);
}   //  end of LookupDictList



int Lookup(Translator *tr, const char *word, char *ph_out)
{//===================================================
	unsigned int flags[2];
	flags[0] = flags[1] = 0;
	char *word1 = (char *)word;
	return(LookupDictList(tr, &word1, ph_out, flags, 0, NULL));
}

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

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



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

	word_copy: make a copy of the original word
	This routine is language specific.  In English it deals with reversing y->i and e-dropping
	that were done when the suffix was added to the original word.
*/
	
	int  i;
	char *word_end;
	int len_ending;
	int end_flags;
	const char *p;
	int  len;
	static char ending[12];
	
	// these lists are language specific, but are only relevent if the 'e' suffix flag is used
	static const char *add_e_exceptions[] = {
		"ion", NULL };

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

	for(word_end = word; *word_end != ' '; word_end++)
	{
		/* replace discarded 'e's */
		if(*word_end == REPLACED_E)
			*word_end = 'e';
	}
	i = word_end - word;
	memcpy(word_copy,word,i);
	word_copy[i] = 0;

	// look for multibyte characters to increase the number of bytes to remove
	for(len_ending = i = (end_type & 0x3f); i>0 ;i--)   // num.of characters of the suffix
	{
		word_end--;
		while((*word_end & 0xc0) == 0x80)
		{
			word_end--;  // for multibyte characters
			len_ending++;
		}
	}
	
	// remove bytes from the end of the word and replace them by spaces
	for(i=0; i<len_ending; i++)
	{
		ending[i] = word_end[i];
		word_end[i] = ' ';
	}
	ending[i] = 0;
	word_end--;     /* now pointing at last character of stem */

	end_flags = (end_type & 0xfff0) | FLAG_SUFX;
	
	/* add an 'e' to the stem if appropriate,
		if  stem ends in vowel+consonant
		or  stem ends in 'c'  (add 'e' to soften it) */
	
	if(end_type & SUFX_I)
	{
		if(word_end[0] == 'i')
			word_end[0] = 'y';
	}
	
	if(end_type & SUFX_E)
	{
		if(tr->translator_name == L('e','n'))
		{
			// add 'e' to end of stem
			if(IsLetter(tr, word_end[-1],LETTERGP_VOWEL2) && IsLetter(tr, word_end[0],1))
			{
				// vowel(incl.'y') + hard.consonant
	
				for(i=0; (p = add_e_exceptions[i]) != NULL; i++)
				{
					len = strlen(p);
					if(memcmp(p,&word_end[1-len],len)==0)
					{
						break;
					}
				}
				if(p == NULL)
					end_flags |= FLAG_SUFX_E_ADDED;  // no exception found
			}
			else
			{
				for(i=0; (p = add_e_additions[i]) != NULL; i++)
				{
					len = strlen(p);
					if(memcmp(p,&word_end[1-len],len)==0)
					{
						end_flags |= FLAG_SUFX_E_ADDED;
						break;
					}
				}
			}
		}
		else
		if(tr->langopts.suffix_add_e != 0)
		{
			end_flags |= FLAG_SUFX_E_ADDED;
		}

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

		if(option_phonemes == 2)
		{
			fprintf(f_trans,"add e\n");
		}
		}
	}

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


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

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

	return(end_flags);
}   /* end of RemoveEnding */


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

Share

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 | Terms of Use | Mobile
Web03 | 2.8.150427.4 | Last Updated 24 Jan 2012
Article Copyright 2012 by Jozef Bozek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid