text.span_overlaps

View source on GitHub

Returns a boolean tensor indicating which source and target spans overlap.

text.span_overlaps(
 source_start,
 source_limit,
 target_start,
 target_limit,
 contains=False,
 contained_by=False,
 partial_overlap=False,
 name=None
)

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 ith 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 jth 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, j] is true if the ith source span overlaps with the jth target span in batch b1...bB, where a source span overlaps a target span if any of the following are true:

  • The spans are identical.
  • contains is true, and the source span contains the target span.
  • contained_by is true, and the source span is contained by the target span.
  • partial_overlap is true, and there is a non-zero overlap between the source span and the target span.

Example:

Given the following source and target spans (with no batch dimensions):

  # 0 5 10 15 20 25 30 35 40
  # |==||||||||
  # Source: [-0-] [-1-] [2] [-3-][-4-][-5-]
  # Target: [-0-][-1-] [-2-] [3] [-4-][-5-]
  # ||||||||==|
 source_start = [0, 10, 16, 20, 25, 30]
 source_limit = [5, 15, 19, 25, 30, 35]
 target_start = [0, 5, 15, 21, 27, 31]
 target_limit = [5, 10, 20, 24, 32, 37]
  
 

result[i, j] will be true at the following locations:

* `[0, 0]` (always)
* `[2, 2]` (if contained_by=True or partial_overlaps=True)
* `[3, 3]` (if contains=True or partial_overlaps=True)
* `[4, 4]` (if partial_overlaps=True)
* `[5, 4]` (if partial_overlaps=True)
* `[5, 5]` (if partial_overlaps=True)

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.
name A name for the operation (optional).

Returns

A B+2 dimensional potentially ragged boolean tensor with shape [D1...DB, source_size, target_size].

Raises

ValueError If the span tensors are incompatible.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年04月11日 UTC.