#!/usr/bin/env python import json import collections examples = [ { "text": "icecream is cold", "parsed": ("exists-property-with-value", 'icecream', 'cold'), }, { "text": "earth is a planet", "parsed": ("pertenence-to-group", 'earth', 'planet'), }, { "text": "Green is a color", "parsed": ("pertenence-to-group", 'green', 'color'), }, { "text": "airplanes do fly", "parsed": ("has-capacity", 'airplane', 'fly') } ] knowledge_base = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.defaultdict())) def property_for_value(value): if "cold": return "temperature" def exists_property_with_value(subj, value): knowledge_base[subj][property_for_value(value)] = value def pertenence_to_group(subj, group): knowledge_base[subj]["group"] = group def has_capacity(subj, capacity): if "capacities" not in knowledge_base[subj]: knowledge_base[subj]["capacities"] = [] knowledge_base[subj]["capacities"].append(capacity) knowledge_ingestion = { "exists-property-with-value": exists_property_with_value, "pertenence-to-group": pertenence_to_group, "has-capacity": has_capacity, } def ingest(example): method = example['parsed'][0] args = example['parsed'][1:] knowledge_ingestion[method](*args) for example in examples: ingest(example) print(json.dumps(knowledge_base, indent=4, sort_keys=True))