compromise
Version:
modest natural language processing
88 lines (82 loc) • 2.41 kB
JavaScript
import { cleanAppend, cleanPrepend } from './lib/insert.js'
import uuid from '../compute/uuid.js'
import expandContraction from './lib/_expand.js'
import { retag } from '../../../API/_helpers.js'
// set new ids for each terms
const addIds = function (terms) {
terms = terms.map(term => {
term.id = uuid(term)
return term
})
return terms
}
const getTerms = function (input, world) {
const { methods } = world
// create our terms from a string
if (typeof input === 'string') {
return methods.one.tokenize.fromString(input, world)[0] //assume one sentence
}
//allow a view object
if (typeof input === 'object' && input.isView) {
return input.clone().docs[0] || [] //assume one sentence
}
//allow an array of terms, too
if (Array.isArray(input)) {
return Array.isArray(input[0]) ? input[0] : input
}
return []
}
// {agree: false} skips moving any capitals or punctuation
const insert = function (input, view, prepend, opts = {}) {
const agree = opts.agree !== false
const { document, world } = view
view.uncache()
// insert words at end of each doc
const ptrs = view.fullPointer
const selfPtrs = view.fullPointer
view.forEach((m, i) => {
const ptr = m.fullPointer[0]
const [n] = ptr
// add-in the words
const home = document[n]
let terms = getTerms(input, world)
// are we inserting nothing?
if (terms.length === 0) {
return
}
terms = addIds(terms)
if (prepend) {
expandContraction(view.update([ptr]).firstTerm())
cleanPrepend(home, ptr, terms, document, agree)
} else {
expandContraction(view.update([ptr]).lastTerm())
cleanAppend(home, ptr, terms, document, agree)
}
// harden the pointer
if (document[n]?.[ptr[1]]) {
ptr[3] = document[n][ptr[1]].id
}
// change self backwards by len
selfPtrs[i] = ptr
// extend the pointer
ptr[2] += terms.length
ptrs[i] = ptr
})
const doc = view.toView(ptrs)
// shift our self pointer, if necessary
view.ptrs = selfPtrs
// try to tag them, too
return retag(doc, ['id', 'index'])
}
const fns = {
insertAfter: function (input, opts) {
return insert(input, this, false, opts)
},
insertBefore: function (input, opts) {
return insert(input, this, true, opts)
},
}
fns.append = fns.insertAfter
fns.prepend = fns.insertBefore
fns.insert = fns.insertAfter
export default fns