From fd61e5bc5afd2800be091bed2ca87faca65060a3 Mon Sep 17 00:00:00 2001 From: dualfroz Date: Fri, 2 Oct 2026 16:45:25 +0200 Subject: [PATCH] Don't add an implicit 'to' after 'wanna' when a noun follows --- src/2-two/contraction-two/compute/index.js | 5 +++++ src/2-two/contraction-two/compute/wanna.js | 24 ++++++++++++++++++++++ tests/two/contractions/expand.test.js | 16 +++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/2-two/contraction-two/compute/wanna.js diff --git a/src/2-two/contraction-two/compute/index.js b/src/2-two/contraction-two/compute/index.js index e1f6a49..c1894e4 100644 --- a/src/2-two/contraction-two/compute/index.js +++ b/src/2-two/contraction-two/compute/index.js @@ -3,6 +3,7 @@ import apostropheS from './apostrophe-s.js' import apostropheD from './apostrophe-d.js' import apostropheT from './apostrophe-t.js' import isPossessive from './isPossessive.js' +import wannaNoun from './wanna.js' const byApostrophe = /'/ @@ -61,6 +62,10 @@ const contractionTwo = view => { document.forEach((terms, n) => { // loop through terms backwards for (let i = terms.length - 1; i >= 0; i -= 1) { + if (wannaNoun(terms, i)) { + reIndex(terms) + continue + } // is it already a contraction if (terms[i].implicit) { continue diff --git a/src/2-two/contraction-two/compute/wanna.js b/src/2-two/contraction-two/compute/wanna.js new file mode 100644 index 0000000..d9fbafe --- /dev/null +++ b/src/2-two/contraction-two/compute/wanna.js @@ -0,0 +1,24 @@ +// 'i wanna pickle' is [want, pickle], not [want, to, pickle] +const hasObject = term => term && ['Determiner', 'Pronoun', 'Possessive'].some(tag => term.tags.has(tag)) + +const wannaNoun = function (terms, i) { + if (terms[i].normal !== 'wanna') { + return false + } + const to = terms[i + 1] + const next = terms[i + 2] + if (!to || to.implicit !== 'to' || !next) { + return false + } + if (next.tags.has('Verb') || next.tags.has('Adverb')) { + return false + } + // 'wanna pickle the beets' is still a verb, before its object + if (hasObject(terms[i + 3])) { + return false + } + terms[i].post = to.post + terms.splice(i + 1, 1) + return true +} +export default wannaNoun diff --git a/tests/two/contractions/expand.test.js b/tests/two/contractions/expand.test.js index 2f75d58..7c0d31e 100644 --- a/tests/two/contractions/expand.test.js +++ b/tests/two/contractions/expand.test.js @@ -33,6 +33,22 @@ test('basic is contractions', function (t) { t.end() }) +test('wanna before a noun', function (t) { + const arr = [ + ['I wanna pickle', 'I want pickle'], + ['I wanna a cookie', 'I want a cookie'], + ['do you wanna coffee?', 'do you want coffee?'], + ['I wanna go', 'I want to go'], + ['I wanna pickle some beets', 'I want to pickle some beets'], + ] + arr.forEach(([str, want]) => { + const r = nlp(str) + r.contractions().expand() + t.equal(r.text(), want, here + str) + }) + t.end() +}) + test('do-not contractions', function (t) { let r = nlp(`please do not eat the marshmellow`) r.contractions().expand()