From cab01386684929f256dd9885f2edb6084354fbf2 Mon Sep 17 00:00:00 2001 From: dualfroz Date: Sat, 3 Oct 2026 03:06:19 +0200 Subject: [PATCH] Contract each match on its own instead of merging them into one --- src/2-two/contraction-two/api/contract.js | 11 ++++++----- tests/two/contractions/contract.test.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/2-two/contraction-two/api/contract.js b/src/2-two/contraction-two/api/contract.js index c697bf7..afde546 100644 --- a/src/2-two/contraction-two/api/contract.js +++ b/src/2-two/contraction-two/api/contract.js @@ -1,10 +1,6 @@ const postPunct = /[,)"';:\-–—.…]/ -const setContraction = function (m, suffix) { - if (!m.found) { - return - } - const terms = m.termList() +const contractTerms = function (terms, suffix) { //avoid any problematic punctuation for (let i = 0; i < terms.length - 1; i++) { const t = terms[i] @@ -27,6 +23,11 @@ const setContraction = function (m, suffix) { } } +// contract each match on its own +const setContraction = function (m, suffix) { + m.docs.forEach(terms => contractTerms(terms, suffix)) +} + /** turn 'i am' into i'm */ const contract = function () { const doc = this.not('@hasContraction') diff --git a/tests/two/contractions/contract.test.js b/tests/two/contractions/contract.test.js index 79f153b..fd9435f 100644 --- a/tests/two/contractions/contract.test.js +++ b/tests/two/contractions/contract.test.js @@ -74,3 +74,22 @@ test('multiple contraction in sentence', function (t) { t.equal(doc.match('he is dead').length, 2, here + 'multi-contraction-count') t.end() }) + +test('contract multiple matches', function (t) { + let doc = nlp('he is here and she is there') + doc.contract() + t.equal(doc.text(), `he's here and she's there`, here + 'two in one sentence') + + doc = nlp('we are tired and they are not') + doc.contract() + t.equal(doc.text(), `we're tired and they're not`, here + 'two of the same') + + doc = nlp('he is here. she is there.') + doc.contract() + t.equal(doc.text(), `he's here. she's there.`, here + 'two sentences') + + doc = nlp('Tony, is here and John is there') + doc.contract() + t.equal(doc.text(), `Tony, is here and John's there`, here + 'skip one with punctuation') + t.end() +})