def normalize(
    source,
    edit,
    delimiter_range_pair,
    task = None,
):
  """Applies sentence level edits to a source document."""

  evidence_delimiter_range = delimiter_range_pair.evidence_delimiter_range
  sentence_delimiter_range = delimiter_range_pair.sentence_delimiter_range

  # Remove context from clean source.
  source, _ = source.split(CONTEXT_MARKER)
  source = source.strip()

  # In controllable setting we need to remove all of the additional markers from
  # the input in order to make it similar to the diff format.
  if task == Task.controllable:
    source = source.replace(ADDITION_MARKER, "")
    source = source.replace(DELETION_MARKER, "")
    source = source.replace(EDIT_MARKER, "")
    source = evidence_delimiter_range.remove_delims(source)

  # Split source into sentences using sentence delimiters, and recover clean
  # source by joining delimiter-less sentences.
  clean_source = sentence_delimiter_range.remove_delims(source)

  # Remove evidence delimiters from edit if present.
  clean_edit = evidence_delimiter_range.remove_delims(edit)

  # Iterate over copy delimiters in edit to identify which source sentences
  # to keep vs edit and join them together.
  # NOTE: Source UIDs are zero-delimited, however there will be at least one
  # (possibly empty) string before the first delimiter, hence we always reject
  # the first "sentence" in the split.
  source_sentences = sentence_delimiter_range.split(source)[1:]
  clean_edited = sentence_delimiter_range.copy_replace(
      clean_edit, src=source_sentences)

  #  # Lastly remove any double whitespace.
  clean_source = re.sub(r"\s{2,}", " ", clean_source)
  clean_edited = re.sub(r"\s{2,}", " ", clean_edited)

  return clean_source, clean_edited
Applies sentence level edits to a source document.
language/fruit/rendering_utils.py