UNPKG

compromise

Version:

modest natural language processing

56 lines (53 loc) • 1.9 kB
import { Sing } from '../../../2-two/preTagger/tagSet/_lib.js' import { keepTags as keep } from '../../_lib.js' import { matchCase, setChunk, isFixed } from './_lib.js' import { addArticle, needsArticle, withAdjectives } from './_article.js' import fixDeterminer from './_determiner.js' import fixVerb from './_verb.js' // 'they are [doctors]' - this must stay plural, like its subject const isPredicate = '(we|they|you) (not|#Adv)* (#Cop|become|became) (#Adv|#Adj)*$' const nounToSingular = function (m, parsed, opts = {}) { // already singular? if (!parsed.isPlural || isFixed(parsed.root)) { return m } const agree = opts.agree !== false if (agree && m.before().has(isPredicate)) { return m } const { methods, model } = m.world const { toSingular } = methods.two.transform.noun // inflect the root noun const str = parsed.root.text('normal') const single = toSingular(str, model) // nothing to do - 'sheep' if (single === str) { return m } // decide this before the phrase is changed const bare = agree && opts.article !== false && (opts.article || needsArticle(m, parsed)) // 'dogs bark' -> 'dog barks' (before our own words move) if (agree && opts.verb !== false) { fixVerb(m, parsed, false) } // 'these dogs' -> 'this dog' if (agree) { fixDeterminer(withAdjectives(m), 'singular') } const before = (parsed.root.docs[0] || []).map(term => term.text) const after = m.replace(parsed.root, single, keep).tag(Sing, 'toSingular') setChunk(after) // the conversion works on the normalized text, so put the original case back const terms = after.docs[0] || [] if (terms.length === before.length) { terms.forEach((term, i) => { term.text = matchCase(before[i], term.text) }) } // 'sells seashells' -> 'sells a seashell' if (bare) { return addArticle(m) } return m } export default nounToSingular