code: def _get_ngrams(segment, max_order):
      """Extracts all n-grams upto 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.
      """
      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
  
file_path: texar/examples/transformer/bleu_tool.py
imports: from argparse import ArgumentParser
  from io import open
  import collections
  import math
  import re
  import sys
  import unicodedata
  import numpy
  import six
  from six.moves import xrange
  from six.moves import zip
label_desc: Use image node_id 1 for calling a global function with example usage: _get_ngrams(segment, max_order) and returns: ngram_counts