import json
import os

multiple_rules = json.load(open("data/multiple_rules.json"))
result_data = {}
# print(multiple_rules)
for rule in multiple_rules:
    n_rule = len(rule['rule'])
    if n_rule not in result_data.keys():
        result_data[n_rule] = {True: [], False: []}
    result_data[n_rule][rule['valid']].append(rule)

combined = []

for k, v in result_data.items():
    if k == 1:
        continue
    print(k, len(v[True]), len(v[False]))
    n = min(len(v[True]), len(v[False]))
    single_result = []
    for i in range(0, n):
        single_result.append(v[True][i])
    for i in range(0, n):
        single_result.append(v[False][i])
    with open("data/multiple_rules_%d.json" % k, "w") as fp:
        json.dump(single_result, fp)
