UNPKG

js-mdict

Version:

mdict (*.mdx, *.mdd) file reader. Licensed under AGPL-3.0 for better community cooperation and commercial value protection.

177 lines 5.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MDX = void 0; const mdict_js_1 = require("./mdict.js"); const utils_js_1 = __importDefault(require("./utils.js")); class MDX extends mdict_js_1.Mdict { /** * lookup the word * @tests ok * @param word search word * @returns word definition */ lookup(word) { const keyWordItem = this.lookupKeyBlockByWord(word); if (!keyWordItem) { return { keyText: word, definition: null }; } const def = this.lookupRecordByKeyBlock(keyWordItem); if (!def) { return { keyText: word, definition: null }; } return { keyText: word, definition: this.meta.decoder.decode(def) }; } ; /** * lookup all entries matching the word * useful when dictionary has duplicate keys (e.g., main entry + image + link) * @param word search word * @returns array of all matching entries */ lookupAll(word) { const matchedItems = this.keywordList.filter(item => { return this.comp(item.keyText, word) === 0; }); return matchedItems.map(item => { const def = this.lookupRecordByKeyBlock(item); return { keyText: item.keyText, definition: def ? this.meta.decoder.decode(def) : null }; }); } fetch(keywordItem) { const def = this.lookupRecordByKeyBlock(keywordItem); if (!def) { return { keyText: keywordItem.keyText, definition: null }; } return { keyText: keywordItem.keyText, definition: this.meta.decoder.decode(def) }; } /** * search the prefix like the phrase in the dictionary * @tests ok * @param prefix prefix search phrase * @returns the prefix related list */ prefix(prefix) { const keywordList = this.associate(prefix); return keywordList.filter(item => { return item.keyText.startsWith(prefix); }); } /** * search matched list of associate words * @tests ok * @param phrase associate search likely workds * @returns matched list */ associate(phrase) { const keyBlockItem = this.lookupKeyBlockByWord(phrase, true); if (!keyBlockItem) { return []; } return this.keywordList.filter((keyword) => { return keyword.keyBlockIdx == keyBlockItem.keyBlockIdx; }); } /** * suggest the phrase with the edit distance * @tests ok * @param phrase search phrase * @param distance edit distance * @returns the suggest list */ suggest(phrase, distance) { if (distance < 0 || distance > 5) { console.log('the edit distance should be in the range of 0 to 5'); return []; } const keywordList = this.associate(phrase); const suggestList = []; keywordList.forEach(item => { const key = this.strip(item.keyText); const ed = utils_js_1.default.levenshteinDistance(key, this.strip(phrase)); if (ed <= distance) { suggestList.push(item); } }); return suggestList; } fetch_definition(keywordItem) { const def = this.lookupRecordByKeyBlock(keywordItem); if (!def) { return { keyText: keywordItem.keyText, definition: null }; } return { keyText: keywordItem.keyText, definition: this.meta.decoder.decode(def) }; } /** * fuzzy search words list * @tests ok * @param word search word * @param fuzzy_size the fuzzy workd size * @param ed_gap edit distance * @returns fuzzy word list */ fuzzy_search(word, fuzzy_size, ed_gap) { const fuzzy_words = []; const keywordList = this.associate(word); keywordList.forEach(item => { const key = this.strip(item.keyText); const ed = utils_js_1.default.levenshteinDistance(key, this.strip(word)); if (ed <= ed_gap) { fuzzy_words.push(Object.assign(Object.assign({}, item), { ed: ed })); } }); fuzzy_words.sort((a, b) => { return a.ed - b.ed; }); return fuzzy_words.slice(0, fuzzy_size); } /** * search words that contain the specified substring * @param substring the text to search for * @param caseSensitive whether to perform case-sensitive search (default: false) * @param limit maximum number of results to return (default: 1000) * @returns list of keywords containing the substring */ contains(substring, caseSensitive = false, limit = 1000) { const searchKey = caseSensitive ? substring : substring.toLowerCase(); const matchedList = []; for (const item of this.keywordList) { const keyText = caseSensitive ? item.keyText : item.keyText.toLowerCase(); if (keyText.includes(searchKey)) { matchedList.push(item); if (matchedList.length >= limit) { break; } } } return matchedList; } } exports.MDX = MDX; //# sourceMappingURL=mdx.js.map