[1.29.11] Move character substitution feature to language *_rules files.

git-svn-id: https://espeak.svn.sourceforge.net/svnroot/espeak/trunk@93 d46cf337-b52f-0410-862d-fd96e6ae7743
This commit is contained in:
jonsd
2007-10-23 13:14:49 +00:00
parent e5f2d6f4c6
commit 5bdf9d63ab
30 changed files with 583 additions and 212 deletions
+101 -36
View File
@@ -176,7 +176,6 @@ int compile_line(char *linebuf, char *dict_line, int *hash)
char encoded_ph[200];
unsigned char bad_phoneme[4];
p = linebuf;
comment = NULL;
phonetic = word = "";
@@ -347,7 +346,8 @@ int compile_line(char *linebuf, char *dict_line, int *hash)
if((word[0] & 0x80)==0) // 7 bit ascii only
{
// 1st letter - need to consider utf8 here
// If first letter is uppercase, convert to lower case. (Only if it's 7bit ascii)
// ??? need to consider utf8 here
word[0] = tolower(word[0]);
}
@@ -789,8 +789,6 @@ char *compile_rule(char *input)
for(ix=0; finish==0; ix++)
{
c = input[ix];
if((c=='/') && (input[ix+1]=='/'))
c = input[ix] = '\n'; // treat command as end of line
switch(c = input[ix])
{
@@ -1167,6 +1165,7 @@ static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
int different;
char *prev_rgroup_name;
unsigned int char_code;
int compile_mode=0;
char *buf;
char buf1[120];
char *rules[N_RULES];
@@ -1184,7 +1183,13 @@ static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
{
linenum++;
buf = fgets(buf1,sizeof(buf1),f_in);
if((buf != NULL) && (buf[0] == '\r')) buf++; // ignore extra \r in \r\n
if(buf != NULL)
{
if((p = (unsigned char *)strstr(buf,"//")) != NULL)
*p = 0;
if(buf[0] == '\r') buf++; // ignore extra \r in \r\n
}
if((buf != NULL) && (memcmp(buf,".L",2)==0))
{
@@ -1196,7 +1201,7 @@ static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
continue;
}
if((buf == NULL) || (memcmp(buf,".group",6)==0))
if((buf == NULL) || (buf[0] == '.'))
{
// next .group or end of file, write out the previous group
@@ -1212,46 +1217,106 @@ static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
}
n_rules = 0;
if(buf == NULL) break; // end of file
p = (unsigned char *)&buf[6];
while((p[0]==' ') || (p[0]=='\t')) p++; // Note: Windows isspace(0xe1) gives TRUE !
ix = 0;
while((*p > ' ') && (ix<12))
group_name[ix++] = *p++;
group_name[ix]=0;
if(sscanf(group_name,"0x%x",&char_code)==1)
if(compile_mode == 2)
{
// group character is given as a character code (max 16 bits)
p = (unsigned char *)group_name;
if(char_code > 0x100)
{
*p++ = (char_code >> 8);
}
*p++ = char_code;
*p = 0;
// end of the character replacements section
fwrite(&n_rules,1,4,f_out); // write a zero word to terminate the replacemenmt list
}
if(strlen(group_name) > 2)
{
if(utf8_in(&c,group_name,0) < 2)
{
fprintf(f_log,"%5d: Group name longer than 2 bytes (UTF8)",linenum);
error_count++;
}
if(buf == NULL) break; // end of file
group_name[2] = 0;
if(memcmp(buf,".replace",8)==0)
{
compile_mode = 2;
fputc(RULE_GROUP_START,f_out);
fputc(RULE_REPLACEMENTS,f_out);
// advance to next word boundary
while((ftell(f_out) & 3) != 0)
fputc(0,f_out);
}
if(memcmp(buf,".group",6)==0)
{
compile_mode = 1;
p = (unsigned char *)&buf[6];
while((p[0]==' ') || (p[0]=='\t')) p++; // Note: Windows isspace(0xe1) gives TRUE !
ix = 0;
while((*p > ' ') && (ix<12))
group_name[ix++] = *p++;
group_name[ix]=0;
if(sscanf(group_name,"0x%x",&char_code)==1)
{
// group character is given as a character code (max 16 bits)
p = (unsigned char *)group_name;
if(char_code > 0x100)
{
*p++ = (char_code >> 8);
}
*p++ = char_code;
*p = 0;
}
if(strlen(group_name) > 2)
{
if(utf8_in(&c,group_name,0) < 2)
{
fprintf(f_log,"%5d: Group name longer than 2 bytes (UTF8)",linenum);
error_count++;
}
group_name[2] = 0;
}
}
continue;
}
prule = compile_rule(buf);
if((prule != NULL) && (n_rules < N_RULES))
switch(compile_mode)
{
rules[n_rules++] = prule;
case 1: // .group
prule = compile_rule(buf);
if((prule != NULL) && (n_rules < N_RULES))
{
rules[n_rules++] = prule;
}
break;
case 2: // .replace
{
int replace1;
int replace2;
char *p;
p = buf;
replace1 = 0;
replace2 = 0;
while(isspace2(*p)) p++;
ix = 0;
while((unsigned char)(*p) > 0x20) // not space or zero-byte
{
p += utf8_in(&c,p,0);
replace1 += (c << ix);
ix += 16;
}
while(isspace2(*p)) p++;
ix = 0;
while((unsigned char)(*p) > 0x20)
{
p += utf8_in(&c,p,0);
replace2 += (c << ix);
ix += 16;
}
if(replace1 != 0)
{
fwrite(&replace1,1,4,f_out);
fwrite(&replace2,1,4,f_out);
}
}
break;
}
}
fclose(f_temp);
+13 -3
View File
@@ -205,9 +205,9 @@ void Translator::InitGroups(void)
int ix;
char *p;
char *p_name;
unsigned int *pw;
unsigned char c, c2;
int len;
int rule_count;
n_groups2 = 0;
for(ix=0; ix<256; ix++)
@@ -228,6 +228,18 @@ void Translator::InitGroups(void)
}
p++;
if(p[0] == RULE_REPLACEMENTS)
{
pw = (unsigned int *)(((int)p+4) & ~3); // advance to next word boundary
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);
continue;
}
if(p[0] == RULE_LETTERGP2)
{
ix = p[1] - 'A';
@@ -266,11 +278,9 @@ void Translator::InitGroups(void)
}
// skip over all the rules in this group
rule_count = 0;
while(*p != RULE_GROUP_END)
{
p += (strlen(p) + 1);
rule_count++;
}
p++;
}
+2
View File
@@ -998,6 +998,8 @@ void ConvertToUtf8()
} // end of ConvertToItf8
//******************************************************************************************************
//#define calcspeedtab
+9 -7
View File
@@ -354,12 +354,14 @@ int Translator::LookupNum2(int value, int control, char *ph_out)
if(langopts.numbers & 0x200)
{
// remove vowel from the end of tens if units starts with a vowel (LANG=Italian)
ix = strlen(ph_tens)-1;
if((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS)
next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type;
if((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL))
ph_tens[ix] = 0;
if((ix = strlen(ph_tens)-1) >= 0)
{
if((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS)
next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type;
if((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL))
ph_tens[ix] = 0;
}
}
sprintf(ph_out,"%s%s",ph_tens,ph_digits);
}
@@ -793,7 +795,7 @@ int Translator::TranslateNumber_1(char *word, char *ph_out, unsigned int *flags,
decimal_point = 0;
}
}
if(ph_out[0] != 0)
if((ph_out[0] != 0) && (ph_out[0] != phonSWITCH))
{
int next_char;
utf8_in(&next_char,&word[n_digits+1],0);
+1 -1
View File
@@ -35,7 +35,7 @@
#include "translate.h"
#include "wave.h"
const char *version_string = "1.29.10 16.Oct.07";
const char *version_string = "1.29.11 23.Oct.07";
const int version_phdata = 0x012901;
int option_device_number = -1;
+44 -30
View File
@@ -38,6 +38,7 @@
#define L_qa 0x716100
#define L_grc 0x677263 // grc Ancient Greek
#define OFFSET_GREEK 0x380
@@ -45,19 +46,41 @@
#define OFFSET_DEVANAGARI 0x900
static const unsigned int replace_cyrillic[] =
{0x430,0x431,0x446,0x45b,0x447,0x45f,0x455,0x434,0x452,
0x435,0x444,0x433,0x445,0x438,0x458,0x43a,0x459,
0x43b,0x43c,0x45a,0x43d,0x43e,0x43f,0x440,0x441,
0x448,0x442,0x443,0x432,0x437,0x436,
0x453,0x45c,0}; // ѓ ѕ ќ
static const unsigned int replace_cyrillic_latin[] =
{'a','b','c',0x107,0x10d,'d'+(0x17e<<16),'d'+('z'<<16),'d',0x111,
'e','f','g','h','i','j','k','l'+('j'<<16),
'l','m','n'+('j'<<16),'n','o','p','r','s',
0x161,'t','u','v','z',0x17e,
0x111,0x107,0};
static const unsigned int replace_cyrillic_latin[] =
{0x430,'a',
0x431,'b',
0x446,'c',
0x45b,0x107,
0x447,0x10d,
0x45f,'d'+(0x17e<<16),
0x455,'d'+('z'<<16),
0x434,'d',
0x452,0x111,
0x435,'e',
0x444,'f',
0x433,'g',
0x445,'h',
0x438,'i',
0x458,'j',
0x43a,'k',
0x459,'l'+('j'<<16),
0x43b,'l',
0x43c,'m',
0x45a,'n'+('j'<<16),
0x43d,'n',
0x43e,'o',
0x43f,'p',
0x440,'r',
0x441,'s',
0x448,0x161,
0x442,'t',
0x443,'u',
0x432,'v',
0x437,'z',
0x436,0x17e,
0x453,0x111,
0x45c,0x107,
0}; // ѓ ѕ ќ
void SetupTranslator(Translator *tr, int *lengths, int *amps)
@@ -132,6 +155,7 @@ Translator *SelectTranslator(const char *name)
break;
case L('e','l'): // Greek
case L_grc: // Ancient Greek
{
static int stress_lengths_el[8] = {155, 180, 210, 210, 0, 0, 270, 300};
static int stress_amps_el[8] = {15,12, 20,20, 20,24, 24,22 }; // 'diminished' is used to mark a quieter, final unstressed syllable
@@ -167,6 +191,12 @@ Translator *SelectTranslator(const char *name)
tr->langopts.numbers = 0xb09;
tr->langopts.numbers2 = 0x2; // variant form of numbers before thousands
if(name2 == L_grc)
{
// ancient greek
tr->langopts.param[LOPT_UNPRONOUNCABLE] = 1;
}
}
break;
@@ -259,8 +289,6 @@ Translator *SelectTranslator(const char *name)
case L('h','i'):
{
static const char dev_consonants2[] = {0x02,0x03,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f};
static const unsigned int replace_chars_hi[11] = {0x966,0x967,0x968,0x969,0x96a,0x96b,0x96c,0x96d,0x96e,0x96f,0}; // digits 0-9
static const unsigned int replacement_chars_hi[11] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0};
static int stress_lengths_hi[8] = {190, 190, 210, 210, 0, 0, 230, 250};
static int stress_amps_hi[8] = {17,14, 20,19, 20,24, 24,22 };
@@ -274,8 +302,6 @@ Translator *SelectTranslator(const char *name)
tr->langopts.numbers = 0x811;
tr->langopts.numbers2 = 0x100;
tr->letter_bits_offset = OFFSET_DEVANAGARI;
tr->langopts.replace_chars = replace_chars_hi;
tr->langopts.replacement_chars = replacement_chars_hi;
memset(tr->letter_bits,0,sizeof(tr->letter_bits));
SetLetterBitsRange(tr,LETTERGP_A,0x06,0x14); // vowel letters
@@ -306,8 +332,7 @@ Translator *SelectTranslator(const char *name)
tr->langopts.numbers = 0x1c0d + 0x4000 + NUM_ROMAN_UC;
tr->langopts.numbers2 = 0x4a; // variant numbers before thousands,milliards
tr->langopts.replace_chars = replace_cyrillic;
tr->langopts.replacement_chars = replace_cyrillic_latin;
tr->langopts.replace_chars = replace_cyrillic_latin;
SetLetterVowel(tr,'y');
SetLetterVowel(tr,'r');
@@ -319,14 +344,10 @@ Translator *SelectTranslator(const char *name)
{
static int stress_amps_hu[8] = {17,17, 19,19, 20,24, 24,22 };
static int stress_lengths_hu[8] = {185,195, 195,190, 0,0, 210,220};
static const unsigned int replace_chars_hu[] = {0xd4,0xf4,0xdb,0xfb,0};
static const unsigned int replacement_chars_hu[] = {0x150,0x151,0x170,0x171,0}; // allow o,u-circumflex for o,u-double-acute
tr = new Translator();
SetupTranslator(tr,stress_lengths_hu,stress_amps_hu);
tr->charset_a0 = charsets[2]; // ISO-8859-2
tr->langopts.replace_chars = replace_chars_hu;
tr->langopts.replacement_chars = replacement_chars_hu;
tr->langopts.vowel_pause = 0x20;
tr->langopts.stress_rule = 0;
@@ -417,9 +438,6 @@ SetLengthMods(tr,3); // all equal
tr->langopts.stress_rule = 4; // antipenultimate
tr->langopts.numbers = 0x0c29 + 0x4000;
tr->langopts.numbers2 = 0x8a; // variant numbers before thousands,milliards
tr->langopts.replace_chars = replace_cyrillic_latin;
tr->langopts.replacement_chars = replace_cyrillic;
}
break;
@@ -495,8 +513,6 @@ SetLengthMods(tr,3); // all equal
{
static int stress_lengths_ro[8] = {170, 170, 180, 180, 0, 0, 240, 260};
static int stress_amps_ro[8] = {15,13, 18,18, 20,22, 22,22 };
static const unsigned int replace_chars_ro[5] = {0x218,0x219,0x21a,0x21b,0};
static const unsigned int replacement_chars_ro[5] = {0x15e,0x15f,0x162,0x163,0}; // replace s-comma, t-comma by s-cedilla, t-cedilla
tr = new Translator();
SetupTranslator(tr,stress_lengths_ro,stress_amps_ro);
@@ -505,8 +521,6 @@ SetLengthMods(tr,3); // all equal
tr->langopts.stress_flags = 0x100 + 0x6;
tr->charset_a0 = charsets[2]; // ISO-8859-2
tr->langopts.replace_chars = replace_chars_ro;
tr->langopts.replacement_chars = replacement_chars_ro;
tr->langopts.numbers = 0x1829+0x6000 + NUM_ROMAN;
tr->langopts.numbers2 = 0x1e; // variant numbers before all thousandplex
}
+19 -9
View File
@@ -665,6 +665,10 @@ if((wmark > 0) && (wmark < 8))
if(!found && iswdigit(first_char))
{
Lookup("_0lang",word_phonemes);
if(word_phonemes[0] == phonSWITCH)
return(0);
found = TranslateNumber(word,phonemes,&dictionary_flags,wflags);
}
@@ -1560,16 +1564,14 @@ static int EmbeddedCommand(unsigned int &source_index)
} // end of EmbeddedCommand
int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert)
{//=====================================================================================================
// To allow language specific examination and replacement of characters
int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, int *insert)
{//==================================================================================
int ix;
unsigned int word;
unsigned int new_c, c2, c_lower;
int upper_case = 0;
static int ignore_next = 0;
const unsigned int *replace_chars;
if(ignore_next)
{
@@ -1578,7 +1580,7 @@ int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned i
}
if(c == 0) return(0);
if(langopts.replace_chars == NULL)
if((replace_chars = tr->langopts.replace_chars) == NULL)
return(c);
// there is a list of character codes to be substituted with alternative codes
@@ -1590,18 +1592,18 @@ int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned i
}
new_c = 0;
for(ix=0; (word = langopts.replace_chars[ix]) != 0; ix++)
for(ix=0; (word = replace_chars[ix]) != 0; ix+=2)
{
if(c_lower == (word & 0xffff))
{
if((word >> 16) == 0)
{
new_c = langopts.replacement_chars[ix];
new_c = replace_chars[ix+1];
break;
}
if((word >> 16) == (unsigned int)tolower(next_in))
{
new_c = langopts.replacement_chars[ix];
new_c = replace_chars[ix+1];
ignore_next = 1;
break;
}
@@ -1625,6 +1627,14 @@ int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned i
if(upper_case)
new_c = towupper(new_c);
return(new_c);
}
int Translator::TranslateChar(char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert)
{//=====================================================================================================
// To allow language specific examination and replacement of characters
return(SubstituteChar(this,c,next_in,insert));
}
+3 -2
View File
@@ -118,6 +118,7 @@
#define RULE_LETTERGP 17 // A B C H F G Y letter group number
#define RULE_LETTERGP2 18 // L + letter group number
#define RULE_CAPITAL 19 // word starts with a capital letter
#define RULE_REPLACEMENTS 20 // section for character replacements
#define RULE_NO_SUFFIX 24 // N
#define RULE_NOTVOWEL 25 // K
#define RULE_IFVERB 26 // V
@@ -278,6 +279,7 @@ typedef struct {
#define NUM_ROMAN 0x20000
#define NUM_ROMAN_UC 0x40000
// bits0-1=which numbers routine to use.
// bit2= thousands separator must be space
// bit3= , decimal separator, not .
@@ -294,7 +296,6 @@ typedef struct {
// bit16=dot after number indicates ordinal
// bit17=recognize roman numbers
// bit18=Roman numbers only if upper case
int numbers;
// bits 1-4 use variant form of numbers before thousands,millions,etc.
@@ -302,6 +303,7 @@ typedef struct {
// bit7=(LANG-ru) use MB for 1 thousand, million, etc
// bit8=(LANG=sw) special word for 100,000s
int numbers2;
int max_roman;
int thousands_sep;
int decimal_sep;
@@ -314,7 +316,6 @@ typedef struct {
char ideographs; // treat as separate words
int testing; // testing options: bit 1= specify stressed syllable in the form: "outdoor/2"
const unsigned int *replace_chars; // characters to be substitutes
const unsigned int *replacement_chars; // substitutes for replace_chars
} LANGUAGE_OPTIONS;