/
USPTOStoreGrantData.py
240 lines (227 loc) · 17.7 KB
/
USPTOStoreGrantData.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# USPTOStoreGrantData.py
# USPTO Bulk Data Parser - Store Grant Data
# Description: Imported to the main USPTOParser.py. Stores grant data in CSV or database.
# Author: Joseph Lee
# Email: joseph@ripplesoftware.ca
# Website: www.ripplesoftware.ca
# Github: www.github.com/rippledj/uspto
# Import Python Modules
import time
import traceback
import os
import sys
# Import USPTO Parser Functions
import USPTOLogger
import SQLProcessor
# Function used to store grant data in CSV and/or database
def store_grant_data(processed_data_array, args_array):
# Set process start time
start_time = time.time()
# Extract some variables from args_array
uspto_xml_format = args_array['uspto_xml_format']
file_name = args_array['file_name']
logger = USPTOLogger.logging.getLogger("USPTO_Database_Construction")
# If the argument specified to store data into csv file or csv is needed for bulk database insertion
if "csv" in args_array["command_args"] or ("database" in args_array['command_args'] and args_array['database_insert_mode'] == "bulk"):
# Process all the collected grant data for one patent record into csv file
# Using the already opened csv.csv.DictWriter object stored in args array.
# Table name must be appended to the dictionary for later processing
if "processed_grant" in processed_data_array and len(processed_data_array['processed_grant']):
for data_item in processed_data_array['processed_grant']:
if args_array['stdout_level'] == 1:
# Print start message to stdout and log
print('- Starting to write {0} to .csv file {1} for document: {2}. Start Time: {3}'.format(args_array['document_type'], file_name, data_item['GrantID'], time.strftime("%c")))
# Move the table name to temp variable and remove from table
table_name = data_item['table_name']
del data_item['table_name']
# Try catch is to avoid failing the whole file when
# htmlentity characters found or other error occurs
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['grant']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['grant']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_applicant" in processed_data_array and len(processed_data_array['processed_applicant']):
for data_item in processed_data_array['processed_applicant']:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['applicant']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['applicant']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_examiner" in processed_data_array and len(processed_data_array['processed_examiner']):
for data_item in processed_data_array['processed_examiner']:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['examiner']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['examiner']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_agent" in processed_data_array and len(processed_data_array['processed_agent']):
for data_item in processed_data_array["processed_agent"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['agent']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['agent']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_assignee" in processed_data_array and len(processed_data_array['processed_assignee']):
for data_item in processed_data_array["processed_assignee"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['assignee']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['assignee']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_inventor" in processed_data_array and len(processed_data_array['processed_inventor']):
for data_item in processed_data_array["processed_inventor"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['inventor']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['inventor']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_gracit" in processed_data_array and len(processed_data_array['processed_gracit']):
for data_item in processed_data_array["processed_gracit"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['gracit']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['gracit']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_nonpatcit" in processed_data_array and len(processed_data_array['processed_nonpatcit']):
for data_item in processed_data_array["processed_nonpatcit"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['nonpatcit']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['nonpatcit']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_forpatcit" in processed_data_array and len(processed_data_array['processed_forpatcit']):
for data_item in processed_data_array["processed_forpatcit"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['forpatcit']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['forpatcit']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_usclass" in processed_data_array and len(processed_data_array['processed_usclass']):
for data_item in processed_data_array["processed_usclass"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['usclass']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['usclass']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_intclass" in processed_data_array and len(processed_data_array['processed_intclass']):
for data_item in processed_data_array["processed_intclass"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['intclass']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['intclass']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_cpcclass" in processed_data_array and len(processed_data_array['processed_cpcclass']):
for data_item in processed_data_array["processed_cpcclass"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['cpcclass']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['cpcclass']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
if "processed_foreignpriority" in processed_data_array and len(processed_data_array['processed_foreignpriority']):
for data_item in processed_data_array["processed_foreignpriority"]:
table_name = data_item['table_name']
del data_item['table_name']
try:
# Write the dictionary of document data to .csv file
args_array['csv_file_array']['foreignpriority']['csv_writer'].writerow(data_item)
# Append the table onto the array
args_array['csv_file_array']['foreignpriority']['table_name'] = table_name
except Exception as e:
print('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
logger.info('- Error writing {0} to .csv file {1} for document: {2} into table {3}. Start Time: {4}'.format(args_array['document_type'], file_name, data_item['GrantID'], table_name, time.strftime("%c")))
traceback.print_exc()
# If command arg is set to put data into database
elif "database" in args_array["command_args"] and args_array['database_insert_mode'] == "each":
# Reset the start time
start_time = time.time()
# Print to stdout if verbose set
if args_array['stdout_level'] == 1:
print('- Starting to write {0} to database. Start Time: {1}'.format(file_name, time.strftime("%c")))
# Strip the processed_grant item off the array and process it first
processed_grant = processed_data_array['processed_grant']
del processed_data_array['processed_grant']
for item in processed_grant:
# Store table name for stdout
args_array['table_name'] = item['table_name']
args_array['document_id'] = item['GrantID']
# Build query and pass to database loader
args_array['database_connection'].load(SQLProcessor.build_sql_insert_query(item, args_array), args_array)
# Loop throught the processed_data_array and create sql queries and execute them
for key, value in list(processed_data_array.items()):
for item in value:
# Store table name for stdout
args_array['table_name'] = item['table_name']
args_array['document_id'] = item['GrantID']
# Build query and pass to database loader
args_array['database_connection'].load(SQLProcessor.build_sql_insert_query(item, args_array), args_array)