from typing import Dict


def load_lookup_from_csv(csv_source: str, count: int) -> Dict[str, int]:
    """
    From Dino, feature extraction utils
    Load a lookup dictionary, mapping string to rank, from a CSV file.
    Assumes the CSV to be pre-sorted.
    :param csv_source: Source data filepath
    :param count: number of strings to load
    :return: dictionary mapping strings to rank
    """
    lookup_dict: Dict[str, int] = dict()
    rank: int = 0
    with open(csv_source, 'r') as fd:
        line = next(fd)
        try:
            while rank < count:
                if line.startswith("#"):
                    line = next(fd)
                    continue
                lookup_dict[line.strip().split(',')[-1]] = rank
                rank += 1
                line = next(fd)
        except StopIteration:
            raise RuntimeError(f"Not enough entries in file. Expected at least {count}, max is {rank}.")

    return lookup_dict