recime-bot-runtime
Version:
This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.
39 lines (38 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Fuse = require('fuse.js');
var flatten = require('lodash.flatten');
var FuzzyMatcher = /** @class */ (function () {
function FuzzyMatcher() {
}
FuzzyMatcher.findMatch = function (intents, payload) {
var maps = flatten(intents.map(function (i) {
return (i.expressions || []).map(function (e) {
return {
intent: i.name,
expression: e.text
};
});
})).filter(function (i) { return i.intent && i.expression; });
var options = {
shouldSort: true,
includeScore: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['expression']
};
var fuse = new Fuse(maps, options);
var result = fuse.search(payload.text || payload);
var exact = result.find(function (i) { return i.score === 0; });
if (!exact) {
return void 0;
}
var intentName = exact.item.intent;
return intents.find(function (i) { return i.name === intentName; });
};
return FuzzyMatcher;
}());
exports.FuzzyMatcher = FuzzyMatcher;