UNPKG

compromise

Version:

modest natural language processing

60 lines (58 loc) • 1.73 kB
// append a new document, somehow const combineDocs = function (homeDocs, inputDocs) { if (homeDocs.length > 0) { // add a space const end = homeDocs.at(-1) const last = end.at(-1) if (!/ /.test(last.post)) { last.post += ' ' } } homeDocs = homeDocs.concat(inputDocs) return homeDocs } const combineViews = function (home, input) { // is it a view from the same document? if (home.document === input.document) { const ptrs = home.fullPointer.concat(input.fullPointer) return home.toView(ptrs).compute('index') } // update n of new pointer, to end of our pointer const ptrs = input.fullPointer ptrs.forEach(a => { a[0] += home.document.length }) home.document = combineDocs(home.document, input.docs) return home.all() } export default { // add string as new match/sentence concat: function (input) { // parse and splice-in new terms if (typeof input === 'string') { const more = this.fromText(input) // easy concat if (!this.found || !this.ptrs) { this.document = this.document.concat(more.document) } else { // if we are in the middle, this is actually a splice operation const ptrs = this.fullPointer const at = ptrs.at(-1)[0] this.document.splice(at, 0, ...more.document) } // put the docs return this.all().compute('index') } // plop some view objects together if (typeof input === 'object' && input.isView) { return combineViews(this, input) } // assume it's an array of terms if (Array.isArray(input)) { const docs = combineDocs(this.document, input) this.document = docs return this.all() } return this }, }