compromise
Version:
natural language processing in the browser
31 lines (27 loc) • 757 B
JavaScript
;
const fns = require('./paths').fns;
const almostMatch = (reg_str, term) => {
return fns.startsWith(term.normal, reg_str);
};
// match ['john', 'smith'] regs, when the term is lumped
const lumpMatch = function(term, regs, reg_i) {
let reg_str = regs[reg_i].normal;
//is this a partial match? 'tony'& 'tony hawk'
if (almostMatch(reg_str, term)) {
//try to grow it
for (reg_i = reg_i + 1; reg_i < regs.length; reg_i++) {
reg_str += ' ' + regs[reg_i].normal;
//is it now perfect?
if (reg_str === term.normal) {
return reg_i;
}
//is it still almost?
if (almostMatch(reg_str, term)) {
continue;
}
return null;
}
}
return null;
};
module.exports = lumpMatch;