Skip to content

Commit

Permalink
fix for #767
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Aug 7, 2020
1 parent 144efb8 commit c52c1c3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ While all _Major_ releases should be reviewed, our only two _large_ releases are
#### 13.3.2

- fix for offset issue #771
- fix for `{min,max}` syntax #767
- typescript fixes
- update deps

Expand Down
4 changes: 2 additions & 2 deletions scratch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nlp = require('./src/index')
nlp.verbose(true)
// nlp.verbose(true)
// let txt = require('./scripts/test/speed/_sotu-text.js')
nlp.extend(require('./plugins/numbers/src'))
nlp.extend(require('./plugins/dates/src'))
Expand All @@ -13,4 +13,4 @@ nlp.extend(require('./plugins/dates/src'))
// doc.sentences().toPastTense().debug()

// console.log(nlp('next week').dates().json())
nlp('you are John, Lisa, and Fred').debug()
nlp('you are John, Lisa, Fred').match('#FirstName{1,2}').debug()
12 changes: 7 additions & 5 deletions src/Phrase/match/03-tryMatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const makeId = require('../../Term/_id')
// i formally apologize for how complicated this is.

//found a match? it's greedy? keep going!
const getGreedy = function(terms, t, reg, until, index, length) {
const getGreedy = function (terms, t, reg, until, index, length) {
let start = t
for (; t < terms.length; t += 1) {
//stop for next-reg match
Expand All @@ -27,7 +27,7 @@ const getGreedy = function(terms, t, reg, until, index, length) {
}

//'unspecific greedy' is a weird situation.
const greedyTo = function(terms, t, nextReg, index, length) {
const greedyTo = function (terms, t, nextReg, index, length) {
//if there's no next one, just go off the end!
if (!nextReg) {
return terms.length
Expand All @@ -43,7 +43,7 @@ const greedyTo = function(terms, t, nextReg, index, length) {
}

// get or create named group
const getOrCreateGroup = function(namedGroups, namedGroupId, terms, startIndex, group) {
const getOrCreateGroup = function (namedGroups, namedGroupId, terms, startIndex, group) {
const g = namedGroups[namedGroupId]

if (g) {
Expand All @@ -62,7 +62,7 @@ const getOrCreateGroup = function(namedGroups, namedGroupId, terms, startIndex,
}

/** tries to match a sequence of terms, starting from here */
const tryHere = function(terms, regs, index, length) {
const tryHere = function (terms, regs, index, length) {
const namedGroups = {}
let previousGroupId = null
let t = 0
Expand Down Expand Up @@ -125,7 +125,6 @@ const tryHere = function(terms, regs, index, length) {

continue
}

//if it looks like a match, continue
//we have a special case where an end-anchored greedy match may need to
//start matching before the actual end; we do this by (temporarily!)
Expand Down Expand Up @@ -172,6 +171,9 @@ const tryHere = function(terms, regs, index, length) {
if (t === null) {
return [false, null] //greedy was too short
}
if (reg.min && reg.min > t) {
return [false, null] //greedy was too short
}
// if this was also an end-anchor match, check to see we really
// reached the end
if (reg.end === true && index + t !== length) {
Expand Down
33 changes: 19 additions & 14 deletions tests/match/greedy-capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,25 @@ const nlp = require('../_lib')
* https://github.com/spencermountain/compromise/issues/654
*/

test('issue-654: named greedy capture', function(t) {
test('issue-654: named greedy capture', function (t) {
let m

m = nlp('ralf eats the glue')
.match('ralf eats [<target>*]')
.groups('target')
m = nlp('ralf eats the glue').match('ralf eats [<target>*]').groups('target')
t.equal(m.out('normal'), 'the glue', 'wildcard capture at the end')

m = nlp('ralf eats the glue')
.match('ralf eats [<target>*] glue')
.groups('target')
m = nlp('ralf eats the glue').match('ralf eats [<target>*] glue').groups('target')
t.equal(m.out('normal'), 'the', 'wildcard capture in the middle')

m = nlp('ralf eats the glue')
.match('ralf eats [<target>.+]')
.groups('target')
m = nlp('ralf eats the glue').match('ralf eats [<target>.+]').groups('target')
t.equal(m.out('normal'), 'the glue', 'wildcard capture at the end')

m = nlp('ralf eats the glue')
.match('ralf eats [<target>.+] glue')
.groups('target')
m = nlp('ralf eats the glue').match('ralf eats [<target>.+] glue').groups('target')
t.equal(m.out('normal'), 'the', 'wildcard capture in the middle')

t.end()
})

test('issue-654: greedy capture', function(t) {
test('issue-654: greedy capture', function (t) {
let m

m = nlp('ralf eats the glue').match('ralf eats [*]', 0)
Expand All @@ -49,3 +41,16 @@ test('issue-654: greedy capture', function(t) {

t.end()
})

test('test greedy min/max', function (t) {
let doc = nlp('hello John, Lisa, Fred').match('#FirstName{3,6}')
t.equal(doc.text(), 'John, Lisa, Fred', 'min met')

doc = nlp('hello John, Lisa, Fred').match('#FirstName{4,6}')
t.equal(doc.found, false, 'min not met')

doc = nlp('hello John, Lisa, Fred').match('#FirstName{1,2}')
t.equal(doc.eq(0).text(), 'John, Lisa', 'max-match')
t.equal(doc.eq(1).text(), 'Fred', 'max-over-run')
t.end()
})

0 comments on commit c52c1c3

Please sign in to comment.