forked from daltondo/Sumerian-network-graph-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_merger.py
More file actions
72 lines (55 loc) · 1.71 KB
/
node_merger.py
File metadata and controls
72 lines (55 loc) · 1.71 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
62
63
64
65
66
67
68
69
70
71
72
import csv
import node_creator
import data_handler
import edge_creator
from collections import defaultdict
# key: p_index, value: list of (name of person, role in this p_index)
p_indexes_to_people = defaultdict(list)
# key: (person, profession), value: Node
new_person_to_info = {}
# traverses through all the newly created persons, in each person has more than 1 node, merge those nodes
def merge_nodes():
number_of_nodes_merged = 0
for key, value in node_creator.person_to_info.items():
if len(value) > 1:
merge_nodes_helper(key, value)
number_of_nodes_merged += 1
return number_of_nodes_merged
# the ith role, profession, p_id all correspond with each other
def merge_nodes_helper(person, list_of_nodes):
roles = []
p_indexes = []
years = []
for node in list_of_nodes:
roles.append(node.role)
p_indexes.append(node.p_index)
years.append(node.year)
node = node_creator.Node(person[0], roles, person[1], p_indexes, years)
new_person_to_info[person] = node
### OPEN new_nodes.csv TO WRITE ###
# writes the new persons (merged nodes) into a nodes list
def create_new_nodes_list():
with open('new_nodes.csv', 'w') as csvfile:
fieldnames = ['id', 'name', 'role', 'profession', 'p_index', 'year']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
curr_id = 1
for person, node in new_person_to_info.items():
name = person[0]
if type(node) is list:
node = node[0]
role = node.role
profession = node.profession
p_index = node.p_index
year = node.year
node.add_id(curr_id)
writer.writerow({
'id': curr_id,
'name': name,
'role': role,
'profession': profession,
'p_index': p_index,
'year': year
})
curr_id += 1
csvfile.close()