From fb06936c51e51d34d081ae73800c573cad4557d4 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 3 Sep 2022 08:15:12 +0300 Subject: [PATCH] code cleanup: start moving translateWord3() to a new source file. The file will be organized to have one callable function only. This should make code structure simpler. Existing code will be changed to use function parameters instead of global variables. Possible problems include too much dependencies with numbers.c. --- Makefile.am | 2 + android/jni/Android.mk | 1 + src/libespeak-ng/translate.c | 32 ++------------- src/libespeak-ng/translateword.c | 70 ++++++++++++++++++++++++++++++++ src/libespeak-ng/translateword.h | 39 ++++++++++++++++++ 5 files changed, 116 insertions(+), 28 deletions(-) create mode 100644 src/libespeak-ng/translateword.c create mode 100644 src/libespeak-ng/translateword.h diff --git a/Makefile.am b/Makefile.am index 0ba39b90..d3861d5b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -174,6 +174,7 @@ src_libespeak_ng_la_SOURCES = \ src/libespeak-ng/synthesize.c \ src/libespeak-ng/synth_mbrola.c \ src/libespeak-ng/translate.c \ + src/libespeak-ng/translateword.c \ src/libespeak-ng/tr_languages.c \ src/libespeak-ng/voices.c \ src/libespeak-ng/wavegen.c @@ -205,6 +206,7 @@ noinst_HEADERS = \ src/libespeak-ng/synthdata.h \ src/libespeak-ng/synthesize.h \ src/libespeak-ng/translate.h \ + src/libespeak-ng/translateword.h \ src/libespeak-ng/voice.h \ src/libespeak-ng/wavegen.h \ src/speechPlayer/include/speechPlayer.h \ diff --git a/android/jni/Android.mk b/android/jni/Android.mk index b9c5c231..9c9f3dba 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -44,6 +44,7 @@ ESPEAK_SOURCES := \ src/libespeak-ng/synthesize.c \ src/libespeak-ng/synth_mbrola.c \ src/libespeak-ng/translate.c \ + src/libespeak-ng/translateword.c \ src/libespeak-ng/tr_languages.c \ src/libespeak-ng/voices.c \ src/libespeak-ng/wavegen.c diff --git a/src/libespeak-ng/translate.c b/src/libespeak-ng/translate.c index 81e9e4f9..fe0dd48b 100644 --- a/src/libespeak-ng/translate.c +++ b/src/libespeak-ng/translate.c @@ -43,6 +43,7 @@ #include "ucd/ucd.h" // for ucd_toupper #include "voice.h" // for voice, voice_t #include "speech.h" // for MAKE_MEM_UNDEFINED +#include "translateword.h" Translator *translator = NULL; // the main translator Translator *translator2 = NULL; // secondary translator for certain words @@ -388,31 +389,6 @@ static void addPluralSuffixes(int flags, Translator *tr, char last_char, char *w } } -static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word) -{ - int posn = 0; - int capitals = 0; - bool non_initial = false; - - if (spell_word > 2) - capitals = 2; // speak 'capital' - if (spell_word > 1) - capitals |= 4; // speak character code for unknown letters - - while ((*word != ' ') && (*word != 0)) { - word += TranslateLetter(tr, word, phonemes, capitals | non_initial, current_alphabet); - posn++; - non_initial = true; - if (phonemes[0] == phonSWITCH) { - // change to another language in order to translate this word - strcpy(word_phonemes, phonemes); - return NULL; - } - } - SetSpellingStress(tr, phonemes, spell_word, posn); - return word; -} - static int CheckDottedAbbrev(char *word1) { int wc; @@ -665,7 +641,7 @@ static int TranslateWord3(Translator *tr, char *word_start, WORD_TAB *wtab, char // Speak as individual letters phonemes[0] = 0; - if (SpeakIndividualLetters(tr, word1, phonemes, spell_word) == NULL) { + if (SpeakIndividualLetters(tr, word1, phonemes, spell_word, current_alphabet, word_phonemes) == NULL) { if (word_length > 1) return FLAG_SPELLWORD; // a mixture of languages, retranslate as individual letters, separated by spaces return 0; @@ -737,7 +713,7 @@ static int TranslateWord3(Translator *tr, char *word_start, WORD_TAB *wtab, char // ?? should we say super/sub-script numbers and letters here? utf8_in(&wc, wordx); if ((word_length == 1) && (IsAlpha(wc) || IsSuperscript(wc))) { - if ((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word)) == NULL) + if ((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word, current_alphabet, word_phonemes)) == NULL) return 0; strcpy(word_phonemes, phonemes); return 0; @@ -824,7 +800,7 @@ static int TranslateWord3(Translator *tr, char *word_start, WORD_TAB *wtab, char strcpy(prefix_phonemes, phonemes); if (dictionary_flags[0] & FLAG_ABBREV) { prefix_phonemes[0] = 0; - SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1); + SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1, current_alphabet, word_phonemes); } } else strcat(prefix_phonemes, end_phonemes); diff --git a/src/libespeak-ng/translateword.c b/src/libespeak-ng/translateword.c new file mode 100644 index 00000000..2eb1dcd4 --- /dev/null +++ b/src/libespeak-ng/translateword.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2005 to 2014 by Jonathan Duddington + * email: jonsd@users.sourceforge.net + * Copyright (C) 2015-2017 Reece H. Dunn + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see: . + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "translate.h" +#include "dictionary.h" // for TranslateRules, LookupDictList, Cha... +#include "numbers.h" // for SetSpellingStress, TranslateLetter +#include "phoneme.h" // for phonSWITCH, PHONEME_TAB, phonPAUSE_... +#include "phonemelist.h" // for MakePhonemeList +#include "readclause.h" // for towlower2, Eof, ReadClause, is_str_... +#include "synthdata.h" // for SelectPhonemeTable, LookupPhonemeTable +#include "synthesize.h" // for PHONEME_LIST2, N_PHONEME_LIST, PHON... +#include "ucd/ucd.h" // for ucd_toupper +#include "voice.h" // for voice, voice_t +#include "speech.h" // for MAKE_MEM_UNDEFINED + +char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word, ALPHABET *current_alphabet, char word_phonemes[]) +{ + int posn = 0; + int capitals = 0; + bool non_initial = false; + + if (spell_word > 2) + capitals = 2; // speak 'capital' + if (spell_word > 1) + capitals |= 4; // speak character code for unknown letters + + while ((*word != ' ') && (*word != 0)) { + word += TranslateLetter(tr, word, phonemes, capitals | non_initial, current_alphabet); + posn++; + non_initial = true; + if (phonemes[0] == phonSWITCH) { + // change to another language in order to translate this word + strcpy(word_phonemes, phonemes); + return NULL; + } + } + SetSpellingStress(tr, phonemes, spell_word, posn); + return word; +} diff --git a/src/libespeak-ng/translateword.h b/src/libespeak-ng/translateword.h new file mode 100644 index 00000000..873ed179 --- /dev/null +++ b/src/libespeak-ng/translateword.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2005 to 2014 by Jonathan Duddington + * email: jonsd@users.sourceforge.net + * Copyright (C) 2015-2017, 2020 Reece H. Dunn + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see: . + */ + +#ifndef ESPEAK_NG_TRANSLATEWORD_H +#define ESPEAK_NG_TRANSLATEWORd_H + +#include + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word, ALPHABET *current_alphabet, char word_phonemes[]); + +#ifdef __cplusplus +} +#endif + +#endif