mdict (*.mdd *.mdx) file reader based on jeka-kiselyov/mdict .
Very thanks to fengdh and jeka-kiselyov.
v6.0.0-alpha-2
NOTE: Not Support Browser!
npm install [email protected]
file: demo.mjs
import { Mdict } from "js-mdict";
const mdict = new Mdict("resources/oald7.mdx");
const def = mdict.lookup("ask");
console.log(def.definition);
/*
<head><link rel="stylesheet" type="text/css" href="O7.css"/></head><body><span class="hw"> ask </span hw><span class="i_g"> <img src="key.gif"/> /<a class="i_phon" href="sound://aask_ggv_r1_oa013910.spx">ɑ:sk</a i_phon><span class="z">; </span z><i>NAmE</i> <a class="y_phon" href="sound://aask_ggx_r1_wpu01057.spx">æsk</a y_phon>/ </span i_g><span class="cls"> verb</span cls><br><span class="sd">QUESTION<span class="chn"> 问题</span chn></span sd>
<div class="define"><span class="numb">1</span numb><span class="cf"> ~ <span class="bra">(</span bra>sb<span class="bra">)</span bra> <span class="bra">(</span bra>about sb/ sth<span class="bra">)</span bra> </span cf><span class="d">to say or write sth in the form of a question, in order to get information<span class="chn"> 问;询问</span chn></span d></div define>
<span class="phrase"><span class="pt"> [<span class="pt_inside">V <span class="pt_bold">speech</span></span><span>]</span> </span pt></span phrase>
<span class="sentence_eng">'Where are you going?' she asked. </span sentence_eng>
<span class="sentence_chi">"你去哪里?"她问道。</span sentence_chi>
<span class="phrase"><span class="pt"> [<span class="pt_inside">VN <span class="pt_bold">speech</span></span><span>]</span> </span pt></span phrase>
<span class="sentence_eng">'Are you sure?' he asked her. </span sentence_eng>
...
</body>
*/
file: demo.mjs
const { Mdict } = require('js-mdict');
const mdict = new Mdict('resources/oald7.mdx');
const def = mdict.lookup('ask');
console.log(def.definition);
/*
<head><link rel="stylesheet" type="text/css" href="O7.css"/></head><body><span class="hw"> ask </span hw><span class="i_g"> <img src="key.gif"/> /<a class="i_phon" href="sound://aask_ggv_r1_oa013910.spx">ɑ:sk</a i_phon><span class="z">; </span z><i>NAmE</i> <a class="y_phon" href="sound://aask_ggx_r1_wpu01057.spx">æsk</a y_phon>/ </span i_g><span class="cls"> verb</span cls><br><span class="sd">QUESTION<span class="chn"> 问题</span chn></span sd>
<div class="define"><span class="numb">1</span numb><span class="cf"> ~ <span class="bra">(</span bra>sb<span class="bra">)</span bra> <span class="bra">(</span bra>about sb/ sth<span class="bra">)</span bra> </span cf><span class="d">to say or write sth in the form of a question, in order to get information<span class="chn"> 问;询问</span chn></span d></div define>
<span class="phrase"><span class="pt"> [<span class="pt_inside">V <span class="pt_bold">speech</span></span><span>]</span> </span pt></span phrase>
<span class="sentence_eng">'Where are you going?' she asked. </span sentence_eng>
<span class="sentence_chi">"你去哪里?"她问道。</span sentence_chi>
<span class="phrase"><span class="pt"> [<span class="pt_inside">VN <span class="pt_bold">speech</span></span><span>]</span> </span pt></span phrase>
<span class="sentence_eng">'Are you sure?' he asked her. </span sentence_eng>
...
</body>
*/
use oale8.mdx as an example:
Mdict#loading time: 0 sec
Mdict#lookup x 1,967 ops/sec ±0.95% (97 runs sampled)
Mdict#prefix x 1,800 ops/sec ±0.70% (96 runs sampled)
Mdict#fuzzy_search x 285 ops/sec ±14.03% (77 runs sampled)
Mdict#associate x 2,059 ops/sec ±4.53% (89 runs sampled)
Fastest is Mdict#associate
depreciate if you use js-mdict @2.0.3, you can use api shown below:
Note: 2.0.3 not supports mdd file, and record info encrypted file
import path from 'path';
import Mdict from 'js-mdict';
const dictPath = path.join(__dirname, '../resource/Collins.mdx');
const mdict = new Mdict(dictPath);
mdict
.build()
.then((_mdict) => {
console.log('hello', _mdict.lookup('hello'));
console.log('world', _mdict.lookup('world'));
console.log(_mdict.attr());
})
.catch((err) => {
console.error(err);
});
- implements with TypeScript
BREAKING CHANGES:
- delete large files in history
- delete suggest, associate etc. functions.
NOTE: This version has a lot of changes, please read the example code example carefully!
注意: 本版本改动了多个 API, 请仔细阅读上方的示例代码。
BREAKING CHANGES:
- Refactor the key search algorithm: build the keyList first, and resort internally, then search the word
- The
suggest
method has been deprecated. - The
parse_definition
method has been deprecated, usefetch_definition
instead - Delete
nspell
anden-dictionary-us
dependencies - Cleanup unstable compare method and key record compare, won't use
keyInfoBlock.firstKey/lastKey
to locaterecordBlock
- Use built-in
TextDecoder
andzlib
, instead of third partyTextDecoder
andpako
. - use pnpm instead of yarn to manage the dependencies.
- Have fun!
- upgrade xmldom to v0.7.5
- support rangeKey API
- add rangeKeyBlock interface
- enhance performance of readKeyBlock
- fix associate can't find special character bug
- fix accurate record start offset parse_definition bug
- fix
findList
returnundefined
will crash theassociate
andprefix
method bug
- fix babel-runtime dependencies issue
- fix UpperCase key sensitive options logic, details #41
- fix 1.2 mdx keyblock read bug
- correct some Header properties (StripKey..)
very thanks to @songxiaocheng
- fix typings declaring and reformat codebase
- fix some
.mdd
file reading issues, and if you search mdd file, uselookup
method, and it will return base64 data
- rewrite
typings/mdict.d.ts
- rename
typings/Mdict.d.ts
totypings/mdict.d.ts
- fix uppercase words comparing missed bug
- fix
out of index error
when cannot locate word offset - if cannot find the word key block, return
undefined
- rename Mdict.js to mdict.js , rename MdictBase.js to mdict-base.js, fix import error on ubuntu.
- support search words by prefix
associate
(the phrase as the words' prefix, not the phrase's prefix as search token just likeprefix
function) - some security updates
very thanks to @Danjame
- ES6 implemention
- rewrite the decode code, more readable decode api
- NOT SUPPORT BROWSER CURRENTLY
- add
fuzzy_search
method, which supports fuzzy word search
this picture is from @ikey4u/wikit
code by terasum with ❤️