def frequency_sampling_paragraph(thief_vocab, thief_probs, para_len=None):
  """Sample words according to a unigram frequency to build a paragraph."""
  if para_len is None:
    # randomly choose a length from 75 to 500
    para_len = np.random.randint(75, 500)
  assert len(thief_probs) == len(thief_vocab)

  bow_para = np.random.multinomial(para_len, thief_probs)
  nonzero_indices = np.nonzero(bow_para)[0]

  bow_indices = []
  for index, freq in zip(nonzero_indices, bow_para[nonzero_indices]):
    bow_indices.extend([index for _ in range(freq)])

  assert len(bow_indices) == para_len
  random.shuffle(bow_indices)

  return " ".join([thief_vocab[i] for i in bow_indices])
Sample words according to a unigram frequency to build a paragraph.
language/bert_extraction/steal_bert_qa/data_generation/preprocess_util.py