japanese-db
Version:
Generate Japanese dictionary SQLite database from open source materials
93 lines (74 loc) • 2.5 kB
JavaScript
/* Copyright (c) 2020 Ezzat Chamudi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* @typedef {import("./types/jmnedict").JMnedict} JMnedict.JMnedict
* @typedef {import("./types/jmnedict").entry} JMnedict.entry
*/
/** */
const fs = require('fs');
const xml2json = require('xml2json');
// eslint-disable-next-line import/prefer-default-export
export class JMnedictUtil {
/**
* @param {string} path
*/
constructor(path, shortEntities = true) {
// Properties
/** @type {string} */
this.dictDate = null;
/** @type {string} */
this.data = null;
this.jmnedictObj = null;
this.jmnedictEntries = null;
/** @type {Object<string, string>} */
this.entities = null;
// Constructor script
this.load(path, shortEntities);
}
/**
* Load JMnedict file
* @param {string} path
* @param {boolean} shortEntities
* If true, the entities will be the short version.
* @returns {void}
*/
load(path, shortEntities = true) {
/** @type {string} */
this.data = fs.readFileSync(path, 'utf8');
/** @type {RegExp} */
const entityRegex = /<!ENTITY (.*?) "(.*?)">/g;
// save entities
this.entities = {};
let captures = entityRegex.exec(this.data);
while (captures !== null) {
const key = captures[1];
const value = captures[2];
this.entities[key] = value;
captures = entityRegex.exec(this.data);
}
// remove entities from data
if (shortEntities) {
this.data = this.data.replace(/<!ENTITY (.*?) "(.*?)">/g, '<!ENTITY $1 "$1">');
}
/** @type {RegExp} */
const createdDateRegex = /<!-- JMnedict created: (.*?) -->/g;
({ 1: this.dictDate } = createdDateRegex.exec(this.data));
}
/**
* Get JMnedict Entries
* @returns {JMnedict.entry[]}
*/
getJMnedictEntries() {
if (this.jmnedictEntries) return this.jmnedictEntries;
const jmnedictObj = /** @type {JMnedict.JMnedict} */ (xml2json.toJson(this.data, {
object: true,
arrayNotation: true,
}));
this.jmnedictEntries = jmnedictObj.JMnedict[0].entry;
return jmnedictObj.JMnedict[0].entry;
}
}