annotate fca/formalConceptAnalysis.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 # conceptual scaling of many-valued feature attributes: complexity - low, medium, high
nothing@0 21 # must be transformed into one-valued context: complexity-low, complexity-medium, complexity-high
nothing@0 22 atmatrix = []
nothing@0 23
nothing@0 24 #which attributes/categories to include
nothing@0 25 cat = ["appdomain", "complexity", "computation", "dimensions", "domain", "level", "temporalscale", "computedIn", "tag"]
nothing@0 26 catdict = {}
nothing@0 27
nothing@0 28 for name in cat:
nothing@0 29 catdict[name] = []
nothing@0 30
nothing@0 31 atcols = []
nothing@0 32 atrows = []
nothing@0 33
nothing@0 34 #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 35 for s, p, o in graph.triples((None, None, OWL.Class)):
nothing@0 36 row = s.split('/')[-1]
nothing@0 37 if atrows.count(row) == 0:
nothing@0 38 atrows.append(row)
nothing@0 39 for su, pr, ob in graph.triples((s, None, None)):
nothing@0 40 col = pr.split('/')[-1]
nothing@0 41 if cat.count(col) != 0:
nothing@0 42 if catdict[col].count(ob) == 0:
nothing@0 43 atcols.append(col+"-"+ob)
nothing@0 44 catdict[col].append(ob)
nothing@0 45
nothing@0 46
nothing@0 47 atrows.sort()
nothing@0 48 atcols.sort()
nothing@0 49
nothing@0 50 #construct the matrix
nothing@0 51 for i in range(len(atrows)):
nothing@0 52 atrow = []
nothing@0 53 for j in range(len(atcols)):
nothing@0 54 atrow.append(0)
nothing@0 55 atmatrix.append(atrow)
nothing@0 56
nothing@0 57
nothing@0 58 for ns, value in graph.namespaces():
nothing@0 59 if ns == 'local':
nothing@0 60 local = value
nothing@0 61
nothing@0 62 index = 0
nothing@0 63 for feature in atrows:
nothing@0 64 for s, p, o in graph.triples((URIRef(local+feature), None, None)):
nothing@0 65 col = p.split('/')[-1]
nothing@0 66 if cat.count(col) != 0:
nothing@0 67 atmatrix[index][atcols.index(col+"-"+o)] = 1
nothing@0 68 index += 1
nothing@0 69
nothing@0 70 #writeHTML('atmatrix.html', atmatrix, atcols, atrows)
nothing@0 71 #writeWikiTable('fcamatrixWiki.txt', atmatrix, atcols, atrows)
nothing@0 72
nothing@0 73 #writeCXT('atmatrix.cxt', atmatrix, atcols, atrows)
nothing@0 74 writeFIMI('atmatrix.fimi', atmatrix, atcols, atrows)
nothing@0 75
nothing@0 76