code: def span_alignment(
      source_start,
      source_limit,
      target_start,
      target_limit,
      contains=False,
      contained_by=False,
      partial_overlap=False,
      multivalent_result=False,
      name=None,
  ):
      """Return an alignment from a set of source spans to a set of target spans.
  
      The source and target spans are specified using B+1 dimensional tensors,
      with `B>=0` batch dimensions followed by a final dimension that lists the
      span offsets for each span in the batch:
  
      * The `i`th source span in batch `b1...bB` starts at
        `source_start[b1...bB, i]` (inclusive), and extends to just before
        `source_limit[b1...bB, i]` (exclusive).
      * The `j`th target span in batch `b1...bB` starts at
        `target_start[b1...bB, j]` (inclusive), and extends to just before
        `target_limit[b1...bB, j]` (exclusive).
  
      `result[b1...bB, i]` contains the index (or indices) of the target span that
      overlaps with the `i`th source span in batch `b1...bB`.  The
      `multivalent_result` parameter indicates whether the result should contain
      a single span that aligns with the source span, or all spans that align with
      the source span.
  
      * If `multivalent_result` is false (the default), then `result[b1...bB, i]=j`
        indicates that the `j`th target span overlaps with the `i`th source span
        in batch `b1...bB`.  If no target spans overlap with the `i`th target span,
        then `result[b1...bB, i]=-1`.
  
      * If `multivalent_result` is true, then `result[b1...bB, i, n]=j` indicates
        that the `j`th target span is the `n`th span that overlaps with the `i`th
        source span in in batch `b1...bB`.
  
      For a definition of span overlap, see the docstring for `span_overlaps()`.
  
      #### Examples:
  
      Given the following source and target spans (with no batch dimensions):
  
      >>> #         0    5    10   15   20   25   30   35   40   45   50   55   60
      >>> #         |====|====|====|====|====|====|====|====|====|====|====|====|
      >>> # Source: [-0-]     [-1-] [2] [3]    [4][-5-][-6-][-7-][-8-][-9-]
      >>> # Target: [-0-][-1-]     [-2-][-3-][-4-] [5] [6]    [7]  [-8-][-9-][10]
      >>> #         |====|====|====|====|====|====|====|====|====|====|====|====|
      >>> source_starts = [0, 10, 16, 20, 27, 30, 35, 40, 45, 50]
      >>> source_limits = [5, 15, 19, 23, 30, 35, 40, 45, 50, 55]
      >>> target_starts = [0,  5, 15, 20, 25, 31, 35, 42, 47, 52, 57]
      >>> target_limits = [5, 10, 20, 25, 30, 34, 38, 45, 52, 57, 61]
      >>> span_alignment(source_starts, source_limits, target_starts, target_limits)
      <tf.Tensor: shape=(10,), dtype=int64,
          numpy=array([ 0, -1, -1, -1, -1, -1, -1, -1, -1, -1])>
      >>> span_alignment(source_starts, source_limits, target_starts, target_limits,
      ...                multivalent_result=True)
      <tf.RaggedTensor [[0], [], [], [], [], [], [], [], [], []]>
      >>> span_alignment(source_starts, source_limits, target_starts, target_limits,
      ...                contains=True)
      <tf.Tensor: shape=(10,), dtype=int64,
          numpy=array([ 0, -1, -1, -1, -1,  5,  6,  7, -1, -1])>
      >>> span_alignment(source_starts, source_limits, target_starts, target_limits,
      ...                 partial_overlap=True, multivalent_result=True)
      <tf.RaggedTensor [[0], [], [2], [3], [4], [5], [6], [7], [8], [8, 9]]>
  
      Args:
        source_start: A B+1 dimensional potentially ragged tensor with shape
          `[D1...DB, source_size]`: the start offset of each source span.
        source_limit: A B+1 dimensional potentially ragged tensor with shape
          `[D1...DB, source_size]`: the limit offset of each source span.
        target_start: A B+1 dimensional potentially ragged tensor with shape
          `[D1...DB, target_size]`: the start offset of each target span.
        target_limit: A B+1 dimensional potentially ragged tensor with shape
          `[D1...DB, target_size]`: the limit offset of each target span.
        contains: If true, then a source span is considered to overlap a target span
          when the source span contains the target span.
        contained_by: If true, then a source span is considered to overlap a target
          span when the source span is contained by the target span.
        partial_overlap: If true, then a source span is considered to overlap a
          target span when the source span partially overlaps the target span.
        multivalent_result: Whether the result should contain a single target span
          index (if `multivalent_result=False`) or a list of target span indices (if
          `multivalent_result=True`) for each source span.
        name: A name for the operation (optional).
  
      Returns:
        An int64 tensor with values in the range: `-1 <= result < target_size`.
        If `multivalent_result=False`, then the returned tensor has shape
          `[source_size]`, where `source_size` is the length of the `source_start`
          and `source_limit` input tensors.  If `multivalent_result=True`, then the
          returned tensor has shape `[source_size, (num_aligned_target_spans)].
      """
      scope_tensors = [
          source_start,
          source_limit,
          target_start,
          target_limit,
      ]
      with ops.name_scope(name, "SpanAlignment", scope_tensors):
          source_start = (
              ragged_tensor.convert_to_tensor_or_ragged_tensor(
                  source_start, name="source_start"
              )
          )
          source_limit = (
              ragged_tensor.convert_to_tensor_or_ragged_tensor(
                  source_limit, name="source_limit"
              )
          )
          target_start = (
              ragged_tensor.convert_to_tensor_or_ragged_tensor(
                  target_start, name="target_start"
              )
          )
          target_limit = (
              ragged_tensor.convert_to_tensor_or_ragged_tensor(
                  target_limit, name="target_limit"
              )
          )
  
          # <bool>[D1...DB, source_size, target_size]
          # overlaps[b1...bB, i, j] is true if source span i overlaps target span j
          # (in batch b1...bB).
          overlaps = span_overlaps(
              source_start,
              source_limit,
              target_start,
              target_limit,
              contains,
              contained_by,
              partial_overlap,
          )
  
          # <int64>[D1...DB, source_size, (num_aligned_spans)]
          # alignment[b1...bB, i, n]=j if target span j is the n'th target span
          # that aligns with source span i (in batch b1...bB).
          alignment = _multivalent_span_alignment(overlaps)
  
          if not multivalent_result:
              # <int64>[D1...DB, source_size]
              # alignment[b1...bB, i]=j if target span j is the last target span
              # that aligns with source span i, or -1 if no target spans align.
              alignment = ragged_functional_ops.map_flat_values(
                  math_ops.maximum,
                  ragged_math_ops.reduce_max(alignment, axis=-1),
                  -1,
              )
          return alignment
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import array_ops_stack
  from tensorflow.python.ops import check_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops.ragged import ragged_functional_ops
  from tensorflow.python.ops.ragged import ragged_gather_ops
  from tensorflow.python.ops.ragged import ragged_math_ops
  from tensorflow.python.ops.ragged import ragged_tensor
  from tensorflow.python.ops.ragged import ragged_where_op
  from tensorflow.python.ops.ragged import segment_id_ops
label_desc: Use image node_id 6 for calling a global function with example usage: span_alignment(source_start, source_limit, target_start, target_limit, contains, contained_by, partial_overlap, multivalent_result, name) and returns: alignment