-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnbi2graph.py
More file actions
174 lines (142 loc) · 4.92 KB
/
nbi2graph.py
File metadata and controls
174 lines (142 loc) · 4.92 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import csv
import datetime
from nbiHeaderMapper import *
from neo4j_helper import *
from nbi_prop_converter import *
from set_place_name import *
import sys
force_init=False
if len(sys.argv)<=1:
print('please input NBI file path.')
exit()
else:
file_paths=sys.argv[1:]
#file_path='MI15.txt'
#file_path='ALL15.txt'
# def gen_prop(row,prop_name,format=True):
# val=row[prop_name]
# if len(val)>2 and val[0]=='\'' and val[-1]=='\'':
# return val if format else val[1:-1]
# return '\''+val+'\'' if format else val
def gen_prop(row,prop_name,format=True):
val=row[prop_name]
if len(val)>2 and val[0]=='\'' and val[-1]=='\'':
val=val[1:-1]
#replace \' in val with white space
idx=val.find('\'')
if idx!=-1:
val=val[:idx]+' '+val[idx+1:]
if len(val)==0:
return ''
if prop_name in prop_converters:
converter=prop_converters[prop_name]
res=converter(val)
if isinstance(res,(int,float)):
return str(res)
elif isinstance(res,datetime.datetime):
return '\''+res.strftime('%Y-%m-%d')+'\'' if format else res.strftime('%Y-%m-%d')
elif isinstance(res,str):
return '\''+res+'\'' if format else res
else:
return '\''+str(res)+'\'' if format else str(res)
return '\''+val+'\'' if format else val
def gen_node_script(row,node_def):
if node_def.constraint:
for k,v in node_def.constraint.items():
if callable(v):
if not v(row[k]):
return ''
elif row[k]!=v:
return ''
id=''
for prop in node_def.id_props:
id+=gen_prop(row,prop,False)+'_'
#s=s[:-1]+'})'
s='MERGE (p:'+node_def.label+'{id:\''+id[:-1]+'\'})'
prop_map='{'
for k,v in node_def.props.items():
p=gen_prop(row,v)
if isinstance(p,str) and len(p)==0:
continue
prop_map+=k+':'+p+','
#TODO: take all default_props as string now, update it later
if node_def.default_props:
for k,v in node_def.default_props.items():
prop_map+=k+':\''+v+'\','
prop_map=prop_map+'id:\''+id[:-1]+'\'}'
return s+' ON MATCH set p+='+prop_map+' ON CREATE set p='+prop_map
def gen_edge_script(row,edge_def):
if edge_def.constraint:
for k,v in edge_def.constraint.items():
if callable(v):
if not v(row[k]):
return ''
elif row[k]!=v:
return ''
src_str='(s:'+edge_def.src_label+'{id:\''
src_id=''
for prop in edge_def.src_id_props:
src_id+=gen_prop(row,prop,False)+'_'
src_str+=src_id[:-1]+'\'})'
dst_str='(d:'+edge_def.dst_label+'{id:\''
dst_id=''
for prop in edge_def.dst_id_props:
dst_id+=gen_prop(row,prop,False)+'_'
dst_str+=dst_id[:-1]+'\'})'
rel_str=edge_def.label
prop_map='{'
if edge_def.props:
for k,v in edge_def.props.items():
p=gen_prop(row,v)
if len(p)==0:
continue
prop_map+=k+':'+p+','
prop_map=prop_map[:-1]
prop_map+='}'
#TODO check directional prop of edge_def later
if len(prop_map)==1:
return 'MATCH '+src_str+','+dst_str+' MERGE (s)-[:'+rel_str+']->(d)'
else:
return 'MATCH '+src_str+','+dst_str+' MERGE (s)-[r:'+rel_str+']->(d) ON MATCH SET r+='+prop_map+' ON CREATE SET r='+prop_map
def create_index():
for mapper in node_mappers:
script='CREATE INDEX ON :'+mapper.label+'(id)'
execute_query(script)
def save_inventory_item(row):
#print(row['STATE_CODE_001'])
#script=gen_node_script(row,county_mapper)
#execute_write(run_query,script)
queries=[]
for mapper in node_mappers:
script=gen_node_script(row,mapper)
#print(script)
if len(script)>0:
res=execute_query(script)
#queries.append(script)
#res=execute_queries(queries)
queries=[]
for mapper in edge_mappers:
script=gen_edge_script(row,mapper)
if len(script)>0:
res=execute_query(script)
#queries.append(script)
#res=execute_queries(queries)
for file_path in file_paths:
if force_init:
init_states_counties()
create_index()
with open(file_path,encoding='utf-8') as csv_file:
csv_reader=csv.DictReader(csv_file,delimiter=',')
line_count=0
header=None
for row in csv_reader:
if line_count==0:
#print('column names are {'+','.join(row)+'}')
header=row
else:
#if len(row['OTHER_STATE_CODE_098A'].strip())==0:continue
save_inventory_item(row)
line_count+=1
#if line_count>100: break
print('row '+str(line_count)+' processed.')
print('file {'+file_path+'} converted.')