nothing@4
|
1 import rdflib
|
nothing@15
|
2 from rdflib import Graph, RDF, RDFS, URIRef, Literal, OWL, Namespace, BNode, XSD
|
nothing@4
|
3
|
nothing@15
|
4 cat = ["complexity", "domain", "level", "temporalscale", "dimensionality", "model"]
|
nothing@4
|
5 val = {
|
nothing@15
|
6 "complexity": ["Low", "Medium", "High"],
|
nothing@4
|
7 "temporalscale": ["Interframe", "Intraframe", "Global"],
|
nothing@15
|
8 "domain": ["Modulation Frequency", "Frequency", "Cepstral", "Eigendomain", "Phase Space", "Temporal", "Correlation"],
|
nothing@4
|
9 "level": ["Perceptual", "Physical"],
|
nothing@4
|
10 "dimensionality": ["One-dimensional", "Multi-dimensional"],
|
nothing@4
|
11 "model": ["Psychoacoustic"]
|
nothing@4
|
12 }
|
nothing@4
|
13
|
nothing@4
|
14 basedir = '/Users/alo/MusicOntology/features/'
|
nothing@15
|
15 readpath = basedir + 'fcadata/baseImplicationsv2.txt'
|
nothing@17
|
16 writepath = basedir + 'fuxi/af-base-rules.n3'
|
nothing@17
|
17 factpath = basedir + 'fuxi/af-base-facts.n3'
|
nothing@17
|
18 ontopath = basedir + 'fuxi/af-base-ontology.rdf'
|
nothing@17
|
19 #ontopath = basedir + 'fuxi/af-base-properties.rdf'
|
nothing@13
|
20 cataloguePath = '/Users/alo/MusicOntology/features/af-catalogue.rdf'
|
nothing@14
|
21 baseuri = 'http://sovarr.c4dm.eecs.qmul.ac.uk/features/af-ontology#'
|
nothing@4
|
22
|
nothing@4
|
23 def readFile(path):
|
nothing@4
|
24 file = open(path)
|
nothing@4
|
25 text = file.read()
|
nothing@4
|
26 file.close()
|
nothing@4
|
27 return text.split("\n")
|
nothing@4
|
28
|
nothing@4
|
29 def writeN3(string, path):
|
nothing@4
|
30 file = open(path, 'w')
|
nothing@4
|
31 file.write(string)
|
nothing@4
|
32 file.close()
|
nothing@4
|
33
|
nothing@4
|
34 def addHeader():
|
nothing@13
|
35 text = "@prefix af: <" + baseuri + "> .\n"
|
nothing@4
|
36 text += "@prefix owl: <http://www.w3.org/2002/07/owl#> .\n\n"
|
nothing@4
|
37 return text
|
nothing@4
|
38
|
nothing@4
|
39 def convertRules(rows):
|
nothing@12
|
40 n3 = addHeader()
|
nothing@14
|
41 #n3 += "?x a af:AudioFeature . \n"
|
nothing@14
|
42
|
nothing@4
|
43 for i in range(len(rows)-1):
|
nothing@14
|
44
|
nothing@4
|
45 row = rows[i]
|
nothing@4
|
46 if row != '':
|
nothing@4
|
47 count = int(row[row.index("<")+1:row.index(">")-1])
|
nothing@4
|
48 premise = row[row.index(">")+2:row.index("==>")-1].split()
|
nothing@4
|
49 conclusion = row[row.index("==>")+4:-1].split()
|
nothing@4
|
50 if count > 0:
|
nothing@15
|
51 var = str(i+1).zfill(2)
|
nothing@15
|
52 n3 += "{ ?x" + var + " a af:AudioFeature . "
|
nothing@4
|
53
|
nothing@4
|
54 for pre in premise:
|
nothing@4
|
55 prop = "af:"+pre.split("-")[-1]
|
nothing@17
|
56 value = "af:"+(" ".join(pre.split("-")[0:-1]).title().replace(" ", "").replace("-", ""))
|
nothing@17
|
57 n3 += ' ?x' + var + ' ' + prop + ' ' + value
|
nothing@4
|
58 n3 += " . "
|
nothing@11
|
59
|
nothing@11
|
60 n3 = n3[:-2] + " } => { "
|
nothing@4
|
61
|
nothing@4
|
62 for con in conclusion:
|
nothing@4
|
63 prop = "af:"+con.split("-")[-1]
|
nothing@17
|
64 value = "af:"+(" ".join(con.split("-")[0:-1]).title().replace(" ", "").replace("-", ""))
|
nothing@17
|
65 n3 += ' ?x' + var + ' ' + prop + ' ' + value
|
nothing@4
|
66 n3 += " . "
|
nothing@4
|
67
|
nothing@11
|
68 n3 = n3[:-2] + " } . \n"
|
nothing@4
|
69
|
nothing@4
|
70 return n3
|
nothing@4
|
71
|
nothing@13
|
72 def writeClasses(path):
|
nothing@4
|
73 graph = Graph()
|
nothing@4
|
74
|
nothing@13
|
75 af = Namespace(baseuri)
|
nothing@4
|
76 graph.bind('af', af)
|
nothing@4
|
77
|
nothing@4
|
78 owl = Namespace('http://www.w3.org/2002/07/owl#')
|
nothing@4
|
79 graph.bind('owl', owl)
|
nothing@4
|
80
|
nothing@4
|
81 afID = af + "AudioFeature"
|
nothing@4
|
82
|
nothing@4
|
83 graph.add((
|
nothing@4
|
84 URIRef(afID),
|
nothing@4
|
85 RDF.type,
|
nothing@4
|
86 OWL.Class
|
nothing@4
|
87 ))
|
nothing@4
|
88
|
nothing@4
|
89 for obj in cat:
|
nothing@4
|
90 objID = af+obj.title()
|
nothing@4
|
91 graph.add((
|
nothing@4
|
92 URIRef(objID),
|
nothing@4
|
93 RDF.type,
|
nothing@4
|
94 OWL.Class
|
nothing@4
|
95 ))
|
nothing@15
|
96 oneof = []
|
nothing@4
|
97 for subcls in val[obj]:
|
nothing@4
|
98 subID = af+subcls.replace(" ", "").replace("-", "")+obj.title()
|
nothing@4
|
99 graph.add((
|
nothing@4
|
100 URIRef(subID),
|
nothing@4
|
101 RDF.type,
|
nothing@4
|
102 OWL.Class
|
nothing@4
|
103 ))
|
nothing@4
|
104 graph.add((
|
nothing@4
|
105 URIRef(subID),
|
nothing@4
|
106 RDFS.subClassOf,
|
nothing@4
|
107 URIRef(objID)
|
nothing@4
|
108 ))
|
nothing@15
|
109 oneof.append(subID)
|
nothing@4
|
110 # add object properties
|
nothing@4
|
111 propID = af+obj
|
nothing@4
|
112 graph.add((
|
nothing@4
|
113 URIRef(propID),
|
nothing@4
|
114 RDF.type,
|
nothing@4
|
115 OWL.ObjectProperty
|
nothing@4
|
116 ))
|
nothing@4
|
117 graph.add((
|
nothing@4
|
118 URIRef(propID),
|
nothing@4
|
119 RDFS.domain,
|
nothing@4
|
120 URIRef(afID)
|
nothing@4
|
121 ))
|
nothing@4
|
122 graph.add((
|
nothing@4
|
123 URIRef(propID),
|
nothing@4
|
124 RDFS.range,
|
nothing@15
|
125 XSD.string
|
nothing@4
|
126 ))
|
nothing@13
|
127
|
nothing@13
|
128 graph.serialize(path)
|
nothing@13
|
129 graph.serialize(path.replace('.rdf', '.n3'), format='n3')
|
nothing@4
|
130
|
nothing@13
|
131 def writeFeatures(path):
|
nothing@8
|
132 source = Graph()
|
nothing@8
|
133 source.parse(cataloguePath)
|
nothing@14
|
134 sourcens = Namespace(baseuri.replace('af-ontology#', ''))
|
nothing@13
|
135
|
nothing@13
|
136 graph = Graph()
|
nothing@13
|
137
|
nothing@13
|
138 af = Namespace(baseuri)
|
nothing@13
|
139 graph.bind('af', af)
|
nothing@13
|
140
|
nothing@13
|
141 owl = Namespace('http://www.w3.org/2002/07/owl#')
|
nothing@13
|
142 graph.bind('owl', owl)
|
nothing@13
|
143
|
nothing@15
|
144 cns = sourcens['computedIn']
|
nothing@15
|
145 tns = sourcens['temporalscale']
|
nothing@8
|
146 for su in source.subjects(RDF.type, OWL.Class):
|
nothing@15
|
147 ccount = sum(1 for _ in source.objects(su,cns))
|
nothing@15
|
148 tcount = sum(1 for _ in source.objects(su,tns))
|
nothing@15
|
149 if ccount > 0 and tcount > 0 and su.find('MPEG7') == -1:
|
nothing@13
|
150 afid = URIRef(af + su.split('/')[-1])
|
nothing@13
|
151 graph.add(( afid, RDF.type, af['AudioFeature'] ))
|
nothing@8
|
152 for pr, ob in source.predicate_objects(su):
|
nothing@8
|
153 if cat.count(pr.split('/')[-1]) == 1:
|
nothing@13
|
154 afpr = URIRef(af + pr.split('/')[-1])
|
nothing@17
|
155 graph.add((afid, afpr,
|
nothing@17
|
156 af[ob.replace("-", "").title().replace(" ", "") + pr.split('/')[-1].title()]
|
nothing@8
|
157 ))
|
nothing@17
|
158 graph.add((afid, af['computation'], af[su.split('/')[-1] + '_computation'] ))
|
nothing@13
|
159
|
nothing@13
|
160 graph.serialize(path, format='n3')
|
nothing@8
|
161
|
nothing@8
|
162
|
nothing@12
|
163
|
nothing@13
|
164 writeClasses(ontopath)
|
nothing@13
|
165 writeFeatures(factpath)
|
nothing@12
|
166 writeN3(convertRules(readFile(readpath)),writepath)
|