def site_level_voting(vertical, target_website, sub_output_dir, prev_voted_lines):
    """Adds the majority voting for the predictions."""

    lines = prev_voted_lines

    field_xpath_freq_dict = dict()

    for line in lines:
        items = line.split("\t")
        assert len(items) >= 5, items
        xpath = items[1]
        pred = items[4]
        if pred == "none":
            continue
        if pred not in field_xpath_freq_dict:
            field_xpath_freq_dict[pred] = dict()
        if xpath not in field_xpath_freq_dict[pred]:
            field_xpath_freq_dict[pred][xpath] = 0
        field_xpath_freq_dict[pred][xpath] += 1

    most_frequent_xpaths = dict()  # Site level voting.
    for field, xpth_freq in field_xpath_freq_dict.items():
        frequent_xpath = sorted(
            xpth_freq.items(), key=lambda kv: kv[1], reverse=True)[0][0]  # Top 1.
        most_frequent_xpaths[field] = frequent_xpath

    voted_lines = []
    for line in lines:
        items = line.split("\t")
        assert len(items) >= 5, items
        xpath = items[1]
        flag = "none"
        for field, most_freq_xpath in most_frequent_xpaths.items():
            if xpath == most_freq_xpath:
                flag = field
        if items[4] == "none" and flag != "none":
            items[4] = flag
        voted_lines.append("\t".join(items))

    output_path = os.path.join(sub_output_dir, "preds", f"{target_website}-final-preds.txt")

    if not os.path.exists(os.path.dirname(output_path)):
        os.makedirs(os.path.dirname(output_path))

    with open(output_path, "w") as f:
        f.write("\n".join(voted_lines))

    return page_hits_level_metric(  # re-eval with the voted prediction
        vertical,
        target_website,
        sub_output_dir,
        voted_lines
    )
Adds the majority voting for the predictions.
markuplm/examples/fine_tuning/run_swde/eval_utils.py