1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2022-12-08 21:43:26 +08:00
commit 9e8671c9c2
3 changed files with 90 additions and 0 deletions

1
data.json Normal file
View File

@ -0,0 +1 @@
{}

11
package.json Normal file
View File

@ -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"
}

78
utils.js Normal file
View File

@ -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
}