Update main code
This commit is contained in:
parent
32746fe4b5
commit
9ca5c24110
|
@ -1,49 +1,61 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
def add_context(input_file_path, output_file_path):
|
||||||
|
with open(input_file_path, 'r', encoding='utf-8') as file:
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
|
for item in data:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
name = item.get("qanon_title")
|
||||||
|
item.update({
|
||||||
|
"context": {
|
||||||
|
"qanon_name": name,
|
||||||
|
"parents_content": "parents_content",
|
||||||
|
"meaning_change": [{
|
||||||
|
"term": "str1",
|
||||||
|
"meaning": "def",
|
||||||
|
"Confidence_level": "0"
|
||||||
|
}],
|
||||||
|
"referential term": [{
|
||||||
|
"term": "this",
|
||||||
|
"referent": "that"
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
with open(output_file_path, 'w', encoding='utf-8') as file:
|
||||||
|
json.dump(data, file, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
def add_parent_content(output_file_path):
|
||||||
|
with open(output_file_path, 'r', encoding='utf-8') as file:
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
|
id_content = {}
|
||||||
|
parent_map = {}
|
||||||
|
for item in data:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
item_id = item.get("id")
|
||||||
|
id_content[item_id] = item.get("content")
|
||||||
|
parent_map[item_id] = item.get("parent_id")
|
||||||
|
|
||||||
|
for item in data:
|
||||||
|
if isinstance(item, dict):
|
||||||
|
current_id = item.get("parent_id")
|
||||||
|
parents_content = []
|
||||||
|
|
||||||
|
while current_id != "0":
|
||||||
|
parent_content = id_content.get(current_id)
|
||||||
|
parents_content.append(parent_content)
|
||||||
|
current_id = parent_map.get(current_id, "0")
|
||||||
|
|
||||||
|
if "context" in item:
|
||||||
|
item["context"]["parents_content"] = parents_content
|
||||||
|
|
||||||
|
with open(output_file_path, 'w', encoding='utf-8') as file:
|
||||||
|
json.dump(data, file, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
input_file_path = "qanon.json" # path
|
input_file_path = "qanon.json" # path
|
||||||
with open(input_file_path, 'r', encoding='utf-8') as file:
|
|
||||||
data = json.load(file)
|
|
||||||
|
|
||||||
id_content = {}
|
|
||||||
parent_map = {}
|
|
||||||
for item in data:
|
|
||||||
if isinstance(item, dict):
|
|
||||||
item_id = item.get("id")
|
|
||||||
id_content[item_id] = item.get("content")
|
|
||||||
parent_map[item_id] = item.get("parent_id")
|
|
||||||
|
|
||||||
for item in data:
|
|
||||||
if isinstance(item, dict):
|
|
||||||
name = item.get("qanon_title")
|
|
||||||
current_id = item.get("parent_id")
|
|
||||||
parents_content = []
|
|
||||||
|
|
||||||
item.update({
|
|
||||||
"context": {
|
|
||||||
"qanon_name": name,
|
|
||||||
"parents_content": parents_content,
|
|
||||||
"meaning_change": [{
|
|
||||||
"term": "str1",
|
|
||||||
"meaning": "def",
|
|
||||||
"Confidence_level": "0"
|
|
||||||
}],
|
|
||||||
"referential term": [{
|
|
||||||
"term": "this",
|
|
||||||
"referent": "that"
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if isinstance(item, dict) and "parents_content" in item.get("context" , []):
|
|
||||||
while current_id != "0":
|
|
||||||
parent_content = id_content.get(current_id)
|
|
||||||
parents_content.append(parent_content)
|
|
||||||
current_id = parent_map.get(current_id, "0")
|
|
||||||
|
|
||||||
|
|
||||||
print("done")
|
|
||||||
|
|
||||||
output_file_path = 'output_data.json'
|
output_file_path = 'output_data.json'
|
||||||
with open(output_file_path, 'w', encoding='utf-8') as file:
|
add_context(input_file_path, output_file_path)
|
||||||
json.dump(data, file, indent=4, ensure_ascii=False)
|
add_parent_content(output_file_path)
|
||||||
print(f"Modified data has been saved to {output_file_path}")
|
print(f"Modified data has been saved to {output_file_path}")
|
Loading…
Reference in New Issue
Block a user