forked from pulipulichen/jieba-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
require-jieba-js.js
159 lines (134 loc) · 4.33 KB
/
require-jieba-js.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
157
158
159
// <script data-main="scripts/main" src="scripts/require.js"></script>
var _JIEBA_CUT_QUEUE = [];
/**
* 自訂詞典
* @type JSON
*/
JIEBA_CUSTOM_DICTIONARY = undefined;
LAST_DICT = null
call_jieba_cut = function (_text, _dict, _callback) {
if (typeof(_dict) === "function") {
_callback = _dict;
_dict = undefined;
}
if (typeof(_callback) !== "function") {
return;
}
if (typeof(_dict) === "string" || (Array.isArray(_dict) && _dict.length > 0)) {
//console.log(_dict)
JIEBA_CUSTOM_DICTIONARY = _dict;
}
if (JIEBA_CUSTOM_DICTIONARY === undefined) {
console.log('沒有資料')
}
if (typeof(jieba_cut) !== 'function') {
//console.log("set queue:" + _text);
_JIEBA_CUT_QUEUE.push({
text: _text,
callback: _callback
});
//_require_dictionary();
}
else {
if (typeof(_callback) === 'function') {
var _result
if (LAST_DICT !== null && JSON.stringify(JIEBA_CUSTOM_DICTIONARY) !== LAST_DICT) {
//console.log(JIEBA_CUSTOM_DICTIONARY)
var _dict = []
for (var _i = 0; _i < PREDIFINED_DICTIONARY.length; _i++) {
_dict.push(PREDIFINED_DICTIONARY[_i]);
}
for (var _i = 0; _i < JIEBA_CUSTOM_DICTIONARY.length; _i++) {
_dict.push(JIEBA_CUSTOM_DICTIONARY[_i]);
}
//console.log(_dict)
_result = jieba_cut(_text, _dict);
//jieba_parsing(JIEBA_CUSTOM_DICTIONARY, function () {
//_result = jieba_cut(_text);
//LAST_DICT = JSON.stringify(JIEBA_CUSTOM_DICTIONARY)
//console.log(_result);
//})
LAST_DICT = JSON.stringify(JIEBA_CUSTOM_DICTIONARY)
_callback(_result);
return
}
else {
_result = jieba_cut(_text);
if (LAST_DICT === null) {
LAST_DICT = JSON.stringify(JIEBA_CUSTOM_DICTIONARY)
}
_callback(_result);
}
}
}
};
call_jieba_cut_join = function (_text, _join, _callback) {
if (typeof(_callback) !== "function") {
return;
}
call_jieba_cut(_text, function (_result) {
_result = _result.join(_join);
while (_result.indexOf(" ") > -1) {
_result = _result.replace(/ /g, ' ');
}
if (typeof(_callback) === "function") {
_callback(_result);
}
});
};
resume_jieba_cut = function () {
//console.log(_JIEBA_CUT_QUEUE.length);
for (var _i = 0; _i < _JIEBA_CUT_QUEUE.length; _i++) {
var _text = _JIEBA_CUT_QUEUE[_i].text;
var _callback = _JIEBA_CUT_QUEUE[_i].callback;
call_jieba_cut(_text, _callback);
}
_JIEBA_CUT_QUEUE = []
};
// ------------------------------------------------------
// ------------------------------------------------------
var _endsWith = function(_str, searchString, position) {
if (_str === undefined) {
return false;
}
var subjectString = _str.toString();
if (typeof position !== 'number'
|| !isFinite(position)
|| Math.floor(position) !== position
|| position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.lastIndexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
get_host = function () {
var _host = undefined;
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; ++i) {
var _src = scripts[i].src;
//console.log(_src);
if (_endsWith(_src, "/require-jieba-js.js")) {
_host = _src;
break;
}
}
if (_host === undefined) {
_host = "//pulipulichen.github.io/jieba-js/";
}
else {
_host = _host.substr(0, _host.lastIndexOf("require-jieba-js.js"));
}
//console.log("host: " + _host);
return _host;
};
$(() => {
if (typeof (document) === "object") {
var _host = get_host();
var s = document.createElement("script");
s.type = "text/javascript";
s.setAttribute("data-main", _host + "scripts/main");
s.src = _host + "scripts/require.js";
document.body.appendChild(s);
}
})