diff --git a/src/3-three/verbs/api/api.js b/src/3-three/verbs/api/api.js new file mode 100644 index 0000000..2cfd367 --- /dev/null +++ b/src/3-three/verbs/api/api.js @@ -0,0 +1,175 @@ +import find from '../find.js' +import coordinate from './conjugate/coordinate.js' +import toJSON from './toJSON.js' +import parseVerb from './parse/index.js' +import toInf from './conjugate/toInfinitive.js' +import toPast from './conjugate/toPast.js' +import toParticiple from './conjugate/toParticiple.js' +import toPresent from './conjugate/toPresent.js' +import toFuture from './conjugate/toFuture.js' +import toGerund from './conjugate/toGerund.js' +import getSubject from './parse/getSubject.js' +import getGrammar from './parse/grammar/index.js' +import toNegative from './conjugate/toNegative.js' +import { getTense } from './lib.js' + + +const api = function (View) { + class Verbs extends View { + constructor(document, pointer, groups) { + super(document, pointer, groups) + this.viewType = 'Verbs' + } + parse(n) { + return this.getNth(n).map(parseVerb) + } + json(opts, n) { + const m = this.getNth(n) + const arr = m.map(vb => { + const json = vb.toView().json(opts)[0] || {} + json.verb = toJSON(vb) + return json + }, []) + return arr + } + subjects(n) { + return this.getNth(n).map(vb => { + const parsed = parseVerb(vb) + return getSubject(vb, parsed).subject + }) + } + adverbs(n) { + return this.getNth(n).map(vb => vb.match('#Adverb')) + } + isSingular(n) { + return this.getNth(n).filter(vb => { + return getSubject(vb).plural !== true + }) + } + isPlural(n) { + return this.getNth(n).filter(vb => { + return getSubject(vb).plural === true + }) + } + isImperative(n) { + return this.getNth(n).filter(vb => vb.has('#Imperative')) + } + toInfinitive(n) { + return coordinate(this.getNth(n), 'infinitive', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toInf(vb, parsed, info.form) + }) + } + toPresentTense(n) { + return coordinate(this.getNth(n), 'present', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toPresent(vb, parsed, info.form) + }) + } + toPastTense(n) { + return coordinate(this.getNth(n), 'past', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toPast(vb, parsed, info.form) + }) + } + toFutureTense(n) { + return coordinate(this.getNth(n), 'future', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toFuture(vb, parsed, info.form) + }) + } + toGerund(n) { + return coordinate(this.getNth(n), 'gerund', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toGerund(vb, parsed, info.form) + }) + } + toPastParticiple(n) { + return coordinate(this.getNth(n), 'participle', vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + if (info.isInfinitive) { + return vb + } + return toParticiple(vb, parsed, info.form) + }) + } + conjugate(n) { + const { conjugate, toInfinitive } = this.world.methods.two.transform.verb + return this.getNth(n).map(vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + // allow imperatives like 'go!' to be conjugated here (only) + if (info.form === 'imperative') { + info.form = 'simple-present' + } + let inf = parsed.root.text('normal') + if (!parsed.root.has('#Infinitive')) { + const tense = getTense(parsed.root) + inf = toInfinitive(inf, vb.model, tense) || inf + } + return conjugate(inf, vb.model) + }, []) + } + + /** return only verbs with 'not'*/ + isNegative() { + return this.if('#Negative') + } + /** return only verbs without 'not'*/ + isPositive() { + return this.ifNo('#Negative') + } + /** remove 'not' from these verbs */ + toPositive() { + const m = this.match('do not #Verb') + if (m.found) { + m.remove('do not') + } + return this.remove('#Negative') + } + toNegative(n) { + return this.getNth(n).map(vb => { + const parsed = parseVerb(vb) + const info = getGrammar(vb, parsed) + return toNegative(vb, parsed, info.form) + }) + } + // overloaded - keep Verb class + update(pointer) { + const m = new Verbs(this.document, pointer) + m._cache = this._cache // share this full thing + return m + } + } + Verbs.prototype.toPast = Verbs.prototype.toPastTense + Verbs.prototype.toPresent = Verbs.prototype.toPresentTense + Verbs.prototype.toFuture = Verbs.prototype.toFutureTense + + View.prototype.verbs = function (n) { + let vb = find(this) + vb = vb.getNth(n) + return new Verbs(this.document, vb.pointer) + } +} +export default api diff --git a/tests/three/sentences/negative-sentences.test.js b/tests/three/sentences/negative-sentences.test.js new file mode 100644 index 0000000..a9fed99 --- /dev/null +++ b/tests/three/sentences/negative-sentences.test.js @@ -0,0 +1,14 @@ +import test from 'tape' +import nlp from '../_lib.js' +const here = '[three/sentence-negative] ' + +test('sentences.toPositive', function (t) { + const doc = nlp(`do not use reverse psychology.`) + doc.sentences().toPositive() + t.equal(doc.text(), 'use reverse psychology.', here + 'neg') + + doc.sentences().toNegative() + t.equal(doc.text(), 'do not use reverse psychology.', here + 'back to neg') + + t.end() +})