Contract each match on its own instead of merging them into one

This commit is contained in:
dualfroz committed 2026-10-03 03:06:19 +02:00
1 parent 163edfec77
commit cab0138668
2 files changed
+25 -5

No files matched your search

+6 -5
View File
@@ -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')
+19
View File
@@ -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()
})