def add_attributes(graph, attribute_data, entity_counts, attribute_counts, min_occurrences): for image in attribute_data: for entity in image["attributes"]: if "attributes" in entity: attributes = entity["attributes"] else: continue for attribute_name in attributes: if attribute_counts[attribute_name] < min_occurrences: continue if graph.get_attribute_id(attribute_name) == None: # add node to graph attribute_node = Attribute_Node(attribute_name, graph) subject_name = entity["name"] if "name" in entity else entity[ "names"] subject_name = entity_to_aliases(subject_name) if entity_counts[str(subject_name)] < min_occurrences: continue subject_node = graph.get_entity_by_name(subject_name) if subject_node == None: subject_node = Entity_Node(subject_name, graph) attribute_id = graph.get_attribute_id(attribute_name) if subject_node.get_attribute_edge(attribute_id) == None: # creating and adding edge new_edge = Attribute_Edge(subject_node.ID, attribute_id) subject_node.add_attribute_edge(new_edge)
def add_objects(graph, object_data): for image in object_data: for entity in image["objects"]: object_name = entity_to_aliases( entity["name"]) if "name" in entity else entity_to_aliases( entity["names"]) if graph.get_entity_id(object_name) == None: # add node to graph Entity_Node(object_name, graph)
def add_objects(graph, object_data, entity_counts, min_occurrences): for image in object_data: for entity in image["objects"]: object_name = entity_to_aliases( entity["name"]) if "name" in entity else entity_to_aliases( entity["names"]) if entity_counts[str(object_name)] >= min_occurrences: if graph.get_entity_id(object_name) == None: # add node to graph Entity_Node(object_name, graph)
def add_predicates(graph, predicate_data): for image in predicate_data: relationships_info = image["relationships"] for relationship_info in relationships_info: predicate_name = predicate_to_aliases( relationship_info["predicate"]) if graph.get_predicate_id(predicate_name) == None: # add node to graph pred_node = Predicate_Node(predicate_name, graph) subject_name = relationship_info["subject"][ "name"] if "name" in relationship_info[ "subject"] else relationship_info["subject"]["names"][0] object_name = relationship_info["object"][ "name"] if "name" in relationship_info[ "object"] else relationship_info["object"]["names"][0] subject_name = entity_to_aliases(subject_name) object_name = entity_to_aliases(object_name) subject_node = graph.get_entity_by_name(subject_name) if subject_node == None: subject_node = Entity_Node(subject_name, graph) object_id = graph.get_entity_id(object_name) if object_id == None: object_node = Entity_Node(object_name, graph) object_id = object_node.ID predicate_id = graph.get_predicate_id(predicate_name) if subject_node.get_predicate_edge(predicate_id, object_id) == None: # creating and adding edge new_edge = Predicate_Edge(subject_node.ID, predicate_id, object_id) subject_node.add_predicate_edge(new_edge)