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: # conceptual scaling of many-valued feature attributes: complexity - low, medium, high nothing@0: # must be transformed into one-valued context: complexity-low, complexity-medium, complexity-high nothing@0: atmatrix = [] nothing@0: nothing@0: #which attributes/categories to include nothing@0: cat = ["appdomain", "complexity", "computation", "dimensions", "domain", "level", "temporalscale", "computedIn", "tag"] nothing@0: catdict = {} nothing@0: nothing@0: for name in cat: nothing@0: catdict[name] = [] nothing@0: nothing@0: atcols = [] nothing@0: atrows = [] nothing@0: nothing@0: #traverse all items of type OWL.Class and then for each class the attributes in the dictionary and enumerate all the possible values nothing@0: for s, p, o in graph.triples((None, None, OWL.Class)): nothing@0: row = s.split('/')[-1] nothing@0: if atrows.count(row) == 0: nothing@0: atrows.append(row) nothing@0: for su, pr, ob in graph.triples((s, None, None)): nothing@0: col = pr.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: if catdict[col].count(ob) == 0: nothing@0: atcols.append(col+"-"+ob) nothing@0: catdict[col].append(ob) nothing@0: nothing@0: nothing@0: atrows.sort() nothing@0: atcols.sort() nothing@0: nothing@0: #construct the matrix nothing@0: for i in range(len(atrows)): nothing@0: atrow = [] nothing@0: for j in range(len(atcols)): nothing@0: atrow.append(0) nothing@0: atmatrix.append(atrow) nothing@0: nothing@0: nothing@0: for ns, value in graph.namespaces(): nothing@0: if ns == 'local': nothing@0: local = value nothing@0: nothing@0: index = 0 nothing@0: for feature in atrows: nothing@0: for s, p, o in graph.triples((URIRef(local+feature), None, None)): nothing@0: col = p.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: atmatrix[index][atcols.index(col+"-"+o)] = 1 nothing@0: index += 1 nothing@0: nothing@0: #writeHTML('atmatrix.html', atmatrix, atcols, atrows) nothing@0: #writeWikiTable('fcamatrixWiki.txt', atmatrix, atcols, atrows) nothing@0: nothing@0: #writeCXT('atmatrix.cxt', atmatrix, atcols, atrows) nothing@0: writeFIMI('atmatrix.fimi', atmatrix, atcols, atrows) nothing@0: nothing@0: