code: def _gen_next(self, history):
          
          orig_history = history
          if not history:
              return helper.START
          history = history[-(self._n-1):]
          kv = [(k, v) for k, v in self._T.items(history)
                if k not in reserved_words]
          total = sum(v for k, v in kv)
          while total == 0 and len(history) > 0:
              history = history[1:]
              kv = [(k, v) for k, v in self._T.items(history) 
                    if k not in reserved_words]
              total = sum(v for k, v in kv)
          assert total > 0, "Sorry there is no n-gram with {!r}".format(orig_history)
          _, sampled_k = list(helper.sample_following_dist(kv, 1, total))[0]
          # print(">>>", repr(sampled_k), len(history))
          return sampled_k[len(history)]
docs: Generate next character sampled from the distribution of characters next.