UNPKG

compromise

Version:

modest natural language processing

82 lines (74 loc) • 2.19 kB
import select from '../../_select.js' import keepClass from '../../_lib.js' import find from '../find.js' import parseNoun from './parse.js' import toJSON from './toJSON.js' import toPlural from './toPlural.js' import hasPlural from './hasPlural.js' import toSingular from './toSingular.js' import eachNoun from './_list.js' const api = function (View) { class Nouns extends View { parse(n) { return this.getNth(n).map(parseNoun) } json(n) { const opts = typeof n === 'object' ? n : {} return this.getNth(n).map(m => { const json = m.toView().json(opts)[0] || {} if (opts && opts.noun !== false) { json.noun = toJSON(m) } return json }, []) } conjugate(n) { const methods = this.world.methods.two.transform.noun return this.getNth(n).map(m => { const parsed = parseNoun(m) const root = parsed.root.compute('root').text('root') const res = { Singular: root, } if (hasPlural(parsed.root)) { res.Plural = methods.toPlural(root, this.model) } // only show plural if one exists if (res.Singular === res.Plural) { delete res.Plural } return res }, []) } isPlural(n) { const res = this.filter(m => parseNoun(m).isPlural) return res.getNth(n) } isSingular(n) { const res = this.filter(m => !parseNoun(m).isPlural) return res.getNth(n) } adjectives(n) { let res = this.update([]) this.forEach(m => { const adj = parseNoun(m).adjectives if (adj.found) { res = res.concat(adj) } }) return res.getNth(n) } // pass { agree: false } to leave the surrounding words alone toPlural(n, opts) { opts = typeof n === 'object' ? n : opts return this.getNth(n).map(m => eachNoun(m, toPlural, opts)) } toSingular(n, opts) { opts = typeof n === 'object' ? n : opts return this.getNth(n).map(m => eachNoun(m, toSingular, opts)) } } keepClass(Nouns, 'Nouns') View.prototype.nouns = select(Nouns, find, true) } export default api