-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
245 lines (212 loc) · 5.94 KB
/
client.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
import { array, float, nullable, object, optional, string } from 'cast.ts'
import { defaultConfig } from './config'
let parser = object({
target_lang: string({ sampleValue: 'en' }),
source_lang: nullable(string({ sampleValue: 'de' })),
detected_langs: optional(array(string({ sampleValue: 'de' }))),
translated: array(string({ sampleValue: 'Hello world' })),
translation_time: float({ sampleValue: 77.64211463928223 }),
})
export type TranslateOptions = {
/** @default 'localhost' */
host?: string
/** @default 24080 */
port?: number
/** @example 'Hello World!' */
text: string
/** @example 'zh' */
target_lang: string
/** @description auto detect if not specified */
source_lang?: string
/** @default false */
debug?: boolean
}
/**
* @description set HTTP request to the translate service running in the docker container
* */
export async function translate(options: TranslateOptions): Promise<string> {
let host = options.host || 'localhost'
let port = options.port || defaultConfig.port
let params = new URLSearchParams({
target_lang: options.target_lang,
text: options.text,
})
if (options.source_lang) {
params.set('source_lang', options.source_lang)
}
let url = `http://${host}:${port}/translate?${params}`
let res = await fetch(url)
let json = await res.json()
if (options.debug) {
console.error('translate:', { options, result: json })
}
let result = parser.parse(json)
return result.translated[0]
}
let async_queue = Promise.resolve()
// source_lang -> target_lang -> in_text -> out_text
let source_target_text_cache = new Map<
string,
Map<string, Map<string, string>>
>()
export type PatchedTranslateOptions = TranslateOptions & {
/**
* @description to avoid ajax timeout when doing lots on translate concurrently
* @default true
* */
async_queue?: boolean
/**
* @description to keep in-memory cache
* @default true
* */
cached?: boolean
/**
* @description to avoid repeating (wrong) result.
* e.g. without wrapping: Transparent -> 透明透明
* @default true
*/
wrap_text?: boolean
/**
* @description trim the output if the input is already trimmed
* @default true
*/
smart_trim?: boolean
}
/**
* @description apply combination of fixes to workaround common errors
*/
export async function patchedTranslate(
options: PatchedTranslateOptions,
): Promise<string> {
if (!options.text.trim()) {
return options.text
}
return cachedTranslate(options)
}
async function cachedTranslate(
options: PatchedTranslateOptions,
): Promise<string> {
if (options.cached == false) {
return trimmedTranslate(options)
}
let source_lang = options.source_lang || '?'
let target_text_cache = source_target_text_cache.get(source_lang)
if (!target_text_cache) {
target_text_cache = new Map()
source_target_text_cache.set(source_lang, target_text_cache)
}
let text_cache = target_text_cache.get(options.target_lang)
if (!text_cache) {
text_cache = new Map()
target_text_cache.set(options.target_lang, text_cache)
}
let out = text_cache.get(options.text)
if (!out) {
out = await trimmedTranslate(options)
text_cache.set(options.text, out)
}
return out
}
async function trimmedTranslate(
options: PatchedTranslateOptions,
): Promise<string> {
if (options.smart_trim == false) {
return wrappedTranslate(options)
}
let in_text = options.text
let out_text = await wrappedTranslate(options)
if (in_text == in_text.trim()) {
out_text = out_text.trim()
}
return out_text
}
async function wrappedTranslate(
options: PatchedTranslateOptions,
): Promise<string> {
let need_wrap = options.source_lang != 'zh'
if (options.wrap_text == false || !need_wrap) {
return queuedTranslate(options)
}
let in_text = options.text
let wrapped_in_text = in_text
// switch case
if (
in_text.length >= 2 &&
isUpperCase(in_text[0]) &&
isLowerCase(in_text[1])
) {
wrapped_in_text = in_text[0].toLocaleLowerCase() + in_text.slice(1)
}
// wrap with sentence ending char
let is_wrapped = in_text.endsWith(':') || in_text.endsWith(':')
if (!is_wrapped) {
wrapped_in_text += ':'
}
options.text = wrapped_in_text
let out_text = await queuedTranslate(options)
// unwrap ending char
if (!is_wrapped && (out_text.endsWith(':') || out_text.endsWith(':'))) {
out_text = out_text.slice(0, -1)
}
return out_text
}
function isLowerCase(char: string) {
return char && char == char.toLocaleLowerCase()
}
function isUpperCase(char: string) {
return char && char == char.toLocaleUpperCase()
}
function queuedTranslate(options: PatchedTranslateOptions): Promise<string> {
if (options.async_queue == false) {
return translate(options)
}
return new Promise<string>((resolve, reject) => {
async_queue = async_queue.then(() =>
translate(options).then(resolve).catch(reject),
)
})
}
/**
* @description release the memory used by patchedTranslate()
*/
export function clearCache() {
for (let target_text_cache of source_target_text_cache.values()) {
for (let text_cache of target_text_cache.values()) {
text_cache.clear()
}
target_text_cache.clear()
}
source_target_text_cache.clear()
}
/**
* @description optionally step to preload the translate model before the actual usage.
*/
export async function preloadModel(options: {
target_lang: string
source_lang: string
/**
* @description to log in console or not
* @default false
*
*/
debug?: boolean
}): Promise<void> {
if (options.debug) {
console.error('preloading model:', {
target_lang: options.target_lang,
source_lang: options.source_lang,
})
}
await translate({
text: 'preload model',
target_lang: options.target_lang,
source_lang: options.source_lang,
debug: options.debug,
})
if (options.debug) {
console.error('preloaded model:', {
target_lang: options.target_lang,
source_lang: options.source_lang,
})
}
}