Created
October 16, 2022 15:05
-
-
Save 7shi/a037ce789cf1c38cb22e94186cd450f7 to your computer and use it in GitHub Desktop.
[Deno] DeepL JSON Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function* splitLines(text: string) { | |
let line = "", prev = ""; | |
for (const ch of text) { | |
if (ch == '\r') { | |
yield line; | |
line = ""; | |
} else if (ch == '\n') { | |
if (prev != '\r') yield line; | |
line = ""; | |
} else { | |
line += ch; | |
} | |
prev = ch; | |
} | |
if (line.length) yield line; | |
} | |
export function readLines(fileName: string) { | |
return Array.from(splitLines(Deno.readTextFileSync(fileName))); | |
} | |
export function writeLines(fileName: string, lines: string[]) { | |
Deno.writeTextFileSync(fileName, lines.join("\n") + "\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writeLines } from "./common.ts"; | |
if (Deno.args.length != 3) { | |
throw "usage: deno run --allow-read --allow-write convert.ts dir out1 out2"; | |
} | |
const [dir, out1, out2] = Deno.args; | |
const files = Array.from(Deno.readDirSync(dir)) | |
.map(de => de.name) | |
.filter(n => n.endsWith(".json")); | |
files.sort(); | |
const src: string[] = [], dst: string[] = []; | |
for (const f of files) { | |
const json = JSON.parse(Deno.readTextFileSync(dir + "/" + f)); | |
if (!json.result) continue; | |
if (!src.length && json.result.texts) { | |
for (const text of json.result.texts) { | |
if (src.length) src[src.length - 1] += "\n"; | |
for (const ch of text.chunks) { | |
if (ch.sentences.length != 1) throw "sentences.length: " + ch.sentences.length; | |
src.push(ch.sentences[0].text); | |
} | |
} | |
} else if (src.length && json.result.translations) { | |
for (const t of json.result.translations) { | |
if (t.beams.length != 1) throw "beams.length: " + t.beams.length; | |
const s = t.beams[0].sentences; | |
if (s.length != 1) throw "sentences.length: " + s.length; | |
if (s[0].ids.length != 1) throw "ids.length: " + s[0].ids; | |
const id = s[0].ids[0]; | |
dst[id] = s[0].text + (src[id].endsWith("\n") ? "\n" : ""); | |
} | |
} | |
} | |
if (src.length != dst.length) throw "length not match"; | |
if (out1 != "-") writeLines(out1, src); | |
if (out2 != "-") writeLines(out2, dst); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readLines, writeLines } from "./common.ts"; | |
if (Deno.args.length < 3) { | |
throw "usage: deno run --allow-read --allow-write mix.ts output input1 input2 [...]"; | |
} | |
const [fout, ...fins] = Deno.args; | |
const texts = fins.map(readLines); | |
for (let i = 1; i < texts.length; i++) { | |
if (texts[0].length != texts[i].length) throw "length not match: " + fins[i]; | |
} | |
const result: string[] = []; | |
for (let i = 0; i < texts[0].length; i++) { | |
for (let j = 0; j < texts.length; j++) { | |
const t = texts[j][i]; | |
result.push(t); | |
if (j == 0 && t == "") break; | |
} | |
} | |
writeLines(fout, result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readLines, writeLines } from "./common.ts"; | |
if (Deno.args.length < 3) { | |
throw "usage: deno run --allow-read --allow-write mix.ts input output1 output2 [...]"; | |
} | |
const [fin, ...fouts] = Deno.args; | |
const lines = readLines(fin); | |
const results: string[][] = Array(fouts.length).fill().map(_ => []); | |
let ln = 0; | |
for (let i = 0; i < lines.length; i++) { | |
const line = lines[i]; | |
if (line == "") { | |
if (ln) throw "illegal new line: " + (i + 1); | |
for (const r of results) r.push(""); | |
} else { | |
results[ln].push(line); | |
ln = (ln + 1) % results.length; | |
} | |
} | |
if (ln) throw "illegal line numbers: " + ln; | |
for (let i = 0; i < fouts.length; i++) { | |
writeLines(fouts[i], results[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment