import typing


def load_phoneme_ids(phonemes_file: typing.TextIO) -> typing.Dict[str, int]:
    """
    Load phoneme id mapping from a text file.
    Format is ID<space>PHONEME
    Comments start with #

    Args:
        phonemes_file: text file

    Returns:
        dict with phoneme -> id
    """
    phoneme_to_id = {}
    for line in phonemes_file:
        line = line.strip("\r\n")
        if (not line) or line.startswith("#") or (" " not in line):
            # Exclude blank lines, comments, or malformed lines
            continue

        phoneme_id, phoneme_str = line.split(" ", maxsplit=1)
        phoneme_to_id[phoneme_str] = int(phoneme_id)

    return phoneme_to_id