/
index.ts
256 lines (213 loc) · 6.76 KB
/
index.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { stringify } from "query-string";
import langId from "./resolver";
type TransOptions = {
from?: string;
to: string;
interfaceLang?: string;
htmlTag?: boolean;
resolve?: boolean;
axiosConfig?: AxiosRequestConfig,
contents?: Array<'t' | 'at' | 'bd' | 'ex' | 'ld' | 'md' | 'qca' | 'rw' | 'rm' | 'ss'>,
};
type PartOfSpeechDefinition = {
[key: string]: [{
definition: string;
example: string;
synonyms: string[];
}];
};
type PartOfSpeechTranslation = {
[key: string]: [{
word: string;
translations: string[];
frequency: string;
}];
};
interface ReadableFormat {
translated?: string;
sourceText?: string;
from: string;
to: string;
isCorrected?: boolean;
corrected?: string;
pronunciation?: string;
definitions?: PartOfSpeechDefinition;
translations?: PartOfSpeechTranslation;
synonyms?: {
[key: string]: string[][];
};
examples?: string[];
related?: string[];
}
interface CustomAxiosResponse extends AxiosResponse {
data: ReadableFormat;
}
async function translate(text: string, options: TransOptions): Promise<CustomAxiosResponse> {
const {
from = 'auto',
to,
interfaceLang = 'en',
htmlTag = false,
resolve = true,
axiosConfig,
contents = ['t', 'at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss'],
} = options;
const property = stringify({
client: 'gtx',
sl: from,
tl: to,
hl: interfaceLang,
dt: contents,
ie: 'UTF-8',
oe: 'UTF-8',
otf: 1,
ssel: 0,
tsel: 0,
kc: 7,
q: text,
});
const url = `https://translate.googleapis.com/translate_a/single?${property}`;
const result = await axios.get(url, axiosConfig);
const { data } = result;
// create return object
let readable: ReadableFormat = {
from: data[8] && data[8][3] || data[8][0] ? data[8][3][0] || data[8][0][0] || 'auto' : 'auto',
to: to,
};
// push translated and sourceText
if (data[0]) {
readable.translated = '';
readable.sourceText = '';
for (const iter of data[0]) {
if (iter[0]) readable.translated += iter[0];
if (iter[1]) readable.sourceText += iter[1];
if (data[0][1] && data[0][1][3])
readable.pronunciation = data[0][1][3] || '';
}
}
// change lang id to full lang name
if (resolve) {
if (readable.from in langId) readable.from = langId[readable.from];
if (readable.to in langId) readable.to = langId[readable.to];
else readable.to = langId['en'];
// if user give lang id incorrectly, google will always translate to english
} else if (!(readable.to in langId)) {
readable.to = 'en';
}
// auto correct feature
if (data[7] && data[7].length) {
readable.isCorrected = true;
readable.corrected = data[7][0] || data[7][1];
if (!htmlTag) {
// replace html tag with markdown format
readable.corrected = readable.corrected?.replace(/(?:<b>|<\/b>)/g, '**').replace(/(?:<i>|<\/i>)/g, '*');
}
} else {
readable.isCorrected = false;
}
const speechList = data[1];
if (speechList) {
for (let i = 0; i < speechList.length; i++) {
speechList[i][2].forEach((elem: any[]) => {
// A hypothesis according to a number in the json file not 100% accurate maybe 80% :)
let freq: string;
if (elem[3] > 0.05) {
freq = 'common';
} else if (elem[3] >= 0.0025) {
freq = 'uncommon';
} else {
freq = 'rare';
}
// push to the readable object where synonymsList[i][0] is oneof :
// "adjective" | "noun" | "verb"
if (!readable.translations) readable.translations = {};
if (!readable.translations[speechList[i][0]])
readable.translations[speechList[i][0]] = [] as any;
readable.translations[speechList[i][0]].push({
word: elem[0],
translations: elem[1],
frequency: freq
});
});
}
}
const synonyms = data[11];
if (synonyms) {
if (!readable.synonyms) readable.synonyms = {};
synonyms.forEach((elem: any[]) => {
const speechName = elem[0]; // "noun" etc.
if (!readable.synonyms![speechName]) readable.synonyms![speechName] = [] as any;
elem[1].forEach((el: any[]) => {
readable.synonyms![speechName].push(el[0]);
});
});
}
// definitions & the examples
if (data[12]) {
if (!readable.definitions) readable.definitions = {};
data[12].forEach((elem: any[]) => {
let speechName = elem[0]; // "noun" etc.
// in case if google give an empty string just add to others property
if (!speechName) speechName = 'others';
// create an empty list if doesn't exist
if (!readable.definitions![speechName]) readable.definitions![speechName] = [] as any;
// elem[1] is list of all synonyms which divided by metaId
// let's just say "m_en_gbus0888500.013" is metaId
elem[1].forEach((defList: string[]) => {
const metaId = defList[1]; // "m_en_gbus0888500.013"
let synonymsList: string[] = [];
if (synonyms) synonyms.forEach((e: any[]) => {
// e[0] is "verb" etc.
if (speechName === e[0]) {
let matching = 0;
// e[1] is list of synonyms at iter[0] and metaId at iter[1]
for (const iter of e[1]) {
if (metaId === iter[1]) {
synonymsList.push(...iter[0]);
matching++;
} else if (matching > 0) {
// for efficiency when the metaId were the same and nothing more it'll break the loop
break;
}
}
}
});
readable.definitions![speechName].push({
definition: defList[0],
example: defList[2] || '',
synonyms: synonymsList,
});
});
});
}
// example sentences
if (data[13]) readable.examples = data[13][0].map((elem: any) => {
if (htmlTag) return elem[0];
// replace html tag to markdown
return elem[0].replace(/(?:<b>|<\/b>)/g, '**');
});
if (data[14] && data[14][0])
readable.related = data[14][0][0];
// appear when you translate v2/v3 word e.g. drunk
result.data = readable;
return result;
}
export = translate;
translate.validateLangId = (langCode: string): string | boolean => {
if (typeof langCode !== 'string') return false;
if (langCode in langId) return langId[langCode]; else return false;
};
translate.getFixedT = (langCode: string) => {
if (!translate.validateLangId(langCode)) throw new Error("Invalid language code");
async function fixed(text: string) {
try {
const translated = await translate(text, { to: langCode, contents: ['t'] })
.then(res => res.data.translated);
return translated;
} catch (e) {
return text;
}
}
return fixed;
};