code: def _get_ngrams_with_counter(segment, max_order):
    
    ngram_counts = collections.Counter()
    for order in xrange(1, max_order + 1):
      for i in xrange(0, len(segment) - order + 1):
        ngram = tuple(segment[i:i + order])
        ngram_counts[ngram] += 1
    return ngram_counts
docs: Extracts all n-grams up to a given maximum order from an input segment.
  
    Args:
      segment: text segment from which n-grams will be extracted.
      max_order: maximum length in tokens of the n-grams returned by this
          methods.
  
    Returns:
      The Counter containing all n-grams upto max_order in segment
      with a count of how many times each n-gram occurred.