Skip to content

Commit

Permalink
fix for obscure runtime error in capture-group
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Apr 18, 2021
1 parent c6c1878 commit f0aae0b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Spencer Kelly <spencermountain@gmail.com> (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",
Expand Down
25 changes: 1 addition & 24 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 4 additions & 3 deletions src/Phrase/match/03-tryMatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/match/capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit f0aae0b

Please sign in to comment.