view fca/formalConceptAnalysis.py @ 0:62d2c72e4223

initial commit
author nothing@tehis.net
date Mon, 25 Feb 2013 14:40:54 +0000
parents
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

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

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')

# 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", "computation", "dimensions", "domain", "level", "temporalscale", "computedIn", "tag"]
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
for s, p, o in graph.triples((None, None, OWL.Class)):
    row = s.split('/')[-1]
    if atrows.count(row) == 0:
        atrows.append(row)
    for su, pr, ob in graph.triples((s, 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('atmatrix.html', atmatrix, atcols, atrows)
#writeWikiTable('fcamatrixWiki.txt', atmatrix, atcols, atrows)

#writeCXT('atmatrix.cxt', atmatrix, atcols, atrows)
writeFIMI('atmatrix.fimi', atmatrix, atcols, atrows)