import re


def _parse_gender(label_str):
  """Parse gender string from STM "<label>" field."""
  gender = re.split(",|_", label_str)[-1][:-1]
  # Fix inconsistencies in the data.
  if not gender:
    gender = -1  # Missing label.
  elif gender == "<NA":  # In TEDLIUM release 3 training data.
    gender = -1  # Missing label.
  elif gender == "F":
    gender = "female"
  elif gender == "M":
    gender = "male"
  return gender