Synthesize mixed-case translations.

This commit is contained in:
Dianne Skoll
2024-12-10 17:02:41 -05:00
parent 13ae49d3cd
commit d3e6c81a3a
6 changed files with 101 additions and 6 deletions

View File

@@ -380,14 +380,19 @@ static int RetStrVal(char const *s, func_info *info)
/***************************************************************/
static int F_(func_info *info)
{
char const *translated;
DynamicBuffer translated;
int r;
DBufInit(&translated);
ASSERT_TYPE(0, STR_TYPE);
translated = GetTranslatedString(ARGSTR(0));
if (!translated) {
r = GetTranslatedStringTryingVariants(ARGSTR(0), &translated);
if (!r) {
DCOPYVAL(RetVal, ARG(0));
return OK;
}
return RetStrVal(translated, info);
r = RetStrVal(DBufValue(&translated), info);
DBufFree(&translated);
return r;
}
/***************************************************************/

View File

@@ -269,6 +269,7 @@ void InitVars(void);
void InitUserFunctions(void);
void InitTranslationTable(void);
char const *GetTranslatedString(char const *orig);
int GetTranslatedStringTryingVariants(char const *orig, DynamicBuffer *out);
char const *GetErr(int r);
char const *t(char const *s);
void print_escaped_string(FILE *fp, char const *s);

View File

@@ -817,6 +817,9 @@ static void ServerWait(struct timeval *sleep_tv)
char cmdLine[256];
char *s;
int r;
DynamicBuffer tx;
DBufInit(&tx);
FD_ZERO(&readSet);
FD_SET(0, &readSet);
@@ -952,7 +955,13 @@ static void ServerWait(struct timeval *sleep_tv)
printf("\"translation\":{\"");
PrintJSONString(cmdLine+10);
printf("\":\"");
PrintJSONString(t(cmdLine+10));
r = GetTranslatedStringTryingVariants(cmdLine+10, &tx);
if (!r) {
PrintJSONString(cmdLine+10);
} else {
PrintJSONString(DBufValue(&tx));
DBufFree(&tx);
}
printf("\"},");
printf("\"command\":\"TRANSLATE\"}\n");
fflush(stdout);

View File

@@ -235,6 +235,69 @@ GetTranslatedString(char const *orig)
return item->translated;
}
int
GetTranslatedStringTryingVariants(char const *orig, DynamicBuffer *out)
{
DynamicBuffer in;
char const *s;
int first_lower = 0;
int has_upper = 0;
DBufInit(&in);
/* Try exact match first */
s = GetTranslatedString(orig);
if (s) {
DBufPuts(out, s);
return 1;
}
/* Classify orig */
s = orig;
while (*s) {
if (isupper(*s)) {
has_upper = 1;
break;
}
s++;
}
if (islower(*orig)) {
first_lower = 1;
}
if (has_upper) {
/* Try all lower-case version */
DBufPuts(&in, orig);
strtolower(DBufValue(&in));
s = GetTranslatedString(DBufValue(&in));
if (s) {
DBufPuts(out, s);
strtolower(DBufValue(out));
*(DBufValue(out)) = toupper(*DBufValue(out));
DBufFree(&in);
return 1;
}
}
if (first_lower) {
/* Try ucfirst version */
DBufFree(&in);
DBufPuts(&in, orig);
strtolower(DBufValue(&in));
*(DBufValue(&in)) = toupper(*(DBufValue(&in)));
s = GetTranslatedString(DBufValue(&in));
if (s) {
DBufPuts(out, s);
strtolower(DBufValue(out));
DBufFree(&in);
return 1;
}
}
DBufFree(&in);
return 0;
}
char const *t(char const *orig)
{
char const *n = GetTranslatedString(orig);

View File

@@ -16100,10 +16100,16 @@ Dedup-9998
Dedup-9999
../tests/test.rem(1262): `/': Ya tried to divide by zero, ya FOOOL!!!!!
../tests/test.rem(1264): `/': Division by zero
_("moo") => "bark"
_("Moo") => "Bark"
_("MOO") => "MOO"
_("meow") => "chirp"
_("Meow") => "Chirp"
_("MEOW") => "Chirp"
Var hash: total = 100141; maxlen = 5; avglen = 1.142
Func hash: total = 100016; maxlen = 5; avglen = 1.140
Dedup hash: total = 10000; maxlen = 7; avglen = 1.828
Trans hash: total = 2; maxlen = 1; avglen = 0.118
Trans hash: total = 4; maxlen = 1; avglen = 0.235
Expression nodes allocated: 300096
Expression nodes high-water: 300073
Expression nodes leaked: 0

View File

@@ -1263,6 +1263,17 @@ set a 1/0
TRANSLATE "Division by zero"
set a 1/0
TRANSLATE "Moo" "Bark"
TRANSLATE "meow" "chirp"
DEBUG +x
set a _("moo")
set a _("Moo")
set a _("MOO")
set a _("meow")
set a _("Meow")
set a _("MEOW")
# Output expression-node stats
DEBUG +s