@@ -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*/
117120var 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 } ) ;
0 commit comments