File size: 3,014 Bytes
1a19f57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import json
from pathlib import Path
# ======== 必填路径 ========
# 二选一:1) 直接用之前保存的 map.json
map_json_path = None # 若没有就置为 None
# 或 2) 用左右两个文件重建映射(与前一轮一致)
right_txt_path = "/nfs/ywang29/Reward_finetuning/VideoX-Fun/VBench/VBench-2.0/prompts/prompt_aug/Wanx_full_text_aug.txt" # 当 map_json_path 为 None 时启用
left_txt_path = "/nfs/ywang29/Reward_finetuning/VideoX-Fun/VBench/VBench-2.0/prompts/VBench2_full_text.txt" # 当 map_json_path 为 None 时启用
# 需要被逐行映射的“查询文件”
query_txt_path = "/nfs/ywang29/Reward_finetuning/VideoX-Fun/VBench/VBench-2.0/missing_prompts.txt"
# 输出
mapped_txt_out ="/nfs/ywang29/Reward_finetuning/VideoX-Fun/VBench/VBench-2.0/missing_prompts_wan_aug.txt"
miss_report_out = "query.missing.txt"
# 是否 strip 每行首尾空白
strip_line = True
# ======== 加载映射 ========
def build_mapping_from_pair(left_path, right_path, strip_line=True):
left_lines = Path(left_path).read_text(encoding="utf-8").splitlines()
right_lines = Path(right_path).read_text(encoding="utf-8").splitlines()
if len(left_lines) != len(right_lines):
raise ValueError(f"行数不一致:{len(left_lines)} vs {len(right_lines)}")
if strip_line:
left_lines = [s.strip() for s in left_lines]
right_lines = [s.strip() for s in right_lines]
mapping = {}
for l, r in zip(left_lines, right_lines):
if l in mapping:
if isinstance(mapping[l], list):
mapping[l].append(r)
else:
mapping[l] = [mapping[l], r]
else:
mapping[l] = r
return mapping
if map_json_path and Path(map_json_path).exists():
mapping = json.loads(Path(map_json_path).read_text(encoding="utf-8"))
else:
mapping = build_mapping_from_pair(left_txt_path, right_txt_path, strip_line=strip_line)
# ======== 逐行查询并保存 ========
query_lines = Path(query_txt_path).read_text(encoding="utf-8").splitlines()
if strip_line:
query_lines = [s.strip() for s in query_lines]
mapped_lines = []
missing = []
for i, q in enumerate(query_lines):
if q in mapping:
v = mapping[q]
# 多值时拼接;也可改为只取第一个:v[0](当 v 为 list)
if isinstance(v, list):
mapped_lines.append(" ||| ".join(v))
else:
mapped_lines.append(str(v))
else:
mapped_lines.append(f"__MISSING__:{q}")
missing.append({"line_idx": i, "query": q})
# 写出结果
Path(mapped_txt_out).write_text("\n".join(mapped_lines), encoding="utf-8")
# 写出缺失报告(若有)
if missing:
Path(miss_report_out).write_text(
json.dumps(missing, ensure_ascii=False, indent=2),
encoding="utf-8"
)
print(f"完成:{mapped_txt_out}\n有 {len(missing)} 条未匹配,详情见 {miss_report_out}")
else:
print(f"完成:{mapped_txt_out}\n全部行成功匹配。")
|