-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
207 lines (190 loc) · 5.66 KB
/
setup.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
# Copyright 2016 Matthias Gazzari
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''A Python library to match an affiliation string to a known institution'''
import os, os.path
import csv
import math
import multiprocessing
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from setuptools.command.test import test
codes = {}
countryInfo = os.path.join('instmatcher', 'data', 'countryInfo.txt')
with open(countryInfo) as csvfile:
data = filter(lambda row: not row[0].startswith('#'), csvfile)
reader = csv.reader(data, delimiter='\t', quoting=csv.QUOTE_NONE)
for row in reader:
codes[row[0]] = row[4]
def create_index(procs, multisegment, ixPath):
from whoosh import index
from whoosh.fields import Schema, TEXT, NUMERIC, STORED, ID
from whoosh.analysis import CharsetFilter, StemmingAnalyzer, LowercaseFilter
from whoosh.support.charset import accent_map
ana = StemmingAnalyzer() | CharsetFilter(accent_map) | LowercaseFilter()
schema = Schema(
name=STORED,
tokens=TEXT(analyzer=ana),
alias=TEXT(analyzer=ana),
lat=NUMERIC(numtype=float, stored=True),
lon=NUMERIC(numtype=float, stored=True),
isni=STORED,
country=STORED,
alpha2=ID(stored=True),
source=ID(stored=True, unique=True),
type=STORED,
)
ix = index.create_in(ixPath, schema)
writer = ix.writer(procs=procs, multisegment=multisegment)
institutions = os.path.join('instmatcher', 'data', 'institutions.csv')
visited = set()
with open(institutions) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['source'] not in visited:
try:
row['country'] = codes[row['alpha2']]
except KeyError:
continue
if not row['lat'] or not row['lon']:
row['lat'], row['lon'] = float('nan'), float('nan')
row['tokens'] = row['name']
writer.add_document(**row)
visited.add(row['source'])
writer.commit()
def create_geoindex(procs, multisegment, ixPath):
from whoosh import index
from whoosh.fields import Schema, STORED, ID, IDLIST
schema = Schema(
name=STORED,
lower=ID,
asci=ID,
alias=IDLIST(expression=r'[^,]+'),
lat=STORED,
lon=STORED,
alpha2=ID(stored=True),
country=ID(stored=True),
)
ix = index.create_in(ixPath, schema)
writer = ix.writer(procs=procs, multisegment=multisegment)
cities = os.path.join('instmatcher', 'data', 'cities1000.txt')
with open(cities) as f:
reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
for row in reader:
population = int(row[14])
writer.add_document(
name=row[1],
lower=row[1].lower(),
asci=row[2].lower(),
alias=row[3].lower(),
lat=row[4],
lon=row[5],
alpha2=row[8],
country=codes[row[8]],
_boost=math.log(max(math.e, population)),
)
writer.commit()
def create_indices(force):
def decorator(command_subclass):
orig_run = command_subclass.run
def new_run(self):
procs = multiprocessing.cpu_count()
multisegment = procs > 1
forceInst = force
ixPath = os.path.join('instmatcher', 'data', 'index')
if not os.path.exists(ixPath):
forceInst = True
os.mkdir(ixPath)
if forceInst:
print('creating the institution index, this may take some time')
create_index(procs, multisegment, ixPath)
forceGeo = force
ixPath = os.path.join('instmatcher', 'data', 'geoindex')
if not os.path.exists(ixPath):
forceGeo = True
os.mkdir(ixPath)
if forceGeo:
print('creating the geoindex, this may take some time')
create_geoindex(procs, multisegment, ixPath)
orig_run(self)
command_subclass.run = new_run
return command_subclass
return decorator
@create_indices(force=True)
class CustomDevelopCommand(develop):
pass
@create_indices(force=True)
class CustomInstallCommand(install):
pass
@create_indices(force=False)
class CustomTestCommand(test):
pass
def readme():
with open('README.rst') as f:
return f.read()
with open(os.path.join('instmatcher', 'version.py'), 'rt') as f:
exec(f.read())
setup(
cmdclass={
'install': CustomInstallCommand,
'develop': CustomDevelopCommand,
'test': CustomTestCommand,
},
name='instmatcher',
version=__version__,
description=__doc__,
long_description=readme(),
author='Matthias Gazzari',
author_email='[email protected]',
url='https://github.com/qtux/instmatcher',
license='Apache License 2.0',
packages=['instmatcher',],
package_data={
'instmatcher': [
'data/abbreviations.csv',
'data/alternativeCountryNames.csv',
'data/countryInfo.txt',
'data/index/*',
'data/geoindex/*',
]
},
install_requires=[
'Whoosh>=2.7.4',
'requests>=2.10.0',
],
extras_require={
'docs':[
'Sphinx>=1.4.5',
'sphinx_rtd_theme>=0.1.10a0',
],
'examples-neo4j':[
'PyYAML>=3.11',
'py2neo>=3.1.1',
],
},
include_package_data=True,
zip_safe=False,
keywords='institute institution affiliation organisation search match',
platforms='any',
classifiers=[
'Development Status :: 4 - Beta',
'License :: OSI Approved :: Apache Software License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.4',
'Topic :: Text Processing :: General',
'Topic :: Software Development :: Libraries :: Python Modules',
],
test_suite="tests",
)