Walk a Librispeech directory and yield examples.

def _walk_librispeech_dir(directory):
  """Walk a Librispeech directory and yield examples."""
  directory = os.path.join(directory, "LibriSpeech")
  for path, _, files in tf.io.gfile.walk(directory):
    if not files:
      continue

    transcript_file = [f for f in files if f.endswith(".txt")]
    if not transcript_file:
      continue
    assert len(transcript_file) == 1
    transcript_file, = transcript_file
    transcripts = {}
    with tf.io.gfile.GFile(os.path.join(path, transcript_file)) as f:
      for line in f:
        line = line.strip()
        key, transcript = line.split(" ", 1)
        transcripts[key] = transcript
    audio_files = [f for f in files if not f.endswith(".txt")]
    for audio_file in audio_files:
      assert audio_file.endswith(".flac")
      key = audio_file[:-len(".flac")]
      transcript = transcripts[key]
      speaker_id, chapter_id = [int(el) for el in key.split("-")[:2]]
      yield LibrispeechExample(
          speaker_id=speaker_id,
          chapter_id=chapter_id,
          audio_file=os.path.join(path, audio_file),
          transcript=transcript)