from typing import TextIO
from typing import Dict
import csv


def read_freq_file(input_fh: TextIO, max_records=-1) -> Dict[str, int]:
    """Reads lines of the form "word, count" and returns a dictionary of this word -> count data

    If there are duplicate keys, the corresponding values are added.
    """
    output_dict = {}
    reader = csv.reader(input_fh)
    lines_read = 1
    for row in reader:
        output_dict[row[0]] = output_dict.get(row[0], 0) + int(row[1])
        lines_read += 1
        if lines_read > max_records > 0:
            break
    return output_dict