forked from edskal/patentprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouch_patent.py
More file actions
48 lines (42 loc) · 1.27 KB
/
couch_patent.py
File metadata and controls
48 lines (42 loc) · 1.27 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
#!/usr/bin/env python
import sys
import couchdb
attrs = ['country', 'patent', 'kind', 'date_grant', 'pat_type', \
'date_app', 'country_app', 'patent_app', 'code_app', \
'clm_num', 'classes', 'abstract', 'invention_title', \
'asg_list', 'cit_list', 'rel_list', \
'inv_list', 'law_list']
couch = couchdb.Server()
db = None
if 'patents' in couch:
db = couch['patents']
else:
db = couch.create('patents')
def get_metadata(patent):
"""
Return metadata dictionary of the patent
"""
metadata = {}
metadata['publication_id'] = patent.patent
metadata['application_id'] = patent.patent_app
metadata['xml'] = patent.orig_xmlstring
metadata['attributes'] = {}
for attr in attrs:
metadata['attributes'][attr] = patent.__getattribute__(attr)
return metadata
def add_doc(metadata):
"""
Adds metadata to couch database
"""
doc_id, doc_rev = db.save(metadata)
return doc_id
def query(tag, value):
"""
Composes a javascript query to be run on the database
"""
mapfun = """function(doc) {{
if(doc.{0} == "{1}")
emit(doc, doc.{0})
}}""".format(tag,value)
res = db.query(mapfun).rows
return res[0].key['xml'] if res else None