This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathengine_mdict.ts
122 lines (113 loc) · 3.41 KB
/
engine_mdict.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fetch = require('node-fetch');
const os = require('os');
const fs = require('fs');
const path = require('path');
const queryString = require('query-string');
import _ = require('lodash');
const { Book, Word } = require('./data');
const Config = require('./config');
const moment = require('moment');
const sqlite3 = require('sqlite3').verbose();
const cheerio = require('cheerio');
class SqliteAsync {
db: any;
constructor(db_path, mode) {
this.db = new sqlite3.Database(db_path, mode);
}
async get(sql, ...params): Promise<any> {
return new Promise((resolve) => {
this.db.get(sql, ...params, (err, row) => {
resolve([err, row]);
});
});
}
}
class MdictClient {
db: SqliteAsync;
path: string;
constructor(dict_path?: string) {
if (dict_path == null) dict_path = path.resolve(__dirname, 'mdict/oalecd9.db'); //默认使用牛津9
this.path = dict_path;
this.db = new SqliteAsync(dict_path, sqlite3.OPEN_READONLY);
}
async lookup(name: string) {
let [err, row] = await this.db.get('SELECT * from mdx where entry = ? limit 1', name);
if (err != null || row == null) return null;
if (row.paraphrase.indexOf('@@@LINK') === 0) return null; //不关联
const $ = cheerio.load(row.paraphrase);
let top = $('top-g');
let word = new Word();
word.name = name;
// 找美音
let audious = top.find('audio-us');
if (audious.length > 0) {
audious = $(audious.get(0));
word.audioUS = { n: audious.find('phon').text(), f: audious.parent().attr('href') };
word.audioUS.f = await this.getRes(word.audioUS.f);
}
let hg = $('h-g');
if (hg.length > 0) {
word.cn = `<h-g>${$('h-g').html()}</h-g>`;
} else {
//多个词性
let divs = $('subentry-g');
word.cn = divs
.map((i, v) => {
return `<div>${$(v).parent().html()}</div>`;
})
.get()
.join('\n');
// debugger
}
// idiom习语
$('idm-g').each((i, v) => {
v = $(v);
let sw = new Word();
let idms = [];
v.find('idm').each((i, idm) => {
idms.push($(idm).text().replace(/[ˌˈ]/g, ''));
});
sw.name = idms.join(' | ');
sw.cn = `<div>${v.html()}</div>`;
sw.tags.push('idm');
word.pharas.push(sw);
});
// phr v
$('pv-g').each((i, pvg) => {
pvg = $(pvg);
let sw = new Word();
let pvs = [];
pvg.find('pv').each((j, pv) => {
pvs.push($(pv).text().replace(/[ˌˈ]/g, ''));
});
sw.name = pvs.join(' | ');
sw.cn = `<div>${pvg.html()}</div>`;
sw.tags.push('phr-v');
word.pharas.push(sw);
});
return word;
}
async getRes(res) {
if (res.indexOf('://') > 0) {
res = '\\' + res.split('://')[1];
}
let fname = path.resolve(__dirname, 'temp', res.substr(1));
if (fs.existsSync(fname)) {
return fname;
}
let [err, row] = await this.db.get('SELECT * from mdd where entry = ?', res);
if (err == null && row != null) {
// let buf = new Buffer.from(row)
fs.writeFileSync(fname, row.file);
return fname;
}
return null;
}
}
async function test() {
// let client =new MdictClient('/Volumes/D/DevEnvs/dicts/牛津高阶第九版 v3.1.2/牛津高阶双解(第9版)_V3.1.2版.mdx')
let client = new MdictClient(path.resolve(__dirname, 'mdict/oalecd9.db'));
let word = await client.lookup('due');
}
// test()
export default MdictClient;