-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgraph.py
36 lines (26 loc) · 829 Bytes
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pickle, networkx as nx
from copy import copy
from collections import defaultdict
with open("l2.pickle") as f:
lyrics = pickle.load(f)
def divide_lyrics(lyric):
for t in lyric["tags"]:
l = copy(lyric)
l["tags"] = t
yield l
lyrics = (l for l in lyrics if l.get("title"))
lyrics = (dl for l in lyrics for dl in divide_lyrics(l))
g = nx.DiGraph()
counts = defaultdict(lambda : defaultdict(float))
for l in lyrics:
if l.get("genre"):
for genre in l["genre"]:
counts[genre][l["tags"]] += 1
for genre in counts:
print genre
for tag in counts[genre]:
g.add_node(genre, t = "genre")
g.add_node(tag, t = "tag")
g.add_edge(genre, tag, weight = counts[genre][tag])
with open("bob.gexf", "w") as f:
nx.write_gexf(g, f)