1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

104 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-12-08 21:43:26 +08:00
const fs = require('fs');
const path = require('path');
const DataFile = path.join(__dirname, './data.json')
2022-12-08 22:08:36 +08:00
const DataFile2 = path.join(__dirname, './data-friendly.json')
2022-12-08 23:36:39 +08:00
const DataFile3 = path.join(__dirname, './data-single-word.txt')
2022-12-08 21:43:26 +08:00
let data = JSON.parse(fs.readFileSync(DataFile, 'utf8'))
function structure() {
return {
// 汉字
word: "",
// 拼音
pinyin: [],
2022-12-08 22:34:27 +08:00
// 部首
radical: [],
2022-12-08 21:43:26 +08:00
// 笔画
stroke: 0,
// 类型 0-未标注 1-常用字 2-生僻字
type: 0,
// 来源从original文件夹中哪个来源录入的
from: [],
// 是否人工确认过
confirm: false,
}
}
function saveData() {
2022-12-08 22:08:36 +08:00
const ObjSort = function (arys) {
2022-12-08 22:50:40 +08:00
var newkey = Object.keys(arys).sort((a, b) => a.localeCompare(b));
2022-12-08 22:08:36 +08:00
var newObj = {}; //创建一个新的对象,用于存放排好序的键值对
for (var i = 0; i < newkey.length; i++) {
newObj[newkey[i]] = arys[newkey[i]];
}
return newObj; //返回排好序的新对象
}
data = ObjSort(data)
2022-12-08 21:43:26 +08:00
fs.writeFileSync(DataFile, JSON.stringify(data), 'utf8')
2022-12-08 22:08:36 +08:00
fs.writeFileSync(DataFile2, JSON.stringify(data, null, 4), 'utf8')
2022-12-08 23:36:39 +08:00
fs.writeFileSync(DataFile3, Object.keys(data).join(''), 'utf8')
2022-12-08 21:43:26 +08:00
}
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) {
2022-12-08 22:50:40 +08:00
// 同时在生僻字和常见字出现 记为常见字
console.log(`${wordStruct.word} 字的类型(type)出现歧义 [${struct.type}, ${wordStruct.type}],当前保存 1`)
struct.type = 1
2022-12-08 21:43:26 +08:00
}
struct.type = wordStruct.type
}
2022-12-08 22:50:40 +08:00
// 合并来源
for (let fr of wordStruct.from) {
if (!struct.from.includes(fr)) {
struct.from.push(fr)
isNeedUpdate = true
}
}
struct.from.sort()
2022-12-08 21:43:26 +08:00
if (isNeedUpdate) {
data[word] = struct
saveData && saveData()
}
} else {
// 还不存在
data[wordStruct.word] = wordStruct
isNeedUpdate = true
saveData && saveData()
}
}
module.exports = {
structure,
addData,
saveData
}