def count_ngrams(text, max_ngram_size, stop_words):
 if (not isinstance(stop_words, set)):
  stop_words = set(stop_words)
 words = [word.lower() for word in WORD_RE.findall(text) if (word.lower() not in stop_words)]
 ngram_counts = defaultdict(int)
 for i in range(len(words)):
  for n in range(1, (max_ngram_size + 1)):
   if ((i + n) <= len(words)):
    ngram = ' '.join(words[i:(i + n)])
    ngram_counts[(n, ngram)] += 1
 for n in range(1, (max_ngram_size + 1)):
  ngram_counts[(n, None)] = ((len(words) - n) + 1)
 return ngram_counts

def count_ngrams ( text , max_ngram_size , stop_words ) : if ( not isinstance ( stop_words , set ) ) : stop_words = set ( stop_words ) words = [ word . lower ( ) for word in WORD_RE . findall ( text ) if ( word . lower ( ) not in stop_words ) ] ngram_counts = defaultdict ( int ) for i in range ( len ( words ) ) : for n in range ( 1 , ( max_ngram_size + 1 ) ) : if ( ( i + n ) <= len ( words ) ) : ngram = ' ' . join ( words [ i : ( i + n ) ] ) ngram_counts [ ( n , ngram ) ] += 1 for n in range ( 1 , ( max_ngram_size + 1 ) ) : ngram_counts [ ( n , None ) ] = ( ( len ( words ) - n ) + 1 ) return ngram_counts
break text down into ngrams .