forked from rust-lang/mdBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunr.zh.js
156 lines (136 loc) · 5.19 KB
/
lunr.zh.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*!
* Lunr languages, `Chinese` language
* https://github.com/MihaiValentin/lunr-languages
*
* Copyright 2019, Felix Lian (repairearth)
* http://www.mozilla.org/MPL/
*/
/*!
* based on
* Snowball zhvaScript Library v0.3
* http://code.google.com/p/urim/
* http://snowball.tartarus.org/
*
* Copyright 2010, Oleg Mazko
* http://www.mozilla.org/MPL/
*/
/**
* export the module via AMD, CommonJS or as a browser global
* Export code from https://github.com/umdjs/umd/blob/master/returnExports.js
*/
;
(await async function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory)
} else if (typeof exports === 'object') {
/**
* Node. Does not work with strict CommonJS, but
* only CommonJS-like environments that support module.exports,
* like Node.
*/
module.exports = factory()
} else {
// Browser globals (root is window)
const {
default: init, cut
} = await import('./jieba_rs_wasm.js');
await init();
factory(cut)(root.lunr);
await import('./searcher.js');
}
}(globalThis, function (cut) {
/**
* Just return a value to define the module export.
* This example returns an object, but the module
* can return a function as the exported value.
*/
return function (lunr) {
/* throw error if lunr is not yet included */
if ('undefined' === typeof lunr) {
throw new Error('Lunr is not present. Please include / require Lunr before this script.');
}
/* throw error if lunr stemmer support is not yet included */
if ('undefined' === typeof lunr.stemmerSupport) {
throw new Error('Lunr stemmer support is not present. Please include / require Lunr stemmer support before this script.');
}
/*
Chinese tokenization is trickier, since it does not
take into account spaces.
Since the tokenization function is represented different
internally for each of the Lunr versions, this had to be done
in order to try to try to pick the best way of doing this based
on the Lunr version
*/
var isLunr2 = lunr.version[0] == "2";
/* register specific locale function */
lunr.zh = function () {
this.pipeline.reset();
this.pipeline.add(
lunr.zh.trimmer,
lunr.zh.stopWordFilter,
lunr.zh.stemmer
);
// change the tokenizer for Chinese one
if (isLunr2) { // for lunr version 2.0.0
this.tokenizer = lunr.zh.tokenizer;
} else {
if (lunr.tokenizer) { // for lunr version 0.6.0
lunr.tokenizer = lunr.zh.tokenizer;
}
if (this.tokenizerFn) { // for lunr version 0.7.0 -> 1.0.0
this.tokenizerFn = lunr.zh.tokenizer;
}
}
};
lunr.zh.tokenizer = function (obj) {
if (!arguments.length || obj == null || obj == undefined) return []
if (Array.isArray(obj)) return obj.map(function (t) {
return isLunr2 ? new lunr.Token(t.toLowerCase()) : t.toLowerCase()
})
var str = obj.toString().trim().toLowerCase();
var tokens = [];
cut(str, false).forEach(function (seg) {
tokens = tokens.concat(seg.split(' '))
})
tokens = tokens.filter(function (token) {
return !!token;
});
var fromIndex = 0
return tokens.map(function (token, index) {
if (isLunr2) {
var start = str.indexOf(token, fromIndex)
var tokenMetadata = {}
tokenMetadata["position"] = [start, token.length]
tokenMetadata["index"] = index
fromIndex = start
return new lunr.Token(token, tokenMetadata);
} else {
return token
}
});
}
/* lunr trimmer function */
lunr.zh.wordCharacters = "\\w\u4e00-\u9fa5";
lunr.zh.trimmer = lunr.trimmerSupport.generateTrimmer(lunr.zh.wordCharacters);
lunr.Pipeline.registerFunction(lunr.zh.trimmer, 'trimmer-zh');
/* lunr stemmer function */
lunr.zh.stemmer = (function () {
/* TODO Chinese stemmer */
return function (word) {
return word;
}
})();
lunr.Pipeline.registerFunction(lunr.zh.stemmer, 'stemmer-zh');
lunr.zh.stopWordFilter = function (token) {
if (lunr.zh.stopWordFilter.stopWords.indexOf(token) === -1) {
return token;
}
};
lunr.zh.stopWordFilter.stopWords = new lunr.SortedSet();
lunr.zh.stopWordFilter.stopWords.length = 150;
/* lunr stop word filter. see https://www.ranks.nl/stopwords/chinese-stopwords */
lunr.zh.stopWordFilter.stopWords.elements = ' 的 一 不 在 人 有 是 为 為 以 于 於 上 他 而 后 後 之 来 來 及 了 因 下 可 到 由 这 這 与 與 也 此 但 并 並 个 個 其 已 无 無 小 我 们 們 起 最 再 今 去 好 只 又 或 很 亦 某 把 那 你 乃 它 吧 被 比 别 趁 当 當 从 從 得 打 凡 儿 兒 尔 爾 该 該 各 给 給 跟 和 何 还 還 即 几 幾 既 看 据 據 距 靠 啦 另 么 麽 每 嘛 拿 哪 您 凭 憑 且 却 卻 让 讓 仍 啥 如 若 使 谁 誰 虽 雖 随 隨 同 所 她 哇 嗡 往 些 向 沿 哟 喲 用 咱 则 則 怎 曾 至 致 着 著 诸 諸 自'.split(' ');
lunr.Pipeline.registerFunction(lunr.zh.stopWordFilter, 'stopWordFilter-zh');
};
}))