Browse Source

code cleanup: reduce duplication with new function DollarRule()

master
Juho Hiltunen 2 years ago
parent
commit
bbfef6a269
1 changed files with 22 additions and 31 deletions
  1. 22
    31
      src/libespeak-ng/dictionary.c

+ 22
- 31
src/libespeak-ng/dictionary.c View File

#include "translate.h" // for Translator, utf8_in, LANGU... #include "translate.h" // for Translator, utf8_in, LANGU...


static int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out); static int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out);
static void DollarRule(char *word[], char *word_start, int consumed, int group_length, char *word_buf, Translator *tr, unsigned int *flags, int command, int *failed, int *add_points);


typedef struct { typedef struct {
int points; int points;


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

int match_type; // left, right, or consume int match_type; // left, right, or consume
int syllable_count; int syllable_count;
int vowel; int vowel;
int n_bytes; int n_bytes;
int add_points; int add_points;
int command; int command;
unsigned int *flags;
unsigned int *flags = NULL;


MatchRecord match; MatchRecord match;
static MatchRecord best; static MatchRecord best;
else else
failed = 1; failed = 1;
} else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) { } else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) {
// $list or $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;
LookupFlags(tr, &word_buf[1], &flags);

if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
add_points = 23;
else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
add_points = 23;
else
failed = 1;
DollarRule(word, word_start, consumed, group_length, word_buf, tr, flags, command, &failed, &add_points);
} }

break; break;
case '-': case '-':
if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER))) if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
pre_ptr++; pre_ptr++;
command = *rule++; command = *rule++;
if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) { if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) {
// $list or $p_alt
// make a copy of the word up to the current character
ix = *word - word_start + 1;
memcpy(word_buf, word_start-1, ix);
word_buf[ix] = ' ';
word_buf[ix+1] = 0;
LookupFlags(tr, &word_buf[1], &flags);

if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
add_points = 23;
else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
add_points = 23;
else
failed = 1;
DollarRule(word, word_start, consumed, group_length, word_buf, tr, flags, command, &failed, &add_points);
} }
break; break;
case RULE_SYLLABLE: case RULE_SYLLABLE:


return end_flags; return end_flags;
} }

static void DollarRule(char *word[], char *word_start, int consumed, int group_length, char *word_buf, Translator *tr, unsigned int *flags, int command, int *failed, int *add_points) {
// $list or $p_alt
// make a copy of the word up to the post-match characters
int ix = *word - word_start + consumed + group_length + 1;
memcpy(word_buf, word_start-1, ix);
word_buf[ix] = ' ';
word_buf[ix+1] = 0;
LookupFlags(tr, &word_buf[1], &flags);

if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
*add_points = 23;
else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
*add_points = 23;
else
*failed = 1;
}

Loading…
Cancel
Save