Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes wildcards with capture groups #472

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/terms/match/lib/applyCaptureGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
//applies the reg capture group setting to the term
const applyCaptureGroup = (term, reg) => {
if (reg.capture) {
term.captureGroup = true;
} else {
term.captureGroup = undefined;
}
};
module.exports = applyCaptureGroup;
11 changes: 5 additions & 6 deletions src/terms/match/lib/isMatch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const applyCaptureGroup = require('./applyCaptureGroup');

//compare 1 term to one reg
const perfectMatch = (term, reg) => {
Expand Down Expand Up @@ -56,16 +57,14 @@ const isMatch = (term, reg, verbose) => {
return false;
}
let found = perfectMatch(term, reg, verbose);
//flag it as a capture-group
if (reg.capture) {
term.captureGroup = true;
} else {
term.captureGroup = undefined;
}
//reverse it for .not()
if (reg.negative) {
found = !Boolean(found);
}
if (found) {
//only apply capture group settings to matches
applyCaptureGroup(term, reg);
}
return found;
};
module.exports = isMatch;
14 changes: 12 additions & 2 deletions src/terms/match/lib/startHere.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const lumpMatch = require('./lumpMatch');
const isMatch = require('./isMatch');
const applyCaptureGroup = require('./applyCaptureGroup');

// match everything until this point - '*'
const greedyUntil = (ts, i, reg) => {
Expand Down Expand Up @@ -56,15 +57,24 @@ const startHere = (ts, startAt, regs, verbose) => {
}

//support '*'
if (regs[reg_i].astrix === true) {
if (reg.astrix === true) {
//just grab until the end..
if (!next_reg) {
return ts.terms.slice(startAt, ts.length);
let terms = ts.terms.slice(startAt, ts.length);
//apply capture group settings for all wildcard terms
for (let wildcardTerm_i = term_i - startAt; wildcardTerm_i < terms.length; wildcardTerm_i++) {
applyCaptureGroup(terms[wildcardTerm_i], reg);
}
return terms;
}
let foundAt = greedyUntil(ts, term_i, regs[reg_i + 1]);
if (!foundAt) {
return null;
}
//apply capture group settings for all wildcard terms
for (let wildcardTerm_i = term_i; wildcardTerm_i < foundAt; wildcardTerm_i++) {
applyCaptureGroup(ts.terms[wildcardTerm_i], reg)
}
term_i = foundAt + 1;
reg_i += 1;
continue;
Expand Down
9 changes: 9 additions & 0 deletions test/unit/match/capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ test('match-capture-group', function(t) {
m = nlp('i saw ralf eat the glue Mrs. Hoover').match('ralf [#Verb the glue] mrs');
t.equal(m.out('normal'), 'eat the glue', 'three-word capture');

m = nlp('ralf eats the glue').match('* [#Verb]');
t.equal(m.out('normal'), 'eats', 'capture after wildcard');

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

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

m = nlp('saw the Toronto International Documentary Film Festival yesterday').match('saw the? [#Noun+] yesterday');
t.equal(m.trim().out('text'), 'Toronto International Documentary Film Festival', 'greedy capture');

Expand Down