-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_network.py
More file actions
61 lines (48 loc) · 1.93 KB
/
build_network.py
File metadata and controls
61 lines (48 loc) · 1.93 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
""" build_network.py
Input: File containing a list of hashtags
Expects: (existence of) files graphs/<hashtag>/nodes{1,2,3}.csv
Output: Files graphs/<hashtag>/edges3.csv containing edge lists for each network
"""
import sys, os, json
from collections import defaultdict
NUM_LINES = 284884514
CACHE_LIMIT = 100000
NUM_WINDOWS = 10
# Node and edge sets
nodes_for_hashtag = defaultdict(set)
edges_for_hashtag = defaultdict(set)
def flush_cache():
print >> sys.stderr, "Flushing edges"
for hashtag in hashtags:
with open("graphs/%s/all_edges.csv" % hashtag, 'a') as e:
e.write("\n".join(edges_for_hashtag[hashtag]))
edges_for_hashtag.clear()
def get_cache_size():
return sum([len(v) for _,v in edges_for_hashtag.iteritems()])
# Read hashtags
with open(sys.argv[1], 'r') as f:
hashtags = map(lambda x: x[:-1], f.readlines())
# Build node sets for each hashtag
for hashtag in hashtags:
for i in xrange(NUM_WINDOWS):
with open("graphs/%s/nodes%d.csv" % (hashtag, i+1), 'r') as f:
nodes_for_hashtag[hashtag].update(set(map(lambda x: x.strip(), f.readlines())))
# Clear previous contents of edge sets
with open("graphs/%s/all_edges.csv" % hashtag, 'w') as f:
pass
# Stream through entire Twitter network and pull out edges relevant to input hashtags
count = 0
with open("big_data/network.txt", 'r') as f:
while (count < NUM_LINES):
line = f.readline().strip()
(node, _ , follower) = line.partition("\t")
# If edge is in a hashtag's network, add it to the edge set
for hashtag, nodes in nodes_for_hashtag.iteritems():
if (node in nodes and follower in nodes):
edges_for_hashtag[hashtag].add("%s\t%s" % (follower, node))
count += 1
if ((count % 1000000) == 0):
print >> sys.stderr, "%d lines read." % count
if (get_cache_size() > CACHE_LIMIT):
flush_cache()
flush_cache()