Created
October 22, 2010 00:38
-
-
Save jazzido/639693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# no me digan nada, es horrible. | |
import csv | |
import json | |
rows = list(csv.reader(open('votos2010.csv').readlines(), delimiter=";"))[1:] | |
diputados = map(lambda x: x[0], rows) | |
partidos = list(set(map(lambda x: x[1], rows))) | |
def comp(vd1, vd2): | |
""" Compara similaridad entre dos diputados segun sus votaciones. | |
Son mas similares por haber estado ambos ausentes en la votacion? (eg, no dieron quorum?) """ | |
q = 0 | |
for i in range(len(vd1)): | |
if vd1[i] == vd2[i]: q += 1 | |
return q | |
arcs = dict() | |
nodes = list() | |
for i in range(len(rows)): | |
d1 = rows[i] | |
for j in range(i+1, len(rows)): | |
d2 = rows[j] | |
q = comp(d1[4:], d2[4:]) | |
if q > 0: arcs[(i,j,)] = q | |
nodes.append({'nodeName': d1[0], 'group': partidos.index(d1[1])}) | |
rv = { 'nodes': nodes, | |
'links': [{'source': k[0], 'target': k[1], 'value': v} for k,v in arcs.items()] | |
} | |
print json.dumps(rv, indent=2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment