-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.js
121 lines (115 loc) · 4.25 KB
/
translate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { executionAsyncResource } from "async_hooks";
import OpenAI from "openai";
import { fileURLToPath } from 'url';
let openai = null;
if (!process.env.OPENAI_API_KEY) {
console.warn("OPENAI_API_KEY not set in environment variables");
} else {
openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
}
export async function translateStrings(textArray, language) {
try {
if (!openai) {
return textArray.map(text => `Server config: translation API key not set`);
}
// replace all double quotes with single quotes and remove single quotes to prevent issues with AI JSON parsing
textArray = textArray.map(text => text.replace(/"/g, "'").replace(/'/g, ""));
const json = JSON.stringify(textArray);
if (language.toLowerCase() === "english")
{
return JSON.parse(json);
}
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: `You will be provided with json array that contains strings describing geographic datasets and types in language ${language}, and your task is to translate these strings to English`},
{ role: "user", content: json}
],
model: "gpt-3.5-turbo",
});
return JSON.parse(completion.choices[0].message.content);
} catch (error) {
console.error(`Error in translateStrings: ${error.message?error.message: error}`);
return null;
}
}
export async function translateSearchString(searchString, userlanguage, language) {
console.log(userlanguage);
// convert navigator.language to language name
switch (userlanguage.slice(0, 2).toLowerCase()) {
case "en":
userlanguage = "English";
break;
case "nl":
userlanguage = "Dutch";
break;
case "fr":
userlanguage = "French";
break;
case "de":
userlanguage = "German";
break;
case "es":
userlanguage = "Spanish";
break;
// Italian
case "it":
userlanguage = "Italian";
// Norwegian
case "no":
userlanguage = "Norwegian";
break;
// Swedish
case "sv":
userlanguage = "Swedish";
break;
// Finnish
case "fi":
userlanguage = "Finnish";
break;
// Danish
default:
break;
}
if (language.toLowerCase() === userlanguage.toLowerCase()) {
return searchString;
}
try {
if (!openai) {
console.log(`Server config: translation API key not set`);
return searchString;
}
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: `You will be provided with a search string likely in language ${userlanguage}, and your task is to translate this string to ${language}`},
{ role: "system", content: `If the searchstring contains acronyms or abbreviations, you can leave them untranslated, except if you know the meaning of the acronym in both the source and target languages (GIS => SIG or AIDS=>SIDA)`},
{ role: "system", content: `The search strings are given to search for geographic datasets and types in a catalog, and the translation should be as accurate as possible to ensure the search results are relevant to the user's query`},
{ role: "system", content: `Your output should be the string to use as a search query in the target language`},
{ role: "user", content: searchString}
],
model: "gpt-3.5-turbo",
});
return completion.choices[0].message.content;
} catch (error) {
console.error(`Error in translateSearchString: ${error.message?error.message: error}`);
return null;
}
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
(async () => {
const result = await translateStrings(
[
"Dit is een tekst",
"Een vreemd verhaal over prinsen en prinsessen",
"Een zakelijke tekst over de economie",
"Afbeeldingen van de natuur",
"Een geografische weergave van welvaartsspreiding",
"Beheerobjecten gemeente Udenrode",
"Certifaten van aandelen",
"Spreiding van zeewier in de Waddenzee",
"De aanleg van de Deltawerken in de 60- en 70-er jaren"
],
"Dutch"
);
console.log(result);
})();
}