compromise
Version:
natural language processing in the browser
115 lines (105 loc) • 3.11 kB
JavaScript
;
//break apart a termlist into (before, match after)
const breakUpHere = (terms, ts) => {
let firstTerm = ts.terms[0];
let len = ts.terms.length;
for (let i = 0; i < terms.length; i++) {
if (terms[i] === firstTerm) {
return {
before: terms.slice(0, i),
match: terms.slice(i, i + len),
after: terms.slice(i + len, terms.length),
};
}
}
return {
after: terms
};
};
const splitMethods = (Terms) => {
const methods = {
/** at the end of the match, split the terms **/
splitAfter: function (reg, verbose) {
let ms = this.match(reg, verbose); //Array[ts]
let termArr = this.terms;
let all = [];
ms.list.forEach((lookFor) => {
let section = breakUpHere(termArr, lookFor);
if (section.before && section.match) {
all.push(section.before.concat(section.match));
}
termArr = section.after;
});
//add the remaining
if (termArr.length) {
all.push(termArr);
}
//make them termlists
all = all.map((ts) => new Terms(ts, ts.lexicon, ts.parent));
return all;
},
/** return only before & after the match**/
splitOn: function (reg, verbose) {
let ms = this.match(reg, verbose); //Array[ts]
let termArr = this.terms;
let all = [];
ms.list.forEach((lookFor) => {
let section = breakUpHere(termArr, lookFor);
if (section.before) {
all.push(section.before);
}
termArr = section.after;
});
//add the remaining
if (termArr.length) {
all.push(termArr);
}
//make them termlists
all = all.filter(a => a && a.length);
all = all.map((ts) => new Terms(ts, ts.lexicon, ts.parent));
return all;
},
/** at the start of the match, split the terms**/
splitBefore: function (reg, verbose) {
let ms = this.match(reg, verbose); //Array[ts]
let termArr = this.terms;
let all = [];
ms.list.forEach((lookFor) => {
let section = breakUpHere(termArr, lookFor);
if (section.before) {
all.push(section.before);
}
if (section.match) {
all.push(section.match);
}
termArr = section.after;
});
//add the remaining
if (termArr.length) {
all.push(termArr);
}
//cleanup-step: merge all (middle) matches with the next one
for (let i = 0; i < all.length; i++) {
for (let o = 0; o < ms.length; o++) {
if (ms.list[o].terms[0] === all[i][0]) {
if (all[i + 1]) {
all[i] = all[i].concat(all[i + 1]);
all[i + 1] = [];
}
}
}
}
//make them termlists
all = all.filter(a => a && a.length);
all = all.map((ts) => new Terms(ts, ts.lexicon, ts.parent));
return all;
},
};
//hook them into result.proto
Object.keys(methods).forEach((k) => {
Terms.prototype[k] = methods[k];
});
return Terms;
};
module.exports = splitMethods;
exports = splitMethods;