def genDictFromFreq(freq_dict, size_b):
    """
    Creates a LZ77 dictionary (initial lookback window) from a dictionary of
    word: frequency

    Args:
        freq_dict   (dict(str)): A dictionary mapping word to frequency
        size_b      (int): output size of frequency dictionary in B

    Returns:
        str: A LZ77 dictionary of size_b scored on len(word) * frequency
    """

    # change value from frequency to score
    for word, freq in freq_dict.items():
        freq_dict[word] = len(word) * freq

    """ superstrings swallow substring scores """
    # 1. sort keys on increasing key (word) length
    sorted_keys = sorted(freq_dict, key=lambda k: len(k))

    # 2. add substring scores to superstring value and flag for
    #    removal (set score to 0)
    for i, key_i in enumerate(sorted_keys):
        # highest scoring superstring should consume substring
        sorted_keys_by_score = sorted(sorted_keys[i+1:],
                                      key=lambda k: freq_dict[k],
                                      reverse=True)
        for j, key_j in enumerate(sorted_keys_by_score):
            if key_i in key_j:
                freq_dict[key_j] += freq_dict[key_i]
                freq_dict[key_i] = 0
                break

    # 3. Remove substring items (has score 0)
    freq_dict = {k: v for k, v in freq_dict.items() if v > 0}

    """ Create LZ77 dictionary string """
    # 1. Join keys (word) on score in ascending) order
    # According to zlib documentation, most common substrings should be placed
    # at the end of the pre-defined dictionary
    dict_str = ''.join(sorted(freq_dict,
                              key=lambda k: freq_dict[k]))

    # 2. trim to size_b if valid
    if 0 < size_b < len(dict_str):
        dict_str = dict_str[len(dict_str)-size_b:]

    return dict_str