forked from iamlemec/fastpat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_grants_gen3.py
162 lines (132 loc) · 4.93 KB
/
parse_grants_gen3.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
#!/usr/bin/python
import sys
import sqlite3
from xml.sax import make_parser
from parse_grants_common import PathHandler
# handle arguments
if len(sys.argv) <= 1:
print 'Usage: parse_grants_gen3.py filename store_db'
sys.exit(0)
in_fname = sys.argv[1]
if sys.argv[2] == '1':
store_db = True
else:
store_db = False
if store_db:
# database file
db_fname = 'store/patents.db'
conn = sqlite3.connect(db_fname)
cur = conn.cursor()
try:
cur.execute("create table patent (patnum int, filedate text, grantdate text, classone int, classtwo int, ipcver text, ipccode text, city text, country text, owner text)")
except sqlite3.OperationalError as e:
print e
# store for batch commit
batch_size = 1000
patents = []
def commitBatch():
if store_db:
cur.executemany('insert into patent values (?,?,?,?,?,?,?,?,?,?)',patents)
del patents[:]
def forceUpper(s):
return s.encode('ascii','ignore').upper()
# SAX hanlder for gen3 patent grants
class GrantHandler(PathHandler):
def __init__(self):
track_keys = ['us-patent-grant','publication-reference','application-reference','doc-number',
'classification-national','main-classification','classification-ipcr','ipc-version-indicator',
'section','class','subclass','main-group','subgroup','date','assignee','orgname','country','city']
start_keys = ['us-patent-grant','classification-ipcr','assignee']
end_keys = ['us-patent-grant','classification-ipcr','assignee']
PathHandler.__init__(self,track_keys=track_keys,start_keys=start_keys,end_keys=end_keys)
self.completed = 0
def startElement(self,name,attrs):
PathHandler.startElement(self,name,attrs)
if name == 'us-patent-grant':
self.patnum = ''
self.grant_date = ''
self.file_date = ''
self.class_str = ''
self.orgnames = []
self.countries = []
self.cities = []
self.ipc_vers = []
self.ipc_codes = []
elif name == 'classification-ipcr':
self.ipc_ver = ''
self.ipc_code = ''
elif name == 'assignee':
self.orgname = ''
self.country = ''
self.city = ''
def endElement(self,name):
PathHandler.endElement(self,name)
if name == 'us-patent-grant':
if self.patnum[0] == '0':
self.addPatent()
elif name == 'classification-ipcr':
self.ipc_vers.append(self.ipc_ver)
self.ipc_codes.append(self.ipc_code)
elif name == 'assignee':
self.orgnames.append(self.orgname)
self.countries.append(self.country)
self.cities.append(self.city)
def characters(self,content):
if len(self.path) < 2:
return
if self.path[-2] == 'publication-reference':
if self.path[-1] == 'doc-number':
self.patnum += content
elif self.path[-1] == 'date':
self.grant_date += content
elif self.path[-2] == 'application-reference' and self.path[-1] == 'date':
self.file_date += content
elif self.path[-2] == 'classification-national' and self.path[-1] == 'main-classification':
self.class_str += content
elif self.path[-2] == 'assignee':
if self.path[-1] == 'orgname':
self.orgname += content
elif self.path[-1] == 'country':
self.country += content
elif self.path[-1] == 'city':
self.city += content
elif self.path[-2] == 'classification-ipcr':
if self.path[-1] in ['section','class','subclass']:
self.ipc_code += content
elif self.path[-1] == 'subgroup':
self.ipc_code += '/' + content
if len(self.path) < 3:
return
if self.path[-3] == 'classification-ipcr':
if self.path[-2] == 'ipc-version-indicator' and self.path[-1] == 'date':
self.ipc_ver += content
def addPatent(self):
self.completed += 1
self.patint = self.patnum[1:]
self.class_one = self.class_str[:3].strip()
self.class_two = self.class_str[3:6].strip()
self.ipc_ver = self.ipc_vers[0] if self.ipc_vers else ''
self.ipc_code = ','.join(self.ipc_codes)
self.orgname = self.orgnames[0] if self.orgnames else ''
self.country = self.countries[0] if self.countries else ''
self.city = self.cities[0] if self.cities else ''
self.city = forceUpper(self.city)
self.orgname = forceUpper(self.orgname)
if not store_db: print '{:7} {} {} {:3.3} {:3.3} {:9.9} {:3} {:15} {:3} {:.30}'.format(self.patint,self.file_date,self.grant_date,self.class_one,self.class_two,self.ipc_codes[0],len(self.ipc_codes),self.city,self.country,self.orgname)
patents.append((self.patint,self.file_date,self.grant_date,self.class_one,self.class_two,self.ipc_ver,self.ipc_code,self.city,self.country,self.orgname))
if len(patents) == batch_size:
commitBatch()
# do parsing
parser = make_parser()
grant_handler = GrantHandler()
parser.setContentHandler(grant_handler)
parser.parse(in_fname)
# clear out the rest
if len(patents) > 0:
commitBatch()
if store_db:
# commit to db and close
conn.commit()
cur.close()
conn.close()
print grant_handler.completed