Skip to content

Commit 68fb316

Browse files
feat(*): issue console warning/error if a single patterns expands beyond limits
applies to compose patterns and therefore to learn custom entities
1 parent d82404d commit 68fb316

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/compose-patterns.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,18 @@ var product = function ( a ) {
110110
* (if any) must be separated by a `|` character. The patterns are composed by
111111
* computing the cartesian product of all the phrases.
112112
*
113+
* If a single patterns expands to a large size then it issues console
114+
* warning/error at 512/65536 level.
115+
*
113116
* @param {string} str the input string.
114117
* @return {string[]} of all possible patterns.
115118
* @private
116119
*/
117120
var composePatterns = function ( str ) {
118121
if ( !str || ( typeof str !== 'string' ) ) return [];
119122

123+
const LIMIT1 = 512;
124+
const LIMIT2 = 65536;
120125
var quotedTextElems = extractEnclosedText( str );
121126
var patterns = [];
122127
var finalPatterns = [];
@@ -126,6 +131,20 @@ var composePatterns = function ( str ) {
126131
patterns.push( e.split( '|' ) );
127132
} );
128133

134+
// Compute the size of the array that will be produced as a result of processing
135+
// the pattern.
136+
const size = patterns.reduce( ( ( prev, curr ) => prev * curr.length ), 1 );
137+
138+
// Issue warning/error if the size is prohibitively large from the end-user
139+
// prespective. Note: while winkNLP can handle even larger sizes, it can
140+
// still break down in the event of explosion!
141+
if ( size > LIMIT1 && size < LIMIT2 ) {
142+
console.warn( 'winkNLP: complex pattern detected, consider simplifying it!' );
143+
} else if ( size > LIMIT2 ) console.error(
144+
'winkNLP: very complex pattern detected, please review and simplify.\n' +
145+
' === It may slow down further execution! ===\n\n'
146+
);
147+
129148
product( patterns ).forEach( function ( e ) {
130149
finalPatterns.push( e.join( ' ' ).trim().split( /\s+/ ) );
131150
} );

test/compose-patterns.specs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@ describe( 'composePatterns()', function () {
6565
it( 'should return [ [ \'text\' ] ] for input "text"', function () {
6666
expect( composePatterns( 'text' ) ).to.deep.equal( [ [ 'text' ] ] );
6767
} );
68+
69+
it( 'should issue warning when patterns > 512', function () {
70+
const text = '[a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d]';
71+
expect( composePatterns( text ).length ).to.equal( 1024 );
72+
} );
73+
74+
it( 'should issue error when patterns > 65536', function () {
75+
const text = '[a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b|c|d] [a|b]';
76+
expect( composePatterns( text ).length ).to.equal( 131072 );
77+
} );
6878
} );

0 commit comments

Comments
 (0)