hanfix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/eslint.config.js +7 -0
- package/package.json +41 -0
- package/src/hanfix.js +42 -0
- package/src/index.js +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Hanfix
|
|
2
|
+
|
|
3
|
+
`hanfix`는 [다음 맞춤법
|
|
4
|
+
검사기](https://dic.daum.net/grammar_checker.do)를 이용한 한글 맞춤법
|
|
5
|
+
검사기입니다. [hanspell](https://github.com/9beach/hanspell)에서
|
|
6
|
+
영감을 받아 작성했습니다. `hanspell`과 다른 점은 다음과 같습니다.
|
|
7
|
+
|
|
8
|
+
- 맞춤법 검사 결과를 JSON으로 리턴해 다른 프로그램(Emacs 등)에서 쉽게
|
|
9
|
+
사용할 수 있습니다.
|
|
10
|
+
- 맞춤법 검사는 다음 맞춤법 검사기만 사용합니다.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import js from "@eslint/js";
|
|
2
|
+
import globals from "globals";
|
|
3
|
+
import { defineConfig } from "eslint/config";
|
|
4
|
+
|
|
5
|
+
export default defineConfig([
|
|
6
|
+
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.node } },
|
|
7
|
+
]);
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hanfix",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Korean grammar checker using Daum grammar checker",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"한글",
|
|
7
|
+
"한국어",
|
|
8
|
+
"맞춤법",
|
|
9
|
+
"Korean",
|
|
10
|
+
"hangul",
|
|
11
|
+
"spell",
|
|
12
|
+
"spellchecker"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/ntalbs/hanfix#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/ntalbs/hanfix/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/ntalbs/hanfix.git"
|
|
21
|
+
},
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"author": "ntalbs",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "src/index.js",
|
|
26
|
+
"bin": {
|
|
27
|
+
"hanfix": "./src/index.js"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"lint": "eslint .",
|
|
31
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"jsdom": "^27.4.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@eslint/js": "^9.39.2",
|
|
38
|
+
"eslint": "^9.39.2",
|
|
39
|
+
"globals": "^17.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/hanfix.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { JSDOM } from 'jsdom';
|
|
2
|
+
|
|
3
|
+
const GRAMMAR_CHECKER_URL = 'https://dic.daum.net/grammar_checker.do';
|
|
4
|
+
const MAX_LEN = 1000;
|
|
5
|
+
|
|
6
|
+
export async function check(text) {
|
|
7
|
+
if (text.length > MAX_LEN) {
|
|
8
|
+
throw new Error(`Too large text. Input should not exceed ${MAX_LEN} characters.`);
|
|
9
|
+
}
|
|
10
|
+
let html = await send(text);
|
|
11
|
+
return parse(html);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function parse(html) {
|
|
15
|
+
const dom = new JSDOM(html);
|
|
16
|
+
const document = dom.window.document;
|
|
17
|
+
|
|
18
|
+
return [...document.querySelectorAll('div.cont_spell a')].map(node => {
|
|
19
|
+
return {
|
|
20
|
+
errorType: node.dataset.errorType,
|
|
21
|
+
input: node.dataset.errorInput,
|
|
22
|
+
output: node.dataset.errorOutput,
|
|
23
|
+
helpText: node.querySelector('[id="help"] li')?.textContent,
|
|
24
|
+
examples: [...node.querySelectorAll('#examples li')].map(e => e.textContent)
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function send(text) {
|
|
30
|
+
const res = await fetch(GRAMMAR_CHECKER_URL, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
34
|
+
},
|
|
35
|
+
body: new URLSearchParams({
|
|
36
|
+
'sentence': text
|
|
37
|
+
})
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = await res.text();
|
|
41
|
+
return result;
|
|
42
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { check } from './hanfix.js';
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const inputText = fs.readFileSync(0, 'utf8');
|
|
8
|
+
const result = await check(inputText);
|
|
9
|
+
|
|
10
|
+
let correctedText = inputText;
|
|
11
|
+
for (const e of result) {
|
|
12
|
+
correctedText = correctedText.replaceAll(e.input, e.output);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const output = {
|
|
16
|
+
errors: result,
|
|
17
|
+
corrected: correctedText
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// process.stdout.write(JSON.stringify(output));
|
|
21
|
+
console.log(output)
|
|
22
|
+
|
|
23
|
+
} catch (err) {
|
|
24
|
+
process.stderr.write(err.message);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|