Skip to content

Instantly share code, notes, and snippets.

@alanthonyc
Created February 9, 2012 18:03
Show Gist options
  • Save alanthonyc/1781638 to your computer and use it in GitHub Desktop.
Save alanthonyc/1781638 to your computer and use it in GitHub Desktop.
import simplejson
import networkx as nx
#http://mlg.ucd.ie/files/summer/tutorial.pdf
from networkx.algorithms import bipartite
g = nx.Graph()
#need to bring in reutersJournalistList
users=simplejson.loads(reutersJournalistList)
#I had some 'issues' with the parsing for some reason? Required this hack in the end...
for user in users:
for x in user:
if x=='users':
u=user[x][0]['twitter_screen_name']
print 'user:',user[x][0]['twitter_screen_name']
for topic in user[x][0]['topics']:
print '- topic:',topic
#Add edges from journalist name to each tag they are associated with
g.add_edge(u,topic)
#print bipartite.is_bipartite(g)
#print bipartite.sets(g)
#Save a graph file we can visualise in Gephi corresponding to bipartite graph
nx.write_graphml(g, "usertags.graphml")
#We can find the sets of names/tags associated with the disjoint sets in the graph
users,tags=bipartite.sets(g)
#Collapse the bipartite graph to a graph of journalists connected via a common tag
ugraph= bipartite.projected_graph(g, users)
nx.write_graphml(ugraph, "users.graphml")
#Collapse the bipartite graph to a set of tags connected via a common journalist
tgraph= bipartite.projected_graph(g, tags)
nx.write_graphml(tgraph, "tags.graphml")
#Dump a list of the journalists Twitter IDs
f=open("users.txt","w+")
for uo in users: f.write(uo+'\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment