| import json |
|
|
| |
| data = { |
| "primes": [ |
| {"value": 2, "category": "prime"}, |
| {"value": 3, "category": "prime"}, |
| {"value": 5, "category": "prime"}, |
| |
| ], |
| "perfect_numbers": [ |
| {"value": 6, "category": "perfect_number"}, |
| {"value": 28, "category": "perfect_number"}, |
| |
| ], |
| "relationships": [ |
| {"source": 2, "target": 3, "relationship": "generates_Mersenne_prime"}, |
| |
| ] |
| } |
|
|
| |
| def infer_relationships(nodes): |
| inferred_relationships = [] |
| node_values = {node['value']: node for node in nodes} |
| |
| |
| for source in nodes: |
| for target in nodes: |
| if source == target: |
| continue |
| |
| source_value = source['value'] |
| target_value = target['value'] |
| |
| |
| if target_value % source_value == 0: |
| factor = target_value // source_value |
| inferred_relationships.append({ |
| "source": source_value, |
| "target": target_value, |
| "relationship": f"multiplied_by_{factor}" |
| }) |
| |
| |
| if target_value - source_value > 0: |
| addend = target_value - source_value |
| inferred_relationships.append({ |
| "source": source_value, |
| "target": target_value, |
| "relationship": f"add_{addend}" |
| }) |
| |
| return inferred_relationships |
|
|
| |
| nodes = data["primes"] + data["perfect_numbers"] |
| new_relationships = infer_relationships(nodes) |
|
|
| |
| data["relationships"].extend(new_relationships) |
|
|
| |
| with open("updated_graph_data.json", "w") as file: |
| json.dump(data, file, indent=4) |