annotate fca/featureCompareMatrices.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents 62d2c72e4223
children
rev   line source
nothing@0 1 #formal concept analysis
nothing@0 2
nothing@0 3 import rdflib
nothing@0 4 from rdflib import plugin, OWL, URIRef
nothing@0 5 from rdflib.graph import Graph
nothing@0 6 from rdflib.namespace import Namespace
nothing@0 7
nothing@0 8 execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py')
nothing@0 9
nothing@0 10 plugin.register(
nothing@0 11 'sparql', rdflib.query.Processor,
nothing@0 12 'rdfextras.sparql.processor', 'Processor')
nothing@0 13 plugin.register(
nothing@0 14 'sparql', rdflib.query.Result,
nothing@0 15 'rdfextras.sparql.query', 'SPARQLQueryResult')
nothing@0 16
nothing@0 17 graph = Graph()
nothing@0 18 graph.parse('/Users/alo/MusicOntology/features/featuresCatalogue.rdf')
nothing@0 19
nothing@0 20 tools = []
nothing@0 21
nothing@0 22 res = graph.query(
nothing@0 23 """SELECT DISTINCT ?tool
nothing@0 24 WHERE {
nothing@0 25 ?x local:computedIn ?tool .
nothing@0 26 ?x local:feature ?feature
nothing@0 27 }
nothing@0 28 ORDER BY ?tool""",
nothing@0 29 initNs=dict(
nothing@0 30 local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/"))
nothing@0 31 )
nothing@0 32
nothing@0 33 for it in res:
nothing@0 34 tools.append(it[0])
nothing@0 35
nothing@0 36 tools.sort()
nothing@0 37
nothing@0 38 features = []
nothing@0 39
nothing@0 40 for s, p, o in graph.triples((None, None, OWL.Class)):
nothing@0 41 features.append(s.split('/')[-1])
nothing@0 42
nothing@0 43 features.sort()
nothing@0 44
nothing@0 45 for ns, value in graph.namespaces():
nothing@0 46 if ns == 'local':
nothing@0 47 local = value
nothing@0 48
nothing@0 49 similarity = {}
nothing@0 50
nothing@0 51 for feature in features:
nothing@0 52 mtrx = []
nothing@0 53 for i in range(len(tools)):
nothing@0 54 row = []
nothing@0 55 for j in range(len(tools)):
nothing@0 56 row.append(0)
nothing@0 57 mtrx.append(row)
nothing@0 58 similarity[feature] = mtrx
nothing@0 59 for s, p, o in graph.triples((URIRef(local+feature), URIRef(local+'computedIn'), None)):
nothing@0 60 if tools.count(o) != 0:
nothing@0 61 similarity[feature][tools.index(o)][tools.index(o)] = 1
nothing@0 62
nothing@0 63
nothing@0 64 html = ''
nothing@0 65 for feature in features:
nothing@0 66 count = 0
nothing@0 67 for i in range(len(similarity[feature])):
nothing@0 68 count += similarity[feature][i].count(1)
nothing@0 69 if count > 1:
nothing@0 70 html += '<div>'
nothing@0 71 html += feature + '<br>'
nothing@0 72 html += writeHTML('', similarity[feature], tools, tools)
nothing@0 73 html += '</div>'
nothing@0 74
nothing@0 75 path = 'similarityMatrices.html'
nothing@0 76 file = open(path, 'w')
nothing@0 77 file.write(html)
nothing@0 78 file.close()
nothing@0 79
nothing@0 80
nothing@0 81 wiki = ''
nothing@0 82 for feature in features:
nothing@0 83 count = 0
nothing@0 84 for i in range(len(similarity[feature])):
nothing@0 85 count += similarity[feature][i].count(1)
nothing@0 86 if count > 1:
nothing@0 87 wiki += '<div>\n'
nothing@0 88 wiki += '====' + feature + '====\n'
nothing@0 89 wiki += writeWikiTable('', similarity[feature], tools, tools)
nothing@0 90 wiki += '</div>\n'
nothing@0 91
nothing@0 92 path = 'similarityMatricesWiki.txt'
nothing@0 93 file = open(path, 'w')
nothing@0 94 file.write(wiki)
nothing@0 95 file.close()
nothing@0 96
nothing@0 97 latex = ''
nothing@0 98 for feature in features:
nothing@0 99 count = 0
nothing@0 100 for i in range(len(similarity[feature])):
nothing@0 101 count += similarity[feature][i].count(1)
nothing@0 102 if count > 1:
nothing@0 103 latex += '\n'
nothing@0 104 latex += '{\large \\bfseries ' + feature + '}\n'
nothing@0 105 latex += writeLatexTable('', similarity[feature], tools, tools)
nothing@0 106 latex += '\n'
nothing@0 107
nothing@0 108 path = 'similarityMatricesLatex.txt'
nothing@0 109 file = open(path, 'w')
nothing@0 110 file.write(latex)
nothing@0 111 file.close()