view fca/fca.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
line wrap: on
line source

#formal concept analysis

import rdflib
from rdflib import plugin, OWL, URIRef
from rdflib.graph import Graph
from rdflib.namespace import Namespace

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')

graph = Graph()
graph.parse('/Users/alo/MusicOntology/features/featuresCatalogue.rdf')


execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py')

# conceptual scaling of many-valued feature attributes: complexity - low, medium, high 
# must be transformed into one-valued context: complexity-low, complexity-medium, complexity-high
atmatrix = []

#which attributes/categories to include
cat = ["appdomain", "complexity", "domain", "level", "temporalscale", "dimensions"]
catdict = {}

for name in cat:
    catdict[name] = []

atcols = []
atrows = []

#traverse all items of type OWL.Class and then for each class the attributes in the dictionary and enumerate all the possible values

res = graph.query(
    """
        SELECT DISTINCT ?feature (COUNT(?tool) as ?tcount)
        	WHERE { 
        	 ?feature rdfs:subClassOf ?ob . 
        	 ?feature af:computedIn ?tool
        	}
        GROUP BY ?feature
        HAVING(COUNT(?tool)>1)
	   ORDER BY ?feature
    """,
    initNs=dict(
        af=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/"))
)
    
for ns, value in graph.namespaces():
    if ns == 'local':
        local = value


for it in res:
    atrows.append(it[0])
    for su, pr, ob in graph.triples((URIRef(local+it[0]), None, None)):
        col = pr.split('/')[-1]
        if cat.count(col) != 0:
            if catdict[col].count(ob) == 0:
                atcols.append(col+"-"+ob)
                catdict[col].append(ob)

                

atrows.sort()
atcols.sort()

#construct the matrix    
for i in range(len(atrows)):
    atrow = []
    for j in range(len(atcols)):
        atrow.append(0)
    atmatrix.append(atrow)


for ns, value in graph.namespaces():
    if ns == 'local':
        local = value

index = 0
for feature in atrows:
    for s, p, o in graph.triples((URIRef(local+feature), None, None)):
        col = p.split('/')[-1]
        if cat.count(col) != 0:
            atmatrix[index][atcols.index(col+"-"+o)] = 1
    index += 1

writeHTML('subset.html', atmatrix, atcols, atrows)
writeWikiTable('subset.txt', atmatrix, atcols, atrows)

writeCXT('subset.cxt', atmatrix, atcols, atrows)
#writeFIMI('atmatrix.fimi', atmatrix, atcols, atrows)
os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")