-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountry_codes.py
159 lines (127 loc) · 4.08 KB
/
country_codes.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
"""
Map org enhanced to 3 digit country codes using address info from
Web of Science org enhanced file. Also map additional desired attributes.
"""
import argparse
import sys
import csv
import json
from slugify import slugify
from publications import waan_uri
from lib import backend
from namespaces import WOS, rq_prefixes, VIVO, OBO
from rdflib import Graph, Literal, URIRef
from log_setup import get_logger
logger = get_logger()
from settings import COUNTRY_CODE_NG, COUNTRY_CODE_KEY_FILE
REPL = {
'UNITED STATES': 'united-states-of-america',
'PEOPLES R CHINA': 'china',
'RUSSIA': 'russian-federation',
'VENEZUELA': 'venezuela-bolivarian-republic-of',
'BOLIVIA': 'bolivia',
'CZECH REPUBLIC': 'czech-republic',
'IVORY COAST': 'ivory-cost',
'TAIWAN': 'china',
'SOUTH KOREA': 'south-korea',
'VIETNAM': 'viet-nam',
'IRAN': 'iran-islamic-republic-of',
'REPUBLIC OF GEORGIA': 'georgia',
'REUNION': 'reunion',
'TANZANIA': 'united-republic-of-tanzania',
'BOSNIA & HERZEGOVINA': 'bosnia-herzegovina',
}
store = backend.get_store()
def mk_slug(raw):
clean = raw.strip().lower()
return slugify(clean)
def fetch_vivo_countries():
q = rq_prefixes + """
SELECT ?uri ?label ?code
WHERE {
?uri a vivo:Country ;
rdfs:label ?label ;
<http://aims.fao.org/aos/geopolitical.owl#codeISO3> ?code .
}
"""
d = {}
for row in store.query(q):
slug = mk_slug(row.label.toPython())
d[slug] = row.uri
return d
def fetch_vivo_states():
q = rq_prefixes + """
SELECT ?uri ?label
WHERE {
?uri a vivo:StateOrProvince ;
rdfs:label ?label .
}
"""
d = {}
for row in store.query(q):
slug = mk_slug(row.label.toPython())
d[slug] = row.uri
return d
def get_orgs():
q = rq_prefixes + """
SELECT ?uri ?label
WHERE {
?uri a wos:UnifiedOrganization ;
rdfs:label ?label .
}
"""
out = []
for row in store.query(q):
out.append(row.uri.toPython())
return out
def index_org_metadata(fpath, to_match):
d = {}
with open(fpath) as raw:
for row in csv.reader(raw, delimiter="\t"):
td = {}
waan_id = row[0]
name = row[1]
uri = waan_uri(name)
country = row[6].split(',')[-1]
clean_country = country.strip().upper()
url = row[9]
d[uri] = dict(
waan_id=waan_id,
name=row[1],
country=clean_country,
)
return d
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process org enhanced')
parser.add_argument('--index', '-i', default=None, help="Index the orgs from raw WOS metadata list. Pass metadata file.")
parser.add_argument('--load', '-l', action="store_true", help="Load updated country codes into VIVO.")
args = parser.parse_args()
if (args.index is None) and (args.load is not True):
print>>sys.stderr, "No action specified"
sys.exit()
COUNTRY_CODE_KEY_FILE = "data/org_key.json"
logger.info("Fetching VIVO countries")
vcountries = fetch_vivo_countries()
if args.index is True:
org_enhanced_meta = sys.argv[1]
orgs = index_org_metadata(org_enhanced_meta, vcountries)
with open(COUNTRY_CODE_KEY_FILE, 'wb') as outf:
json.dump(orgs, outf)
if args.load is True:
with open(COUNTRY_CODE_KEY_FILE) as inf:
org_enhanced = json.load(inf)
orgs = get_orgs()
g = Graph()
for org in orgs:
ometa = org_enhanced.get(org)
if ometa is None:
logger.info("No org metadata found for {}.".format(org))
continue
country = ometa['country']
name = REPL.get(country) or mk_slug(country)
try:
curi = vcountries[name]
g.add((URIRef(org), OBO['RO_0001025'], URIRef(curi)))
except:
logger.info("Can't match {}.".format(name))
backend.post_updates(COUNTRY_CODE_NG, g)