File size: 2,151 Bytes
f5b1acc |
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 |
import json
# Sample data structure based on your JSON
data = {
"primes": [
{"value": 2, "category": "prime"},
{"value": 3, "category": "prime"},
{"value": 5, "category": "prime"},
# add other nodes as needed
],
"perfect_numbers": [
{"value": 6, "category": "perfect_number"},
{"value": 28, "category": "perfect_number"},
# add other nodes as needed
],
"relationships": [
{"source": 2, "target": 3, "relationship": "generates_Mersenne_prime"},
# add other predefined relationships
]
}
# Inferred relationship function
def infer_relationships(nodes):
inferred_relationships = []
node_values = {node['value']: node for node in nodes}
# Iterate over node pairs to find possible relationships
for source in nodes:
for target in nodes:
if source == target:
continue
source_value = source['value']
target_value = target['value']
# Check for multiplication relationship
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}"
})
# Check for addition relationship
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
# Infer relationships
nodes = data["primes"] + data["perfect_numbers"]
new_relationships = infer_relationships(nodes)
# Append new relationships to the original data
data["relationships"].extend(new_relationships)
# Output the updated JSON
with open("updated_graph_data.json", "w") as file:
json.dump(data, file, indent=4) |