import re def generatePythonCodeFromStatements(textfile, outfile): outfile.write('from rdflib import Namespace, RDF, RDFS, ConjunctiveGraph\n\n') outfile.write('def statements() :\n\n') outfile.write('\tSKOS = Namespace(\'http://www.w3.org/2008/04/skos/core#\')\n') outfile.write('\tOWL = Namespace(\'http://www.w3.org/2002/07/owl#\')\n\n') outfile.write('\tstatements = []\n\n') statementRe = re.compile('#\\s(\\S+)') typeRe = re.compile('# S\\d+ skos:(\\w+) is an instance of owl:(\w+)') typeTwoRe = re.compile('# S\\d+ skos:(\\w+) and skos:(\\w+) are each instances of owl:(\\w+)') typeMultiRe = re.compile('# S\\d+ ((?:skos:\\w+, )+)skos:(\\w+) and skos:(\\w+) are each instances of owl:(\\w+)') domainRangeRe = re.compile('# S\\d+ The rdfs:(range|domain) of skos:(\\w+) is the class (\\w+):(\\w+)') disjointClassRe = re.compile('# S\\d+ skos:(\\w+) is disjoint with skos:(\\w+)') subPropertiesRe = re.compile('# S\\d+ ((?:skos:\\w+, )+)skos:(\\w+) and skos:(\\w+) are each sub-properties of skos:(\\w+)') subPropertyRe = re.compile('skos:(\\w+) is a sub-property of skos:(\\w+)') inverseRe = re.compile('skos:(\\w+) is the owl:inverseOf the property skos:(\\w+)') disjointClassesRe = re.compile('# S\\d+ skos:(\\w+) is disjoint with each of ((?:skos:\\w+, )*)skos:(\\w+) and skos:(\\w+)') subClassRe = re.compile('skos:(\\w+) is a sub-class of skos:(\\w+)') for line in textfile : # output line as comment outfile.write('\t'+line) # output statement array initialisation sid = statementRe.search(line).group(1) outfile.write('\t'+sid+' = []\n') # output triples appended typeReMatch = typeRe.search(line) typeTwoReMatch = typeTwoRe.search(line) typeMultiReMatch = typeMultiRe.search(line) domainRangeReMatch = domainRangeRe.search(line) disjointClassReMatch = disjointClassRe.search(line) subPropertiesReMatch = subPropertiesRe.search(line) subPropertyReMatchGroups = subPropertyRe.findall(line) inverseReMatchGroups = inverseRe.findall(line) disjointClassesReMatch = disjointClassesRe.search(line) subClassReMatchGroups = subClassRe.findall(line) if typeReMatch != None : outfile.write('\t'+sid+'.append((SKOS[\''+typeReMatch.group(1)+'\'], RDF.type, OWL[\''+typeReMatch.group(2)+'\']))\n') elif typeTwoReMatch != None : # outfile.write('\t# groups: '+str(typeTwoReMatch.groups())+'\n') outfile.write('\t'+sid+'.append((SKOS[\''+typeTwoReMatch.group(1)+'\'], RDF.type, OWL[\''+typeTwoReMatch.group(3)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+typeTwoReMatch.group(2)+'\'], RDF.type, OWL[\''+typeTwoReMatch.group(3)+'\']))\n') elif typeMultiReMatch != None : # outfile.write('\t# groups: '+str(typeMultiReMatch.groups())+'\n') # comma values for ln in typeMultiReMatch.group(1).split(',') : ln = ln.replace(' ','') if len(ln) > 0 : outfile.write('\t'+sid+'.append((SKOS[\''+ln.partition(':')[2]+'\'], RDF.type, OWL[\''+typeMultiReMatch.group(4)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+typeMultiReMatch.group(2)+'\'], RDF.type, OWL[\''+typeMultiReMatch.group(4)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+typeMultiReMatch.group(3)+'\'], RDF.type, OWL[\''+typeMultiReMatch.group(4)+'\']))\n') elif domainRangeReMatch != None : if domainRangeReMatch.group(3) == 'skos' : v = 'SKOS[\''+domainRangeReMatch.group(4)+'\']' elif domainRangeReMatch.group(3) == 'rdf' : v = 'RDF.'+domainRangeReMatch.group(4) elif domainRangeReMatch.group(3) == 'rdfs' : v = 'RDFS.'+domainRangeReMatch.group(4) elif domainRangeReMatch.group(3) == 'owl' : v = 'OWL.'+domainRangeReMatch.group(4) else : v = 'FIXME' outfile.write('\t'+sid+'.append((SKOS[\''+domainRangeReMatch.group(2)+'\'], RDFS.'+domainRangeReMatch.group(1)+', '+v+'))\n') elif disjointClassReMatch != None : outfile.write('\t'+sid+'.append((SKOS[\''+disjointClassReMatch.group(1)+'\'], OWL[\'disjointWith\'], SKOS[\''+disjointClassReMatch.group(2)+'\']))\n') elif subPropertiesReMatch != None : # outfile.write('\t# groups: '+str(subPropertiesReMatch.groups())+'\n') # comma values for ln in subPropertiesReMatch.group(1).split(',') : ln = ln.replace(' ','') if len(ln) > 0 : outfile.write('\t'+sid+'.append((SKOS[\''+ln.partition(':')[2]+'\'], RDFS.subPropertyOf, SKOS[\''+subPropertiesReMatch.group(4)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+subPropertiesReMatch.group(2)+'\'], RDFS.subPropertyOf, SKOS[\''+subPropertiesReMatch.group(4)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+subPropertiesReMatch.group(3)+'\'], RDFS.subPropertyOf, SKOS[\''+subPropertiesReMatch.group(4)+'\']))\n') elif len(subPropertyReMatchGroups)>0 : for group in subPropertyReMatchGroups : outfile.write('\t'+sid+'.append((SKOS[\''+group[0]+'\'], RDFS.subPropertyOf, SKOS[\''+group[1]+'\']))\n') elif len(inverseReMatchGroups)>0 : for group in inverseReMatchGroups : outfile.write('\t'+sid+'.append((SKOS[\''+group[0]+'\'], OWL[\'inverseOf\'], SKOS[\''+group[1]+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+group[1]+'\'], OWL[\'inverseOf\'], SKOS[\''+group[0]+'\']))\n') elif disjointClassesReMatch != None : # outfile.write('\t# groups: '+str(disjointClassesReMatch.groups())+'\n') # comma values for ln in disjointClassesReMatch.group(2).split(',') : ln = ln.replace(' ','') if len(ln) > 0 : outfile.write('\t'+sid+'.append((SKOS[\''+disjointClassesReMatch.group(1)+'\'], OWL[\'disjointWith\'], SKOS[\''+ln.partition(':')[2]+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+disjointClassesReMatch.group(1)+'\'], OWL[\'disjointWith\'], SKOS[\''+disjointClassesReMatch.group(3)+'\']))\n') outfile.write('\t'+sid+'.append((SKOS[\''+disjointClassesReMatch.group(1)+'\'], OWL[\'disjointWith\'], SKOS[\''+disjointClassesReMatch.group(4)+'\']))\n') elif len(subClassReMatchGroups)>0 : for group in subClassReMatchGroups : outfile.write('\t'+sid+'.append((SKOS[\''+group[0]+'\'], RDFS.subClassOf, SKOS[\''+group[1]+'\']))\n') else : outfile.write('\t# no triples to add\n') # output statements appended outfile.write('\tstatements.append('+sid+')\n\n') outfile.write('\treturn statements\n\n') outfile.write('def schema() :\n\n') outfile.write('\tschema = ConjunctiveGraph()\n') outfile.write('\tfor statement in statements() :\n') outfile.write('\t for triple in statement :\n') outfile.write('\t schema.add(triple)\n\n') outfile.write('\treturn schema\n') outfile.write('# all done') textfile = open('semantics.txt') outfile = file('semantics.py', 'w') generatePythonCodeFromStatements(textfile, outfile) textfile.close() outfile.close()