def character_ngrams(string='', n=3, top=None, threshold=0, exclude=[], **kwargs):
 count = defaultdict(int)
 if (n > 0):
  for i in xrange(((len(string) - n) + 1)):
   w = string[i:(i + n)]
   if (w not in exclude):
    count[w] += 1
 if (threshold > 0):
  count = dict(((k, v) for (k, v) in count.items() if (v > threshold)))
 if (top is not None):
  count = dict(heapq.nsmallest(top, count.items(), key=(lambda kv: ((- kv[1]), kv[0]))))
 return kwargs.get('dict', dict)(count)

def character_ngrams ( string = '' , n = 3 , top = None , threshold = 0 , exclude = [ ] , ** kwargs ) : count = defaultdict ( int ) if ( n > 0 ) : for i in xrange ( ( ( len ( string ) - n ) + 1 ) ) : w = string [ i : ( i + n ) ] if ( w not in exclude ) : count [ w ] += 1 if ( threshold > 0 ) : count = dict ( ( ( k , v ) for ( k , v ) in count . items ( ) if ( v > threshold ) ) ) if ( top is not None ) : count = dict ( heapq . nsmallest ( top , count . items ( ) , key = ( lambda kv : ( ( - kv [ 1 ] ) , kv [ 0 ] ) ) ) ) return kwargs . get ( 'dict' , dict ) ( count )
returns a dictionary of -items .