-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey-match.js
172 lines (140 loc) · 5.78 KB
/
key-match.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
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* @desc: 自动问答系统主逻辑
* @author: jiguang
* @mail: jiguang1984#gmail.com
* @date: 2012-10-22
*/
var KeyMatch = function(opt){
// init setting
this.opt = opt || {};
this.sort = opt.sort || false; // for high performance, DO NOT SORT
this.unique = opt.unique || true; // is keywords unique
this.debug = this.opt.debug || false; // is debug
this.matchedLimit = this.opt.matchedLimit || 0; // 0: no limit, [num] limit
// keywords and matches
this.keywords = [];
this.matches = [];
// initial keywords regex
this.keywordsReg;
this.matchedKeywordsReg;
// matched results
this.matched = [];
// 'best' matched, which has the most matched keywords number
this.bestMatched = '';
// is data loaded
this.isLoaded = false;
// load data
this.load = function(keywords/*Array*/, matches/*Array*/){
if(!keywords || !matches){
this.log('No data received.');
return false;
}
if((keywords instanceof Array) && (matches instanceof Array)){
if(keywords.length === 0 || matches.length === 0){
this.log('Arguments can not be empty Array.');
return false;
}else{
// load
this.keywords = keywords;
this.matches = matches;
// contain all keywords
this.keywordsReg = new RegExp('('+this.keywords.join('|')+')', 'g');
this.isLoaded = true;
return true;
}
}else{
this.log('Arguments must be [Array].');
return false;
}
};
// match
this.match = function(str/*String*/){
if(!str || str.replace(/(^\s*|\s*$)/,'') === ''){
this.log('No input string.');
return {
'status': 0 // has match(es):1, no match:0
};
}
// if data loaded
if(this.isLoaded){
// clear history
this.matched = [];
this.matchedKeywords = [];
// if there is some keywords in the input string
if(this.matchedKeywords = str.match(this.keywordsReg)){
this.log('Input string matched keywords:'+ this.matchedKeywords.join('|'));
// contain all keywords in the input string
this.matchedKeywordsReg = new RegExp('('+this.matchedKeywords.join('|')+')', 'g');
// find all matches
for(var i = 0, j = this.matches.length; i<j; i++){
// test every string in matches Array
if(this.currentMatchedKeywords = this.matches[i].match(this.matchedKeywordsReg)){
// remove duplicate keywords
if(this.unique){
this.currentMatchedKeywords.sort();
for ( var k = 1; k < this.currentMatchedKeywords.length; k++ ) {
if ( this.currentMatchedKeywords[k] === this.currentMatchedKeywords[ k - 1 ] ) {
this.currentMatchedKeywords.splice( k--, 1 );
}
}
}
this.log('Current matched keywords:'+ this.currentMatchedKeywords.join('|'));
// init 'best' matched
this.bestMatched = this.matches[i];
// save all sorted matches
if(this.sort){
this.matched.push({
"content": this.matches[i],
"matchedKeywordsCount": this.currentMatchedKeywords.length
});
}else{
this.matched.push(this.matches[i]);
}
// input string which has the most matched keywords was the 'best' match
// CAN NOT distinguish two matches which have the same number of keywords
if(this.currentMatchedKeywords.length >= this.bestMatched.match(this.matchedKeywordsReg).length){
this.bestMatched = this.matches[i];
}
}
}
// sort matched Array by matched keywords count
if(this.sort){
var matched = [];
this.matched = this.matched.sort(function(a, b){
return b.matchedKeywordsCount - a.matchedKeywordsCount;
});
for(var m = 0, n = this.matched.length; m < n; m++){
matched.push(this.matched[m].content);
}
this.matched = matched;
}
// match limit
if(this.matchedLimit !== 0){
this.matched.length = this.matchedLimit;
}
// return value
return {
'status': 1, // has result:1, no result:0
'matched': this.matched,
'bestMatched': this.bestMatched
};
}else{
this.log('No matches.');
return {
'status': 0
}
}
}else{
this.log('Data not loaded');
return {
'status': 0
}
}
};
// log
this.log = function(msg){
if(this.debug && msg && typeof msg === 'string'){
console.log('Log:', msg);
}
}
};