-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetrarch2.py
executable file
·546 lines (444 loc) · 19.2 KB
/
petrarch2.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import glob
import time
import types
import logging
import argparse
import xml.etree.ElementTree as ET
# from petrarch2.utilities import
# petrarch.py
##
# Automated event data coder
##
# SYSTEM REQUIREMENTS
# This program has been successfully run under Mac OS 10.10; it is standard Python 2.7
# so it should also run in Unix or Windows.
#
# INITIAL PROVENANCE:
# Programmers:
# Philip A. Schrodt
# Parus Analytics
# Charlottesville, VA, 22901 U.S.A.
# http://eventdata.parusanalytics.com
#
# John Beieler
# Caerus Associates/Penn State University
# Washington, DC / State College, PA, 16801 U.S.A.
# http://caerusassociates.com
# http://bdss.psu.edu
#
# GitHub repository: https://github.com/openeventdata/petrarch
#
# Copyright (c) 2014 Philip A. Schrodt. All rights reserved.
#
# This project is part of the Open Event Data Alliance tool set; earlier developments
# were funded in part by National Science Foundation grant SES-1259190
#
# This code is covered under the MIT license
#
# Report bugs to: [email protected]
#
# REVISION HISTORY:
# 22-Nov-13: Initial version
# Summer-14: Numerous modifications to handle synonyms in actor and verb dictionaries
# 20-Nov-14: write_actor_root/text added to parse_Config
# ------------------------------------------------------------------------
import PETRglobals # global variables
import PETRreader # input routines
import PETRwriter
import utilities
import PETRtree
# ========================== VALIDATION FUNCTIONS ========================== #
def get_version():
return "1.0.0"
# ========================== OUTPUT/ANALYSIS FUNCTIONS ========================== #
def open_tex(filename):
file = open(filename,'w')
'''file.write('Run time: ',
print("""
\\documentclass[11pt]{article}
\\usepackage{tikz-qtree}
\\usepackage{ifpdf}
\\usepackage{fullpage}
\\usepackage[landscape]{geometry}
\\ifpdf
\\pdfcompresslevel=9
\\usepackage[pdftex, % sets up hyperref to use pdftex driver
plainpages=false, % allows page i and 1 to exist in the same document
breaklinks=true, % link texts can be broken at the end of line
colorlinks=true,
pdftitle=My Document
pdfauthor=My Good Self
]{hyperref}
\\usepackage{thumbpdf}
\\else
\\usepackage{graphicx} % to include graphics
\\usepackage{hyperref} % to simplify the use of \href
\\fi
\\title{Petrarch Output}
\\date{}
\\begin{document}
""", file = file)'''
return file
def close_tex(file):
return
print("\n\\end{document})",file=file)
# ========================== PRIMARY CODING FUNCTIONS ====================== #
def check_discards(SentenceText):
"""
Checks whether any of the discard phrases are in SentenceText, giving
priority to the + matches. Returns [indic, match] where indic
0 : no matches
1 : simple match
2 : story match [+ prefix]
"""
sent = SentenceText.upper().split() # case insensitive matching
size = len(sent)
level = PETRglobals.DiscardList
depart_index = [0]
discardPhrase = ""
for i in range(len(sent)):
if '+' in level:
return [2, '+ ' + discardPhrase]
elif '$' in level:
return [1, ' ' + discardPhrase]
elif sent[i] in level:
# print(sent[i],SentenceText.upper(),level[sent[i]])
depart_index.append(i)
level = level[sent[i]]
discardPhrase += " " + sent[i]
else:
if len(depart_index) == 0:
continue
i = depart_index[0]
level = PETRglobals.DiscardList
return [0, '']
def get_issues(SentenceText):
"""
Finds the issues in SentenceText, returns as a list of [code,count]
<14.02.28> stops coding and sets the issues to zero if it finds *any*
ignore phrase
"""
def recurse(words,path,length):
if '#' in path: # <16.06.06 pas> Swapped the ordering if these checks since otherwise it crashes when '#' is a "word" in the text
return path['#'],length
elif words and words[0] in path:
return recurse(words[1:],path[words[0]],length+1)
return False
sent = SentenceText.upper().split() # case insensitive matching
issues = []
index = 0
while index < len(sent):
match = recurse(sent[index:],PETRglobals.IssueList,0)
if match:
index += match[1]
code = PETRglobals.IssueCodes[match[0]]
if code[0] == '~': # ignore code, so bail
return []
ka = 0
gotcode = False
while ka < len(issues):
if code == issues[ka][0]:
issues[ka][1] += 1
break
ka += 1
if ka == len(issues): # didn't find the code, so add it
issues.append([code, 1])
else:
index += 1
return issues
def do_coding(event_dict, out_file):
"""
Main coding loop Note that entering any character other than 'Enter' at the
prompt will stop the program: this is deliberate.
<14.02.28>: Bug: PETRglobals.PauseByStory actually pauses after the first
sentence of the *next* story
"""
treestr = ""
NStory = 0
NSent = 0
NEvents = 0
NEmpty = 0
NDiscardSent = 0
NDiscardStory = 0
if out_file:
file = open_tex(out_file)
logger = logging.getLogger('petr_log')
times = 0
sents = 0
for key, val in sorted(event_dict.items()):
NStory += 1
prev_code = []
SkipStory = False
print('\n\nProcessing story {}'.format(key))
StoryDate = event_dict[key]['meta']['date']
StorySource = 'TEMP'
for sent in val['sents']:
NSent += 1
if 'parsed' in event_dict[key]['sents'][sent]:
# if 'config' in val['sents'][sent]:
# for id, config in event_dict[key][
# 'sents'][sent]['config'].items():
# change_Config_Options(config)
SentenceID = '{}_{}'.format(key, sent)
SentenceText = event_dict[key]['sents'][sent]['content']
SentenceDate = event_dict[key]['sents'][sent][
'date'] if 'date' in event_dict[key]['sents'][sent] else StoryDate
Date = PETRreader.dstr_to_ordate(SentenceDate)
SentenceSource = 'TEMP'
print("\n",SentenceID)
parsed = event_dict[key]['sents'][sent]['parsed']
treestr = parsed
disc = check_discards(SentenceText)
if disc[0] > 0:
if disc[0] == 1:
print("Discard sentence:", disc[1])
logger.info('\tSentence discard. {}'.format(disc[1]))
NDiscardSent += 1
continue
else:
print("Discard story:", disc[1])
logger.info('\tStory discard. {}'.format(disc[1]))
SkipStory = True
NDiscardStory += 1
break
t1 = time.time()
sentence = PETRtree.Sentence(treestr,SentenceText,Date)
print(sentence.actor)
print(sentence.agent)
coded_events , meta = sentence.get_events() # this is the entry point into the processing in PETRtree
# print(meta)
code_time = time.time()-t1
event_dict[key]['meta']['verbs'] = meta # 16.04.30 pas: we're using the key value 'meta' at two very different
# levels of event_dict -- see the code about ten lines below -- and
# this is potentially confusing, so it probably would be useful to
# change one of those
if out_file:
sentence.print_to_file(sentence.tree,file = file)
del(sentence)
times+=code_time
sents += 1
#print('\t\t',code_time)
if coded_events:
event_dict[key]['sents'][sent]['events'] = coded_events
event_dict[key]['sents'][sent]['meta'] = meta
"""print('DC-events:', coded_events) # --
print('DC-meta:', meta) # --
print('+++',event_dict[key]['sents'][sent]) # --"""
if PETRglobals.WriteActorText or PETRglobals.WriteEventText or PETRglobals.WriteActorRoot :
text_dict = utilities.extract_phrases(event_dict[key]['sents'][sent],SentenceID)
# -- print('DC-td1:',text_dict) # --
if text_dict:
event_dict[key]['sents'][sent]['meta']['actortext'] = {}
event_dict[key]['sents'][sent]['meta']['eventtext'] = {}
event_dict[key]['sents'][sent]['meta']['actorroot'] = {}
# -- print('DC1:',text_dict) # --
for evt in coded_events:
if evt in text_dict: # 16.04.30 pas bypasses problems with expansion of compounds
event_dict[key]['sents'][sent]['meta']['actortext'][evt] = text_dict[evt][:2]
event_dict[key]['sents'][sent]['meta']['eventtext'][evt] = text_dict[evt][2]
event_dict[key]['sents'][sent]['meta']['actorroot'][evt] = text_dict[evt][3:5]
if coded_events and PETRglobals.IssueFileName != "":
event_issues = get_issues(SentenceText)
if event_issues:
event_dict[key]['sents'][sent]['issues'] = event_issues
if PETRglobals.PauseBySentence:
if len(input("Press Enter to continue...")) > 0:
sys.exit()
prev_code = coded_events
NEvents += len(coded_events)
if len(coded_events) == 0:
NEmpty += 1
else:
logger.info(
'{} has no parse information. Passing.'.format(SentenceID))
pass
if SkipStory:
event_dict[key]['sents'] = None
if out_file:
close_tex(file)
print("\nSummary:")
print(
"Stories read:",
NStory,
" Sentences coded:",
NSent,
" Events generated:",
NEvents)
print(
"Discards: Sentence",
NDiscardSent,
" Story",
NDiscardStory,
" Sentences without events:",
NEmpty)
print("Average Coding time = ", times/sents if sents else 0)
# -- print('DC-exit:',event_dict)
return event_dict
def parse_cli_args():
"""Function to parse the command-line arguments for PETRARCH."""
__description__ = """
PETRARCH
(https://openeventdata.github.io/) (v. 0.01)
"""
aparse = argparse.ArgumentParser(prog='petrarch',
description=__description__)
sub_parse = aparse.add_subparsers(dest='command_name')
parse_command = sub_parse.add_parser('parse', help=""" DEPRECATED Command to run the
PETRARCH parser. Do not use unless you've used it before. If you need to
process unparsed text, see the README""",
description="""DEPRECATED Command to run the
PETRARCH parser. Do not use unless you've used it before.If you need to
process unparsed text, see the README""")
parse_command.add_argument('-i', '--inputs',
help='File, or directory of files, to parse.',
required=True)
parse_command.add_argument('-P', '--parsed', action='store_true',
default=False, help="""Whether the input
document contains StanfordNLP-parsed text.""")
parse_command.add_argument('-o', '--output',
help='File to write parsed events.',
required=True)
parse_command.add_argument('-c', '--config',
help="""Filepath for the PETRARCH configuration
file. Defaults to PETR_config.ini""",
required=False)
batch_command = sub_parse.add_parser('batch', help="""Command to run a batch
process from parsed files specified by
an optional config file.""",
description="""Command to run a batch
process from parsed files specified by
an optional config file.""")
batch_command.add_argument('-c', '--config',
help="""Filepath for the PETRARCH configuration
file. Defaults to PETR_config.ini""",
required=False)
batch_command.add_argument('-i', '--inputs',
help="""Filepath for the input XML file. Defaults to
data/text/Gigaword.sample.PETR.xml""",
required=False)
batch_command.add_argument('-o', '--outputs',
help="""Filepath for the input XML file. Defaults to
data/text/Gigaword.sample.PETR.xml""",
required=False)
args = aparse.parse_args()
return args
def main():
cli_args = parse_cli_args()
utilities.init_logger('PETRARCH.log')
logger = logging.getLogger('petr_log')
PETRglobals.RunTimeString = time.asctime()
if cli_args.command_name == 'parse' or cli_args.command_name == 'batch':
if cli_args.config:
print('Using user-specified config: {}'.format(cli_args.config))
logger.info(
'Using user-specified config: {}'.format(cli_args.config))
PETRreader.parse_Config(cli_args.config)
else:
logger.info('Using default config file.')
PETRreader.parse_Config(utilities._get_data('data/config/',
'PETR_config.ini'))
read_dictionaries()
start_time = time.time()
print('\n\n')
paths = PETRglobals.TextFileList
if cli_args.inputs:
if os.path.isdir(cli_args.inputs):
if cli_args.inputs[-1] != '/':
paths = glob.glob(cli_args.inputs + '/*.xml')
else:
paths = glob.glob(cli_args.inputs + '*.xml')
elif os.path.isfile(cli_args.inputs):
paths = [cli_args.inputs]
else:
print(
'\nFatal runtime error:\n"' +
cli_args.inputs +
'" could not be located\nPlease enter a valid directory or file of source texts.')
sys.exit()
out = "" #PETRglobals.EventFileName
if cli_args.outputs:
out = cli_args.outputs
if cli_args.command_name == 'parse':
run(paths, out, cli_args.parsed)
else:
run(paths, out , True) ## <===
print("Coding time:", time.time() - start_time)
print("Finished")
def read_dictionaries(validation=False):
print('Verb dictionary:', PETRglobals.VerbFileName)
verb_path = utilities._get_data(
'data/dictionaries',
PETRglobals.VerbFileName)
PETRreader.read_verb_dictionary(verb_path)
print('Actor dictionaries:', PETRglobals.ActorFileList)
for actdict in PETRglobals.ActorFileList:
actor_path = utilities._get_data('data/dictionaries', actdict)
PETRreader.read_actor_dictionary(actor_path)
print('Agent dictionary:', PETRglobals.AgentFileName)
agent_path = utilities._get_data('data/dictionaries',
PETRglobals.AgentFileName)
PETRreader.read_agent_dictionary(agent_path)
print('Discard dictionary:', PETRglobals.DiscardFileName)
discard_path = utilities._get_data('data/dictionaries',
PETRglobals.DiscardFileName)
PETRreader.read_discard_list(discard_path)
if PETRglobals.IssueFileName != "":
print('Issues dictionary:', PETRglobals.IssueFileName)
issue_path = utilities._get_data('data/dictionaries',
PETRglobals.IssueFileName)
PETRreader.read_issue_list(issue_path)
def run(filepaths, out_file, s_parsed):
# this is the routine called from main()
events = PETRreader.read_xml_input(filepaths, s_parsed)
# if not s_parsed:
# events = utilities.stanford_parse(events)
updated_events = do_coding(events, out_file)
PETRwriter.write_events(updated_events, 'evts.' + out_file)
#Newly added code by Sayeed Salam, [email protected]
def gen_cameo_event(jsonString):
events = PETRreader.read_json(jsonString)
if events:
updated_events = do_coding(events, None)
return updated_events
else:
return {}
def run_pipeline(data, out_file=None, config=None, write_output=True,
parsed=False):
# this is called externally
utilities.init_logger('PETRARCH.log')
logger = logging.getLogger('petr_log')
if config:
print('Using user-specified config: {}'.format(config))
logger.info('Using user-specified config: {}'.format(config))
PETRreader.parse_Config(config)
else:
logger.info('Using default config file.')
logger.info('Config path: {}'.format(utilities._get_data('data/config/',
'PETR_config.ini')))
PETRreader.parse_Config(utilities._get_data('data/config/',
'PETR_config.ini'))
read_dictionaries()
logger.info('Hitting read events...')
events = PETRreader.read_pipeline_input(data)
if parsed:
logger.info('Hitting do_coding')
updated_events = do_coding(events, None)
# else:
# events = utilities.stanford_parse(events)
# updated_events = do_coding(events, None)
if not write_output:
output_events = PETRwriter.pipe_output(updated_events)
return output_events
elif write_output and not out_file:
print('Please specify an output file...')
logger.warning('Need an output file. ¯\_(ツ)_/¯')
sys.exit()
elif write_output and out_file:
PETRwriter.write_events(updated_events, out_file)
if __name__ == '__main__':
main()