-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_format_converter.py
49 lines (40 loc) · 1.41 KB
/
json_format_converter.py
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
import json
# Load the input JSON data
def convert_recipes_to_graph(input_file, output_file):
with open(input_file, 'r') as f:
recipes = json.load(f)
# Initialize nodes and links
nodes = []
links = []
# Create a mapping of item names to node IDs
item_to_id = {}
for i, item in enumerate(recipes.keys(), start=1):
item_to_id[item] = str(item)
nodes.append({"id": str(item)})
# Build links based on ingredients
for item, details in recipes.items():
current_id = item_to_id[item]
for ingredient, count in details.get("ingredients", {}).items():
if ingredient in item_to_id:
ingredient_id = item_to_id[ingredient]
label = f"{count}x{details.get('craftedCount', 1)}"
links.append({
"source": ingredient_id,
"target": current_id,
"weight": count,
"label": label
})
# Combine nodes and links into the final graph
graph = {
"nodes": nodes,
"links": links
}
# Save the graph to the output file
with open(output_file, 'w') as f:
json.dump(graph, f, indent=2)
# Specify input and output file paths
input_file = "recipes.json"
output_file = "graph.json"
# Convert recipes to graph
convert_recipes_to_graph(input_file, output_file)
print(f"Graph saved to {output_file}")