from typing import Counter


def _get_dicts(x_raw, y_raw):
    """Map features and classes to integer values, "indices".

    Arguments:
        x_raw: [[string]
            list of list of string features; outer list represents samples
            e.g., [['age_8', 'gender_M', '31', 'V72.3'],
                   ['age_55', 'gender_F', '31']]
        y_raw: [string]
            list of classes

    Returns:
        feat_idx_dict: {string: int}
            dictionary mapping features to feature indices
        idx_feat_dict: {int: string}
            dictionary mapping feature indices to features
        class_idx_dict: {string: int}
            dictionary mapping classes to class indices
        idx_class_dict: {int: string}
            dictionary mapping class indices to classes
    """
    feat_counts = Counter([f for line in x_raw for f in line])
    class_counts = Counter(y_raw)

    feat_idx_dict, idx_feat_dict, class_idx_dict, idx_class_dict = {}, {}, {}, {}

    for idx, (c, _) in enumerate(class_counts.most_common()):
        class_idx_dict[c] = idx
        idx_class_dict[idx] = c

    for idx, (feat, _) in enumerate(feat_counts.most_common()):
        feat_idx_dict[feat] = idx
        idx_feat_dict[idx] = feat

    return feat_idx_dict, idx_feat_dict, class_idx_dict, idx_class_dict