from pathlib import Path


def read_keyed_text_file(text_file: Path, to_list=True):
    """
    Often used to read e.g. Kaldi `text`, `wav.scp` or `spk2utt`.

    Args:
        text_file: Path with file in format: <utterance_id> <else>
        to_list: If true, all items after the first will be split to list.

    Returns:

    """
    text_file = Path(text_file).expanduser().resolve()
    with text_file.open() as f:
        if to_list:
            return {
                line.strip().split()[0]:
                    line.strip().split()[1:] for line in f
            }
        else:
            return {
                line.strip().split()[0]: line.split(' ', maxsplit=1)[1].strip()
                for line in f.readlines()
            }