From f0aae0bb1d64cecad8a6fc803e6d3e2047f6c4ce Mon Sep 17 00:00:00 2001 From: spencer kelly Date: Sun, 18 Apr 2021 14:35:26 -0400 Subject: [PATCH] fix for obscure runtime error in capture-group --- changelog.md | 5 +++++ package.json | 2 +- scratch.js | 25 +------------------------ src/Phrase/match/03-tryMatch.js | 7 ++++--- tests/match/capture.test.js | 10 ++++++++++ 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/changelog.md b/changelog.md index 2f0806d8e..a43dea54f 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,11 @@ While all _Major_ releases should be reviewed, our only two _large_ releases are --> +#### 13.11.1 [April 2021] + +- **[fix]** - obscure runtime error in capture-groups + _plugin-releases_: typeahead + #### 13.11.0 [April 2021] - **[change]** - use babel default build target (drop ie11 polyfill) diff --git a/package.json b/package.json index 50e77e03e..200eacf8d 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": "13.11.0", + "version": "13.11.1", "main": "./builds/compromise.js", "unpkg": "./builds/compromise.min.js", "module": "./builds/compromise.mjs", diff --git a/scratch.js b/scratch.js index 6479b3b5d..4081e721c 100644 --- a/scratch.js +++ b/scratch.js @@ -9,31 +9,8 @@ nlp.extend(require('./plugins/typeahead/src')) // nlp.verbose(true) // runtime error: -/* let doc = nlp(`to foo`) -let m = doc.match('[.+] to', 0) -console.log(m) -m.clone().debug() -*/ - -// create a newton plugin -const plugin = function (_Doc, world, _nlp) { - let obj = { - size: 2, - words: { - july: 'Month', - august: 'Month', - september: 'Month', - }, - } - _nlp.typeahead(obj.words, { min: obj.size, safe: false }) -} -nlp.extend(plugin) -let doc = nlp('20 septem') -doc.autoFill() -doc.debug() -console.log(doc.termList(1)) -console.log(doc.dates().get()) +// let m = doc.match('[.+] to').debug() // doc.clone().dates() // console.log(doc.text()) diff --git a/src/Phrase/match/03-tryMatch.js b/src/Phrase/match/03-tryMatch.js index a2003806a..5cf7cd2cf 100644 --- a/src/Phrase/match/03-tryMatch.js +++ b/src/Phrase/match/03-tryMatch.js @@ -47,6 +47,10 @@ const tryHere = function (terms, regs, start_i, phrase_length) { //support 'unspecific greedy' .* properly if (reg.anything === true && reg.greedy === true) { let skipto = logic.greedyTo(state, regs[state.r + 1]) + //maybe we couldn't find it + if (skipto === null || skipto === 0) { + return null + } // ensure it's long enough if (reg.min !== undefined && skipto - state.t < reg.min) { return null @@ -56,9 +60,6 @@ const tryHere = function (terms, regs, start_i, phrase_length) { state.t = state.t + reg.max continue } - if (skipto === null) { - return null //couldn't find it - } // is it really this easy?.... if (state.hasGroup === true) { const g = logic.getGroup(state, state.t, reg.named) diff --git a/tests/match/capture.test.js b/tests/match/capture.test.js index 5ea7aab96..0cb36df92 100644 --- a/tests/match/capture.test.js +++ b/tests/match/capture.test.js @@ -49,5 +49,15 @@ test('tricky capture', function (t) { let doc = nlp.tokenize('during august') let m = doc.match('^(on|during|in) [.]', 0) t.equal(m.text('normal'), 'august', 'found capture') + + // test for this weird non-match + doc = nlp(`to foo`) + m = doc.match('[.+] to') + t.equal(m.text(), '', 'optional-group not found') + // this is a creepy bug + m = doc.match('[.+] to', 0) + m.clone().text() + t.ok(true, 'creepy-group bug') + t.end() })