-
Notifications
You must be signed in to change notification settings - Fork 33
/
utilities.py
301 lines (237 loc) · 10.7 KB
/
utilities.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from __future__ import print_function
from __future__ import unicode_literals
import re
import logging
import os
from collections import namedtuple
from pymongo import MongoClient
try:
from ConfigParser import ConfigParser
except ImportError:
from configparser import ConfigParser
global logger
def parse_config(config_filename):
"""
Parse the config file and return relevant information.
Parameters
----------
config_filename: String.
Path to config file.
Returns
-------
server_list: Named tuple.
Config information specifically related to the remote
server for FTP uploading.
geo_list : Named tuple.
Config information for geocoding.
file_list: Named tuple.
All the other config information not in ``server_list``.
petrarch_version: Int
Either 1 or 2, indicating whether Petrarch or Petrarch2 should be used.
"""
parser = ConfigParser()
parser.read(config_filename)
print('Found a config file in working directory')
try:
serv_name = parser.get('Server', 'server_name')
username = parser.get('Server', 'username')
password = parser.get('Server', 'password')
server_dir = parser.get('Server', 'server_dir')
server_attrs = namedtuple('ServerAttributes', ['serv_name',
'username',
'password',
'server_dir'])
server_list = server_attrs(serv_name, username, password,
server_dir)
geo_service = parser.get('Geolocation', 'geo_service')
cliff_host = parser.get('Geolocation', 'cliff_host')
cliff_port = parser.get('Geolocation', 'cliff_port')
mordecai_host = parser.get('Geolocation', 'mordecai_host')
mordecai_port = parser.get('Geolocation', 'mordecai_port')
geo_attrs = namedtuple('GeolocationAttributes', ['geo_service',
'cliff_host',
'cliff_port',
'mordecai_host',
'mordecai_port'
])
geo_list = geo_attrs(geo_service, cliff_host, cliff_port,
mordecai_host, mordecai_port)
# these are listed in the order generated
scraper_stem = parser.get('Pipeline', 'scraper_stem')
recordfile_stem = parser.get('Pipeline', 'recordfile_stem')
fullfile_stem = parser.get('Pipeline', 'fullfile_stem')
eventfile_stem = parser.get('Pipeline', 'eventfile_stem')
dupfile_stem = parser.get('Pipeline', 'dupfile_stem')
outputfile_stem = parser.get('Pipeline', 'outputfile_stem')
oneaday_filter = parser.get('Pipeline', 'oneaday_filter')
if 'Auth' in parser.sections():
auth_db = parser.get('Auth', 'auth_db')
auth_user = parser.get('Auth', 'auth_user')
auth_pass = parser.get('Auth', 'auth_pass')
db_host = parser.get('Auth', 'db_host')
else:
auth_db = ''
auth_user = ''
auth_pass = ''
db_host = os.getenv('MONGO_HOST') or None
if 'Logging' in parser.sections():
log_file = parser.get('Logging', 'log_file')
else:
log_file = ''
petrarch_version = parser.get('Petrarch', 'petrarch_version')
if 'Mongo' in parser.sections():
db_db = parser.get('Mongo', 'db')
db_collection = parser.get('Mongo', 'collection')
else:
db_db = 'event_scrape'
db_collection = 'stories'
file_attrs = namedtuple('FileAttributes', ['scraper_stem',
'recordfile_stem',
'fullfile_stem',
'eventfile_stem',
'dupfile_stem',
'outputfile_stem',
'oneaday_filter',
'log_file',
'auth_db',
'auth_user',
'auth_pass',
'db_host',
'db_db',
'db_collection'])
file_list = file_attrs(scraper_stem, recordfile_stem, fullfile_stem,
eventfile_stem, dupfile_stem, outputfile_stem,
oneaday_filter, log_file, auth_db, auth_user,
auth_pass, db_host, db_db, db_collection)
return server_list, geo_list, file_list, petrarch_version
except Exception as e:
print('Problem parsing config file. {}'.format(e))
def init_logger(logger_filename):
"""
Initialize a log file.
Parameters
----------
logger_filename: String.
Path to the log file.
"""
logger = logging.getLogger('pipeline_log')
logger.setLevel(logging.INFO)
fh = logging.FileHandler(logger_filename, 'w')
formatter = logging.Formatter('%(levelname)s %(asctime)s: %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Running')
logger.info('PHOX.pipeline run')
def do_RuntimeError(st1, filename='', st2=''):
"""
This is a general routine for raising the RuntimeError: the reason to make
this a separate procedure is to allow the error message information to be
specified only once. As long as it isn't caught explicitly, the error
appears to propagate out to the calling program, which can deal with it.
"""
logger = logging.getLogger('pipeline_log')
print(st1, filename, st2)
logger.error(st1 + ' ' + filename + ' ' + st2 + '\n')
raise RuntimeError(st1 + ' ' + filename + ' ' + st2)
def make_conn(db_db, db_collection, db_auth, db_user, db_pass, db_host=None):
"""
Function to establish a connection to a local MonoDB instance.
Parameters
----------
db_auth: String.
MongoDB database that should be used for user authentication.
db_user: String.
Username for MongoDB authentication.
db_user: String.
Password for MongoDB authentication.
Returns
-------
collection: pymongo.collection.Collection.
Collection within MongoDB that holds the scraped news stories.
"""
if db_host:
client = MongoClient(db_host)
else:
client = MongoClient()
if db_auth:
client[db_auth].authenticate(db_user, db_pass)
database = client[db_db]
collection = database[db_collection]
return collection
def sentence_segmenter(paragr):
"""
Function to break a string 'paragraph' into a list of sentences based on
the following rules:
1. Look for terminal [.,?,!] followed by a space and [A-Z]
2. If ., check against abbreviation list ABBREV_LIST: Get the string
between the . and the previous blank, lower-case it, and see if it is in
the list. Also check for single-letter initials. If true, continue search
for terminal punctuation
3. Extend selection to balance (...) and "...". Reapply termination rules
4. Add to sentlist if the length of the string is between MIN_SENTLENGTH
and MAX_SENTLENGTH
5. Returns sentlist
Parameters
----------
paragr: String.
Content that will be split into constituent sentences.
Returns
-------
sentlist: List.
List of sentences.
"""
# this is relatively high because we are only looking for sentences that
# will have subject and object
MIN_SENTLENGTH = 100
MAX_SENTLENGTH = 512
# sentence termination pattern used in sentence_segmenter(paragr)
terpat = re.compile('[\.\?!]\s+[A-Z\"]')
# source: LbjNerTagger1.11.release/Data/KnownLists/known_title.lst from
# University of Illinois with editing
ABBREV_LIST = ['mrs.', 'ms.', 'mr.', 'dr.', 'gov.', 'sr.', 'rev.', 'r.n.',
'pres.', 'treas.', 'sect.', 'maj.', 'ph.d.', 'ed. psy.',
'proc.', 'fr.', 'asst.', 'p.f.c.', 'prof.', 'admr.',
'engr.', 'mgr.', 'supt.', 'admin.', 'assoc.', 'voc.',
'hon.', 'm.d.', 'dpty.', 'sec.', 'capt.', 'c.e.o.',
'c.f.o.', 'c.i.o.', 'c.o.o.', 'c.p.a.', 'c.n.a.', 'acct.',
'llc.', 'inc.', 'dir.', 'esq.', 'lt.', 'd.d.', 'ed.',
'revd.', 'psy.d.', 'v.p.', 'senr.', 'gen.', 'prov.',
'cmdr.', 'sgt.', 'sen.', 'col.', 'lieut.', 'cpl.', 'pfc.',
'k.p.h.', 'cent.', 'deg.', 'doz.', 'Fahr.', 'Cel.', 'F.',
'C.', 'K.', 'ft.', 'fur.', 'gal.', 'gr.', 'in.', 'kg.',
'km.', 'kw.', 'l.', 'lat.', 'lb.', 'lb per sq in.', 'long.',
'mg.', 'mm.,, m.p.g.', 'm.p.h.', 'cc.', 'qr.', 'qt.', 'sq.',
't.', 'vol.', 'w.', 'wt.']
sentlist = []
# controls skipping over non-terminal conditions
searchstart = 0
terloc = terpat.search(paragr)
while terloc:
isok = True
if paragr[terloc.start()] == '.':
if (paragr[terloc.start() - 1].isupper() and
paragr[terloc.start() - 2] == ' '):
isok = False # single initials
else:
# check abbreviations
loc = paragr.rfind(' ', 0, terloc.start() - 1)
if loc > 0:
if paragr[loc + 1:terloc.start() + 1].lower() in ABBREV_LIST:
isok = False
if paragr[:terloc.start()].count('(') != paragr[:terloc.start()].count(')'):
isok = False
if paragr[:terloc.start()].count('"') % 2 != 0:
isok = False
if isok:
if (len(paragr[:terloc.start()]) > MIN_SENTLENGTH and
len(paragr[:terloc.start()]) < MAX_SENTLENGTH):
sentlist.append(paragr[:terloc.start() + 2])
paragr = paragr[terloc.end() - 1:]
searchstart = 0
else:
searchstart = terloc.start() + 2
terloc = terpat.search(paragr, searchstart)
# add final sentence
if (len(paragr) > MIN_SENTLENGTH and len(paragr) < MAX_SENTLENGTH):
sentlist.append(paragr)
return sentlist