Filter out examples that exceed max_edit_ratio between source and target.

  Args:
    source_target_input:     a list of [source, target] pairs
    max_equal_to_diff_ratio: cutoff for ratio of equal chars / diff chars
      between source and target

  Returns:
    source_target_output:    filtered subset of [source, target] input pairs
    thrown_out_count:        number of examples filtered out

def edit_distance_filter(source_target_input, max_equal_to_diff_ratio=0):
  """Filter out examples that exceed max_edit_ratio between source and target.

  Args:
    source_target_input:     a list of [source, target] pairs
    max_equal_to_diff_ratio: cutoff for ratio of equal chars / diff chars
      between source and target

  Returns:
    source_target_output:    filtered subset of [source, target] input pairs
    thrown_out_count:        number of examples filtered out
  """
  thrown_out_count = 0
  source_target_output = []

  if not max_equal_to_diff_ratio:
    return source_target_input, thrown_out_count

  for src_tgt in source_target_input:
    opcodes = fast_match_sequences(*src_tgt)
    diff_char_count = 0
    equal_char_count = 0
    for tag, i1, i2, j1, j2 in opcodes:
      if tag == "diff":
        # max() prevents double-counting substitutions.
        diff_char_count += max(i2 - i1, j2 - j1)
      else:
        equal_char_count += i2 - i1
    if diff_char_count <= max_equal_to_diff_ratio * equal_char_count:
      source_target_output.append(src_tgt)
    else:
      thrown_out_count += 1
  return source_target_output, thrown_out_count