def get_random_span(text, p, max_span_len, max_iter=10):
  """Get random subspan from text token sequence, following heuristics.

  Heuristics:
    1) Should not start or end mid-wordpiece.
    2) Must contain at least one non-stopword token.
    3) Length should be drawn from Geo(p) and less than max_span_len.

  Args:
    text: <string> [], space-separated token string.
    p: <float32> Geometric distribution parameter.
    max_span_len: Length to pad or truncate to.
    max_iter: Maximum rejection sampling iterations.

  Returns:
    span_wid: <string>
  """
  # Split text into tokens.
  tokens = tf.string_split([text]).values
  seq_len = tf.size(tokens)

  def reject(start, end):
    """Reject span sample."""
    span = tokens[start:end + 1]
    wordpiece_boundary = tf.logical_or(
        tf.strings.regex_full_match(span[0], r"^##.*"),
        tf.strings.regex_full_match(span[-1], r"^##.*"))
    span = tokens[start:end]
    stopwords = list(nltk_utils.get_stopwords() | set(string.punctuation))
    non_stopword = tf.setdiff1d(span, stopwords)
    all_stopword = tf.equal(tf.size(non_stopword.out), 0)
    length = tf.equal(tf.size(span), 0)
    return tf.reduce_any([wordpiece_boundary, all_stopword, length])

  def sample(start, end):
    """Sample length from truncated Geo(p)."""
    # Sample from truncated geometric distribution.
    geometric = lambda k: (1 - p)**(k - 1) * p
    probs = np.array([geometric(k) for k in range(1, max_span_len + 1)])
    probs /= probs.sum()
    length = tf.distributions.Categorical(probs=probs).sample() + 1

    # Sample start uniformly.
    max_offset = tf.maximum(1, seq_len - length + 1)
    start = tf.random.uniform([], 0, max_offset, dtype=tf.int32)
    end = start + length

    # Return span.
    return [start, end]

  # Rejection sample. Start with dummy span variable.
  start = tf.constant(0)
  end = tf.constant(0)
  start, end = tf.while_loop(
      reject, sample, [start, end], maximum_iterations=max_iter)
  span = tf.strings.reduce_join(tokens[start:end], separator=" ")

  return span
Get random subspan from text token sequence, following heuristics.

  Heuristics:
    1) Should not start or end mid-wordpiece.
    2) Must contain at least one non-stopword token.
    3) Length should be drawn from Geo(p) and less than max_span_len.

  Args:
    text: <string> [], space-separated token string.
    p: <float32> Geometric distribution parameter.
    max_span_len: Length to pad or truncate to.
    max_iter: Maximum rejection sampling iterations.

  Returns:
    span_wid: <string>
  
language/capwap/utils/text_utils.py