with open(os.path.join(output_path, "closed_rel_paths.jsonl"), "w") as f: # 打开文件,准备写入闭合关系路径for head in tqdm(rdict.rel2idx): # 遍历头部关系paths = set() # 初始化路径集合if head == "None" or "inv_" in head: # 如果头部关系是"None"或包含"inv_",则跳过。# "None" 表示缺失关系或空关系,不包含实际的关系信息,因此不应该作为路径的一部分。以 "inv_" 开头的关系通常表示关系的逆向或反向关系,与正常的关系具有相反的含义。continue# 采样锚点sampled_rdf = sample_anchor_rdf(fact_dict[head], num=anchor_num) # 从头部关系的事实中采样锚点#将sampled_rdf的结果写入文件,保存到sampled_rdf.jsonwith open("family/sampled_rdf.json", "w") as f:json.dump(sampled_rdf, f)with Pool(cores) as p: # 使用多进程池# 使用部分函数搜索封闭关系路径for path_seq in p.map(partial(search_closed_rel_paths, entity2desced=entity2desced, max_path_len=max_path_len), sampled_rdf):paths = paths.union(set(path_seq)) # 将路径集合合并paths = list(paths) # 转换为列表#将paths的结果写入文件,保存到list(paths).jsonwith open("family/list(paths).json", "w") as f:json.dump(list(paths), f)tqdm.write("Head relation: {}".format(head)) # 打印头部关系tqdm.write("Number of paths: {}".format(len(paths))) # 打印路径数量tqdm.write("Saving paths...") # 打印保存路径提示json.dump({"head": head, "paths": paths}, f) # 将头部关系和路径写入文件f.write("\n") # 写入换行符f.flush() # 刷新文件缓冲区
Traceback (most recent call last):
File "/home/sdjt208/three_paper/ChatRule/path_sampler.py", line 110, in <module>
main(args) # 调用主函数,并将args作为参数传递
File "/home/sdjt208/three_paper/ChatRule/path_sampler.py", line 92, in main
sample_paths(max_path_len, n_anchor, all_rdf, entity2desced, rdict, args.cores, output_path)
File "/home/sdjt208/three_paper/ChatRule/path_sampler.py", line 52, in sample_paths
json.dump({"head": head, "paths": paths}, f) # 将头部关系和路径写入文件
File "/home/sdjt208/anaconda3/envs/ChatRule/lib/python3.9/json/__init__.py", line 180, in dump
fp.write(chunk)
ValueError: I/O operation on closed file.
您可以在第二个块中使用不同的文件句柄名来解决问题。这样可以确保每个文件都有自己独立的句柄,避免在写入时引发错误。这里是修改后的代码示例:
with open(os.path.join(output_path, "closed_rel_paths.jsonl"), "w") as f_out: # 打开文件,准备写入闭合关系路径for head in tqdm(rdict.rel2idx): # 遍历头部关系paths = set() # 初始化路径集合if head == "None" or "inv_" in head: # 如果头部关系是"None"或包含"inv_",则跳过。# "None" 表示缺失关系或空关系,不包含实际的关系信息,因此不应该作为路径的一部分。以 "inv_" 开头的关系通常表示关系的逆向或反向关系,与正常的关系具有相反的含义。continue# 采样锚点sampled_rdf = sample_anchor_rdf(fact_dict[head], num=anchor_num) # 从头部关系的事实中采样锚点#将sampled_rdf的结果写入文件,保存到sampled_rdf.jsonwith open("family/sampled_rdf.json", "w") as f_in:json.dump(sampled_rdf, f_in)with Pool(cores) as p: # 使用多进程池# 使用部分函数搜索封闭关系路径for path_seq in p.map(partial(search_closed_rel_paths, entity2desced=entity2desced, max_path_len=max_path_len), sampled_rdf):paths = paths.union(set(path_seq)) # 将路径集合合并paths = list(paths) # 转换为列表#将paths的结果写入文件,保存到list(paths).jsonwith open("family/list(paths).json", "w") as f_paths:json.dump(list(paths), f_paths)tqdm.write("Head relation: {}".format(head)) # 打印头部关系tqdm.write("Number of paths: {}".format(len(paths))) # 打印路径数量tqdm.write("Saving paths...") # 打印保存路径提示json.dump({"head": head, "paths": paths}, f_out) # 将头部关系和路径写入文件f_out.write("\n") # 写入换行符f_out.flush() # 刷新文件缓冲区