import json


def read_file_to_set(file_name):
    file_set = set()
    with open(file_name, 'r') as f:
        for line in f:
            line = line.strip()
            prefix = '../../vselector/experiment_code/data/sv-benchmarks/c/'
            if line.startswith(prefix):
                line = line[len(prefix):]
            line = line.replace('/','-')
            if line.endswith('.i'):
                line = line[:-2] + '.c'
            file_set.add(line)
    return file_set

# 加载结果文件
with open("trainRobust-CPG.json", 'r') as f:
    json_data = json.load(f)

# 创建一个新的字典，存储过滤后的数据
filtered_data = {}
max_item_0_values = {}
filtered_filenames = set()
count = 0
all_leq_zero_count = 0
#read_file_set = "../data_names_train.txt"
#file_set = read_file_to_set(read_file_set)
# 遍历原始 JSON 数据
for key, value_list in json_data.items():
    # 检查每条记录的第一个元素是否全部小于等于 0
#    if all(item[0] > 0 for item in value_list):
#        continue
        # 如果是，过滤掉该条记录
    cleaned_filename = key.split('|||')[0]
    if cleaned_filename.endswith('.json'):
        cleaned_filename = cleaned_filename[:-5]   
#    if cleaned_filename in file_set:
#    filtered_data[key] = value_list
    filtered_filenames.add(cleaned_filename)
  #      all_leq_zero_count += 1
#        continue
#    else:
        # 否则保留该条记录
#        filtered_data[key] = value_list
#        cleaned_filename = key.split('|||')[0]
#    filtered_filenames.add(key)
        # 提取每条记录的 item[0] 值
#        item_0_values = [item[0] for item in value_list]
        # 找到 item[0] 中的最大值
#        max_value = max(item_0_values)
        # 将 key 和最大值存储到新的字典中
#        max_item_0_values[key] = max_value

        #  print("item[0] 的最大值已保存到 max_item_0_values.json 文件中")
    count += 1

# 将过滤后的数据写入文件
#json.dump(filtered_data, open("select-test.json", 'w'))
print(count)
#print(all_leq_zero_count)
#print("all <= 0", all_leq_zero_count)
#print("过滤后的数据已保存到 filtered_data.json 文件中")
#with open("max_item_0_values.json", 'w') as f_out:
#    json.dump(max_item_0_values, f_out, indent=4)

with open("../data_names_train.txt", 'w') as f_out:
    for filename in filtered_filenames:
        f_out.write(filename + '\n')

#print("item[0] 的最大值已保存到 max_item_0_values.json 文件中")
#print("被过滤的文件名已保存到 filtered_filenames.txt 文件中")
