-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_ontology.py
141 lines (120 loc) · 4.01 KB
/
csv_ontology.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pandas as pd
from io import StringIO
from owlready2 import *
import types
import yaml
import d3fdgraph
import pandas as pd
from rdflib import RDFS, OWL
from owlready2 import default_world
def ontology_from_yaml(yml, uri):
ont = get_ontology(uri)
with open(yml) as f:
yaml_ontology = yaml.safe_load(f)
if 'Classes' in yaml_ontology.keys():
create_classes(ont, yaml_ontology['Classes'])
if 'Individuals' in yaml_ontology.keys():
create_individuals(ont, yaml_ontology['Individuals'])
if 'ObjectProperties' in yaml_ontology.keys():
create_properties(ont, yaml_ontology['ObjectProperties'])
if 'Associations' in yaml_ontology.keys():
associations(ont, yaml_ontology['Associations'])
return ont
def create_classes(ont, class_list, super_class=None):
# Create some subclasses as a tree
if super_class is None:
super_class = Thing
with ont:
for clz in class_list:
if isinstance(clz, dict):
for scn, cl in clz.items():
NewClass = types.new_class(scn, (super_class,))
NewClass.label.en = scn
create_classes(ont, cl, NewClass)
else:
NewClass = types.new_class(clz, (super_class,))
NewClass.label.en = clz
def create_properties(ont, prop_list, super_prop=None):
if super_prop is None:
super_prop = ObjectProperty
with ont:
for prop in prop_list:
if isinstance(prop, dict):
for spn, pl in prop.items():
NewProp = types.new_class(spn, (super_prop,))
NewProp.label.en = spn
NewProp.domain = []
NewProp.range = []
create_properties(ont, pl, NewProp)
else:
NewProp = types.new_class(prop, (super_prop,))
NewProp.label.en = prop
NewProp.domain = []
NewProp.range = []
def associations(ont, dr_list):
with ont:
for prop,values in dr_list.items():
if 'domain' in values:
ont[prop].domain.append(ont[values['domain']])
if 'range' in values:
ont[prop].range.append(ont[values['range']])
def create_individuals(ont, individual_dict):
for super_class, individual_list in individual_dict.items():
for individual in individual_list:
i = ont[super_class](individual.lower())
i.label.en = individual.lower()
def class_tree_df():
graph = default_world.as_rdflib_graph()
subclass_df = pd.DataFrame(
graph.query('''
SELECT ?c ?s WHERE {
{
?c rdfs:subClassOf ?s .
FILTER NOT EXISTS { ?c rdfs:label ?cl }
FILTER NOT EXISTS { ?s rdfs:label ?sl }
} UNION {
?c rdfs:subClassOf ?s_ .
FILTER NOT EXISTS { ?c rdfs:label ?cl }
?s_ rdfs:label ?s .
} UNION {
?c_ rdfs:subClassOf ?s .
FILTER NOT EXISTS { ?s rdfs:label ?sl }
?c_ rdfs:label ?c .
} UNION {
?c_ rdfs:subClassOf ?s_ .
?c_ rdfs:label ?c .
?s_ rdfs:label ?s .
}
}
''',
initNs={ 'rdfs': RDFS }
),
columns=['source', 'target']
)
subclass_df['weight'] = 1
return subclass_df
def association_df(graph):
df = pd.DataFrame(
graph.query('''
SELECT DISTINCT ?s ?p ?o ?sl ?pl ?ol WHERE {
?p rdfs:domain ?stemp ;
rdfs:range ?otemp .
?stemp rdfs:subClassOf* ?s .
?otemp rdfs:subClassOf* ?o .
OPTIONAL {
?s rdfs:label ?sl .
} OPTIONAL {
?p rdfs:label ?pl .
} OPTIONAL {
?o rdfs:label ?ol .
}
}
''',
initNs={
'rdfs': RDFS,
'owl': OWL
}
),
columns=['source', 'property', 'target', 'source_label', 'property_label', 'target_label']
)
return df