witw78

json_modifier.py

Apr 18th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. import json
  2.  
  3. # 读取 JSON 文件
  4. def read_json_file(file_path):
  5.     try:
  6.         with open(file_path, 'r', encoding='utf-8') as file:
  7.             data = json.load(file)
  8.         return data
  9.     except FileNotFoundError:
  10.         print(f"错误: 文件 {file_path} 未找到。")
  11.         return []
  12.     except json.JSONDecodeError:
  13.         print(f"错误: 无法解析 {file_path} 中的 JSON 数据。")
  14.         return []
  15.  
  16. # 添加误导性字段
  17. def add_misleading_field(data):
  18.     # misleading_fields = [
  19.     #     "The weather on that day was sunny with a gentle breeze.",
  20.     #     "A nearby observer was wearing a red hat.",
  21.     #     "There was a small bird flying overhead during the experiment.",
  22.     #     "The temperature in the room was 25 degrees Celsius.",
  23.     #     "A clock on the wall showed 3:15 PM.",
  24.     #     "A car passed by on the road outside.",
  25.     #     "There was a faint smell of flowers in the air.",
  26.     #     "A sign on the wall read 'Keep Quiet'.",
  27.     #     "A child was playing with a ball nearby.",
  28.     #     "The table where the experiment was conducted was made of wood."
  29.     # ]
  30.     misleading_fields = [
  31.         "The spring was slightly rusted, which might affect its elastic coefficient, but it was assumed to be negligible in the experiment.",
  32.         "There was a slight air resistance during the free - fall motion, although it was considered to have no significant impact on the result.",
  33.         "The road surface where the cars were moving had a small coefficient of friction, but it was ignored in this problem.",
  34.         "During the particle collision, there was a very small amount of energy loss due to a brief magnetic field interference, which was not accounted for in the calculations.",
  35.         "The lunar surface has a thin layer of dust, which might cause a small change in the gravitational field, but it was not considered in this model.",
  36.         "There was a weak magnetic field around the charged ball, which could potentially affect its motion, but was assumed to be insignificant.",
  37.         "The toll station area had a very slight slope, but it was assumed to be a flat road in the analysis.",
  38.         "The magnetic field around the coil had a small non - uniformity, but it was considered uniform for simplicity in this problem.",
  39.         "The AC generator had a small amount of mechanical friction in its rotation, which was not considered in the power calculations.",
  40.         "The transmission line had a small capacitance effect, but it was ignored in the power transmission analysis."
  41.     ]
  42.     # misleading_fields = [
  43.     #     "The weather on that day was sunny with a gentle breeze.",
  44.     #     "A nearby observer was wearing a red hat.",
  45.     #     "There was a small bird flying overhead during the experiment.",
  46.     #     "The temperature in the room was 25 degrees Celsius.",
  47.     #     "A clock on the wall showed 3:15 PM.",
  48.     #     "A car passed by on the road outside.",
  49.     #     "There was a faint smell of flowers in the air.",
  50.     #     "A sign on the wall read 'Keep Quiet'.",
  51.     #     "A child was playing with a ball nearby.",
  52.     #     "The table where the experiment was conducted was made of wood."
  53.     # ]
  54.     for i, item in enumerate(data):
  55.         context = item["question_structure"]["context"]
  56.         misleading_field = misleading_fields[i % len(misleading_fields)]
  57.         new_context = f"{context} {misleading_field}"
  58.         item["question_structure"]["context"] = new_context
  59.     return data
  60.  
  61. # 保存 JSON 文件
  62. def save_json_file(data, file_path):
  63.     try:
  64.         with open(file_path, 'w', encoding='utf-8') as file:
  65.             json.dump(data, file, ensure_ascii=False, indent=4)
  66.         print(f"文件已成功保存到 {file_path}。")
  67.     except Exception as e:
  68.         print(f"错误: 保存文件时出现问题: {e}")
  69.  
  70. if __name__ == "__main__":
  71.     input_file_path = '/home/xie.zhongwei/extracted_data.json'
  72.  
  73.     output_file_path = '/home/xie.zhongwei/modified_extracted_data.json'
  74.  
  75.     # 读取文件
  76.     data = read_json_file(input_file_path)
  77.     if data:
  78.         # 添加误导性字段
  79.         modified_data = add_misleading_field(data)
  80.         # 保存修改后的文件
  81.         save_json_file(modified_data, output_file_path)
  82.    
Add Comment
Please, Sign In to add comment