def encode_edges(
    edges,
    edge_types,
    skip_unknown = False
):
  """Converts each string edge type into an index in `edge_types`.

  Args:
    edges: Edges to encode.
    edge_types: Ordered list of types, used to determine the integer indices.
    skip_unknown: Whether to ignore edge types that aren't in the ordered list.

  Returns:
    Encoded edges.

  Raises:
    KeyError: if skip_unknown=False and an edge type isn't recognized.
  """
  type_to_idx_map = {type_name: i for i, type_name in enumerate(edge_types)}
  if len(type_to_idx_map) != len(edge_types):
    raise ValueError(f"Duplicate values in edge type list {edge_types}")
  result = []
  for s, d, t in edges:
    if t in type_to_idx_map:
      result.append((s, d, type_to_idx_map[t]))
    elif not skip_unknown:
      raise KeyError(f"Unrecognized edge type {t}")
  return result