UNPKG

compromise

Version:

modest natural language processing

129 lines (127 loc) • 3.36 kB
import world from './world.js' import api from './methods/index.js' import { retag } from './_helpers.js' class View { constructor(document, pointer, groups = {}) { // invisible props const props = [ ['document', document], ['world', world], ['_groups', groups], ['_cache', null], ] props.forEach(a => { Object.defineProperty(this, a[0], { value: a[1], writable: true, }) }) this.ptrs = pointer } /* getters: */ get docs() { let docs = this.document if (this.ptrs) { docs = world.methods.one.getDoc(this.ptrs, this.document) } return docs } get pointer() { return this.ptrs } get methods() { return this.world.methods } get model() { return this.world.model } get hooks() { return this.world.hooks } get isView() { return true //this comes in handy sometimes } // is the view not-empty? get found() { return this.docs.length > 0 } // how many matches we have get length() { return this.docs.length } // return a more-hackable pointer get fullPointer() { const { docs, ptrs, document } = this // compute a proper pointer, from docs const pointers = ptrs || docs.map((_d, n) => [n]) // do we need to repair it, first? return pointers.map(a => { // eslint-disable-next-line prefer-const let [n, start, end, id, endId] = a start ||= 0 end ||= (document[n] || []).length //add frozen id, for good-measure if (document[n]?.[start]) { id ||= document[n][start].id if (document[n][end - 1]) { endId ||= document[n][end - 1].id } } return [n, start, end, id, endId] }) } // create a new View, from this one update(pointer) { const m = new View(this.document, pointer) // send the cache down, too? if (this._cache && pointer?.length > 0) { // only keep cache if it's a full-sentence const cache = [] pointer.forEach((ptr, i) => { const [n, start, end] = ptr if (ptr.length === 1) { cache[i] = this._cache[n] } else if (start === 0 && this.document[n].length === end) { cache[i] = this._cache[n] } }) if (cache.length > 0) { m._cache = cache } } m.world = this.world return m } // create a new View, from this one toView(pointer) { return new View(this.document, pointer || this.pointer) } fromText(input) { const { methods } = this //assume ./01-tokenize is installed const document = methods.one.tokenize.fromString(input, this.world) const doc = new View(document) doc.world = this.world return retag(doc, ['normal']) } clone() { // clone the whole document let document = this.document.slice(0) //node 17: structuredClone(document); document = document.map(terms => { return terms.map(term => { term = { ...term } term.tags = new Set(term.tags) return term }) }) // clone only sub-document ? const m = this.update(this.pointer) m.document = document m._cache = this._cache //clone this too? return m } } Object.assign(View.prototype, api) // subclasses set their own name on their prototype View.prototype.viewType = 'View' export default View