From 9e8671c9c20777900cda5a8ffdb3935a8fc4c0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Thu, 8 Dec 2022 21:43:26 +0800 Subject: [PATCH] init --- data.json | 1 + package.json | 11 ++++++++ utils.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 data.json create mode 100644 package.json create mode 100644 utils.js diff --git a/data.json b/data.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..39d4e6d --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "chinese-word", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..45b7a05 --- /dev/null +++ b/utils.js @@ -0,0 +1,78 @@ +const fs = require('fs'); +const path = require('path'); + +const DataFile = path.join(__dirname, './data.json') +let data = JSON.parse(fs.readFileSync(DataFile, 'utf8')) + +function structure() { + return { + // 汉字 + word: "", + // 拼音 + pinyin: [], + // 笔画 + stroke: 0, + // 类型 0-未标注 1-常用字 2-生僻字 + type: 0, + // 来源(从original文件夹中哪个来源录入的) + from: [], + // 是否人工确认过 + confirm: false, + } +} + +function saveData() { + fs.writeFileSync(DataFile, JSON.stringify(data), 'utf8') +} + +function addData(wordStruct, saveData = true) { + let word = wordStruct.word + if (Object.keys(data).includes(word)) { + // 已经存在 + let struct = data[word] + let isNeedUpdate = false + + // 合并拼音 + for (let py of wordStruct.pinyin) { + if (!struct.pinyin.includes(py)) { + struct.pinyin.push(py) + isNeedUpdate = true + } + } + struct.pinyin.sort() + + // 合并笔画 + if (wordStruct.stroke > 0 && wordStruct.stroke != struct.stroke) { + if (struct.stroke != 0) { + console.log(`${wordStruct.word} 字的笔画(stroke)出现歧义 [${struct.stroke}, ${wordStruct.stroke}],当前保存 ${wordStruct.stroke}`) + } + struct.stroke = wordStruct.stroke + } + + // 合并类型 + if (wordStruct.type > 0 && wordStruct.type != struct.type) { + if (struct.type != 0) { + console.log(`${wordStruct.word} 字的类型(type)出现歧义 [${struct.type}, ${wordStruct.type}],当前保存 ${wordStruct.type}`) + } + struct.type = wordStruct.type + } + + if (isNeedUpdate) { + data[word] = struct + saveData && saveData() + } + } else { + // 还不存在 + data[wordStruct.word] = wordStruct + isNeedUpdate = true + + saveData && saveData() + } + +} + +module.exports = { + structure, + addData, + saveData +} \ No newline at end of file