nothing@0: #formal concept analysis nothing@0: nothing@0: import rdflib nothing@0: from rdflib import plugin, OWL, URIRef nothing@0: from rdflib.graph import Graph nothing@0: from rdflib.namespace import Namespace nothing@0: nothing@0: execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py') nothing@0: nothing@0: plugin.register( nothing@0: 'sparql', rdflib.query.Processor, nothing@0: 'rdfextras.sparql.processor', 'Processor') nothing@0: plugin.register( nothing@0: 'sparql', rdflib.query.Result, nothing@0: 'rdfextras.sparql.query', 'SPARQLQueryResult') nothing@0: nothing@0: graph = Graph() nothing@0: graph.parse('/Users/alo/MusicOntology/features/featuresCatalogue.rdf') nothing@0: nothing@0: tools = [] nothing@0: nothing@0: res = graph.query( nothing@0: """SELECT DISTINCT ?tool nothing@0: WHERE { nothing@0: ?x local:computedIn ?tool . nothing@0: ?x local:feature ?feature nothing@0: } nothing@0: ORDER BY ?tool""", nothing@0: initNs=dict( nothing@0: local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/")) nothing@0: ) nothing@0: nothing@0: for it in res: nothing@0: tools.append(it[0]) nothing@0: nothing@0: tools.sort() nothing@0: nothing@0: features = [] nothing@0: nothing@0: for s, p, o in graph.triples((None, None, OWL.Class)): nothing@0: features.append(s.split('/')[-1]) nothing@0: nothing@0: features.sort() nothing@0: nothing@0: for ns, value in graph.namespaces(): nothing@0: if ns == 'local': nothing@0: local = value nothing@0: nothing@0: similarity = {} nothing@0: nothing@0: for feature in features: nothing@0: mtrx = [] nothing@0: for i in range(len(tools)): nothing@0: row = [] nothing@0: for j in range(len(tools)): nothing@0: row.append(0) nothing@0: mtrx.append(row) nothing@0: similarity[feature] = mtrx nothing@0: for s, p, o in graph.triples((URIRef(local+feature), URIRef(local+'computedIn'), None)): nothing@0: if tools.count(o) != 0: nothing@0: similarity[feature][tools.index(o)][tools.index(o)] = 1 nothing@0: nothing@0: nothing@0: html = '' nothing@0: for feature in features: nothing@0: count = 0 nothing@0: for i in range(len(similarity[feature])): nothing@0: count += similarity[feature][i].count(1) nothing@0: if count > 1: nothing@0: html += '
' nothing@0: html += feature + '
' nothing@0: html += writeHTML('', similarity[feature], tools, tools) nothing@0: html += '
' nothing@0: nothing@0: path = 'similarityMatrices.html' nothing@0: file = open(path, 'w') nothing@0: file.write(html) nothing@0: file.close() nothing@0: nothing@0: nothing@0: wiki = '' nothing@0: for feature in features: nothing@0: count = 0 nothing@0: for i in range(len(similarity[feature])): nothing@0: count += similarity[feature][i].count(1) nothing@0: if count > 1: nothing@0: wiki += '
\n' nothing@0: wiki += '====' + feature + '====\n' nothing@0: wiki += writeWikiTable('', similarity[feature], tools, tools) nothing@0: wiki += '
\n' nothing@0: nothing@0: path = 'similarityMatricesWiki.txt' nothing@0: file = open(path, 'w') nothing@0: file.write(wiki) nothing@0: file.close() nothing@0: nothing@0: latex = '' nothing@0: for feature in features: nothing@0: count = 0 nothing@0: for i in range(len(similarity[feature])): nothing@0: count += similarity[feature][i].count(1) nothing@0: if count > 1: nothing@0: latex += '\n' nothing@0: latex += '{\large \\bfseries ' + feature + '}\n' nothing@0: latex += writeLatexTable('', similarity[feature], tools, tools) nothing@0: latex += '\n' nothing@0: nothing@0: path = 'similarityMatricesLatex.txt' nothing@0: file = open(path, 'w') nothing@0: file.write(latex) nothing@0: file.close()