view fca/featureCompareMatrices.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')

tools = []

res = graph.query(
    """SELECT DISTINCT ?tool
       WHERE {
	   	?x local:computedIn ?tool .
		?x local:feature ?feature 
       }
	   ORDER BY ?tool""",
    initNs=dict(
        local=Namespace("http://sovarr.c4dm.eecs.qmul.ac.uk/features/"))
)

for it in res:
    tools.append(it[0])

tools.sort()

features = []

for s, p, o in graph.triples((None, None, OWL.Class)):
    features.append(s.split('/')[-1])
    
features.sort()

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

similarity = {}

for feature in features:
    mtrx = []
    for i in range(len(tools)):
        row = []
        for j in range(len(tools)):
            row.append(0)
        mtrx.append(row)
    similarity[feature] = mtrx
    for s, p, o in graph.triples((URIRef(local+feature), URIRef(local+'computedIn'), None)):
        if tools.count(o) != 0:
            similarity[feature][tools.index(o)][tools.index(o)] = 1


html = ''
for feature in features:
    count = 0
    for i in range(len(similarity[feature])):
        count += similarity[feature][i].count(1)
    if count > 1:
        html += '<div>'
        html += feature + '<br>'
        html += writeHTML('', similarity[feature], tools, tools)
        html += '</div>'

path = 'similarityMatrices.html'    
file = open(path, 'w')
file.write(html)
file.close()    


wiki = ''
for feature in features:
    count = 0
    for i in range(len(similarity[feature])):
        count += similarity[feature][i].count(1)
    if count > 1:
        wiki += '<div>\n'
        wiki += '====' + feature + '====\n'
        wiki += writeWikiTable('', similarity[feature], tools, tools)
        wiki += '</div>\n'

path = 'similarityMatricesWiki.txt'    
file = open(path, 'w')
file.write(wiki)
file.close()    

latex = ''
for feature in features:
    count = 0
    for i in range(len(similarity[feature])):
        count += similarity[feature][i].count(1)
    if count > 1:
        latex += '\n'
        latex += '{\large \\bfseries ' + feature + '}\n'
        latex += writeLatexTable('', similarity[feature], tools, tools)
        latex += '\n'

path = 'similarityMatricesLatex.txt'    
file = open(path, 'w')
file.write(latex)
file.close()