annotate fca/featureToolMatrix.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
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 res = graph.query(
nothing@0 21 """SELECT DISTINCT ?tool
nothing@0 22 WHERE {
nothing@0 23 ?x local:computedIn ?tool .
nothing@0 24 ?x local:feature ?feature
nothing@0 25 }
nothing@0 26 ORDER BY ?tool""",
nothing@0 27 initNs=dict(
nothing@0 28 local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/"))
nothing@0 29 )
nothing@0 30
nothing@0 31 cols = []
nothing@0 32
nothing@0 33 for it in res:
nothing@0 34 cols.append(it[0])
nothing@0 35
nothing@0 36 res = graph.query(
nothing@0 37 """SELECT DISTINCT ?feature
nothing@0 38 WHERE {
nothing@0 39 ?x local:computedIn ?tool .
nothing@0 40 ?x local:feature ?feature
nothing@0 41 }
nothing@0 42 ORDER BY ?feature""",
nothing@0 43 initNs=dict(
nothing@0 44 local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/"))
nothing@0 45 )
nothing@0 46
nothing@0 47 rows = []
nothing@0 48
nothing@0 49 for it in res:
nothing@0 50 rows.append(it[0])
nothing@0 51
nothing@0 52 colmap = {}
nothing@0 53
nothing@0 54 for i in range(len(cols)):
nothing@0 55 it = cols[i]
nothing@0 56 colmap[it] = i
nothing@0 57
nothing@0 58 rowmap = {}
nothing@0 59
nothing@0 60 for i in range(len(rows)):
nothing@0 61 it = rows[i]
nothing@0 62 rowmap[it] = i
nothing@0 63
nothing@0 64 res = graph.query(
nothing@0 65 """SELECT DISTINCT ?feature ?tool
nothing@0 66 WHERE {
nothing@0 67 ?x local:computedIn ?tool .
nothing@0 68 ?x local:feature ?feature
nothing@0 69 }
nothing@0 70 ORDER BY ?feature""",
nothing@0 71 initNs=dict(
nothing@0 72 local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/")))
nothing@0 73
nothing@0 74 afmatrix = []
nothing@0 75
nothing@0 76 for i in range(len(rows)):
nothing@0 77 row = []
nothing@0 78 for j in range(len(cols)):
nothing@0 79 row.append(0)
nothing@0 80 afmatrix.append(row)
nothing@0 81
nothing@0 82 for it in res:
nothing@0 83 afmatrix[rowmap[it[0]]][colmap[it[1]]] = 1
nothing@0 84
nothing@0 85
nothing@0 86 #writeHTML('afmatrix.html', afmatrix, cols, rows )
nothing@0 87 #writeWikiTable('featureToolWiki.txt', afmatrix, cols, rows )
nothing@0 88 writeLatexTable('featureToolLatex.txt', afmatrix, cols, rows)
nothing@0 89