added simple translation program

This commit is contained in:
松浦 知也 Matsuura Tomoya 2023-12-04 15:10:47 +09:00
parent 4b35e15829
commit 7c6297bac9
4 changed files with 242 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
resources resources
public/ public/
.DS_Store .DS_Store
content/.obsidian content/.obsidian
node_modules

158
package-lock.json generated Normal file
View File

@ -0,0 +1,158 @@
{
"name": "teach-matsuuratomoya-com",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "teach-matsuuratomoya-com",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"deepl-node": "^1.11.0"
}
},
"node_modules/@types/node": {
"version": "20.10.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.3.tgz",
"integrity": "sha512-XJavIpZqiXID5Yxnxv3RUDKTN5b81ddNC3ecsA0SoFXz/QU8OGBwZGMomiq0zw+uuqbL/krztv/DINAQ/EV4gg==",
"dependencies": {
"undici-types": "~5.26.4"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz",
"integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==",
"dependencies": {
"follow-redirects": "^1.15.0",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/axios/node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/deepl-node": {
"version": "1.11.0",
"resolved": "https://registry.npmjs.org/deepl-node/-/deepl-node-1.11.0.tgz",
"integrity": "sha512-PLz38WIfWzVScbz4S+32vI9yCUAXy3cpkKSMPC+IDpvrwKrQ2TSzwkoBh7CJOkPjaX8ZMvjwAc+C5eTCPamQTg==",
"dependencies": {
"@types/node": ">=12.0",
"axios": ">=0.21.2 <1.2.0 || >=1.2.2",
"form-data": "^3.0.0",
"loglevel": ">=1.6.2"
},
"engines": {
"node": ">=12.0"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/follow-redirects": {
"version": "1.15.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz",
"integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/loglevel": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/loglevel/-/loglevel-1.8.1.tgz",
"integrity": "sha512-tCRIJM51SHjAayKwC+QAg8hT8vg6z7GSgLJKGvzuPb1Wc+hLzqtuVLxp6/HzSPOozuK+8ErAhy7U/sVzw8Dgfg==",
"engines": {
"node": ">= 0.6.0"
},
"funding": {
"type": "tidelift",
"url": "https://tidelift.com/funding/github/npm/loglevel"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="
}
}
}

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "teach-matsuuratomoya-com",
"version": "1.0.0",
"description": "[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc-sa/4.0/)",
"main": "translate.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"deepl-node": "^1.11.0"
}
}

68
translate.js Normal file
View File

@ -0,0 +1,68 @@
const fs = require("fs").promises;
const path = require("path");
const API_KEY = process.env.DEEPL_API_KEY;
const deepl = require('deepl-node');
const translator = new deepl.Translator(API_KEY);
const main = async () => {
try {
if (!process.argv[2]) {
throw new Error("File path is not set.")
}
const file_path = process.argv[2];
const writepath = path.dirname(file_path) + '/' + path.parse(file_path).name + ".en" + path.extname(file_path);
console.log(`source: ${file_path}\ndst: ${String(writepath)}`)
console.log(`API_KEY:${API_KEY}`)
const data = await fs.readFile(file_path, 'utf8');
let chunk = [];
const last = data.split('\n').reduce((acc, line) => {
if (acc && acc.length > 5000) {
chunk.push(acc);
acc = "";
return line
} else {
return acc + "\n" + line
}
}, "");
chunk.push(last);
if (chunk[0]) {
const res = await Promise.all(chunk.map(c => {
console.debug(c);
return deeplTranslate(c)
}
))
const resstr = fixWrongFormatting(res.join("\n"));
await fs.writeFile(writepath, resstr)
}
} catch (error) {
console.error(error)
}
}
async function deeplTranslate(deeplInput) {
return translator
.translateText(deeplInput, "ja", 'en-us')
.then((result) => {
return result.text
})
}
function fixWrongFormatting(src) {
let res = "";
//fix codeblock
res = src.replace(/You can use the ``sh/g, "```sh");
res = res.replace(/`{2}sh\n/g, "```sh\n");
res = res.replace(/^.*``([a-z]*)\n/g, "```$1\n");
res = res.replace(/`{4,}/g, "```");
//fix inline code
res = res.replace(/`{2}(.+)`{2}/g, "`$1`");
//fix image link
res = res.replace(/![\t\s]+\[/g, "![");
//fix link
res = res.replace(/\][\t\s]+\(/g, "](");
//fix hugo shortcode
res = res.replace(/>\}{2,}/g, ">}}");
return res
}
main();