mm_matcher 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/tools/exact/index.js +21 -0
- package/lib/tools/fuzzy/index.js +22 -0
- package/lib/tools/index.js +16 -0
- package/lib/tools/mqtt_topic/index.js +88 -0
- package/lib/tools/prefix/index.js +21 -0
- package/lib/tools/recommend/index.js +85 -0
- package/lib/tools/regex/index.js +31 -0
- package/lib/tools/route/index.js +35 -0
- package/lib/tools/rule_engine/index.js +152 -0
- package/lib/tools/similarity/index.js +53 -0
- package/lib/tools/wildcard/index.js +25 -0
- package/package.json +35 -34
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 精确匹配
|
|
5
|
+
* @param {object} ctx 上下文对象
|
|
6
|
+
* @param {object} params 步骤参数
|
|
7
|
+
*/
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
var source = params.source || '';
|
|
10
|
+
var target = params.target || '';
|
|
11
|
+
var matched = source === target;
|
|
12
|
+
ctx.output = {
|
|
13
|
+
matched: matched,
|
|
14
|
+
tip: matched ? '精确匹配成功' : '精确匹配失败',
|
|
15
|
+
source: source,
|
|
16
|
+
target: target
|
|
17
|
+
};
|
|
18
|
+
return ctx.output;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = { main };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 模糊匹配
|
|
5
|
+
* @param {object} ctx 上下文对象
|
|
6
|
+
* @param {object} params 步骤参数
|
|
7
|
+
*/
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
var query = params.query || '';
|
|
10
|
+
var target = params.target || '';
|
|
11
|
+
var matched = target.indexOf(query) !== -1;
|
|
12
|
+
ctx.output = {
|
|
13
|
+
matched: matched,
|
|
14
|
+
tip: matched ? '模糊匹配成功' : '模糊匹配失败',
|
|
15
|
+
query: query,
|
|
16
|
+
target: target,
|
|
17
|
+
method: 'indexOf'
|
|
18
|
+
};
|
|
19
|
+
return ctx.output;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { main };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
/** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
|
|
5
|
+
module.exports = new Proxy({}, {
|
|
6
|
+
get(_t, key) {
|
|
7
|
+
if (typeof key !== 'string') return undefined;
|
|
8
|
+
if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
|
|
9
|
+
return require('./' + key);
|
|
10
|
+
},
|
|
11
|
+
has(_t, key) {
|
|
12
|
+
return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
|
|
13
|
+
},
|
|
14
|
+
ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
|
|
15
|
+
getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
|
|
16
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MQTT主题匹配器
|
|
3
|
+
* 遵循 MQTT 通配规范: + 匹配单层, # 匹配零个或多个末级层级
|
|
4
|
+
* @param {object} ctx 上下文对象
|
|
5
|
+
* @param {object} params 步骤参数
|
|
6
|
+
*/
|
|
7
|
+
const MULTI_WILDCARD = '#';
|
|
8
|
+
|
|
9
|
+
const SINGLE_WILDCARD = '+';
|
|
10
|
+
|
|
11
|
+
const LEVEL_SEPARATOR = '/';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 校验主题滤波器是否合法
|
|
15
|
+
* 通配符必须独占一层, 且 # 只能出现在末级
|
|
16
|
+
* @param {string} pattern 主题滤波器
|
|
17
|
+
* @returns {boolean} 合法返回 true
|
|
18
|
+
*/
|
|
19
|
+
function _isValidFilter(pattern) {
|
|
20
|
+
var levels = String(pattern).split(LEVEL_SEPARATOR);
|
|
21
|
+
for (var i = 0; i < levels.length; i++) {
|
|
22
|
+
var level = levels[i];
|
|
23
|
+
if (level === MULTI_WILDCARD) {
|
|
24
|
+
if (i !== levels.length - 1) return false;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (level.indexOf(MULTI_WILDCARD) !== -1) return false;
|
|
28
|
+
if (level === SINGLE_WILDCARD) continue;
|
|
29
|
+
if (level.indexOf(SINGLE_WILDCARD) !== -1) return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 转义正则元字符
|
|
36
|
+
* @param {string} text 待转义文本
|
|
37
|
+
* @returns {string} 转义结果
|
|
38
|
+
*/
|
|
39
|
+
function _escape(text) {
|
|
40
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 由主题滤波器构造匹配正则
|
|
45
|
+
* # 需同时匹配父层级本身(如 sensor/# 匹配 sensor), 故不与分隔符拼接
|
|
46
|
+
* @param {string} pattern 主题滤波器
|
|
47
|
+
* @returns {RegExp} 匹配正则
|
|
48
|
+
*/
|
|
49
|
+
function _buildRegex(pattern) {
|
|
50
|
+
var levels = String(pattern).split(LEVEL_SEPARATOR);
|
|
51
|
+
var source = '';
|
|
52
|
+
for (var i = 0; i < levels.length; i++) {
|
|
53
|
+
var level = levels[i];
|
|
54
|
+
if (level === MULTI_WILDCARD) {
|
|
55
|
+
source += i === 0 ? '.*' : '(?:/.*)?';
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (i > 0) source += LEVEL_SEPARATOR;
|
|
59
|
+
source += level === SINGLE_WILDCARD ? '[^/]*' : _escape(level);
|
|
60
|
+
}
|
|
61
|
+
return new RegExp('^' + source + '$');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function main(params, ctx, options) {
|
|
65
|
+
var source = params.source || '';
|
|
66
|
+
var pattern = params.pattern || '';
|
|
67
|
+
|
|
68
|
+
if (!_isValidFilter(pattern)) {
|
|
69
|
+
ctx.output = {
|
|
70
|
+
matched: false,
|
|
71
|
+
tip: 'Topic滤波器不合法: ' + pattern,
|
|
72
|
+
topic: source,
|
|
73
|
+
pattern: pattern
|
|
74
|
+
};
|
|
75
|
+
return ctx.output;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
var result = _buildRegex(pattern).test(String(source));
|
|
79
|
+
ctx.output = {
|
|
80
|
+
matched: result,
|
|
81
|
+
tip: result ? 'Topic匹配成功' : 'Topic匹配失败',
|
|
82
|
+
topic: source,
|
|
83
|
+
pattern: pattern
|
|
84
|
+
};
|
|
85
|
+
return ctx.output;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { main };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 前缀匹配
|
|
5
|
+
* @param {object} ctx 上下文对象
|
|
6
|
+
* @param {object} params 步骤参数
|
|
7
|
+
*/
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
const source = params.source || '';
|
|
10
|
+
const target = params.target || '';
|
|
11
|
+
const result = String(source).indexOf(String(target)) === 0;
|
|
12
|
+
ctx.output = {
|
|
13
|
+
matched: result,
|
|
14
|
+
tip: result ? '前缀匹配成功' : '前缀匹配失败',
|
|
15
|
+
source: source,
|
|
16
|
+
prefix_list: [target]
|
|
17
|
+
};
|
|
18
|
+
return ctx.output;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = { main };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 推荐匹配器
|
|
3
|
+
* 通用相似度推荐匹配
|
|
4
|
+
*/
|
|
5
|
+
async function main(params, ctx, options) {
|
|
6
|
+
var source = params.source || '';
|
|
7
|
+
var pool = params.pool || [];
|
|
8
|
+
var options = params.options || {};
|
|
9
|
+
var limit = options.limit || 10;
|
|
10
|
+
|
|
11
|
+
if (!source || pool.length === 0) {
|
|
12
|
+
ctx.output = {
|
|
13
|
+
matched: false,
|
|
14
|
+
tip: '无推荐结果',
|
|
15
|
+
items: [],
|
|
16
|
+
source: source,
|
|
17
|
+
total: pool.length,
|
|
18
|
+
algo: 'token_hit'
|
|
19
|
+
};
|
|
20
|
+
return ctx.output;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var source_words = tokenize(source);
|
|
24
|
+
var scored = [];
|
|
25
|
+
|
|
26
|
+
for (var i = 0; i < pool.length; i++) {
|
|
27
|
+
var item = pool[i];
|
|
28
|
+
var name = item.name || '';
|
|
29
|
+
var tags = item.tags || [];
|
|
30
|
+
var desc = item.desc || '';
|
|
31
|
+
var target_text = name + ' ' + desc;
|
|
32
|
+
|
|
33
|
+
var tag_text = '';
|
|
34
|
+
if (Array.isArray(tags)) {
|
|
35
|
+
tag_text = tags.join(' ');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var text_score = calcHitScore(source_words, target_text);
|
|
39
|
+
var tag_score = calcHitScore(source_words, tag_text) * 0.5;
|
|
40
|
+
|
|
41
|
+
var score = text_score + tag_score;
|
|
42
|
+
|
|
43
|
+
scored.push({
|
|
44
|
+
id: item.id,
|
|
45
|
+
name: item.name || '',
|
|
46
|
+
score: score
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
scored.sort(function (a, b) {
|
|
51
|
+
return b.score - a.score;
|
|
52
|
+
});
|
|
53
|
+
var results = scored.slice(0, limit);
|
|
54
|
+
|
|
55
|
+
ctx.output = {
|
|
56
|
+
matched: true,
|
|
57
|
+
tip: '找到 ' + results.length + ' 个推荐',
|
|
58
|
+
items: results,
|
|
59
|
+
source: source,
|
|
60
|
+
total: pool.length,
|
|
61
|
+
algo: 'token_hit'
|
|
62
|
+
};
|
|
63
|
+
return ctx.output;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function tokenize(text) {
|
|
67
|
+
if (!text) return [];
|
|
68
|
+
return text.split(/[\s,,、。;;]+/).filter(function (w) {
|
|
69
|
+
return w.length > 0;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function calcHitScore(source_words, target_text) {
|
|
74
|
+
if (!target_text || source_words.length === 0) return 0;
|
|
75
|
+
var lower_target = target_text.toLowerCase();
|
|
76
|
+
var hits = 0;
|
|
77
|
+
for (var i = 0; i < source_words.length; i++) {
|
|
78
|
+
if (lower_target.indexOf(source_words[i].toLowerCase()) !== -1) {
|
|
79
|
+
hits++;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return hits / source_words.length;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = { main };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 正则匹配
|
|
3
|
+
* @param {object} ctx 上下文对象
|
|
4
|
+
* @param {object} params 步骤参数
|
|
5
|
+
*/
|
|
6
|
+
async function main(params, ctx, options) {
|
|
7
|
+
var pattern = params.pattern || '';
|
|
8
|
+
var text = params.text || '';
|
|
9
|
+
|
|
10
|
+
var regex;
|
|
11
|
+
try {
|
|
12
|
+
regex = new RegExp(pattern);
|
|
13
|
+
} catch (_) {
|
|
14
|
+
ctx.output = {
|
|
15
|
+
matched: false,
|
|
16
|
+
tip: '正则表达式无效: ' + pattern,
|
|
17
|
+
error: { code: 'MATCH_REGEX_INVALID', message: '正则表达式无效: ' + pattern }
|
|
18
|
+
};
|
|
19
|
+
return ctx.output;
|
|
20
|
+
}
|
|
21
|
+
var result = regex.test(text);
|
|
22
|
+
ctx.output = {
|
|
23
|
+
matched: result,
|
|
24
|
+
tip: result ? '正则匹配成功' : '正则匹配失败',
|
|
25
|
+
pattern: pattern,
|
|
26
|
+
input: text
|
|
27
|
+
};
|
|
28
|
+
return ctx.output;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { main };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由路径匹配器
|
|
3
|
+
* @param {object} ctx 上下文对象
|
|
4
|
+
* @param {object} params 步骤参数
|
|
5
|
+
*/
|
|
6
|
+
async function main(params, ctx, options) {
|
|
7
|
+
const source = params.source || '';
|
|
8
|
+
const pattern = params.pattern || '';
|
|
9
|
+
|
|
10
|
+
const source_parts = source.split('/').filter(Boolean);
|
|
11
|
+
const pattern_parts = pattern.split('/').filter(Boolean);
|
|
12
|
+
if (source_parts.length !== pattern_parts.length) {
|
|
13
|
+
ctx.output = { matched: false, tip: '路由匹配失败', path: source, params: {} };
|
|
14
|
+
return ctx.output;
|
|
15
|
+
}
|
|
16
|
+
let matched = true;
|
|
17
|
+
const route_params = {};
|
|
18
|
+
for (let i = 0; i < pattern_parts.length; i++) {
|
|
19
|
+
if (pattern_parts[i].startsWith(':')) {
|
|
20
|
+
route_params[pattern_parts[i].slice(1)] = source_parts[i];
|
|
21
|
+
} else if (pattern_parts[i] !== source_parts[i]) {
|
|
22
|
+
matched = false;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
ctx.output = {
|
|
27
|
+
matched: matched,
|
|
28
|
+
tip: matched ? '路由匹配成功' : '路由匹配失败',
|
|
29
|
+
path: source,
|
|
30
|
+
params: route_params
|
|
31
|
+
};
|
|
32
|
+
return ctx.output;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = { main };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 规则引擎
|
|
3
|
+
* 用 global.__rule_sets 持久化规则集
|
|
4
|
+
*/
|
|
5
|
+
async function main(params, ctx, options) {
|
|
6
|
+
var action = params.action;
|
|
7
|
+
var rule_name = params.rule_name || '';
|
|
8
|
+
var conditions = params.conditions || [];
|
|
9
|
+
var operator = params.operator || 'and';
|
|
10
|
+
var fact = params.fact || {};
|
|
11
|
+
|
|
12
|
+
if (!global.__rule_sets) {
|
|
13
|
+
global.__rule_sets = {};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
var rules_count = Object.keys(global.__rule_sets).length;
|
|
17
|
+
|
|
18
|
+
switch (action) {
|
|
19
|
+
case 'add_rule': {
|
|
20
|
+
global.__rule_sets[rule_name] = {
|
|
21
|
+
rule_name: rule_name,
|
|
22
|
+
conditions: conditions,
|
|
23
|
+
operator: operator
|
|
24
|
+
};
|
|
25
|
+
ctx.output = {
|
|
26
|
+
msg: '规则执行完成',
|
|
27
|
+
results: { rule_name: rule_name, added: true },
|
|
28
|
+
rules_count: Object.keys(global.__rule_sets).length
|
|
29
|
+
};
|
|
30
|
+
return ctx.output;
|
|
31
|
+
}
|
|
32
|
+
case 'evaluate': {
|
|
33
|
+
var stored = global.__rule_sets[rule_name];
|
|
34
|
+
var eval_conditions;
|
|
35
|
+
var eval_operator;
|
|
36
|
+
if (stored) {
|
|
37
|
+
eval_conditions = stored.conditions;
|
|
38
|
+
eval_operator = stored.operator;
|
|
39
|
+
} else {
|
|
40
|
+
eval_conditions = conditions;
|
|
41
|
+
eval_operator = operator;
|
|
42
|
+
}
|
|
43
|
+
var matched = evaluateConditions(eval_conditions, eval_operator, fact);
|
|
44
|
+
ctx.output = {
|
|
45
|
+
msg: '规则执行完成',
|
|
46
|
+
results: { rule_name: rule_name, matched: matched, fact: fact },
|
|
47
|
+
rules_count: rules_count
|
|
48
|
+
};
|
|
49
|
+
return ctx.output;
|
|
50
|
+
}
|
|
51
|
+
case 'remove_rule': {
|
|
52
|
+
delete global.__rule_sets[rule_name];
|
|
53
|
+
ctx.output = {
|
|
54
|
+
msg: '规则执行完成',
|
|
55
|
+
results: { rule_name: rule_name, removed: true },
|
|
56
|
+
rules_count: Object.keys(global.__rule_sets).length
|
|
57
|
+
};
|
|
58
|
+
return ctx.output;
|
|
59
|
+
}
|
|
60
|
+
case 'list_rules': {
|
|
61
|
+
var rule_names = Object.keys(global.__rule_sets);
|
|
62
|
+
ctx.output = {
|
|
63
|
+
msg: '规则执行完成',
|
|
64
|
+
results: { rules: rule_names },
|
|
65
|
+
rules_count: rule_names.length
|
|
66
|
+
};
|
|
67
|
+
return ctx.output;
|
|
68
|
+
}
|
|
69
|
+
default: {
|
|
70
|
+
ctx.output = {
|
|
71
|
+
error: {
|
|
72
|
+
code: 'EXEC_UNKNOWN_ACTION',
|
|
73
|
+
message: '未知操作: ' + action
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
return ctx.output;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function evaluateConditions(conditions, operator, fact) {
|
|
82
|
+
if (!conditions || conditions.length === 0) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
var matched = true;
|
|
86
|
+
for (var i = 0; i < conditions.length; i++) {
|
|
87
|
+
var cond = conditions[i];
|
|
88
|
+
var fact_value = fact[cond.field];
|
|
89
|
+
var passed;
|
|
90
|
+
switch (cond.operator) {
|
|
91
|
+
case '=':
|
|
92
|
+
passed = fact_value === cond.value;
|
|
93
|
+
break;
|
|
94
|
+
case '!=':
|
|
95
|
+
passed = fact_value !== cond.value;
|
|
96
|
+
break;
|
|
97
|
+
case '>':
|
|
98
|
+
passed = fact_value > cond.value;
|
|
99
|
+
break;
|
|
100
|
+
case '>=':
|
|
101
|
+
passed = fact_value >= cond.value;
|
|
102
|
+
break;
|
|
103
|
+
case '<':
|
|
104
|
+
passed = fact_value < cond.value;
|
|
105
|
+
break;
|
|
106
|
+
case '<=':
|
|
107
|
+
passed = fact_value <= cond.value;
|
|
108
|
+
break;
|
|
109
|
+
case 'in':
|
|
110
|
+
passed = (cond.value || []).indexOf(fact_value) >= 0;
|
|
111
|
+
break;
|
|
112
|
+
case 'between':
|
|
113
|
+
passed = fact_value >= (cond.value || [])[0] && fact_value <= (cond.value || [])[1];
|
|
114
|
+
break;
|
|
115
|
+
case 'not_in':
|
|
116
|
+
passed = (cond.value || []).indexOf(fact_value) === -1;
|
|
117
|
+
break;
|
|
118
|
+
case 'contains':
|
|
119
|
+
passed = String(fact_value).indexOf(String(cond.value)) !== -1;
|
|
120
|
+
break;
|
|
121
|
+
case 'starts_with':
|
|
122
|
+
passed = String(fact_value).startsWith(String(cond.value));
|
|
123
|
+
break;
|
|
124
|
+
case 'ends_with':
|
|
125
|
+
passed = String(fact_value).endsWith(String(cond.value));
|
|
126
|
+
break;
|
|
127
|
+
case 'regex':
|
|
128
|
+
try {
|
|
129
|
+
passed = new RegExp(String(cond.value)).test(String(fact_value));
|
|
130
|
+
} catch (_) {
|
|
131
|
+
passed = false;
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
default:
|
|
135
|
+
passed = true;
|
|
136
|
+
}
|
|
137
|
+
if (operator === 'and' && !passed) {
|
|
138
|
+
matched = false;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
if (operator === 'or' && passed) {
|
|
142
|
+
matched = true;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
if (i === conditions.length - 1 && operator === 'or') {
|
|
146
|
+
matched = passed;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return matched;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
module.exports = { main };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 相似度匹配器 - 计算两个字符串的相似度
|
|
5
|
+
* @param {object} ctx 上下文对象
|
|
6
|
+
* @param {object} params 步骤参数
|
|
7
|
+
*/
|
|
8
|
+
async function main(params, ctx, options) {
|
|
9
|
+
const source = params.source || '';
|
|
10
|
+
const target = params.target || '';
|
|
11
|
+
const threshold = params.threshold || 0.5;
|
|
12
|
+
const source_str = String(source);
|
|
13
|
+
const target_str = String(target);
|
|
14
|
+
const max_len = Math.max(source_str.length, target_str.length);
|
|
15
|
+
let distance = 0;
|
|
16
|
+
if (max_len === 0) {
|
|
17
|
+
ctx.output.similarity = 1;
|
|
18
|
+
} else {
|
|
19
|
+
const matrix = [];
|
|
20
|
+
for (let i = 0; i <= source_str.length; i++) {
|
|
21
|
+
matrix[i] = [i];
|
|
22
|
+
}
|
|
23
|
+
for (let j = 0; j <= target_str.length; j++) {
|
|
24
|
+
matrix[0][j] = j;
|
|
25
|
+
}
|
|
26
|
+
for (let i = 1; i <= source_str.length; i++) {
|
|
27
|
+
for (let j = 1; j <= target_str.length; j++) {
|
|
28
|
+
const cost = source_str[i - 1] === target_str[j - 1] ? 0 : 1;
|
|
29
|
+
matrix[i][j] = Math.min(
|
|
30
|
+
matrix[i - 1][j] + 1,
|
|
31
|
+
matrix[i][j - 1] + 1,
|
|
32
|
+
matrix[i - 1][j - 1] + cost
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
distance = matrix[source_str.length][target_str.length];
|
|
37
|
+
ctx.output.similarity = 1 - distance / max_len;
|
|
38
|
+
}
|
|
39
|
+
ctx.output.distance = distance;
|
|
40
|
+
const score = ctx.output.similarity;
|
|
41
|
+
ctx.output = {
|
|
42
|
+
matched: score >= threshold,
|
|
43
|
+
tip: score >= threshold ? '相似度达标' : '相似度不足',
|
|
44
|
+
score: score,
|
|
45
|
+
source: source_str,
|
|
46
|
+
target: target_str,
|
|
47
|
+
method: 'levenshtein',
|
|
48
|
+
threshold: threshold
|
|
49
|
+
};
|
|
50
|
+
return ctx.output;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = { main };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通配符匹配
|
|
3
|
+
* @param {object} ctx 上下文对象
|
|
4
|
+
* @param {object} params 步骤参数
|
|
5
|
+
*/
|
|
6
|
+
async function main(params, ctx, options) {
|
|
7
|
+
var source = params.source || '';
|
|
8
|
+
var pattern = params.pattern || '';
|
|
9
|
+
|
|
10
|
+
var escaped = String(pattern)
|
|
11
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
12
|
+
.replace(/\*/g, '.*')
|
|
13
|
+
.replace(/\?/g, '.');
|
|
14
|
+
var regex = new RegExp('^' + escaped + '$');
|
|
15
|
+
var result = regex.test(String(source));
|
|
16
|
+
ctx.output = {
|
|
17
|
+
matched: result,
|
|
18
|
+
tip: result ? '通配符匹配成功' : '通配符匹配失败',
|
|
19
|
+
pattern: pattern,
|
|
20
|
+
input: String(source)
|
|
21
|
+
};
|
|
22
|
+
return ctx.output;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { main };
|
package/package.json
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "mm_matcher",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "mm_workflow 匹配器工具包:精确、模糊、MQTT主题、前缀、推荐、正则、路由、规则引擎、相似度、通配符 等 10 个匹配工具模块",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_matcher.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"mm_mod",
|
|
13
|
+
"operator",
|
|
14
|
+
"matcher",
|
|
15
|
+
"超级美眉",
|
|
16
|
+
"工作流工具"
|
|
17
|
+
],
|
|
18
|
+
"layer": "shared",
|
|
19
|
+
"group": "operator",
|
|
20
|
+
"operator": "matcher",
|
|
21
|
+
"author": "邱文武",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"files": [
|
|
24
|
+
"index.js",
|
|
25
|
+
"lib",
|
|
26
|
+
"tool"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node -e \"const t=require('./index.js').loadTools();console.log(Object.keys(t).length+' tools loaded')\""
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"mm_tool_host": "^1.0.0"
|
|
36
|
+
}
|
|
36
37
|
}
|