forked from timothyasp/PageRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
56 lines (41 loc) · 1.27 KB
/
utils.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re, sys, math, random, csv, types, networkx as nx
from collections import defaultdict
def parse(filename, isDirected):
reader = csv.reader(open(filename, 'r'), delimiter=',')
data = [row for row in reader]
print "Reading and parsing the data into memory..."
if isDirected:
return parse_directed(data)
else:
return parse_undirected(data)
def parse_undirected(data):
G = nx.Graph()
nodes = set([row[0] for row in data])
edges = [(row[0], row[2]) for row in data]
num_nodes = len(nodes)
rank = 1/float(num_nodes)
G.add_nodes_from(nodes, rank=rank)
G.add_edges_from(edges)
return G
def parse_directed(data):
DG = nx.DiGraph()
for i, row in enumerate(data):
node_a = format_key(row[0])
node_b = format_key(row[2])
val_a = digits(row[1])
val_b = digits(row[3])
DG.add_edge(node_a, node_b)
if val_a >= val_b:
DG.add_path([node_a, node_b])
else:
DG.add_path([node_b, node_a])
return DG
def digits(val):
return int(re.sub("\D", "", val))
def format_key(key):
key = key.strip()
if key.startswith('"') and key.endswith('"'):
key = key[1:-1]
return key
def print_results(f, method, results):
print method