From 83c2f65dea17f411a41998bc3bdb1c48e7640006 Mon Sep 17 00:00:00 2001 From: spencer kelly Date: Thu, 10 Oct 2024 08:27:26 -0400 Subject: [PATCH] fix for #1150 --- changelog.md | 5 ++++- package.json | 4 ++-- scratch.js | 32 +++++++++++++++++++------------- src/1-one/change/api/replace.js | 14 +++++++++++--- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/changelog.md b/changelog.md index 7f6f5af92..31c0f6242 100644 --- a/changelog.md +++ b/changelog.md @@ -20,7 +20,10 @@ While all _Major_ releases should be reviewed, our only _large_ releases are **v - **[fix]** - .before() .after() --> -#### 14.14.1 [July 2024] +#### 14.14.2 [Oct 2024] +- **[fix]** - runtime error in punctuation replace #1150 + +#### 14.14.1 [Oct 2024] - **[update]** - compromise-dates 3.7.0 - **[fix]** - runtime error in number parser #1145 - **[update]** - dependencies diff --git a/package.json b/package.json index 8c5bbfed1..aa830b1e5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Spencer Kelly (http://spencermounta.in)", "name": "compromise", "description": "modest natural language processing", - "version": "14.14.1", + "version": "14.14.2", "module": "./src/three.js", "main": "./src/three.js", "unpkg": "./builds/compromise.js", @@ -132,4 +132,4 @@ "_tests/**" ], "license": "MIT" -} +} \ No newline at end of file diff --git a/scratch.js b/scratch.js index f6cbcaf81..8b308d0d2 100644 --- a/scratch.js +++ b/scratch.js @@ -5,19 +5,25 @@ nlp.plugin(plg) // nlp.verbose('tagger') - -const context = { - today: '2024-09-24' -} - -let text = 'first half of march' -// let text = 'in 2-3 years' -// let text = 'March 28 next year' -// let text = 'June next year' -const doc1 = nlp(text).debug() -const dates1 = doc1.dates(context).get()[0] -console.log(text) -console.log(dates1) +const text = 'to the window' + +// Throws an error, capturing group at beginning, replacing with an empty string +const doc1 = nlp(text) +doc1.match('[to] the window', 0).replaceWith('by') +doc1.debug() +doc1.match('[by]', 0).debug().replaceWith('') + +// Works, capturing group at beginning, replacing with a non-empty string +const doc2 = nlp(text) +doc2.match('[to] the window', 0).replaceWith('near') +doc2.match('[near]', 0).replaceWith('by') +console.log('doc2', doc2.text()) + +// Works, capture group at end, replacing with an empty string +const doc3 = nlp(text) +doc3.match('to the [window]', 0).replaceWith('wall') +doc3.match('[wall]', 0).replaceWith('') +console.log('doc3', doc3.text()) // const doc = nlp('one match match after') diff --git a/src/1-one/change/api/replace.js b/src/1-one/change/api/replace.js index cfda1dc76..b43c5b7ba 100644 --- a/src/1-one/change/api/replace.js +++ b/src/1-one/change/api/replace.js @@ -76,9 +76,17 @@ fns.replaceWith = function (input, keep = {}) { } } - // try to keep some pre-post punctuation - if (originalPre) main.docs[0][0].pre = originalPre - if (originalPost && !main.docs[0][main.docs[0].length - 1].post.trim()) main.docs[0][main.docs[0].length - 1].post = originalPost + // try to keep some pre-punctuation + if (originalPre) { + main.docs[0][0].pre = originalPre + } + // try to keep any post-punctuation + if (originalPost && main.docs[0]) { + let lastOne = main.docs[0][main.docs[0].length - 1] + if (!lastOne.post.trim()) { + lastOne.post = originalPost + } + } // what should we return? let m = main.toView(ptrs).compute(['index', 'freeze', 'lexicon'])