Fix translation chunk splitting and translation process
This commit is contained in:
@@ -225,44 +225,41 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl
|
|||||||
chunks: [],
|
chunks: [],
|
||||||
resolvers: []
|
resolvers: []
|
||||||
}]
|
}]
|
||||||
|
|
||||||
|
|
||||||
async function translateTranslationChunks(force:boolean = false, additionalChunkLength = 0){
|
async function translateTranslationChunks(force:boolean = false, additionalChunkLength = 0){
|
||||||
const text: string = translationChunks[translationChunks.length-1].chunks.join('\n')
|
const currentChunk = translationChunks[translationChunks.length-1]
|
||||||
|
const text: string = currentChunk.chunks.join('\n■\n')
|
||||||
|
|
||||||
|
console.log(text)
|
||||||
if(!force && text.length + additionalChunkLength < 5000){
|
if(!force && text.length + additionalChunkLength < 5000){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
translationChunks.push({
|
||||||
|
chunks: [],
|
||||||
|
resolvers: []
|
||||||
|
})
|
||||||
|
|
||||||
const translated = await translate(text, reverse)
|
const translated = await translate(text, reverse)
|
||||||
|
|
||||||
const split = translated.split('\n')
|
const split = translated.split('■')
|
||||||
|
|
||||||
|
console.log(split.length, currentChunk.chunks.length)
|
||||||
|
|
||||||
let i=0;
|
if(split.length !== currentChunk.chunks.length){
|
||||||
let pointer = 0;
|
//try translating one by one
|
||||||
let pointer2 = 0;
|
for(let i = 0; i < currentChunk.chunks.length; i++){
|
||||||
let cl = ''
|
currentChunk.resolvers[i](
|
||||||
console.log(split, translationChunks[translationChunks.length-1].chunks)
|
await translate(currentChunk.chunks[i]
|
||||||
while(i < split.length){
|
, reverse))
|
||||||
const chunk = translationChunks[translationChunks.length-1]?.chunks?.[pointer]?.split('\n')
|
|
||||||
if(!chunk){
|
|
||||||
//error, should not happen. but if it does, just break the loop
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
cl += split[i] + '\n'
|
|
||||||
pointer2++
|
|
||||||
|
|
||||||
if(chunk.length === pointer2){
|
|
||||||
translationChunks[translationChunks.length-1].resolvers[pointer](cl.trim())
|
|
||||||
pointer++
|
|
||||||
pointer2 = 0
|
|
||||||
cl = ''
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
translationChunks[translationChunks.length-1].chunks = []
|
for(let i = 0; i < split.length; i++){
|
||||||
translationChunks[translationChunks.length-1].resolvers = []
|
console.log(split[i])
|
||||||
|
currentChunk.resolvers[i](split[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -354,7 +351,7 @@ export async function translateHTML(html: string, reverse:boolean, charArg:simpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
function needSuperChunkedTranslate(){
|
function needSuperChunkedTranslate(){
|
||||||
return false //not needed for now
|
return true //not needed for now
|
||||||
}
|
}
|
||||||
|
|
||||||
let llmCache = new Map<string, string>()
|
let llmCache = new Map<string, string>()
|
||||||
|
|||||||
Reference in New Issue
Block a user