def remove_repetitive_trigram(preds, lengths, vocab, hps):
  """Select from the beam a prediction without repetitive trigrams."""
  ret_preds, ret_lengths = [], []
  for (pred, length) in zip(preds, lengths):
    flag = True
    for i in xrange(hps.beam_width):
      l = length[Ellipsis, i]
      p = pred[Ellipsis, i][:l]
      tokens = data.id2text(p, vocab=vocab, use_bpe=hps.use_bpe).split()
      flag = repetitive_ngrams(3, tokens) or bad_tok(tokens, vocab)
      if not flag:
        ret_preds.append(pred[Ellipsis, i])
        ret_lengths.append(length[Ellipsis, i])
        break
    if flag:
      ret_preds.append(pred[Ellipsis, 0])
      ret_lengths.append(length[Ellipsis, 0])

  predictions = np.int32(np.stack(ret_preds)), np.int32(np.stack(ret_lengths))
  return predictions
Select from the beam a prediction without repetitive trigrams.
language/labs/exemplar_decoding/models/model_function.py