template_generate_ace.py (202462B)
1 import sys 2 import ipdb 3 4 INPUT_STYLE_SET = ['event_type', 'event_type_sent', 'keywords', 'triggers', 'template'] 5 OUTPUT_STYLE_SET = ['trigger:sentence', 'argument:sentence'] 6 ROLE_PH_MAP = { 7 'Person': 'somebody', 8 'Entity': 'some people or some organization', 9 'Defendant': 'somebody', 10 'Prosecutor': 'some other', 11 'Plaintiff': 'some other', 12 'Buyer': 'someone', 13 'Artifact': 'something', 14 'Seller': 'some seller', 15 'Destination': 'somewhere', 16 'Origin': 'some place', 17 'Vehicle': 'some vehicle', 18 'Agent': 'somebody or some organization', 19 'Attacker': 'some attacker', 20 'Target': 'some facility, someone, or some organization', 21 'Victim': 'some victim', 22 'Instrument': 'some way', 23 'Giver': 'someone', 24 'Recipient': 'some other', 25 'Org': 'some organization', 26 'Place': 'somewhere', 27 'Adjudicator': 'some adjudicator' 28 } 29 30 class eve_template_generator(): 31 def __init__(self, passage, triggers, roles, input_style, output_style, vocab, instance_base=False): 32 """ 33 generate strctured information for events 34 35 args: 36 passage(List): a list of tokens 37 triggers(List): a list of triggers 38 roles(List): a list of Roles 39 input_style(List): List of elements; elements belongs to INPUT_STYLE_SET 40 input_style(List): List of elements; elements belongs to OUTPUT_STYLE_SET 41 instance_base(Bool): if instance_base, we generate only one pair (use for trigger generation), else, we generate trigger_base (use for argument generation) 42 """ 43 self.raw_passage = passage 44 self.triggers = triggers 45 self.roles = roles 46 self.events = self.process_events(passage, triggers, roles) 47 self.input_style = input_style 48 self.output_style = output_style 49 self.vocab = vocab 50 self.event_templates = [] 51 if instance_base: 52 for e_type in self.vocab['event_type_itos']: 53 theclass = getattr(sys.modules[__name__], e_type.replace(':', '_').replace('-', '_'), False) 54 if theclass: 55 self.event_templates.append(theclass(self.input_style, self.output_style, passage, e_type, self.events)) 56 else: 57 print(e_type) 58 59 else: 60 for event in self.events: 61 theclass = getattr(sys.modules[__name__], event['event type'].replace(':', '_').replace('-', '_'), False) 62 assert theclass 63 self.event_templates.append(theclass(self.input_style, self.output_style, event['tokens'], event['event type'], event)) 64 self.data = [x.generate_pair(x.trigger_text) for x in self.event_templates] 65 self.data = [x for x in self.data if x] 66 67 def get_training_data(self): 68 return self.data 69 70 def process_events(self, passage, triggers, roles): 71 """ 72 Given a list of token and event annotation, return a list of structured event 73 74 structured_event: 75 { 76 'trigger text': str, 77 'trigger span': (start, end), 78 'event type': EVENT_TYPE(str), 79 'arguments':{ 80 ROLE_TYPE(str):[{ 81 'argument text': str, 82 'argument span': (start, end) 83 }], 84 ROLE_TYPE(str):..., 85 ROLE_TYPE(str):.... 86 } 87 'passage': PASSAGE 88 } 89 """ 90 91 events = {trigger: [] for trigger in triggers} 92 93 for argument in roles: 94 trigger = argument[0] 95 events[trigger].append(argument) 96 97 event_structures = [] 98 for trigger, arguments in events.items(): 99 eve_type = trigger[2] 100 eve_text = ' '.join(passage[trigger[0]:trigger[1]]) 101 eve_span = (trigger[0], trigger[1]) 102 argus = {} 103 for argument in arguments: 104 role_type = argument[1][2] 105 if role_type not in argus.keys(): 106 argus[role_type] = [] 107 argus[role_type].append({ 108 'argument text': ' '.join(passage[argument[1][0]:argument[1][1]]), 109 'argument span': (argument[1][0], argument[1][1]), 110 }) 111 event_structures.append({ 112 'trigger text': eve_text, 113 'trigger span': eve_span, 114 'event type': eve_type, 115 'arguments': argus, 116 'passage': ' '.join(passage), 117 'tokens': passage 118 }) 119 return event_structures 120 121 class event_template(): 122 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 123 self.input_style = input_style 124 self.output_style = output_style 125 self.output_template = self.get_output_template() 126 self.passage = ' '.join(passage) 127 self.tokens = passage 128 self.event_type = event_type 129 if gold_event is not None: 130 self.gold_event = gold_event 131 if isinstance(gold_event, list): 132 # instance base 133 self.trigger_text = " and ".join([x['trigger text'] for x in gold_event if x['event type']==event_type]) 134 self.trigger_span = [x['trigger span'] for x in gold_event if x['event type']==event_type] 135 self.arguments = [x['arguments'] for x in gold_event if x['event type']==event_type] 136 else: 137 # trigger base 138 self.trigger_text = gold_event['trigger text'] 139 self.trigger_span = [gold_event['trigger span']] 140 self.arguments = [gold_event['arguments']] 141 else: 142 self.gold_event = None 143 144 @classmethod 145 def get_keywords(self): 146 pass 147 148 def generate_pair(self, query_trigger): 149 """ 150 Generate model input sentence and output sentence pair 151 """ 152 input_str = self.generate_input_str(query_trigger) 153 output_str, gold_sample = self.generate_output_str(query_trigger) 154 return (input_str, output_str, self.gold_event, gold_sample, self.event_type, self.tokens) 155 156 def generate_input_str(self, query_trigger): 157 return None 158 159 def generate_output_str(self, query_trigger): 160 return (None, False) 161 162 def decode(self, prediction): 163 pass 164 165 def evaluate(self, predict_output): 166 assert self.gold_event is not None 167 # categorize prediction 168 pred_trigger = [] 169 pred_argument = [] 170 for pred in predict_output: 171 if pred[1] == self.event_type: 172 pred_trigger.append(pred) 173 else: 174 pred_argument.append(pred) 175 # trigger score 176 gold_tri_num = len(self.trigger_span) 177 pred_tris = [] 178 for pred in pred_trigger: 179 pred_span = self.predstr2span(pred[0]) 180 if pred_span[0] > -1: 181 pred_tris.append((pred_span[0], pred_span[1], pred[1])) 182 pred_tri_num = len(pred_tris) 183 match_tri = 0 184 for pred in pred_tris: 185 id_flag = False 186 for gold_span in self.trigger_span: 187 if gold_span[0] == pred[0] and gold_span[1] == pred[1]: 188 id_flag = True 189 match_tri += int(id_flag) 190 191 # argument score 192 converted_gold = self.get_converted_gold() 193 gold_arg_num = len(converted_gold) 194 pred_arg = [] 195 for pred in pred_argument: 196 # find corresponding trigger 197 pred_span = None 198 if isinstance(self.gold_event, list): 199 # end2end case 200 try: 201 # we need this ``try'' because we cannot gurantee the model will be bug-free on the matching 202 cor_tri = pred_trigger[pred[2]['cor tri cnt']] 203 cor_tri_span = self.predstr2span(cor_tri[0])[0] 204 if cor_tri_span > -1: 205 pred_span = self.predstr2span(pred[0], cor_tri_span) 206 else: 207 continue 208 except Exception as e: 209 print(e) 210 else: 211 # argument only case 212 pred_span = self.predstr2span(pred[0], self.trigger_span[0][0]) 213 if (pred_span is not None) and (pred_span[0] > -1): 214 pred_arg.append((pred_span[0], pred_span[1], pred[1])) 215 pred_arg = list(set(pred_arg)) 216 pred_arg_num = len(pred_arg) 217 218 target = converted_gold 219 match_id = 0 220 match_type = 0 221 for pred in pred_arg: 222 id_flag = False 223 id_type = False 224 for gold in target: 225 if gold[0]==pred[0] and gold[1]==pred[1]: 226 id_flag = True 227 if gold[2] == pred[2]: 228 id_type = True 229 break 230 match_id += int(id_flag) 231 match_type += int(id_type) 232 return { 233 'gold_tri_num': gold_tri_num, 234 'pred_tri_num': pred_tri_num, 235 'match_tri_num': match_tri, 236 'gold_arg_num': gold_arg_num, 237 'pred_arg_num': pred_arg_num, 238 'match_arg_id': match_id, 239 'match_arg_cls': match_type 240 } 241 242 def get_converted_gold(self): 243 converted_gold = [] 244 for argu in self.arguments: 245 for arg_type, arg_list in argu.items(): 246 for arg in arg_list: 247 converted_gold.append((arg['argument span'][0], arg['argument span'][1], arg_type)) 248 return list(set(converted_gold)) 249 250 def predstr2span(self, pred_str, trigger_idx=None): 251 sub_words = [_.strip() for _ in pred_str.strip().lower().split()] 252 candidates=[] 253 for i in range(len(self.tokens)): 254 j = 0 255 while j < len(sub_words) and i+j < len(self.tokens): 256 if self.tokens[i+j].lower() == sub_words[j]: 257 j += 1 258 else: 259 break 260 if j == len(sub_words): 261 candidates.append((i, i+len(sub_words))) 262 if len(candidates) < 1: 263 return -1, -1 264 else: 265 if trigger_idx is not None: 266 return sorted(candidates, key=lambda x: abs(trigger_idx-x[0]))[0] 267 else: 268 return candidates[0] 269 270 class Life_Be_Born(event_template): 271 272 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 273 super().__init__(input_style, output_style, passage, event_type, gold_event) 274 275 @classmethod 276 def get_keywords(self): 277 return ['born', 'birth', 'bore'] 278 279 def get_output_template(self): 280 output_template = '' 281 for o_style in OUTPUT_STYLE_SET: 282 if o_style in self.output_style: 283 if o_style == 'trigger:sentence': 284 output_template += ' \n {}'.format('Event trigger is <Trigger>') 285 if o_style == 'argument:sentence': 286 output_template += ' \n {}'.format('somebody was born in somewhere.') 287 return ('\n'.join(output_template.split('\n')[1:])).strip() 288 289 def generate_input_str(self, query_trigger): 290 input_str = self.passage 291 for i_style in INPUT_STYLE_SET: 292 if i_style in self.input_style: 293 if i_style == 'event_type': 294 input_str += ' \n {}'.format('life event, be-born sub-type') 295 if i_style == 'event_type_sent': 296 input_str += ' \n {}'.format('The event is related to life and someone is given birth to.') 297 if i_style == 'keywords': 298 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 299 if i_style == 'triggers': 300 input_str += ' \n The event trigger word is {}'.format(query_trigger) 301 if i_style == 'template': 302 input_str += ' \n {}'.format(self.output_template) 303 304 return input_str 305 306 def generate_output_str(self, query_trigger): 307 assert self.gold_event is not None 308 output_str = '' 309 gold_sample = False 310 for o_style in OUTPUT_STYLE_SET: 311 if o_style in self.output_style: 312 if o_style == 'trigger:sentence': 313 if self.trigger_text != '': 314 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 315 gold_sample = True 316 else: 317 output_str += ' \n Event trigger is <Trigger>' 318 if o_style == 'argument:sentence': 319 output_texts = [] 320 for argu in self.arguments: 321 filler = ( 322 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 323 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 324 ) 325 output_texts.append("{} was born in {}.".format(*filler)) 326 gold_sample = True 327 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 328 329 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 330 return (output_str, gold_sample) 331 332 def decode(self, preds): 333 output = [] 334 for cnt, pred in enumerate(preds.split('\n')): 335 used_o_cnt = 0 336 full_pred = pred.strip() 337 for o_style in OUTPUT_STYLE_SET: 338 if o_style in self.output_style: 339 if o_style == 'trigger:sentence': 340 if used_o_cnt == cnt: 341 try: 342 triggers = full_pred.split('Event trigger is ', 1)[1] 343 triggers = triggers.split(' and ') 344 for t_cnt, t in enumerate(triggers): 345 if t != '<Trigger>': 346 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 347 except: 348 pass 349 used_o_cnt += 1 350 if o_style == 'argument:sentence': 351 if used_o_cnt == cnt: 352 try: 353 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 354 person = prediction.split(' was born in ', 1)[0] 355 person = person.split(' and ') 356 place = prediction.split(' was born in ', 1)[1].rsplit('.', 1)[0] 357 place = place.split(' and ') 358 for arg in person: 359 if arg != ROLE_PH_MAP['Person']: 360 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 361 for arg in place: 362 if arg != ROLE_PH_MAP['Place']: 363 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 364 except: 365 pass 366 used_o_cnt += 1 367 368 return output 369 370 class Life_Marry(event_template): 371 372 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 373 super().__init__(input_style, output_style, passage, event_type, gold_event) 374 375 @classmethod 376 def get_keywords(self): 377 return ['marry', 'marriage', 'married'] 378 379 def get_output_template(self): 380 output_template = '' 381 for o_style in OUTPUT_STYLE_SET: 382 if o_style in self.output_style: 383 if o_style == 'trigger:sentence': 384 output_template += ' \n {}'.format('Event trigger is <Trigger>') 385 if o_style == 'argument:sentence': 386 output_template += ' \n {}'.format('somebody got married in somewhere.') 387 return ('\n'.join(output_template.split('\n')[1:])).strip() 388 389 def generate_input_str(self, query_trigger): 390 input_str = self.passage 391 for i_style in INPUT_STYLE_SET: 392 if i_style in self.input_style: 393 if i_style == 'event_type': 394 input_str += ' \n {}'.format('life event, marry sub-type') 395 if i_style == 'event_type_sent': 396 input_str += ' \n {}'.format('The event is related to life and someone is married.') 397 if i_style == 'keywords': 398 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 399 if i_style == 'triggers': 400 input_str += ' \n The event trigger word is {}'.format(query_trigger) 401 if i_style == 'template': 402 input_str += ' \n {}'.format(self.output_template) 403 return input_str 404 405 def generate_output_str(self, query_trigger): 406 assert self.gold_event is not None 407 output_str = '' 408 gold_sample = False 409 for o_style in OUTPUT_STYLE_SET: 410 if o_style in self.output_style: 411 if o_style == 'trigger:sentence': 412 if self.trigger_text != '': 413 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 414 gold_sample = True 415 else: 416 output_str += ' \n Event trigger is <Trigger>' 417 if o_style == 'argument:sentence': 418 output_texts = [] 419 for argu in self.arguments: 420 filler = ( 421 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 422 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 423 ) 424 output_texts.append("{} got married in {}.".format(*filler)) 425 gold_sample = True 426 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 427 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 428 return (output_str, gold_sample) 429 430 def decode(self, preds): 431 output = [] 432 for cnt, pred in enumerate(preds.split('\n')): 433 used_o_cnt = 0 434 full_pred = pred.strip() 435 for o_style in OUTPUT_STYLE_SET: 436 if o_style in self.output_style: 437 if o_style == 'trigger:sentence': 438 if used_o_cnt == cnt: 439 try: 440 triggers = full_pred.split('Event trigger is ', 1)[1] 441 triggers = triggers.split(' and ') 442 for t_cnt, t in enumerate(triggers): 443 if t != '<Trigger>': 444 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 445 except: 446 pass 447 used_o_cnt += 1 448 if o_style == 'argument:sentence': 449 if used_o_cnt == cnt: 450 try: 451 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 452 person = prediction.split(' got married in ', 1)[0] 453 person = person.split(' and ') 454 place = prediction.split(' got married in ', 1)[1].rsplit('.', 1)[0] 455 place = place.split(' and ') 456 for arg in person: 457 if arg != ROLE_PH_MAP['Person']: 458 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 459 for arg in place: 460 if arg != ROLE_PH_MAP['Place']: 461 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 462 except: 463 pass 464 used_o_cnt += 1 465 return output 466 467 class Life_Divorce(event_template): 468 469 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 470 super().__init__(input_style, output_style, passage, event_type, gold_event) 471 472 @classmethod 473 def get_keywords(self): 474 return ['divorce', 'divorced', 'Divorce'] 475 476 def get_output_template(self): 477 output_template = '' 478 for o_style in OUTPUT_STYLE_SET: 479 if o_style in self.output_style: 480 if o_style == 'trigger:sentence': 481 output_template += ' \n {}'.format('Event trigger is <Trigger>') 482 if o_style == 'argument:sentence': 483 output_template += ' \n {}'.format('somebody divorced in somewhere.') 484 return ('\n'.join(output_template.split('\n')[1:])).strip() 485 486 def generate_input_str(self, query_trigger): 487 input_str = self.passage 488 for i_style in INPUT_STYLE_SET: 489 if i_style in self.input_style: 490 if i_style == 'event_type': 491 input_str += ' \n {}'.format('life event, divorce sub-type') 492 if i_style == 'event_type_sent': 493 input_str += ' \n {}'.format('The event is related to life and someone was divorced.') 494 if i_style == 'keywords': 495 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 496 if i_style == 'triggers': 497 input_str += ' \n The event trigger word is {}'.format(query_trigger) 498 if i_style == 'template': 499 input_str += ' \n {}'.format(self.output_template) 500 501 return input_str 502 503 def generate_output_str(self, query_trigger): 504 assert self.gold_event is not None 505 output_str = '' 506 gold_sample = False 507 for o_style in OUTPUT_STYLE_SET: 508 if o_style in self.output_style: 509 if o_style == 'trigger:sentence': 510 if self.trigger_text != '': 511 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 512 gold_sample = True 513 else: 514 output_str += ' \n Event trigger is <Trigger>' 515 if o_style == 'argument:sentence': 516 output_texts = [] 517 for argu in self.arguments: 518 filler = ( 519 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 520 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 521 ) 522 output_texts.append("{} divorced in {}.".format(*filler)) 523 gold_sample = True 524 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 525 526 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 527 return (output_str, gold_sample) 528 529 def decode(self, preds): 530 output = [] 531 for cnt, pred in enumerate(preds.split('\n')): 532 used_o_cnt = 0 533 full_pred = pred.strip() 534 for o_style in OUTPUT_STYLE_SET: 535 if o_style in self.output_style: 536 if o_style == 'trigger:sentence': 537 if used_o_cnt == cnt: 538 try: 539 triggers = full_pred.split('Event trigger is ', 1)[1] 540 triggers = triggers.split(' and ') 541 for t_cnt, t in enumerate(triggers): 542 if t != '<Trigger>': 543 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 544 except: 545 pass 546 used_o_cnt += 1 547 if o_style == 'argument:sentence': 548 if used_o_cnt == cnt: 549 try: 550 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 551 person = prediction.split(' divorced in ', 1)[0] 552 person = person.split(' and ') 553 place = prediction.split(' divorced in ', 1)[1].rsplit('.', 1)[0] 554 place = place.split(' and ') 555 for arg in person: 556 if arg != ROLE_PH_MAP['Person']: 557 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 558 for arg in place: 559 if arg != ROLE_PH_MAP['Place']: 560 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 561 except: 562 pass 563 used_o_cnt += 1 564 565 return output 566 567 class Life_Injure(event_template): 568 569 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 570 super().__init__(input_style, output_style, passage, event_type, gold_event) 571 572 @classmethod 573 def get_keywords(self): 574 return ['injure', 'wounded', 'hurt'] 575 576 def get_output_template(self): 577 output_template = '' 578 for o_style in OUTPUT_STYLE_SET: 579 if o_style in self.output_style: 580 if o_style == 'trigger:sentence': 581 output_template += ' \n {}'.format('Event trigger is <Trigger>') 582 if o_style == 'argument:sentence': 583 output_template += ' \n {}'.format('somebody or some organization led to some victim injured by some way in somewhere.') 584 return ('\n'.join(output_template.split('\n')[1:])).strip() 585 586 def generate_input_str(self, query_trigger): 587 input_str = self.passage 588 for i_style in INPUT_STYLE_SET: 589 if i_style in self.input_style: 590 if i_style == 'event_type': 591 input_str += ' \n {}'.format('life event, injure sub-type') 592 if i_style == 'event_type_sent': 593 input_str += ' \n {}'.format('The event is related to life and someone is injured.') 594 if i_style == 'keywords': 595 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 596 if i_style == 'triggers': 597 input_str += ' \n The event trigger word is {}'.format(query_trigger) 598 if i_style == 'template': 599 input_str += ' \n {}'.format(self.output_template) 600 601 return input_str 602 603 def generate_output_str(self, query_trigger): 604 assert self.gold_event is not None 605 output_str = '' 606 gold_sample = False 607 for o_style in OUTPUT_STYLE_SET: 608 if o_style in self.output_style: 609 if o_style == 'trigger:sentence': 610 if self.trigger_text != '': 611 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 612 gold_sample = True 613 else: 614 output_str += ' \n Event trigger is <Trigger>' 615 if o_style == 'argument:sentence': 616 output_texts = [] 617 for argu in self.arguments: 618 filler = ( 619 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'], 620 " and ".join([ a['argument text'] for a in argu['Victim']]) if "Victim" in argu.keys() else ROLE_PH_MAP['Victim'], 621 " and ".join([ a['argument text'] for a in argu['Instrument']]) if "Instrument" in argu.keys() else ROLE_PH_MAP['Instrument'], 622 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 623 ) 624 output_texts.append("{} led to {} injured by {} in {}.".format(*filler)) 625 gold_sample = True 626 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 627 628 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 629 return (output_str, gold_sample) 630 631 def decode(self, preds): 632 output = [] 633 for cnt, pred in enumerate(preds.split('\n')): 634 used_o_cnt = 0 635 full_pred = pred.strip() 636 for o_style in OUTPUT_STYLE_SET: 637 if o_style in self.output_style: 638 if o_style == 'trigger:sentence': 639 if used_o_cnt == cnt: 640 try: 641 triggers = full_pred.split('Event trigger is ', 1)[1] 642 triggers = triggers.split(' and ') 643 for t_cnt, t in enumerate(triggers): 644 if t != '<Trigger>': 645 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 646 except: 647 pass 648 used_o_cnt += 1 649 if o_style == 'argument:sentence': 650 if used_o_cnt == cnt: 651 try: 652 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 653 agent = prediction.split(' led to ', 1)[0] 654 agent = agent.split(' and ') 655 victim = (prediction.split(' led to ', 1)[1]).split(' injured by ', 1)[0] 656 victim = victim.split(' and ') 657 instrument = ((prediction.split(' led to ', 1)[1]).split(' injured by ', 1)[1]).split(' in ', )[0] 658 instrument = instrument.split(' and ') 659 place = ((prediction.split(' led to ', 1)[1]).split(' injured by ', 1)[1]).split(' in ', 1)[1].rsplit('.',1)[0] 660 place = place.split(' and ') 661 for arg in agent: 662 if arg != ROLE_PH_MAP['Agent']: 663 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 664 for arg in victim: 665 if arg != ROLE_PH_MAP['Victim']: 666 output.append((arg, 'Victim', {'cor tri cnt': a_cnt})) 667 for arg in instrument: 668 if arg != ROLE_PH_MAP['Instrument']: 669 output.append((arg, 'Instrument', {'cor tri cnt': a_cnt})) 670 for arg in place: 671 if arg != ROLE_PH_MAP['Place']: 672 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 673 except Exception as e: 674 pass 675 used_o_cnt += 1 676 677 return output 678 679 class Life_Die(event_template): 680 681 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 682 super().__init__(input_style, output_style, passage, event_type, gold_event) 683 684 @classmethod 685 def get_keywords(self): 686 return ['kill', 'death', 'assassination'] 687 688 def get_output_template(self): 689 output_template = '' 690 for o_style in OUTPUT_STYLE_SET: 691 if o_style in self.output_style: 692 if o_style == 'trigger:sentence': 693 output_template += ' \n {}'.format('Event trigger is <Trigger>') 694 if o_style == 'argument:sentence': 695 output_template += ' \n {}'.format('somebody or some organization led to some victim died by some way in somewhere.') 696 return ('\n'.join(output_template.split('\n')[1:])).strip() 697 698 def generate_input_str(self, query_trigger): 699 input_str = self.passage 700 for i_style in INPUT_STYLE_SET: 701 if i_style in self.input_style: 702 if i_style == 'event_type': 703 input_str += ' \n {}'.format('life event, die sub-type') 704 if i_style == 'event_type_sent': 705 input_str += ' \n {}'.format('The event is related to life and someone died.') 706 if i_style == 'keywords': 707 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 708 if i_style == 'triggers': 709 input_str += ' \n The event trigger word is {}'.format(query_trigger) 710 if i_style == 'template': 711 input_str += ' \n {}'.format(self.output_template) 712 713 return input_str 714 715 def generate_output_str(self, query_trigger): 716 assert self.gold_event is not None 717 output_str = '' 718 gold_sample = False 719 for o_style in OUTPUT_STYLE_SET: 720 if o_style in self.output_style: 721 if o_style == 'trigger:sentence': 722 if self.trigger_text != '': 723 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 724 gold_sample = True 725 else: 726 output_str += ' \n Event trigger is <Trigger>' 727 if o_style == 'argument:sentence': 728 output_texts = [] 729 for argu in self.arguments: 730 filler = ( 731 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'], 732 " and ".join([ a['argument text'] for a in argu['Victim']]) if "Victim" in argu.keys() else ROLE_PH_MAP['Victim'], 733 " and ".join([ a['argument text'] for a in argu['Instrument']]) if "Instrument" in argu.keys() else ROLE_PH_MAP['Instrument'], 734 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 735 ) 736 output_texts.append("{} led to {} died by {} in {}.".format(*filler)) 737 gold_sample = True 738 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 739 740 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 741 return (output_str, gold_sample) 742 743 def decode(self, preds): 744 output = [] 745 for cnt, pred in enumerate(preds.split('\n')): 746 used_o_cnt = 0 747 full_pred = pred.strip() 748 for o_style in OUTPUT_STYLE_SET: 749 if o_style in self.output_style: 750 if o_style == 'trigger:sentence': 751 if used_o_cnt == cnt: 752 try: 753 triggers = full_pred.split('Event trigger is ', 1)[1] 754 triggers = triggers.split(' and ') 755 for t_cnt, t in enumerate(triggers): 756 if t != '<Trigger>': 757 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 758 except: 759 pass 760 used_o_cnt += 1 761 if o_style == 'argument:sentence': 762 if used_o_cnt == cnt: 763 try: 764 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 765 agent = prediction.split(' led to ', 1)[0] 766 agent = agent.split(' and ') 767 victim = (prediction.split(' led to ', 1)[1]).split(' died by ', 1)[0] 768 victim = victim.split(' and ') 769 instrument = ((prediction.split(' led to ', 1)[1]).split(' died by ', 1)[1]).split(' in ', )[0] 770 instrument = instrument.split(' and ') 771 place = ((prediction.split(' led to ', 1)[1]).split(' died by ', 1)[1]).split(' in ', 1)[1].rsplit('.',1)[0] 772 place = place.split(' and ') 773 for arg in agent: 774 if arg != ROLE_PH_MAP['Agent']: 775 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 776 for arg in victim: 777 if arg != ROLE_PH_MAP['Victim']: 778 output.append((arg, 'Victim', {'cor tri cnt': a_cnt})) 779 for arg in instrument: 780 if arg != ROLE_PH_MAP['Instrument']: 781 output.append((arg, 'Instrument', {'cor tri cnt': a_cnt})) 782 for arg in place: 783 if arg != ROLE_PH_MAP['Place']: 784 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 785 except Exception as e: 786 pass 787 used_o_cnt += 1 788 return output 789 790 class Movement_Transport(event_template): 791 792 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 793 super().__init__(input_style, output_style, passage, event_type, gold_event) 794 795 @classmethod 796 def get_keywords(self): 797 return ['travel', 'go', 'move'] 798 799 def get_output_template(self): 800 output_template = '' 801 for o_style in OUTPUT_STYLE_SET: 802 if o_style in self.output_style: 803 if o_style == 'trigger:sentence': 804 output_template += ' \n {}'.format('Event trigger is <Trigger>') 805 if o_style == 'argument:sentence': 806 output_template += ' \n {}'.format('something was sent to somewhere from some place by some vehicle. somebody or some organization was responsible for the transport.') 807 return ('\n'.join(output_template.split('\n')[1:])).strip() 808 809 def generate_input_str(self, query_trigger): 810 input_str = self.passage 811 for i_style in INPUT_STYLE_SET: 812 if i_style in self.input_style: 813 if i_style == 'event_type': 814 input_str += ' \n {}'.format('movement event, transport sub-type') 815 if i_style == 'event_type_sent': 816 input_str += ' \n {}'.format('The event is related to movement. The event occurs when a weapon or vehicle is moved from one place to another.') 817 if i_style == 'keywords': 818 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 819 if i_style == 'triggers': 820 input_str += ' \n The event trigger word is {}'.format(query_trigger) 821 if i_style == 'template': 822 input_str += ' \n {}'.format(self.output_template) 823 824 return input_str 825 826 def generate_output_str(self, query_trigger): 827 assert self.gold_event is not None 828 output_str = '' 829 gold_sample = False 830 for o_style in OUTPUT_STYLE_SET: 831 if o_style in self.output_style: 832 if o_style == 'trigger:sentence': 833 if self.trigger_text != '': 834 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 835 gold_sample = True 836 else: 837 output_str += ' \n Event trigger is <Trigger>' 838 if o_style == 'argument:sentence': 839 output_texts = [] 840 for argu in self.arguments: 841 filler = ( 842 " and ".join([ a['argument text'] for a in argu['Artifact']]) if "Artifact" in argu.keys() else ROLE_PH_MAP['Artifact'], 843 " and ".join([ a['argument text'] for a in argu['Destination']]) if "Destination" in argu.keys() else ROLE_PH_MAP['Destination'], 844 " and ".join([ a['argument text'] for a in argu['Origin']]) if "Origin" in argu.keys() else ROLE_PH_MAP['Origin'], 845 " and ".join([ a['argument text'] for a in argu['Vehicle']]) if "Vehicle" in argu.keys() else ROLE_PH_MAP['Vehicle'], 846 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'] 847 ) 848 output_texts.append("{} was sent to {} from {} by {}. {} was responsible for the transport.".format(*filler)) 849 gold_sample = True 850 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 851 852 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 853 return (output_str, gold_sample) 854 855 def decode(self, preds): 856 output = [] 857 for cnt, pred in enumerate(preds.split('\n')): 858 used_o_cnt = 0 859 full_pred = pred.strip() 860 for o_style in OUTPUT_STYLE_SET: 861 if o_style in self.output_style: 862 if o_style == 'trigger:sentence': 863 if used_o_cnt == cnt: 864 try: 865 triggers = full_pred.split('Event trigger is ', 1)[1] 866 triggers = triggers.split(' and ') 867 for t_cnt, t in enumerate(triggers): 868 if t != '<Trigger>': 869 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 870 except: 871 pass 872 used_o_cnt += 1 873 if o_style == 'argument:sentence': 874 if used_o_cnt == cnt: 875 try: 876 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 877 artifact = prediction.split(' was sent to ', 1)[0] 878 artifact = artifact.split(' and ') 879 destination = (prediction.split(' was sent to ', 1)[1]).split(' from ', 1)[0] 880 destination = destination.split(' and ') 881 origin = ((prediction.split(' was sent to ', 1)[1]).split(' from ', 1)[1]).split(' by ', 1)[0] 882 origin = origin.split(' and ') 883 vehicle = (((prediction.split(' was sent to ', 1)[1]).split(' from ', 1)[1]).split(' by ', 1)[1]).split('.', 1)[0] 884 vehicle = vehicle.split(' and ') 885 remain = (((prediction.split(' was sent to ', 1)[1]).split(' from ', 1)[1]).split(' by ', 1)[1]).split('.', 1)[1] 886 if 'was responsible for the transport' in remain: 887 agent = (remain.split(' was responsible for the transport.')[0]).strip() 888 agent = agent.split(' and ') 889 else: 890 agent = [] 891 892 for arg in agent: 893 if arg != ROLE_PH_MAP['Agent']: 894 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 895 for arg in artifact: 896 if arg != ROLE_PH_MAP['Artifact']: 897 output.append((arg, 'Artifact', {'cor tri cnt': a_cnt})) 898 for arg in destination: 899 if arg != ROLE_PH_MAP['Destination']: 900 output.append((arg, 'Destination', {'cor tri cnt': a_cnt})) 901 for arg in origin: 902 if arg != ROLE_PH_MAP['Origin']: 903 output.append((arg, 'Origin', {'cor tri cnt': a_cnt})) 904 for arg in vehicle: 905 if arg != ROLE_PH_MAP['Vehicle']: 906 output.append((arg, 'Vehicle', {'cor tri cnt': a_cnt})) 907 except: 908 pass 909 used_o_cnt += 1 910 911 return output 912 913 class Transaction_Transfer_Ownership(event_template): 914 915 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 916 super().__init__(input_style, output_style, passage, event_type, gold_event) 917 918 @classmethod 919 def get_keywords(self): 920 return ['sell', 'buy', 'acquire'] 921 922 def get_output_template(self): 923 output_template = '' 924 for o_style in OUTPUT_STYLE_SET: 925 if o_style in self.output_style: 926 if o_style == 'trigger:sentence': 927 output_template += ' \n {}'.format('Event trigger is <Trigger>') 928 if o_style == 'argument:sentence': 929 output_template += ' \n {}'.format('someone got something from some seller in somewhere.') 930 return ('\n'.join(output_template.split('\n')[1:])).strip() 931 932 def generate_input_str(self, query_trigger): 933 input_str = self.passage 934 for i_style in INPUT_STYLE_SET: 935 if i_style in self.input_style: 936 if i_style == 'event_type': 937 input_str += ' \n {}'.format('transaction event, transfer ownership sub-type') 938 if i_style == 'event_type_sent': 939 input_str += ' \n {}'.format('The event is related to transaction. The event occurs when an item or an organization is sold or gave to some other.') 940 if i_style == 'keywords': 941 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 942 if i_style == 'triggers': 943 input_str += ' \n The event trigger word is {}'.format(query_trigger) 944 if i_style == 'template': 945 input_str += ' \n {}'.format(self.output_template) 946 947 return input_str 948 949 def generate_output_str(self, query_trigger): 950 assert self.gold_event is not None 951 output_str = '' 952 gold_sample = False 953 for o_style in OUTPUT_STYLE_SET: 954 if o_style in self.output_style: 955 if o_style == 'trigger:sentence': 956 if self.trigger_text != '': 957 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 958 gold_sample = True 959 else: 960 output_str += ' \n Event trigger is <Trigger>' 961 if o_style == 'argument:sentence': 962 output_texts = [] 963 for argu in self.arguments: 964 buyer_list = [] 965 if "Beneficiary" in argu.keys(): 966 buyer_list.extend([ a['argument text'] for a in argu['Beneficiary']]) 967 if "Buyer" in argu.keys(): 968 buyer_list.extend([ a['argument text'] for a in argu['Buyer']]) 969 filler = ( 970 " and ".join(buyer_list) if len(buyer_list)>0 else ROLE_PH_MAP['Buyer'], 971 " and ".join([ a['argument text'] for a in argu['Artifact']]) if "Artifact" in argu.keys() else ROLE_PH_MAP['Artifact'], 972 " and ".join([ a['argument text'] for a in argu['Seller']]) if "Seller" in argu.keys() else ROLE_PH_MAP['Seller'], 973 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 974 ) 975 output_texts.append("{} got {} from {} in {}.".format(*filler)) 976 gold_sample = True 977 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 978 979 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 980 return (output_str, gold_sample) 981 982 def decode(self, preds): 983 output = [] 984 for cnt, pred in enumerate(preds.split('\n')): 985 used_o_cnt = 0 986 full_pred = pred.strip() 987 for o_style in OUTPUT_STYLE_SET: 988 if o_style in self.output_style: 989 if o_style == 'trigger:sentence': 990 if used_o_cnt == cnt: 991 try: 992 triggers = full_pred.split('Event trigger is ', 1)[1] 993 triggers = triggers.split(' and ') 994 for t_cnt, t in enumerate(triggers): 995 if t != '<Trigger>': 996 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 997 except: 998 pass 999 used_o_cnt += 1 1000 if o_style == 'argument:sentence': 1001 if used_o_cnt == cnt: 1002 try: 1003 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1004 buyer = prediction.split(' got ', 1)[0] 1005 buyer = buyer.split(' and ') 1006 remain = prediction.split(' got ', 1)[1] 1007 1008 artifact = remain.split(' from ', 1)[0] 1009 artifact = artifact.split(' and ') 1010 remain = remain.split(' from ', 1)[1] 1011 1012 seller = remain.split(' in ', 1)[0] 1013 seller = seller.split(' and ') 1014 remain = remain.split(' in ', 1)[1] 1015 1016 place = remain.rsplit('.',1)[0] 1017 place = place.split(' and ') 1018 1019 for arg in buyer: 1020 if arg != ROLE_PH_MAP['Buyer']: 1021 output.append((arg, 'Buyer', {'cor tri cnt': a_cnt})) 1022 for arg in artifact: 1023 if arg != ROLE_PH_MAP['Artifact']: 1024 output.append((arg, 'Artifact', {'cor tri cnt': a_cnt})) 1025 for arg in seller: 1026 if arg != ROLE_PH_MAP['Seller']: 1027 output.append((arg, 'Seller', {'cor tri cnt': a_cnt})) 1028 for arg in place: 1029 if arg != ROLE_PH_MAP['Place']: 1030 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1031 except: 1032 pass 1033 used_o_cnt += 1 1034 1035 return output 1036 1037 class Transaction_Transfer_Money(event_template): 1038 1039 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1040 super().__init__(input_style, output_style, passage, event_type, gold_event) 1041 1042 @classmethod 1043 def get_keywords(self): 1044 return ['pay', 'donation', 'loan'] 1045 1046 def get_output_template(self): 1047 output_template = '' 1048 for o_style in OUTPUT_STYLE_SET: 1049 if o_style in self.output_style: 1050 if o_style == 'trigger:sentence': 1051 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1052 if o_style == 'argument:sentence': 1053 output_template += ' \n {}'.format('someone paid some other in somewhere.') 1054 return ('\n'.join(output_template.split('\n')[1:])).strip() 1055 1056 def generate_input_str(self, query_trigger): 1057 input_str = self.passage 1058 for i_style in INPUT_STYLE_SET: 1059 if i_style in self.input_style: 1060 if i_style == 'event_type': 1061 input_str += ' \n {}'.format('transaction event, transfer money sub-type') 1062 if i_style == 'event_type_sent': 1063 input_str += ' \n {}'.format('The event is related to transaction. The event occurs when someone is giving, receiving, borrowing, or lending money.') 1064 if i_style == 'keywords': 1065 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1066 if i_style == 'triggers': 1067 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1068 if i_style == 'template': 1069 input_str += ' \n {}'.format(self.output_template) 1070 1071 return input_str 1072 1073 def generate_output_str(self, query_trigger): 1074 assert self.gold_event is not None 1075 output_str = '' 1076 gold_sample = False 1077 for o_style in OUTPUT_STYLE_SET: 1078 if o_style in self.output_style: 1079 if o_style == 'trigger:sentence': 1080 if self.trigger_text != '': 1081 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1082 gold_sample = True 1083 else: 1084 output_str += ' \n Event trigger is <Trigger>' 1085 if o_style == 'argument:sentence': 1086 output_texts = [] 1087 for argu in self.arguments: 1088 recipient_list = [] 1089 if "Recipient" in argu.keys(): 1090 recipient_list.extend([ a['argument text'] for a in argu['Recipient']]) 1091 if "Beneficiary" in argu.keys(): 1092 recipient_list.extend([ a['argument text'] for a in argu['Beneficiary']]) 1093 filler = ( 1094 " and ".join([ a['argument text'] for a in argu['Giver']]) if "Giver" in argu.keys() else ROLE_PH_MAP['Giver'], 1095 " and ".join(recipient_list) if len(recipient_list)>0 else ROLE_PH_MAP['Recipient'], 1096 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1097 ) 1098 output_texts.append("{} paid {} in {}.".format(*filler)) 1099 gold_sample = True 1100 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1101 1102 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1103 return (output_str, gold_sample) 1104 1105 def decode(self, preds): 1106 output = [] 1107 for cnt, pred in enumerate(preds.split('\n')): 1108 used_o_cnt = 0 1109 full_pred = pred.strip() 1110 for o_style in OUTPUT_STYLE_SET: 1111 if o_style in self.output_style: 1112 if o_style == 'trigger:sentence': 1113 if used_o_cnt == cnt: 1114 try: 1115 triggers = full_pred.split('Event trigger is ', 1)[1] 1116 triggers = triggers.split(' and ') 1117 for t_cnt, t in enumerate(triggers): 1118 if t != '<Trigger>': 1119 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1120 except: 1121 pass 1122 used_o_cnt += 1 1123 if o_style == 'argument:sentence': 1124 if used_o_cnt == cnt: 1125 try: 1126 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1127 giver = prediction.split(' paid ', 1)[0] 1128 giver = giver.split(' and ') 1129 remain = prediction.split(' paid ', 1)[1] 1130 1131 recipient = remain.split(' in ', 1)[0] 1132 recipient = recipient.split(' and ') 1133 remain = remain.split(' in ', 1)[1] 1134 1135 place = remain.rsplit('.',1)[0] 1136 place = place.split(' and ') 1137 1138 for arg in giver: 1139 if arg != ROLE_PH_MAP['Giver']: 1140 output.append((arg, 'Giver', {'cor tri cnt': a_cnt})) 1141 for arg in recipient: 1142 if arg != ROLE_PH_MAP['Recipient']: 1143 output.append((arg, 'Recipient', {'cor tri cnt': a_cnt})) 1144 for arg in place: 1145 if arg != ROLE_PH_MAP['Place']: 1146 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1147 except: 1148 pass 1149 used_o_cnt += 1 1150 1151 return output 1152 1153 class Business_Start_Org(event_template): 1154 1155 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1156 super().__init__(input_style, output_style, passage, event_type, gold_event) 1157 1158 @classmethod 1159 def get_keywords(self): 1160 return ['founded', 'create', 'launch'] 1161 1162 def get_output_template(self): 1163 output_template = '' 1164 for o_style in OUTPUT_STYLE_SET: 1165 if o_style in self.output_style: 1166 if o_style == 'trigger:sentence': 1167 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1168 if o_style == 'argument:sentence': 1169 output_template += ' \n {}'.format('somebody or some organization launched some organization in somewhere.') 1170 return ('\n'.join(output_template.split('\n')[1:])).strip() 1171 1172 def generate_input_str(self, query_trigger): 1173 input_str = self.passage 1174 for i_style in INPUT_STYLE_SET: 1175 if i_style in self.input_style: 1176 if i_style == 'event_type': 1177 input_str += ' \n {}'.format('business event, start organization sub-type') 1178 if i_style == 'event_type_sent': 1179 input_str += ' \n {}'.format('The event is related to a new organization being created.') 1180 if i_style == 'keywords': 1181 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1182 if i_style == 'triggers': 1183 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1184 if i_style == 'template': 1185 input_str += ' \n {}'.format(self.output_template) 1186 1187 return input_str 1188 1189 def generate_output_str(self, query_trigger): 1190 assert self.gold_event is not None 1191 output_str = '' 1192 gold_sample = False 1193 for o_style in OUTPUT_STYLE_SET: 1194 if o_style in self.output_style: 1195 if o_style == 'trigger:sentence': 1196 if self.trigger_text != '': 1197 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1198 gold_sample = True 1199 else: 1200 output_str += ' \n Event trigger is <Trigger>' 1201 if o_style == 'argument:sentence': 1202 output_texts = [] 1203 for argu in self.arguments: 1204 filler = ( 1205 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'], 1206 " and ".join([ a['argument text'] for a in argu['Org']]) if "Org" in argu.keys() else ROLE_PH_MAP['Org'], 1207 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1208 ) 1209 output_texts.append("{} launched {} in {}.".format(*filler)) 1210 gold_sample = True 1211 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1212 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1213 return (output_str, gold_sample) 1214 1215 def decode(self, preds): 1216 output = [] 1217 for cnt, pred in enumerate(preds.split('\n')): 1218 used_o_cnt = 0 1219 full_pred = pred.strip() 1220 for o_style in OUTPUT_STYLE_SET: 1221 if o_style in self.output_style: 1222 if o_style == 'trigger:sentence': 1223 if used_o_cnt == cnt: 1224 try: 1225 triggers = full_pred.split('Event trigger is ', 1)[1] 1226 triggers = triggers.split(' and ') 1227 for t_cnt, t in enumerate(triggers): 1228 if t != '<Trigger>': 1229 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1230 except: 1231 pass 1232 used_o_cnt += 1 1233 if o_style == 'argument:sentence': 1234 if used_o_cnt == cnt: 1235 try: 1236 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1237 agent = prediction.split(' launched ', 1)[0] 1238 agent = agent.split(' and ') 1239 remain = prediction.split(' launched ', 1)[1] 1240 1241 org = remain.split(' in ', 1)[0] 1242 org = org.split(' and ') 1243 remain = remain.split(' in ', 1)[1] 1244 1245 place = remain.rsplit('.',1)[0] 1246 place = place.split(' and ') 1247 for arg in agent: 1248 if arg != ROLE_PH_MAP['Agent']: 1249 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 1250 for arg in org: 1251 if arg != ROLE_PH_MAP['Org']: 1252 output.append((arg, 'Org', {'cor tri cnt': a_cnt})) 1253 for arg in place: 1254 if arg != ROLE_PH_MAP['Place']: 1255 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1256 except: 1257 pass 1258 used_o_cnt += 1 1259 1260 return output 1261 1262 class Business_Merge_Org(event_template): 1263 1264 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1265 super().__init__(input_style, output_style, passage, event_type, gold_event) 1266 1267 @classmethod 1268 def get_keywords(self): 1269 return ['merge', 'merging', 'merger'] 1270 1271 def get_output_template(self): 1272 output_template = '' 1273 for o_style in OUTPUT_STYLE_SET: 1274 if o_style in self.output_style: 1275 if o_style == 'trigger:sentence': 1276 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1277 if o_style == 'argument:sentence': 1278 output_template += ' \n {}'.format('some organization was merged.') 1279 return ('\n'.join(output_template.split('\n')[1:])).strip() 1280 1281 def generate_input_str(self, query_trigger): 1282 input_str = self.passage 1283 for i_style in INPUT_STYLE_SET: 1284 if i_style in self.input_style: 1285 if i_style == 'event_type': 1286 input_str += ' \n {}'.format('business event, merge organization sub-type') 1287 if i_style == 'event_type_sent': 1288 input_str += ' \n {}'.format('The event is related to two or more organization coming together to form a new organization.') 1289 if i_style == 'keywords': 1290 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1291 if i_style == 'triggers': 1292 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1293 if i_style == 'template': 1294 input_str += ' \n {}'.format(self.output_template) 1295 1296 return input_str 1297 1298 def generate_output_str(self, query_trigger): 1299 assert self.gold_event is not None 1300 output_str = '' 1301 gold_sample = False 1302 for o_style in OUTPUT_STYLE_SET: 1303 if o_style in self.output_style: 1304 if o_style == 'trigger:sentence': 1305 if self.trigger_text != '': 1306 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1307 gold_sample = True 1308 else: 1309 output_str += ' \n Event trigger is <Trigger>' 1310 if o_style == 'argument:sentence': 1311 output_texts = [] 1312 for argu in self.arguments: 1313 filler = ( 1314 " and ".join([ a['argument text'] for a in argu['Org']]) if "Org" in argu.keys() else ROLE_PH_MAP['Org'] 1315 ) 1316 output_texts.append("{} was merged.".format(filler)) 1317 gold_sample = True 1318 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1319 1320 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1321 return (output_str, gold_sample) 1322 1323 def decode(self, preds): 1324 output = [] 1325 for cnt, pred in enumerate(preds.split('\n')): 1326 used_o_cnt = 0 1327 full_pred = pred.strip() 1328 for o_style in OUTPUT_STYLE_SET: 1329 if o_style in self.output_style: 1330 if o_style == 'trigger:sentence': 1331 if used_o_cnt == cnt: 1332 try: 1333 triggers = full_pred.split('Event trigger is ', 1)[1] 1334 triggers = triggers.split(' and ') 1335 for t_cnt, t in enumerate(triggers): 1336 if t != '<Trigger>': 1337 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1338 except: 1339 pass 1340 used_o_cnt += 1 1341 if o_style == 'argument:sentence': 1342 if used_o_cnt == cnt: 1343 try: 1344 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1345 org = prediction.split(' was merged.', 1)[0] 1346 org = org.split(' and ') 1347 for arg in org: 1348 if arg != ROLE_PH_MAP['Org']: 1349 output.append((arg, 'Org', {'cor tri cnt': a_cnt})) 1350 except: 1351 pass 1352 used_o_cnt += 1 1353 1354 return output 1355 1356 class Business_Declare_Bankruptcy(event_template): 1357 1358 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1359 super().__init__(input_style, output_style, passage, event_type, gold_event) 1360 1361 @classmethod 1362 def get_keywords(self): 1363 return ['bankruptcy', 'bankrupt', 'Bankruptcy'] 1364 1365 def get_output_template(self): 1366 output_template = '' 1367 for o_style in OUTPUT_STYLE_SET: 1368 if o_style in self.output_style: 1369 if o_style == 'trigger:sentence': 1370 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1371 if o_style == 'argument:sentence': 1372 output_template += ' \n {}'.format('some organization declared bankruptcy.') 1373 return ('\n'.join(output_template.split('\n')[1:])).strip() 1374 1375 def generate_input_str(self, query_trigger): 1376 input_str = self.passage 1377 for i_style in INPUT_STYLE_SET: 1378 if i_style in self.input_style: 1379 if i_style == 'event_type': 1380 input_str += ' \n {}'.format('business event, declare bankruptcy sub-type') 1381 if i_style == 'event_type_sent': 1382 input_str += ' \n {}'.format('The event is related to some organization declaring bankruptcy.') 1383 if i_style == 'keywords': 1384 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1385 if i_style == 'triggers': 1386 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1387 if i_style == 'template': 1388 input_str += ' \n {}'.format(self.output_template) 1389 1390 return input_str 1391 1392 def generate_output_str(self, query_trigger): 1393 assert self.gold_event is not None 1394 output_str = '' 1395 gold_sample = False 1396 for o_style in OUTPUT_STYLE_SET: 1397 if o_style in self.output_style: 1398 if o_style == 'trigger:sentence': 1399 if self.trigger_text != '': 1400 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1401 gold_sample = True 1402 else: 1403 output_str += ' \n Event trigger is <Trigger>' 1404 if o_style == 'argument:sentence': 1405 output_texts = [] 1406 for argu in self.arguments: 1407 filler = ( 1408 " and ".join([ a['argument text'] for a in argu['Org']]) if "Org" in argu.keys() else ROLE_PH_MAP['Org'] 1409 ) 1410 output_texts.append("{} declared bankruptcy.".format(filler)) 1411 gold_sample = True 1412 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1413 1414 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1415 return (output_str, gold_sample) 1416 1417 def decode(self, preds): 1418 output = [] 1419 for cnt, pred in enumerate(preds.split('\n')): 1420 used_o_cnt = 0 1421 full_pred = pred.strip() 1422 for o_style in OUTPUT_STYLE_SET: 1423 if o_style in self.output_style: 1424 if o_style == 'trigger:sentence': 1425 if used_o_cnt == cnt: 1426 try: 1427 triggers = full_pred.split('Event trigger is ', 1)[1] 1428 triggers = triggers.split(' and ') 1429 for t_cnt, t in enumerate(triggers): 1430 if t != '<Trigger>': 1431 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1432 except: 1433 pass 1434 used_o_cnt += 1 1435 if o_style == 'argument:sentence': 1436 if used_o_cnt == cnt: 1437 try: 1438 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1439 org = prediction.split(' declared bankruptcy.', 1)[0] 1440 org = org.split(' and ') 1441 for arg in org: 1442 if arg != ROLE_PH_MAP['Org']: 1443 output.append((arg, 'Org', {'cor tri cnt': a_cnt})) 1444 except: 1445 pass 1446 used_o_cnt += 1 1447 1448 return output 1449 1450 class Business_End_Org(event_template): 1451 1452 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1453 super().__init__(input_style, output_style, passage, event_type, gold_event) 1454 1455 @classmethod 1456 def get_keywords(self): 1457 return ['dissolve', 'disbanded', 'close'] 1458 1459 def get_output_template(self): 1460 output_template = '' 1461 for o_style in OUTPUT_STYLE_SET: 1462 if o_style in self.output_style: 1463 if o_style == 'trigger:sentence': 1464 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1465 if o_style == 'argument:sentence': 1466 output_template += ' \n {}'.format('some organization dissolved.') 1467 return ('\n'.join(output_template.split('\n')[1:])).strip() 1468 1469 def generate_input_str(self, query_trigger): 1470 input_str = self.passage 1471 for i_style in INPUT_STYLE_SET: 1472 if i_style in self.input_style: 1473 if i_style == 'event_type': 1474 input_str += ' \n {}'.format('business event, end organization sub-type') 1475 if i_style == 'event_type_sent': 1476 input_str += ' \n {}'.format('The event is related to some organization ceasing to exist.') 1477 if i_style == 'keywords': 1478 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1479 if i_style == 'triggers': 1480 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1481 if i_style == 'template': 1482 input_str += ' \n {}'.format(self.output_template) 1483 1484 return input_str 1485 1486 def generate_output_str(self, query_trigger): 1487 assert self.gold_event is not None 1488 output_str = '' 1489 gold_sample = False 1490 for o_style in OUTPUT_STYLE_SET: 1491 if o_style in self.output_style: 1492 if o_style == 'trigger:sentence': 1493 if self.trigger_text != '': 1494 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1495 gold_sample = True 1496 else: 1497 output_str += ' \n Event trigger is <Trigger>' 1498 if o_style == 'argument:sentence': 1499 output_texts = [] 1500 for argu in self.arguments: 1501 filler = ( 1502 " and ".join([ a['argument text'] for a in argu['Org']]) if "Org" in argu.keys() else ROLE_PH_MAP['Org'] 1503 ) 1504 output_texts.append("{} dissolved.".format(filler)) 1505 gold_sample = True 1506 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1507 1508 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1509 return (output_str, gold_sample) 1510 1511 def decode(self, preds): 1512 output = [] 1513 for cnt, pred in enumerate(preds.split('\n')): 1514 used_o_cnt = 0 1515 full_pred = pred.strip() 1516 for o_style in OUTPUT_STYLE_SET: 1517 if o_style in self.output_style: 1518 if o_style == 'trigger:sentence': 1519 if used_o_cnt == cnt: 1520 try: 1521 triggers = full_pred.split('Event trigger is ', 1)[1] 1522 triggers = triggers.split(' and ') 1523 for t_cnt, t in enumerate(triggers): 1524 if t != '<Trigger>': 1525 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1526 except: 1527 pass 1528 used_o_cnt += 1 1529 if o_style == 'argument:sentence': 1530 if used_o_cnt == cnt: 1531 try: 1532 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1533 org = prediction.split(' dissolved.', 1)[0] 1534 org = org.split(' and ') 1535 for arg in org: 1536 if arg != ROLE_PH_MAP['Org']: 1537 output.append((arg, 'Org', {'cor tri cnt': a_cnt})) 1538 except: 1539 pass 1540 used_o_cnt += 1 1541 1542 return output 1543 1544 class Conflict_Attack(event_template): 1545 1546 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1547 super().__init__(input_style, output_style, passage, event_type, gold_event) 1548 1549 @classmethod 1550 def get_keywords(self): 1551 return ['war', 'attack', 'terrorism'] 1552 1553 def get_output_template(self): 1554 output_template = '' 1555 for o_style in OUTPUT_STYLE_SET: 1556 if o_style in self.output_style: 1557 if o_style == 'trigger:sentence': 1558 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1559 if o_style == 'argument:sentence': 1560 output_template += ' \n {}'.format('some attacker attacked some facility, someone, or some organization by some way in somewhere.') 1561 return ('\n'.join(output_template.split('\n')[1:])).strip() 1562 1563 def generate_input_str(self, query_trigger): 1564 input_str = self.passage 1565 for i_style in INPUT_STYLE_SET: 1566 if i_style in self.input_style: 1567 if i_style == 'event_type': 1568 input_str += ' \n {}'.format('conflict event, attack sub-type') 1569 if i_style == 'event_type_sent': 1570 input_str += ' \n {}'.format('The event is related to conflict and some violent physical act.') 1571 if i_style == 'keywords': 1572 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1573 if i_style == 'triggers': 1574 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1575 if i_style == 'template': 1576 input_str += ' \n {}'.format(self.output_template) 1577 1578 return input_str 1579 1580 def generate_output_str(self, query_trigger): 1581 assert self.gold_event is not None 1582 output_str = '' 1583 gold_sample = False 1584 for o_style in OUTPUT_STYLE_SET: 1585 if o_style in self.output_style: 1586 if o_style == 'trigger:sentence': 1587 if self.trigger_text != '': 1588 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1589 gold_sample = True 1590 else: 1591 output_str += ' \n Event trigger is <Trigger>' 1592 if o_style == 'argument:sentence': 1593 output_texts = [] 1594 for argu in self.arguments: 1595 attacker_list = [] 1596 if "Attacker" in argu.keys(): 1597 attacker_list.extend([ a['argument text'] for a in argu['Attacker']]) 1598 if "Agent" in argu.keys(): 1599 attacker_list.extend([ a['argument text'] for a in argu['Agent']]) 1600 filler = ( 1601 " and ".join(attacker_list) if len(attacker_list)>0 else ROLE_PH_MAP['Attacker'], 1602 " and ".join([ a['argument text'] for a in argu['Target']]) if "Target" in argu.keys() else ROLE_PH_MAP['Target'], 1603 " and ".join([ a['argument text'] for a in argu['Instrument']]) if "Instrument" in argu.keys() else ROLE_PH_MAP['Instrument'], 1604 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1605 ) 1606 output_texts.append("{} attacked {} by {} in {}.".format(*filler)) 1607 gold_sample = True 1608 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1609 1610 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1611 return (output_str, gold_sample) 1612 1613 def decode(self, preds): 1614 output = [] 1615 for cnt, pred in enumerate(preds.split('\n')): 1616 used_o_cnt = 0 1617 full_pred = pred.strip() 1618 for o_style in OUTPUT_STYLE_SET: 1619 if o_style in self.output_style: 1620 if o_style == 'trigger:sentence': 1621 if used_o_cnt == cnt: 1622 try: 1623 triggers = full_pred.split('Event trigger is ', 1)[1] 1624 triggers = triggers.split(' and ') 1625 for t_cnt, t in enumerate(triggers): 1626 if t != '<Trigger>': 1627 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1628 except Exception as e: 1629 #print(e) 1630 pass 1631 used_o_cnt += 1 1632 if o_style == 'argument:sentence': 1633 if used_o_cnt == cnt: 1634 try: 1635 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1636 attack = prediction.split(' attacked ', 1)[0] 1637 attack = attack.split(' and ') 1638 remain = prediction.split(' attacked ', 1)[1] 1639 1640 target = remain.split(' by ', 1)[0] 1641 target = target.split(' and ') 1642 remain = remain.split(' by ', 1)[1] 1643 1644 instrument = remain.split(' in ', 1)[0] 1645 instrument = instrument.split(' and ') 1646 remain = remain.split(' in ', 1)[1] 1647 1648 place = remain.rsplit('.',1)[0] 1649 place = place.split(' and ') 1650 for arg in attack: 1651 if arg != ROLE_PH_MAP['Attacker']: 1652 output.append((arg, 'Attacker', {'cor tri cnt': a_cnt})) 1653 for arg in target: 1654 if arg != ROLE_PH_MAP['Target']: 1655 output.append((arg, 'Target', {'cor tri cnt': a_cnt})) 1656 for arg in instrument: 1657 if arg != ROLE_PH_MAP['Instrument']: 1658 output.append((arg, 'Instrument', {'cor tri cnt': a_cnt})) 1659 for arg in place: 1660 if arg != ROLE_PH_MAP['Place']: 1661 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1662 except: 1663 pass 1664 used_o_cnt += 1 1665 1666 return output 1667 1668 class Conflict_Demonstrate(event_template): 1669 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1670 super().__init__(input_style, output_style, passage, event_type, gold_event) 1671 1672 @classmethod 1673 def get_keywords(self): 1674 return ['rally', 'protest', 'demonstrate'] 1675 1676 def get_output_template(self): 1677 output_template = '' 1678 for o_style in OUTPUT_STYLE_SET: 1679 if o_style in self.output_style: 1680 if o_style == 'trigger:sentence': 1681 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1682 if o_style == 'argument:sentence': 1683 output_template += ' \n {}'.format('some people or some organization protest at somewhere.') 1684 return ('\n'.join(output_template.split('\n')[1:])).strip() 1685 1686 def generate_input_str(self, query_trigger): 1687 input_str = self.passage 1688 for i_style in INPUT_STYLE_SET: 1689 if i_style in self.input_style: 1690 if i_style == 'event_type': 1691 input_str += ' \n {}'.format('conflict event, demonstrate sub-type') 1692 if i_style == 'event_type_sent': 1693 input_str += ' \n {}'.format('The event is related to a large number of people coming together to protest.') 1694 if i_style == 'keywords': 1695 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1696 if i_style == 'triggers': 1697 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1698 if i_style == 'template': 1699 input_str += ' \n {}'.format(self.output_template) 1700 1701 return input_str 1702 1703 def generate_output_str(self, query_trigger): 1704 assert self.gold_event is not None 1705 output_str = '' 1706 gold_sample = False 1707 for o_style in OUTPUT_STYLE_SET: 1708 if o_style in self.output_style: 1709 if o_style == 'trigger:sentence': 1710 if self.trigger_text != '': 1711 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1712 gold_sample = True 1713 else: 1714 output_str += ' \n Event trigger is <Trigger>' 1715 if o_style == 'argument:sentence': 1716 output_texts = [] 1717 for argu in self.arguments: 1718 filler = ( 1719 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 1720 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1721 ) 1722 output_texts.append("{} protest at {}.".format(*filler)) 1723 gold_sample = True 1724 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1725 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1726 return (output_str, gold_sample) 1727 1728 def decode(self, preds): 1729 output = [] 1730 for cnt, pred in enumerate(preds.split('\n')): 1731 used_o_cnt = 0 1732 full_pred = pred.strip() 1733 for o_style in OUTPUT_STYLE_SET: 1734 if o_style in self.output_style: 1735 if o_style == 'trigger:sentence': 1736 if used_o_cnt == cnt: 1737 try: 1738 triggers = full_pred.split('Event trigger is ', 1)[1] 1739 triggers = triggers.split(' and ') 1740 for t_cnt, t in enumerate(triggers): 1741 if t != '<Trigger>': 1742 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1743 except: 1744 pass 1745 used_o_cnt += 1 1746 if o_style == 'argument:sentence': 1747 if used_o_cnt == cnt: 1748 try: 1749 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1750 entity = prediction.split(' protest at ', 1)[0] 1751 entity = entity.split(' and ') 1752 remain = prediction.split(' protest at ', 1)[1] 1753 1754 place = remain.rsplit('.',1)[0] 1755 place = place.split(' and ') 1756 for arg in entity: 1757 if arg != ROLE_PH_MAP['Entity']: 1758 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 1759 for arg in place: 1760 if arg != ROLE_PH_MAP['Place']: 1761 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1762 except: 1763 pass 1764 used_o_cnt += 1 1765 1766 return output 1767 1768 class Contact_Meet(event_template): 1769 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1770 super().__init__(input_style, output_style, passage, event_type, gold_event) 1771 1772 @classmethod 1773 def get_keywords(self): 1774 return ['meeting', 'met', 'summit'] 1775 1776 def get_output_template(self): 1777 output_template = '' 1778 for o_style in OUTPUT_STYLE_SET: 1779 if o_style in self.output_style: 1780 if o_style == 'trigger:sentence': 1781 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1782 if o_style == 'argument:sentence': 1783 output_template += ' \n {}'.format('some people or some organization met at somewhere.') 1784 return ('\n'.join(output_template.split('\n')[1:])).strip() 1785 1786 def generate_input_str(self, query_trigger): 1787 input_str = self.passage 1788 for i_style in INPUT_STYLE_SET: 1789 if i_style in self.input_style: 1790 if i_style == 'event_type': 1791 input_str += ' \n {}'.format('contact event, meet sub-type') 1792 if i_style == 'event_type_sent': 1793 input_str += ' \n {}'.format('The event is related to a group of people meeting and interacting with one another face-to-face.') 1794 if i_style == 'keywords': 1795 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1796 if i_style == 'triggers': 1797 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1798 if i_style == 'template': 1799 input_str += ' \n {}'.format(self.output_template) 1800 1801 return input_str 1802 1803 def generate_output_str(self, query_trigger): 1804 assert self.gold_event is not None 1805 output_str = '' 1806 gold_sample = False 1807 for o_style in OUTPUT_STYLE_SET: 1808 if o_style in self.output_style: 1809 if o_style == 'trigger:sentence': 1810 if self.trigger_text != '': 1811 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1812 gold_sample = True 1813 else: 1814 output_str += ' \n Event trigger is <Trigger>' 1815 if o_style == 'argument:sentence': 1816 output_texts = [] 1817 for argu in self.arguments: 1818 filler = ( 1819 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 1820 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1821 ) 1822 output_texts.append("{} met at {}.".format(*filler)) 1823 gold_sample = True 1824 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1825 1826 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1827 return (output_str, gold_sample) 1828 1829 def decode(self, preds): 1830 output = [] 1831 for cnt, pred in enumerate(preds.split('\n')): 1832 used_o_cnt = 0 1833 full_pred = pred.strip() 1834 for o_style in OUTPUT_STYLE_SET: 1835 if o_style in self.output_style: 1836 if o_style == 'trigger:sentence': 1837 if used_o_cnt == cnt: 1838 try: 1839 triggers = full_pred.split('Event trigger is ', 1)[1] 1840 triggers = triggers.split(' and ') 1841 for t_cnt, t in enumerate(triggers): 1842 if t != '<Trigger>': 1843 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1844 except: 1845 pass 1846 used_o_cnt += 1 1847 if o_style == 'argument:sentence': 1848 if used_o_cnt == cnt: 1849 try: 1850 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1851 entity = prediction.split(' met at ', 1)[0] 1852 entity = entity.split(' and ') 1853 remain = prediction.split(' met at ', 1)[1] 1854 1855 place = remain.rsplit('.',1)[0] 1856 place = place.split(' and ') 1857 for arg in entity: 1858 if arg != ROLE_PH_MAP['Entity']: 1859 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 1860 for arg in place: 1861 if arg != ROLE_PH_MAP['Place']: 1862 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1863 except: 1864 pass 1865 used_o_cnt += 1 1866 1867 return output 1868 1869 class Contact_Phone_Write(event_template): 1870 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1871 super().__init__(input_style, output_style, passage, event_type, gold_event) 1872 1873 @classmethod 1874 def get_keywords(self): 1875 return ['call', 'communicate', 'e-mail'] 1876 1877 def get_output_template(self): 1878 output_template = '' 1879 for o_style in OUTPUT_STYLE_SET: 1880 if o_style in self.output_style: 1881 if o_style == 'trigger:sentence': 1882 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1883 if o_style == 'argument:sentence': 1884 output_template += ' \n {}'.format('some people or some organization called or texted messages at somewhere.') 1885 return ('\n'.join(output_template.split('\n')[1:])).strip() 1886 1887 def generate_input_str(self, query_trigger): 1888 input_str = self.passage 1889 for i_style in INPUT_STYLE_SET: 1890 if i_style in self.input_style: 1891 if i_style == 'event_type': 1892 input_str += ' \n {}'.format('contact event, phone write sub-type') 1893 if i_style == 'event_type_sent': 1894 input_str += ' \n {}'.format('The event is related to people phone calling or messaging one another.') 1895 if i_style == 'keywords': 1896 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1897 if i_style == 'triggers': 1898 input_str += ' \n The event trigger word is {}'.format(query_trigger) 1899 if i_style == 'template': 1900 input_str += ' \n {}'.format(self.output_template) 1901 1902 return input_str 1903 1904 def generate_output_str(self, query_trigger): 1905 assert self.gold_event is not None 1906 output_str = '' 1907 gold_sample = False 1908 for o_style in OUTPUT_STYLE_SET: 1909 if o_style in self.output_style: 1910 if o_style == 'trigger:sentence': 1911 if self.trigger_text != '': 1912 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 1913 gold_sample = True 1914 else: 1915 output_str += ' \n Event trigger is <Trigger>' 1916 if o_style == 'argument:sentence': 1917 output_texts = [] 1918 for argu in self.arguments: 1919 filler = ( 1920 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 1921 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 1922 ) 1923 output_texts.append("{} called or texted messages at {}.".format(*filler)) 1924 gold_sample = True 1925 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 1926 1927 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 1928 return (output_str, gold_sample) 1929 1930 def decode(self, preds): 1931 output = [] 1932 for cnt, pred in enumerate(preds.split('\n')): 1933 used_o_cnt = 0 1934 full_pred = pred.strip() 1935 for o_style in OUTPUT_STYLE_SET: 1936 if o_style in self.output_style: 1937 if o_style == 'trigger:sentence': 1938 if used_o_cnt == cnt: 1939 try: 1940 triggers = full_pred.split('Event trigger is ', 1)[1] 1941 triggers = triggers.split(' and ') 1942 for t_cnt, t in enumerate(triggers): 1943 if t != '<Trigger>': 1944 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 1945 except: 1946 pass 1947 used_o_cnt += 1 1948 if o_style == 'argument:sentence': 1949 if used_o_cnt == cnt: 1950 try: 1951 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 1952 entity = prediction.split(' called or texted messages at ', 1)[0] 1953 entity = entity.split(' and ') 1954 remain = prediction.split(' called or texted messages at ', 1)[1] 1955 1956 place = remain.rsplit('.',1)[0] 1957 place = place.split(' and ') 1958 for arg in entity: 1959 if arg != ROLE_PH_MAP['Entity']: 1960 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 1961 for arg in place: 1962 if arg != ROLE_PH_MAP['Place']: 1963 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 1964 except: 1965 pass 1966 used_o_cnt += 1 1967 1968 return output 1969 1970 class Personnel_Start_Position(event_template): 1971 1972 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 1973 super().__init__(input_style, output_style, passage, event_type, gold_event) 1974 1975 @classmethod 1976 def get_keywords(self): 1977 return ['hire', 'appoint', 'join'] 1978 1979 def get_output_template(self): 1980 output_template = '' 1981 for o_style in OUTPUT_STYLE_SET: 1982 if o_style in self.output_style: 1983 if o_style == 'trigger:sentence': 1984 output_template += ' \n {}'.format('Event trigger is <Trigger>') 1985 if o_style == 'argument:sentence': 1986 output_template += ' \n {}'.format('somebody got new job and was hired by some people or some organization in somewhere.') 1987 return ('\n'.join(output_template.split('\n')[1:])).strip() 1988 1989 def generate_input_str(self, query_trigger): 1990 input_str = self.passage 1991 for i_style in INPUT_STYLE_SET: 1992 if i_style in self.input_style: 1993 if i_style == 'event_type': 1994 input_str += ' \n {}'.format('personnel event, start position sub-type') 1995 if i_style == 'event_type_sent': 1996 input_str += ' \n {}'.format('The event is related to a person begins working for an organization or a hiring manager.') 1997 if i_style == 'keywords': 1998 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 1999 if i_style == 'triggers': 2000 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2001 if i_style == 'template': 2002 input_str += ' \n {}'.format(self.output_template) 2003 2004 return input_str 2005 2006 def generate_output_str(self, query_trigger): 2007 assert self.gold_event is not None 2008 output_str = '' 2009 gold_sample = False 2010 for o_style in OUTPUT_STYLE_SET: 2011 if o_style in self.output_style: 2012 if o_style == 'trigger:sentence': 2013 if self.trigger_text != '': 2014 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2015 gold_sample = True 2016 else: 2017 output_str += ' \n Event trigger is <Trigger>' 2018 if o_style == 'argument:sentence': 2019 output_texts = [] 2020 for argu in self.arguments: 2021 filler = ( 2022 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2023 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 2024 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 2025 ) 2026 output_texts.append("{} got new job and was hired by {} in {}.".format(*filler)) 2027 gold_sample = True 2028 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2029 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2030 return (output_str, gold_sample) 2031 2032 def decode(self, preds): 2033 output = [] 2034 for cnt, pred in enumerate(preds.split('\n')): 2035 used_o_cnt = 0 2036 full_pred = pred.strip() 2037 for o_style in OUTPUT_STYLE_SET: 2038 if o_style in self.output_style: 2039 if o_style == 'trigger:sentence': 2040 if used_o_cnt == cnt: 2041 try: 2042 triggers = full_pred.split('Event trigger is ', 1)[1] 2043 triggers = triggers.split(' and ') 2044 for t_cnt, t in enumerate(triggers): 2045 if t != '<Trigger>': 2046 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2047 except: 2048 pass 2049 used_o_cnt += 1 2050 if o_style == 'argument:sentence': 2051 if used_o_cnt == cnt: 2052 try: 2053 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2054 person = prediction.split(' got new job and was hired by ', 1)[0] 2055 person = person.split(' and ') 2056 remain = prediction.split(' got new job and was hired by ', 1)[1] 2057 2058 entity = remain.split(' in ', 1)[0] 2059 entity = entity.split(' and ') 2060 remain = remain.split(' in ', 1)[1] 2061 2062 place = remain.rsplit('.',1)[0] 2063 place = place.split(' and ') 2064 for arg in person: 2065 if arg != ROLE_PH_MAP['Person']: 2066 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2067 for arg in entity: 2068 if arg != ROLE_PH_MAP['Entity']: 2069 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 2070 for arg in place: 2071 if arg != ROLE_PH_MAP['Place']: 2072 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2073 except: 2074 pass 2075 used_o_cnt += 1 2076 return output 2077 2078 class Personnel_End_Position(event_template): 2079 2080 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2081 super().__init__(input_style, output_style, passage, event_type, gold_event) 2082 2083 @classmethod 2084 def get_keywords(self): 2085 return ['former', 'laid off', 'fired'] 2086 2087 def get_output_template(self): 2088 output_template = '' 2089 for o_style in OUTPUT_STYLE_SET: 2090 if o_style in self.output_style: 2091 if o_style == 'trigger:sentence': 2092 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2093 if o_style == 'argument:sentence': 2094 output_template += ' \n {}'.format('somebody stopped working for some people or some organization at somewhere.') 2095 return ('\n'.join(output_template.split('\n')[1:])).strip() 2096 2097 def generate_input_str(self, query_trigger): 2098 input_str = self.passage 2099 for i_style in INPUT_STYLE_SET: 2100 if i_style in self.input_style: 2101 if i_style == 'event_type': 2102 input_str += ' \n {}'.format('personnel event, end position sub-type') 2103 if i_style == 'event_type_sent': 2104 input_str += ' \n {}'.format('The event is related to a person stops working for an organization or a hiring manager.') 2105 if i_style == 'keywords': 2106 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2107 if i_style == 'triggers': 2108 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2109 if i_style == 'template': 2110 input_str += ' \n {}'.format(self.output_template) 2111 2112 return input_str 2113 2114 def generate_output_str(self, query_trigger): 2115 assert self.gold_event is not None 2116 output_str = '' 2117 gold_sample = False 2118 for o_style in OUTPUT_STYLE_SET: 2119 if o_style in self.output_style: 2120 if o_style == 'trigger:sentence': 2121 if self.trigger_text != '': 2122 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2123 gold_sample = True 2124 else: 2125 output_str += ' \n Event trigger is <Trigger>' 2126 if o_style == 'argument:sentence': 2127 output_texts = [] 2128 for argu in self.arguments: 2129 filler = ( 2130 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2131 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 2132 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 2133 ) 2134 output_texts.append("{} stopped working for {} at {}.".format(*filler)) 2135 gold_sample = True 2136 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2137 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2138 return (output_str, gold_sample) 2139 2140 def decode(self, preds): 2141 output = [] 2142 for cnt, pred in enumerate(preds.split('\n')): 2143 used_o_cnt = 0 2144 full_pred = pred.strip() 2145 for o_style in OUTPUT_STYLE_SET: 2146 if o_style in self.output_style: 2147 if o_style == 'trigger:sentence': 2148 if used_o_cnt == cnt: 2149 try: 2150 triggers = full_pred.split('Event trigger is ', 1)[1] 2151 triggers = triggers.split(' and ') 2152 for t_cnt, t in enumerate(triggers): 2153 if t != '<Trigger>': 2154 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2155 except: 2156 pass 2157 used_o_cnt += 1 2158 if o_style == 'argument:sentence': 2159 if used_o_cnt == cnt: 2160 try: 2161 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2162 person = prediction.split(' stopped working for ', 1)[0] 2163 person = person.split(' and ') 2164 remain = prediction.split(' stopped working for ', 1)[1] 2165 2166 entity = remain.split(' at ', 1)[0] 2167 entity = entity.split(' and ') 2168 remain = remain.split(' at ', 1)[1] 2169 2170 place = remain.rsplit('.',1)[0] 2171 place = place.split(' and ') 2172 for arg in person: 2173 if arg != ROLE_PH_MAP['Person']: 2174 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2175 for arg in entity: 2176 if arg != ROLE_PH_MAP['Entity']: 2177 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 2178 for arg in place: 2179 if arg != ROLE_PH_MAP['Place']: 2180 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2181 except: 2182 pass 2183 used_o_cnt += 1 2184 return output 2185 2186 class Personnel_Nominate(event_template): 2187 2188 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2189 super().__init__(input_style, output_style, passage, event_type, gold_event) 2190 2191 @classmethod 2192 def get_keywords(self): 2193 return ['named', 'nomination', 'nominate'] 2194 2195 def get_output_template(self): 2196 output_template = '' 2197 for o_style in OUTPUT_STYLE_SET: 2198 if o_style in self.output_style: 2199 if o_style == 'trigger:sentence': 2200 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2201 if o_style == 'argument:sentence': 2202 output_template += ' \n {}'.format('somebody was nominated by somebody or some organization to do a job.') 2203 return ('\n'.join(output_template.split('\n')[1:])).strip() 2204 2205 def generate_input_str(self, query_trigger): 2206 input_str = self.passage 2207 for i_style in INPUT_STYLE_SET: 2208 if i_style in self.input_style: 2209 if i_style == 'event_type': 2210 input_str += ' \n {}'.format('personnel event, nominate position sub-type') 2211 if i_style == 'event_type_sent': 2212 input_str += ' \n {}'.format('The event is related to a person being nominated for a position.') 2213 if i_style == 'keywords': 2214 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2215 if i_style == 'triggers': 2216 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2217 if i_style == 'template': 2218 input_str += ' \n {}'.format(self.output_template) 2219 2220 return input_str 2221 2222 def generate_output_str(self, query_trigger): 2223 assert self.gold_event is not None 2224 output_str = '' 2225 gold_sample = False 2226 for o_style in OUTPUT_STYLE_SET: 2227 if o_style in self.output_style: 2228 if o_style == 'trigger:sentence': 2229 if self.trigger_text != '': 2230 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2231 gold_sample = True 2232 else: 2233 output_str += ' \n Event trigger is <Trigger>' 2234 if o_style == 'argument:sentence': 2235 output_texts = [] 2236 for argu in self.arguments: 2237 filler = ( 2238 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2239 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'] 2240 ) 2241 output_texts.append("{} was nominated by {} to do a job.".format(*filler)) 2242 gold_sample = True 2243 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2244 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2245 return (output_str, gold_sample) 2246 2247 def decode(self, preds): 2248 output = [] 2249 for cnt, pred in enumerate(preds.split('\n')): 2250 used_o_cnt = 0 2251 full_pred = pred.strip() 2252 for o_style in OUTPUT_STYLE_SET: 2253 if o_style in self.output_style: 2254 if o_style == 'trigger:sentence': 2255 if used_o_cnt == cnt: 2256 try: 2257 triggers = full_pred.split('Event trigger is ', 1)[1] 2258 triggers = triggers.split(' and ') 2259 for t_cnt, t in enumerate(triggers): 2260 if t != '<Trigger>': 2261 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2262 except: 2263 pass 2264 used_o_cnt += 1 2265 if o_style == 'argument:sentence': 2266 if used_o_cnt == cnt: 2267 try: 2268 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2269 person = prediction.split(' was nominated by ', 1)[0] 2270 person = person.split(' and ') 2271 remain = prediction.split(' was nominated by ', 1)[1] 2272 2273 agent = remain.rsplit(' to do a job.',1)[0] 2274 agent = agent.split(' and ') 2275 for arg in person: 2276 if arg != ROLE_PH_MAP['Person']: 2277 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2278 for arg in agent: 2279 if arg != ROLE_PH_MAP['Agent']: 2280 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 2281 except: 2282 pass 2283 used_o_cnt += 1 2284 return output 2285 2286 class Personnel_Elect(event_template): 2287 2288 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2289 super().__init__(input_style, output_style, passage, event_type, gold_event) 2290 2291 @classmethod 2292 def get_keywords(self): 2293 return ['election', 'elect', 'elected'] 2294 2295 def get_output_template(self): 2296 output_template = '' 2297 for o_style in OUTPUT_STYLE_SET: 2298 if o_style in self.output_style: 2299 if o_style == 'trigger:sentence': 2300 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2301 if o_style == 'argument:sentence': 2302 output_template += ' \n {}'.format('somebody was elected a position, and the election was voted by some people or some organization in somewhere.') 2303 return ('\n'.join(output_template.split('\n')[1:])).strip() 2304 2305 def generate_input_str(self, query_trigger): 2306 input_str = self.passage 2307 for i_style in INPUT_STYLE_SET: 2308 if i_style in self.input_style: 2309 if i_style == 'event_type': 2310 input_str += ' \n {}'.format('personnel event, elect position sub-type') 2311 if i_style == 'event_type_sent': 2312 input_str += ' \n {}'.format('The event is related to a candidate wins an election.') 2313 if i_style == 'keywords': 2314 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2315 if i_style == 'triggers': 2316 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2317 if i_style == 'template': 2318 input_str += ' \n {}'.format(self.output_template) 2319 2320 return input_str 2321 2322 def generate_output_str(self, query_trigger): 2323 assert self.gold_event is not None 2324 output_str = '' 2325 gold_sample = False 2326 for o_style in OUTPUT_STYLE_SET: 2327 if o_style in self.output_style: 2328 if o_style == 'trigger:sentence': 2329 if self.trigger_text != '': 2330 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2331 gold_sample = True 2332 else: 2333 output_str += ' \n Event trigger is <Trigger>' 2334 if o_style == 'argument:sentence': 2335 output_texts = [] 2336 for argu in self.arguments: 2337 filler = ( 2338 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2339 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 2340 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 2341 ) 2342 output_texts.append("{} was elected a position, and the election was voted by {} in {}.".format(*filler)) 2343 gold_sample = True 2344 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2345 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2346 return (output_str, gold_sample) 2347 2348 def decode(self, preds): 2349 output = [] 2350 for cnt, pred in enumerate(preds.split('\n')): 2351 used_o_cnt = 0 2352 full_pred = pred.strip() 2353 for o_style in OUTPUT_STYLE_SET: 2354 if o_style in self.output_style: 2355 if o_style == 'trigger:sentence': 2356 if used_o_cnt == cnt: 2357 try: 2358 triggers = full_pred.split('Event trigger is ', 1)[1] 2359 triggers = triggers.split(' and ') 2360 for t_cnt, t in enumerate(triggers): 2361 if t != '<Trigger>': 2362 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2363 except: 2364 pass 2365 used_o_cnt += 1 2366 if o_style == 'argument:sentence': 2367 if used_o_cnt == cnt: 2368 try: 2369 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2370 person = prediction.split(' was elected a position, and the election was voted by ', 1)[0] 2371 person = person.split(' and ') 2372 remain = prediction.split(' was elected a position, and the election was voted by ', 1)[1] 2373 2374 entity = remain.split(' in ', 1)[0] 2375 entity = entity.split(' and ') 2376 remain = remain.split(' in ', 1)[1] 2377 2378 place = remain.rsplit('.',1)[0] 2379 place = place.split(' and ') 2380 for arg in person: 2381 if arg != ROLE_PH_MAP['Person']: 2382 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2383 for arg in entity: 2384 if arg != ROLE_PH_MAP['Entity']: 2385 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 2386 for arg in place: 2387 if arg != ROLE_PH_MAP['Place']: 2388 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2389 except: 2390 pass 2391 used_o_cnt += 1 2392 return output 2393 2394 class Justice_Arrest_Jail(event_template): 2395 2396 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2397 super().__init__(input_style, output_style, passage, event_type, gold_event) 2398 2399 @classmethod 2400 def get_keywords(self): 2401 return ['arrest', 'jail', 'detained'] 2402 2403 def get_output_template(self): 2404 output_template = '' 2405 for o_style in OUTPUT_STYLE_SET: 2406 if o_style in self.output_style: 2407 if o_style == 'trigger:sentence': 2408 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2409 if o_style == 'argument:sentence': 2410 output_template += ' \n {}'.format('somebody was sent to jailed or arrested by somebody or some organization in somewhere.') 2411 return ('\n'.join(output_template.split('\n')[1:])).strip() 2412 2413 def generate_input_str(self, query_trigger): 2414 input_str = self.passage 2415 for i_style in INPUT_STYLE_SET: 2416 if i_style in self.input_style: 2417 if i_style == 'event_type': 2418 input_str += ' \n {}'.format('justice event, arrest jail sub-type') 2419 if i_style == 'event_type_sent': 2420 input_str += ' \n {}'.format('The event is related to a person getting arrested or a person being sent to jail.') 2421 if i_style == 'keywords': 2422 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2423 if i_style == 'triggers': 2424 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2425 if i_style == 'template': 2426 input_str += ' \n {}'.format(self.output_template) 2427 2428 return input_str 2429 2430 def generate_output_str(self, query_trigger): 2431 assert self.gold_event is not None 2432 output_str = '' 2433 gold_sample = False 2434 for o_style in OUTPUT_STYLE_SET: 2435 if o_style in self.output_style: 2436 if o_style == 'trigger:sentence': 2437 if self.trigger_text != '': 2438 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2439 gold_sample = True 2440 else: 2441 output_str += ' \n Event trigger is <Trigger>' 2442 if o_style == 'argument:sentence': 2443 output_texts = [] 2444 for argu in self.arguments: 2445 filler = ( 2446 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2447 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'], 2448 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 2449 ) 2450 output_texts.append("{} was sent to jailed or arrested by {} in {}.".format(*filler)) 2451 gold_sample = True 2452 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2453 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2454 return (output_str, gold_sample) 2455 2456 def decode(self, preds): 2457 output = [] 2458 for cnt, pred in enumerate(preds.split('\n')): 2459 used_o_cnt = 0 2460 full_pred = pred.strip() 2461 for o_style in OUTPUT_STYLE_SET: 2462 if o_style in self.output_style: 2463 if o_style == 'trigger:sentence': 2464 if used_o_cnt == cnt: 2465 try: 2466 triggers = full_pred.split('Event trigger is ', 1)[1] 2467 triggers = triggers.split(' and ') 2468 for t_cnt, t in enumerate(triggers): 2469 if t != '<Trigger>': 2470 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2471 except: 2472 pass 2473 used_o_cnt += 1 2474 if o_style == 'argument:sentence': 2475 if used_o_cnt == cnt: 2476 try: 2477 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2478 person = prediction.split(' was sent to jailed or arrested by ', 1)[0] 2479 person = person.split(' and ') 2480 remain = prediction.split(' was sent to jailed or arrested by ', 1)[1] 2481 2482 agent = remain.split(' in ', 1)[0] 2483 agent = agent.split(' and ') 2484 remain = remain.split(' in ', 1)[1] 2485 2486 place = remain.rsplit('.',1)[0] 2487 place = place.split(' and ') 2488 for arg in person: 2489 if arg != ROLE_PH_MAP['Person']: 2490 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2491 for arg in agent: 2492 if arg != ROLE_PH_MAP['Agent']: 2493 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 2494 for arg in place: 2495 if arg != ROLE_PH_MAP['Place']: 2496 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2497 except Exception as e: 2498 pass 2499 used_o_cnt += 1 2500 return output 2501 2502 class Justice_Release_Parole(event_template): 2503 2504 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2505 super().__init__(input_style, output_style, passage, event_type, gold_event) 2506 2507 @classmethod 2508 def get_keywords(self): 2509 return ['parole', 'release', 'free'] 2510 2511 def get_output_template(self): 2512 output_template = '' 2513 for o_style in OUTPUT_STYLE_SET: 2514 if o_style in self.output_style: 2515 if o_style == 'trigger:sentence': 2516 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2517 if o_style == 'argument:sentence': 2518 output_template += ' \n {}'.format('somebody was released by some people or some organization from somewhere.') 2519 return ('\n'.join(output_template.split('\n')[1:])).strip() 2520 2521 def generate_input_str(self, query_trigger): 2522 input_str = self.passage 2523 for i_style in INPUT_STYLE_SET: 2524 if i_style in self.input_style: 2525 if i_style == 'event_type': 2526 input_str += ' \n {}'.format('justice event, release parole sub-type') 2527 if i_style == 'event_type_sent': 2528 input_str += ' \n {}'.format("The event is related to an end to someone's custody in prison.") 2529 if i_style == 'keywords': 2530 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2531 if i_style == 'triggers': 2532 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2533 if i_style == 'template': 2534 input_str += ' \n {}'.format(self.output_template) 2535 2536 return input_str 2537 2538 def generate_output_str(self, query_trigger): 2539 assert self.gold_event is not None 2540 output_str = '' 2541 gold_sample = False 2542 for o_style in OUTPUT_STYLE_SET: 2543 if o_style in self.output_style: 2544 if o_style == 'trigger:sentence': 2545 if self.trigger_text != '': 2546 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2547 gold_sample = True 2548 else: 2549 output_str += ' \n Event trigger is <Trigger>' 2550 if o_style == 'argument:sentence': 2551 output_texts = [] 2552 for argu in self.arguments: 2553 filler = ( 2554 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 2555 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 2556 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 2557 ) 2558 output_texts.append("{} was released by {} from {}.".format(*filler)) 2559 gold_sample = True 2560 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2561 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2562 return (output_str, gold_sample) 2563 2564 def decode(self, preds): 2565 output = [] 2566 for cnt, pred in enumerate(preds.split('\n')): 2567 used_o_cnt = 0 2568 full_pred = pred.strip() 2569 for o_style in OUTPUT_STYLE_SET: 2570 if o_style in self.output_style: 2571 if o_style == 'trigger:sentence': 2572 if used_o_cnt == cnt: 2573 try: 2574 triggers = full_pred.split('Event trigger is ', 1)[1] 2575 triggers = triggers.split(' and ') 2576 for t_cnt, t in enumerate(triggers): 2577 if t != '<Trigger>': 2578 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2579 except: 2580 pass 2581 used_o_cnt += 1 2582 if o_style == 'argument:sentence': 2583 if used_o_cnt == cnt: 2584 try: 2585 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2586 person = prediction.split(' was released by ', 1)[0] 2587 person = person.split(' and ') 2588 remain = prediction.split(' was released by ', 1)[1] 2589 2590 entity = remain.split(' from ', 1)[0] 2591 entity = entity.split(' and ') 2592 remain = remain.split(' from ', 1)[1] 2593 2594 place = remain.rsplit('.',1)[0] 2595 place = place.split(' and ') 2596 for arg in person: 2597 if arg != ROLE_PH_MAP['Person']: 2598 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 2599 for arg in entity: 2600 if arg != ROLE_PH_MAP['Entity']: 2601 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 2602 for arg in place: 2603 if arg != ROLE_PH_MAP['Place']: 2604 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2605 except: 2606 pass 2607 used_o_cnt += 1 2608 return output 2609 2610 class Justice_Trial_Hearing(event_template): 2611 2612 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2613 super().__init__(input_style, output_style, passage, event_type, gold_event) 2614 2615 @classmethod 2616 def get_keywords(self): 2617 return ['trial', 'hearing', 'proceeding'] 2618 2619 def get_output_template(self): 2620 output_template = '' 2621 for o_style in OUTPUT_STYLE_SET: 2622 if o_style in self.output_style: 2623 if o_style == 'trigger:sentence': 2624 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2625 if o_style == 'argument:sentence': 2626 output_template += ' \n {}'.format('somebody, prosecuted by some other, faced a trial in somewhere. The hearing was judged by some adjudicator.') 2627 return ('\n'.join(output_template.split('\n')[1:])).strip() 2628 2629 def generate_input_str(self, query_trigger): 2630 input_str = self.passage 2631 for i_style in INPUT_STYLE_SET: 2632 if i_style in self.input_style: 2633 if i_style == 'event_type': 2634 input_str += ' \n {}'.format('justice event, trial hearing sub-type') 2635 if i_style == 'event_type_sent': 2636 input_str += ' \n {}'.format("The event is related to a trial or hearing for someone.") 2637 if i_style == 'keywords': 2638 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2639 if i_style == 'triggers': 2640 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2641 if i_style == 'template': 2642 input_str += ' \n {}'.format(self.output_template) 2643 return input_str 2644 2645 def generate_output_str(self, query_trigger): 2646 assert self.gold_event is not None 2647 output_str = '' 2648 gold_sample = False 2649 for o_style in OUTPUT_STYLE_SET: 2650 if o_style in self.output_style: 2651 if o_style == 'trigger:sentence': 2652 if self.trigger_text != '': 2653 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2654 gold_sample = True 2655 else: 2656 output_str += ' \n Event trigger is <Trigger>' 2657 if o_style == 'argument:sentence': 2658 output_texts = [] 2659 for argu in self.arguments: 2660 filler = ( 2661 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 2662 " and ".join([ a['argument text'] for a in argu['Prosecutor']]) if "Prosecutor" in argu.keys() else ROLE_PH_MAP['Prosecutor'], 2663 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 2664 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 2665 ) 2666 output_texts.append("{}, prosecuted by {}, faced a trial in {}. The hearing was judged by {}.".format(*filler)) 2667 gold_sample = True 2668 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2669 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2670 return (output_str, gold_sample) 2671 2672 def decode(self, preds): 2673 output = [] 2674 for cnt, pred in enumerate(preds.split('\n')): 2675 used_o_cnt = 0 2676 full_pred = pred.strip() 2677 for o_style in OUTPUT_STYLE_SET: 2678 if o_style in self.output_style: 2679 if o_style == 'trigger:sentence': 2680 if used_o_cnt == cnt: 2681 try: 2682 triggers = full_pred.split('Event trigger is ', 1)[1] 2683 triggers = triggers.split(' and ') 2684 for t_cnt, t in enumerate(triggers): 2685 if t != '<Trigger>': 2686 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2687 except: 2688 pass 2689 used_o_cnt += 1 2690 if o_style == 'argument:sentence': 2691 if used_o_cnt == cnt: 2692 try: 2693 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2694 defendant = prediction.split(', prosecuted by ', 1)[0] 2695 defendant = defendant.split(' and ') 2696 remain = prediction.split(', prosecuted by ', 1)[1] 2697 2698 prosecutor = remain.split(', faced a trial in ', 1)[0] 2699 prosecutor = prosecutor.split(' and ') 2700 remain = remain.split(', faced a trial in ', 1)[1] 2701 2702 place = remain.split('. The hearing was judged by ', 1)[0] 2703 place = place.split(' and ') 2704 remain = remain.split('. The hearing was judged by ', 1)[1] 2705 2706 adjudicator = remain.rsplit('.',1)[0] 2707 adjudicator = adjudicator.split(' and ') 2708 for arg in defendant: 2709 if arg != ROLE_PH_MAP['Defendant']: 2710 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 2711 for arg in prosecutor: 2712 if arg != ROLE_PH_MAP['Prosecutor']: 2713 output.append((arg, 'Prosecutor', {'cor tri cnt': a_cnt})) 2714 for arg in place: 2715 if arg != ROLE_PH_MAP['Place']: 2716 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2717 for arg in adjudicator: 2718 if arg != ROLE_PH_MAP['Adjudicator']: 2719 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 2720 except Exception as e: 2721 pass 2722 used_o_cnt += 1 2723 return output 2724 2725 class Justice_Charge_Indict(event_template): 2726 2727 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2728 super().__init__(input_style, output_style, passage, event_type, gold_event) 2729 2730 @classmethod 2731 def get_keywords(self): 2732 return ['indict', 'charged', 'accused'] 2733 2734 def get_output_template(self): 2735 output_template = '' 2736 for o_style in OUTPUT_STYLE_SET: 2737 if o_style in self.output_style: 2738 if o_style == 'trigger:sentence': 2739 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2740 if o_style == 'argument:sentence': 2741 output_template += ' \n {}'.format('somebody was charged by some other in somewhere. The adjudication was judged by some adjudicator.') 2742 return ('\n'.join(output_template.split('\n')[1:])).strip() 2743 2744 def generate_input_str(self, query_trigger): 2745 input_str = self.passage 2746 for i_style in INPUT_STYLE_SET: 2747 if i_style in self.input_style: 2748 if i_style == 'event_type': 2749 input_str += ' \n {}'.format('justice event, charge indict sub-type') 2750 if i_style == 'event_type_sent': 2751 input_str += ' \n {}'.format("The event is related to someone or some organization being accused of a crime.") 2752 if i_style == 'keywords': 2753 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2754 if i_style == 'triggers': 2755 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2756 if i_style == 'template': 2757 input_str += ' \n {}'.format(self.output_template) 2758 return input_str 2759 2760 def generate_output_str(self, query_trigger): 2761 assert self.gold_event is not None 2762 output_str = '' 2763 gold_sample = False 2764 for o_style in OUTPUT_STYLE_SET: 2765 if o_style in self.output_style: 2766 if o_style == 'trigger:sentence': 2767 if self.trigger_text != '': 2768 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2769 gold_sample = True 2770 else: 2771 output_str += ' \n Event trigger is <Trigger>' 2772 if o_style == 'argument:sentence': 2773 output_texts = [] 2774 for argu in self.arguments: 2775 filler = ( 2776 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 2777 " and ".join([ a['argument text'] for a in argu['Prosecutor']]) if "Prosecutor" in argu.keys() else ROLE_PH_MAP['Prosecutor'], 2778 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 2779 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 2780 ) 2781 output_texts.append("{} was charged by {} in {}. The adjudication was judged by {}.".format(*filler)) 2782 gold_sample = True 2783 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2784 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2785 return (output_str, gold_sample) 2786 2787 def decode(self, preds): 2788 output = [] 2789 for cnt, pred in enumerate(preds.split('\n')): 2790 used_o_cnt = 0 2791 full_pred = pred.strip() 2792 for o_style in OUTPUT_STYLE_SET: 2793 if o_style in self.output_style: 2794 if o_style == 'trigger:sentence': 2795 if used_o_cnt == cnt: 2796 try: 2797 triggers = full_pred.split('Event trigger is ', 1)[1] 2798 triggers = triggers.split(' and ') 2799 for t_cnt, t in enumerate(triggers): 2800 if t != '<Trigger>': 2801 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2802 except: 2803 pass 2804 used_o_cnt += 1 2805 if o_style == 'argument:sentence': 2806 if used_o_cnt == cnt: 2807 try: 2808 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2809 defendant = prediction.split(' was charged by ', 1)[0] 2810 defendant = defendant.split(' and ') 2811 remain = prediction.split(' was charged by ', 1)[1] 2812 2813 prosecutor = remain.split(' in ', 1)[0] 2814 prosecutor = prosecutor.split(' and ') 2815 remain = remain.split(' in ', 1)[1] 2816 2817 place = remain.split('. The adjudication was judged by ', 1)[0] 2818 place = place.split(' and ') 2819 remain = remain.split('. The adjudication was judged by ', 1)[1] 2820 2821 adjudicator = remain.rsplit('.',1)[0] 2822 adjudicator = adjudicator.split(' and ') 2823 for arg in defendant: 2824 if arg != ROLE_PH_MAP['Defendant']: 2825 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 2826 for arg in prosecutor: 2827 if arg != ROLE_PH_MAP['Prosecutor']: 2828 output.append((arg, 'Prosecutor', {'cor tri cnt': a_cnt})) 2829 for arg in place: 2830 if arg != ROLE_PH_MAP['Place']: 2831 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2832 for arg in adjudicator: 2833 if arg != ROLE_PH_MAP['Adjudicator']: 2834 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 2835 except: 2836 pass 2837 used_o_cnt += 1 2838 return output 2839 2840 class Justice_Sue(event_template): 2841 2842 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2843 super().__init__(input_style, output_style, passage, event_type, gold_event) 2844 2845 @classmethod 2846 def get_keywords(self): 2847 return ['sue', 'lawsuit', 'suit'] 2848 2849 def get_output_template(self): 2850 output_template = '' 2851 for o_style in OUTPUT_STYLE_SET: 2852 if o_style in self.output_style: 2853 if o_style == 'trigger:sentence': 2854 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2855 if o_style == 'argument:sentence': 2856 output_template += ' \n {}'.format('somebody was sued by some other in somewhere. The adjudication was judged by some adjudicator.') 2857 return ('\n'.join(output_template.split('\n')[1:])).strip() 2858 2859 def generate_input_str(self, query_trigger): 2860 input_str = self.passage 2861 for i_style in INPUT_STYLE_SET: 2862 if i_style in self.input_style: 2863 if i_style == 'event_type': 2864 input_str += ' \n {}'.format('justice event, sue sub-type') 2865 if i_style == 'event_type_sent': 2866 input_str += ' \n {}'.format("The event is related to a court proceeding that has been initiated and someone sue the other.") 2867 if i_style == 'keywords': 2868 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2869 if i_style == 'triggers': 2870 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2871 if i_style == 'template': 2872 input_str += ' \n {}'.format(self.output_template) 2873 return input_str 2874 2875 def generate_output_str(self, query_trigger): 2876 assert self.gold_event is not None 2877 output_str = '' 2878 gold_sample = False 2879 for o_style in OUTPUT_STYLE_SET: 2880 if o_style in self.output_style: 2881 if o_style == 'trigger:sentence': 2882 if self.trigger_text != '': 2883 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2884 gold_sample = True 2885 else: 2886 output_str += ' \n Event trigger is <Trigger>' 2887 if o_style == 'argument:sentence': 2888 output_texts = [] 2889 for argu in self.arguments: 2890 filler = ( 2891 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 2892 " and ".join([ a['argument text'] for a in argu['Plaintiff']]) if "Plaintiff" in argu.keys() else ROLE_PH_MAP['Plaintiff'], 2893 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 2894 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 2895 ) 2896 output_texts.append("{} was sued by {} in {}. The adjudication was judged by {}.".format(*filler)) 2897 gold_sample = True 2898 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 2899 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 2900 return (output_str, gold_sample) 2901 2902 def decode(self, preds): 2903 output = [] 2904 for cnt, pred in enumerate(preds.split('\n')): 2905 used_o_cnt = 0 2906 full_pred = pred.strip() 2907 for o_style in OUTPUT_STYLE_SET: 2908 if o_style in self.output_style: 2909 if o_style == 'trigger:sentence': 2910 if used_o_cnt == cnt: 2911 try: 2912 triggers = full_pred.split('Event trigger is ', 1)[1] 2913 triggers = triggers.split(' and ') 2914 for t_cnt, t in enumerate(triggers): 2915 if t != '<Trigger>': 2916 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 2917 except: 2918 pass 2919 used_o_cnt += 1 2920 if o_style == 'argument:sentence': 2921 if used_o_cnt == cnt: 2922 try: 2923 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 2924 defendant = prediction.split(' was sued by ', 1)[0] 2925 defendant = defendant.split(' and ') 2926 remain = prediction.split(' was sued by ', 1)[1] 2927 2928 plaintiff = remain.split(' in ', 1)[0] 2929 plaintiff = plaintiff.split(' and ') 2930 remain = remain.split(' in ', 1)[1] 2931 2932 place = remain.split('. The adjudication was judged by ', 1)[0] 2933 place = place.split(' and ') 2934 remain = remain.split('. The adjudication was judged by ', 1)[1] 2935 2936 adjudicator = remain.rsplit('.',1)[0] 2937 adjudicator = adjudicator.split(' and ') 2938 for arg in defendant: 2939 if arg != ROLE_PH_MAP['Defendant']: 2940 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 2941 for arg in plaintiff: 2942 if arg != ROLE_PH_MAP['Plaintiff']: 2943 output.append((arg, 'Plaintiff', {'cor tri cnt': a_cnt})) 2944 for arg in place: 2945 if arg != ROLE_PH_MAP['Place']: 2946 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 2947 for arg in adjudicator: 2948 if arg != ROLE_PH_MAP['Adjudicator']: 2949 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 2950 except: 2951 pass 2952 used_o_cnt += 1 2953 return output 2954 2955 class Justice_Convict(event_template): 2956 2957 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 2958 super().__init__(input_style, output_style, passage, event_type, gold_event) 2959 2960 @classmethod 2961 def get_keywords(self): 2962 return ['convicted', 'guilty', 'verdict'] 2963 2964 def get_output_template(self): 2965 output_template = '' 2966 for o_style in OUTPUT_STYLE_SET: 2967 if o_style in self.output_style: 2968 if o_style == 'trigger:sentence': 2969 output_template += ' \n {}'.format('Event trigger is <Trigger>') 2970 if o_style == 'argument:sentence': 2971 output_template += ' \n {}'.format('somebody was convicted of a crime in somewhere. The adjudication was judged by some adjudicator.') 2972 return ('\n'.join(output_template.split('\n')[1:])).strip() 2973 2974 def generate_input_str(self, query_trigger): 2975 input_str = self.passage 2976 for i_style in INPUT_STYLE_SET: 2977 if i_style in self.input_style: 2978 if i_style == 'event_type': 2979 input_str += ' \n {}'.format('justice event, convict sub-type') 2980 if i_style == 'event_type_sent': 2981 input_str += ' \n {}'.format("The event is related to someone being found guilty of a crime.") 2982 if i_style == 'keywords': 2983 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 2984 if i_style == 'triggers': 2985 input_str += ' \n The event trigger word is {}'.format(query_trigger) 2986 if i_style == 'template': 2987 input_str += ' \n {}'.format(self.output_template) 2988 return input_str 2989 2990 def generate_output_str(self, query_trigger): 2991 assert self.gold_event is not None 2992 output_str = '' 2993 gold_sample = False 2994 for o_style in OUTPUT_STYLE_SET: 2995 if o_style in self.output_style: 2996 if o_style == 'trigger:sentence': 2997 if self.trigger_text != '': 2998 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 2999 gold_sample = True 3000 else: 3001 output_str += ' \n Event trigger is <Trigger>' 3002 if o_style == 'argument:sentence': 3003 output_texts = [] 3004 for argu in self.arguments: 3005 filler = ( 3006 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 3007 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 3008 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3009 ) 3010 output_texts.append("{} was convicted of a crime in {}. The adjudication was judged by {}.".format(*filler)) 3011 gold_sample = True 3012 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3013 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3014 return (output_str, gold_sample) 3015 3016 def decode(self, preds): 3017 output = [] 3018 for cnt, pred in enumerate(preds.split('\n')): 3019 used_o_cnt = 0 3020 full_pred = pred.strip() 3021 for o_style in OUTPUT_STYLE_SET: 3022 if o_style in self.output_style: 3023 if o_style == 'trigger:sentence': 3024 if used_o_cnt == cnt: 3025 try: 3026 triggers = full_pred.split('Event trigger is ', 1)[1] 3027 triggers = triggers.split(' and ') 3028 for t_cnt, t in enumerate(triggers): 3029 if t != '<Trigger>': 3030 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3031 except: 3032 pass 3033 used_o_cnt += 1 3034 if o_style == 'argument:sentence': 3035 if used_o_cnt == cnt: 3036 try: 3037 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3038 defendant = prediction.split(' was convicted of a crime in ', 1)[0] 3039 defendant = defendant.split(' and ') 3040 remain = prediction.split(' was convicted of a crime in ', 1)[1] 3041 3042 place = remain.split('. The adjudication was judged by ', 1)[0] 3043 place = place.split(' and ') 3044 remain = remain.split('. The adjudication was judged by ', 1)[1] 3045 3046 adjudicator = remain.rsplit('.',1)[0] 3047 adjudicator = adjudicator.split(' and ') 3048 for arg in defendant: 3049 if arg != ROLE_PH_MAP['Defendant']: 3050 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 3051 for arg in place: 3052 if arg != ROLE_PH_MAP['Place']: 3053 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 3054 for arg in adjudicator: 3055 if arg != ROLE_PH_MAP['Adjudicator']: 3056 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3057 except: 3058 pass 3059 used_o_cnt += 1 3060 return output 3061 3062 class Justice_Sentence(event_template): 3063 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3064 super().__init__(input_style, output_style, passage, event_type, gold_event) 3065 3066 @classmethod 3067 def get_keywords(self): 3068 return ['sentenced', 'sentencing', 'sentence'] 3069 3070 def get_output_template(self): 3071 output_template = '' 3072 for o_style in OUTPUT_STYLE_SET: 3073 if o_style in self.output_style: 3074 if o_style == 'trigger:sentence': 3075 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3076 if o_style == 'argument:sentence': 3077 output_template += ' \n {}'.format('somebody was sentenced to punishment in somewhere. The adjudication was judged by some adjudicator.') 3078 return ('\n'.join(output_template.split('\n')[1:])).strip() 3079 3080 def generate_input_str(self, query_trigger): 3081 input_str = self.passage 3082 for i_style in INPUT_STYLE_SET: 3083 if i_style in self.input_style: 3084 if i_style == 'event_type': 3085 input_str += ' \n {}'.format('justice event, sentence sub-type') 3086 if i_style == 'event_type_sent': 3087 input_str += ' \n {}'.format("The event is related to someone being sentenced to punishment because of a crime.") 3088 if i_style == 'keywords': 3089 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3090 if i_style == 'triggers': 3091 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3092 if i_style == 'template': 3093 input_str += ' \n {}'.format(self.output_template) 3094 return input_str 3095 3096 def generate_output_str(self, query_trigger): 3097 assert self.gold_event is not None 3098 output_str = '' 3099 gold_sample = False 3100 for o_style in OUTPUT_STYLE_SET: 3101 if o_style in self.output_style: 3102 if o_style == 'trigger:sentence': 3103 if self.trigger_text != '': 3104 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3105 gold_sample = True 3106 else: 3107 output_str += ' \n Event trigger is <Trigger>' 3108 if o_style == 'argument:sentence': 3109 output_texts = [] 3110 for argu in self.arguments: 3111 filler = ( 3112 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 3113 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 3114 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3115 ) 3116 output_texts.append("{} was sentenced to punishment in {}. The adjudication was judged by {}.".format(*filler)) 3117 gold_sample = True 3118 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3119 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3120 return (output_str, gold_sample) 3121 3122 def decode(self, preds): 3123 output = [] 3124 for cnt, pred in enumerate(preds.split('\n')): 3125 used_o_cnt = 0 3126 full_pred = pred.strip() 3127 for o_style in OUTPUT_STYLE_SET: 3128 if o_style in self.output_style: 3129 if o_style == 'trigger:sentence': 3130 if used_o_cnt == cnt: 3131 try: 3132 triggers = full_pred.split('Event trigger is ', 1)[1] 3133 triggers = triggers.split(' and ') 3134 for t_cnt, t in enumerate(triggers): 3135 if t != '<Trigger>': 3136 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3137 except: 3138 pass 3139 used_o_cnt += 1 3140 if o_style == 'argument:sentence': 3141 if used_o_cnt == cnt: 3142 try: 3143 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3144 defendant = prediction.split(' was sentenced to punishment in ', 1)[0] 3145 defendant = defendant.split(' and ') 3146 remain = prediction.split(' was sentenced to punishment in ', 1)[1] 3147 3148 place = remain.split('. The adjudication was judged by ', 1)[0] 3149 place = place.split(' and ') 3150 remain = remain.split('. The adjudication was judged by ', 1)[1] 3151 3152 adjudicator = remain.rsplit('.',1)[0] 3153 adjudicator = adjudicator.split(' and ') 3154 for arg in defendant: 3155 if arg != ROLE_PH_MAP['Defendant']: 3156 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 3157 for arg in place: 3158 if arg != ROLE_PH_MAP['Place']: 3159 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 3160 for arg in adjudicator: 3161 if arg != ROLE_PH_MAP['Adjudicator']: 3162 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3163 except: 3164 pass 3165 used_o_cnt += 1 3166 return output 3167 3168 class Justice_Fine(event_template): 3169 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3170 super().__init__(input_style, output_style, passage, event_type, gold_event) 3171 3172 @classmethod 3173 def get_keywords(self): 3174 return ['fine', 'fined', 'payouts'] 3175 3176 def get_output_template(self): 3177 output_template = '' 3178 for o_style in OUTPUT_STYLE_SET: 3179 if o_style in self.output_style: 3180 if o_style == 'trigger:sentence': 3181 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3182 if o_style == 'argument:sentence': 3183 output_template += ' \n {}'.format('some people or some organization in somewhere was ordered by some adjudicator to pay a fine.') 3184 return ('\n'.join(output_template.split('\n')[1:])).strip() 3185 3186 def generate_input_str(self, query_trigger): 3187 input_str = self.passage 3188 for i_style in INPUT_STYLE_SET: 3189 if i_style in self.input_style: 3190 if i_style == 'event_type': 3191 input_str += ' \n {}'.format('justice event, fine sub-type') 3192 if i_style == 'event_type_sent': 3193 input_str += ' \n {}'.format("The event is related to someone being issued a financial punishment.") 3194 if i_style == 'keywords': 3195 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3196 if i_style == 'triggers': 3197 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3198 if i_style == 'template': 3199 input_str += ' \n {}'.format(self.output_template) 3200 return input_str 3201 3202 def generate_output_str(self, query_trigger): 3203 assert self.gold_event is not None 3204 output_str = '' 3205 gold_sample = False 3206 for o_style in OUTPUT_STYLE_SET: 3207 if o_style in self.output_style: 3208 if o_style == 'trigger:sentence': 3209 if self.trigger_text != '': 3210 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3211 gold_sample = True 3212 else: 3213 output_str += ' \n Event trigger is <Trigger>' 3214 if o_style == 'argument:sentence': 3215 output_texts = [] 3216 for argu in self.arguments: 3217 filler = ( 3218 " and ".join([ a['argument text'] for a in argu['Entity']]) if "Entity" in argu.keys() else ROLE_PH_MAP['Entity'], 3219 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 3220 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3221 ) 3222 output_texts.append("{} in {} was ordered by {} to pay a fine.".format(*filler)) 3223 gold_sample = True 3224 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3225 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3226 return (output_str, gold_sample) 3227 3228 def decode(self, preds): 3229 output = [] 3230 for cnt, pred in enumerate(preds.split('\n')): 3231 used_o_cnt = 0 3232 full_pred = pred.strip() 3233 for o_style in OUTPUT_STYLE_SET: 3234 if o_style in self.output_style: 3235 if o_style == 'trigger:sentence': 3236 if used_o_cnt == cnt: 3237 try: 3238 triggers = full_pred.split('Event trigger is ', 1)[1] 3239 triggers = triggers.split(' and ') 3240 for t_cnt, t in enumerate(triggers): 3241 if t != '<Trigger>': 3242 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3243 except: 3244 pass 3245 used_o_cnt += 1 3246 if o_style == 'argument:sentence': 3247 if used_o_cnt == cnt: 3248 try: 3249 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3250 entity = prediction.split(' in ', 1)[0] 3251 entity = entity.split(' and ') 3252 remain = prediction.split(' in ', 1)[1] 3253 3254 place = remain.split(' was ordered by ', 1)[0] 3255 place = place.split(' and ') 3256 remain = remain.split(' was ordered by ', 1)[1] 3257 3258 adjudicator = remain.rsplit(' to pay a fine.',1)[0] 3259 adjudicator = adjudicator.split(' and ') 3260 for arg in entity: 3261 if arg != ROLE_PH_MAP['Entity']: 3262 output.append((arg, 'Entity', {'cor tri cnt': a_cnt})) 3263 for arg in place: 3264 if arg != ROLE_PH_MAP['Place']: 3265 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 3266 for arg in adjudicator: 3267 if arg != ROLE_PH_MAP['Adjudicator']: 3268 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3269 except Exception as e: 3270 pass 3271 used_o_cnt += 1 3272 return output 3273 3274 class Justice_Execute(event_template): 3275 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3276 super().__init__(input_style, output_style, passage, event_type, gold_event) 3277 3278 @classmethod 3279 def get_keywords(self): 3280 return ['execution', 'executed', 'execute'] 3281 3282 def get_output_template(self): 3283 output_template = '' 3284 for o_style in OUTPUT_STYLE_SET: 3285 if o_style in self.output_style: 3286 if o_style == 'trigger:sentence': 3287 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3288 if o_style == 'argument:sentence': 3289 output_template += ' \n {}'.format('somebody was executed by somebody or some organization at somewhere.') 3290 return ('\n'.join(output_template.split('\n')[1:])).strip() 3291 3292 def generate_input_str(self, query_trigger): 3293 input_str = self.passage 3294 for i_style in INPUT_STYLE_SET: 3295 if i_style in self.input_style: 3296 if i_style == 'event_type': 3297 input_str += ' \n {}'.format('justice event, execute sub-type') 3298 if i_style == 'event_type_sent': 3299 input_str += ' \n {}'.format("The event is related to someone being executed to death.") 3300 if i_style == 'keywords': 3301 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3302 if i_style == 'triggers': 3303 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3304 if i_style == 'template': 3305 input_str += ' \n {}'.format(self.output_template) 3306 return input_str 3307 3308 def generate_output_str(self, query_trigger): 3309 assert self.gold_event is not None 3310 output_str = '' 3311 gold_sample = False 3312 for o_style in OUTPUT_STYLE_SET: 3313 if o_style in self.output_style: 3314 if o_style == 'trigger:sentence': 3315 if self.trigger_text != '': 3316 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3317 gold_sample = True 3318 else: 3319 output_str += ' \n Event trigger is <Trigger>' 3320 if o_style == 'argument:sentence': 3321 output_texts = [] 3322 for argu in self.arguments: 3323 filler = ( 3324 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 3325 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'], 3326 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'] 3327 ) 3328 output_texts.append("{} was executed by {} at {}.".format(*filler)) 3329 gold_sample = True 3330 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3331 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3332 return (output_str, gold_sample) 3333 3334 def decode(self, preds): 3335 output = [] 3336 for cnt, pred in enumerate(preds.split('\n')): 3337 used_o_cnt = 0 3338 full_pred = pred.strip() 3339 for o_style in OUTPUT_STYLE_SET: 3340 if o_style in self.output_style: 3341 if o_style == 'trigger:sentence': 3342 if used_o_cnt == cnt: 3343 try: 3344 triggers = full_pred.split('Event trigger is ', 1)[1] 3345 triggers = triggers.split(' and ') 3346 for t_cnt, t in enumerate(triggers): 3347 if t != '<Trigger>': 3348 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3349 except: 3350 pass 3351 used_o_cnt += 1 3352 if o_style == 'argument:sentence': 3353 if used_o_cnt == cnt: 3354 try: 3355 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3356 person = prediction.split(' was executed by ', 1)[0] 3357 person = person.split(' and ') 3358 remain = prediction.split(' was executed by ', 1)[1] 3359 3360 agent = remain.split(' at ', 1)[0] 3361 agent = agent.split(' and ') 3362 remain = remain.split(' at ', 1)[1] 3363 3364 place = remain.rsplit('.',1)[0] 3365 place = place.split(' and ') 3366 for arg in person: 3367 if arg != ROLE_PH_MAP['Person']: 3368 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 3369 for arg in place: 3370 if arg != ROLE_PH_MAP['Place']: 3371 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 3372 for arg in agent: 3373 if arg != ROLE_PH_MAP['Agent']: 3374 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 3375 except: 3376 pass 3377 used_o_cnt += 1 3378 return output 3379 3380 class Justice_Extradite(event_template): 3381 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3382 super().__init__(input_style, output_style, passage, event_type, gold_event) 3383 3384 @classmethod 3385 def get_keywords(self): 3386 return ['extradition', 'extradited', 'extraditing'] 3387 3388 def get_output_template(self): 3389 output_template = '' 3390 for o_style in OUTPUT_STYLE_SET: 3391 if o_style in self.output_style: 3392 if o_style == 'trigger:sentence': 3393 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3394 if o_style == 'argument:sentence': 3395 output_template += ' \n {}'.format('somebody was extradicted to somewhere from some place. somebody or some organization was responsible for the extradition.') 3396 return ('\n'.join(output_template.split('\n')[1:])).strip() 3397 3398 def generate_input_str(self, query_trigger): 3399 input_str = self.passage 3400 for i_style in INPUT_STYLE_SET: 3401 if i_style in self.input_style: 3402 if i_style == 'event_type': 3403 input_str += ' \n {}'.format('justice event, extradite sub-type') 3404 if i_style == 'event_type_sent': 3405 input_str += ' \n {}'.format('The event is related to justice. The event occurs when a person was extradited from one place to another place.') 3406 if i_style == 'keywords': 3407 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3408 if i_style == 'triggers': 3409 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3410 if i_style == 'template': 3411 input_str += ' \n {}'.format(self.output_template) 3412 3413 return input_str 3414 3415 def generate_output_str(self, query_trigger): 3416 assert self.gold_event is not None 3417 output_str = '' 3418 gold_sample = False 3419 for o_style in OUTPUT_STYLE_SET: 3420 if o_style in self.output_style: 3421 if o_style == 'trigger:sentence': 3422 if self.trigger_text != '': 3423 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3424 gold_sample = True 3425 else: 3426 output_str += ' \n Event trigger is <Trigger>' 3427 if o_style == 'argument:sentence': 3428 output_texts = [] 3429 for argu in self.arguments: 3430 filler = ( 3431 " and ".join([ a['argument text'] for a in argu['Person']]) if "Person" in argu.keys() else ROLE_PH_MAP['Person'], 3432 " and ".join([ a['argument text'] for a in argu['Destination']]) if "Destination" in argu.keys() else ROLE_PH_MAP['Destination'], 3433 " and ".join([ a['argument text'] for a in argu['Origin']]) if "Origin" in argu.keys() else ROLE_PH_MAP['Origin'], 3434 " and ".join([ a['argument text'] for a in argu['Agent']]) if "Agent" in argu.keys() else ROLE_PH_MAP['Agent'] 3435 ) 3436 output_texts.append("{} was extradicted to {} from {}. {} was responsible for the extradition.".format(*filler)) 3437 gold_sample = True 3438 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3439 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3440 return (output_str, gold_sample) 3441 3442 def decode(self, preds): 3443 output = [] 3444 for cnt, pred in enumerate(preds.split('\n')): 3445 used_o_cnt = 0 3446 full_pred = pred.strip() 3447 for o_style in OUTPUT_STYLE_SET: 3448 if o_style in self.output_style: 3449 if o_style == 'trigger:sentence': 3450 if used_o_cnt == cnt: 3451 try: 3452 triggers = full_pred.split('Event trigger is ', 1)[1] 3453 triggers = triggers.split(' and ') 3454 for t_cnt, t in enumerate(triggers): 3455 if t != '<Trigger>': 3456 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3457 except: 3458 pass 3459 used_o_cnt += 1 3460 if o_style == 'argument:sentence': 3461 if used_o_cnt == cnt: 3462 try: 3463 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3464 person = prediction.split(' was extradicted to ')[0] 3465 person = person.split(' and ') 3466 3467 destination = (prediction.split(' was extradicted to ')[1]).split(' from ')[0] 3468 destination = destination.split(' and ') 3469 3470 origin = ((prediction.split(' was extradicted to ')[1]).split(' from ')[1]).split('.', 1)[0] 3471 origin = origin.split(' and ') 3472 3473 remain = ((prediction.split(' was extradicted to ')[1]).split(' from ')[1]).split('.', 1)[1] 3474 3475 if 'was responsible for' in remain: 3476 agent = (remain.split(' was responsible for the extradition.')[0]).strip() 3477 agent = agent.split(' and ') 3478 else: 3479 agent = [] 3480 3481 for arg in agent: 3482 if arg != ROLE_PH_MAP['Agent']: 3483 output.append((arg, 'Agent', {'cor tri cnt': a_cnt})) 3484 for arg in person: 3485 if arg != ROLE_PH_MAP['Person']: 3486 output.append((arg, 'Person', {'cor tri cnt': a_cnt})) 3487 for arg in destination: 3488 if arg != ROLE_PH_MAP['Destination']: 3489 output.append((arg, 'Destination', {'cor tri cnt': a_cnt})) 3490 for arg in origin: 3491 if arg != ROLE_PH_MAP['Origin']: 3492 output.append((arg, 'Origin', {'cor tri cnt': a_cnt})) 3493 except: 3494 pass 3495 used_o_cnt += 1 3496 return output 3497 3498 class Justice_Acquit(event_template): 3499 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3500 super().__init__(input_style, output_style, passage, event_type, gold_event) 3501 3502 @classmethod 3503 def get_keywords(self): 3504 return ['acquitted', 'acquittal', 'acquit'] 3505 3506 def get_output_template(self): 3507 output_template = '' 3508 for o_style in OUTPUT_STYLE_SET: 3509 if o_style in self.output_style: 3510 if o_style == 'trigger:sentence': 3511 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3512 if o_style == 'argument:sentence': 3513 output_template += ' \n {}'.format('somebody was acquitted of the charges by some adjudicator.') 3514 return ('\n'.join(output_template.split('\n')[1:])).strip() 3515 3516 def generate_input_str(self, query_trigger): 3517 input_str = self.passage 3518 for i_style in INPUT_STYLE_SET: 3519 if i_style in self.input_style: 3520 if i_style == 'event_type': 3521 input_str += ' \n {}'.format('justice event, acquit sub-type') 3522 if i_style == 'event_type_sent': 3523 input_str += ' \n {}'.format("The event is related to someone being acquitted.") 3524 if i_style == 'keywords': 3525 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3526 if i_style == 'triggers': 3527 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3528 if i_style == 'template': 3529 input_str += ' \n {}'.format(self.output_template) 3530 return input_str 3531 3532 def generate_output_str(self, query_trigger): 3533 assert self.gold_event is not None 3534 output_str = '' 3535 gold_sample = False 3536 for o_style in OUTPUT_STYLE_SET: 3537 if o_style in self.output_style: 3538 if o_style == 'trigger:sentence': 3539 if self.trigger_text != '': 3540 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3541 gold_sample = True 3542 else: 3543 output_str += ' \n Event trigger is <Trigger>' 3544 if o_style == 'argument:sentence': 3545 output_texts = [] 3546 for argu in self.arguments: 3547 filler = ( 3548 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 3549 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3550 ) 3551 output_texts.append("{} was acquitted of the charges by {}.".format(*filler)) 3552 gold_sample = True 3553 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3554 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3555 return (output_str, gold_sample) 3556 3557 def decode(self, preds): 3558 output = [] 3559 for cnt, pred in enumerate(preds.split('\n')): 3560 used_o_cnt = 0 3561 full_pred = pred.strip() 3562 for o_style in OUTPUT_STYLE_SET: 3563 if o_style in self.output_style: 3564 if o_style == 'trigger:sentence': 3565 if used_o_cnt == cnt: 3566 try: 3567 triggers = full_pred.split('Event trigger is ', 1)[1] 3568 triggers = triggers.split(' and ') 3569 for t_cnt, t in enumerate(triggers): 3570 if t != '<Trigger>': 3571 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3572 except: 3573 pass 3574 used_o_cnt += 1 3575 if o_style == 'argument:sentence': 3576 if used_o_cnt == cnt: 3577 try: 3578 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3579 defendant = prediction.split(' was acquitted of the charges by ', 1)[0] 3580 defendant = defendant.split(' and ') 3581 remain = prediction.split(' was acquitted of the charges by ', 1)[1] 3582 3583 adjudicator = remain.rsplit('.',1)[0] 3584 adjudicator = adjudicator.split(' and ') 3585 for arg in defendant: 3586 if arg != ROLE_PH_MAP['Defendant']: 3587 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 3588 for arg in adjudicator: 3589 if arg != ROLE_PH_MAP['Adjudicator']: 3590 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3591 except: 3592 pass 3593 used_o_cnt += 1 3594 return output 3595 3596 class Justice_Pardon(event_template): 3597 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3598 super().__init__(input_style, output_style, passage, event_type, gold_event) 3599 3600 @classmethod 3601 def get_keywords(self): 3602 return ['pardon', 'pardoned', 'remission'] 3603 3604 def get_output_template(self): 3605 output_template = '' 3606 for o_style in OUTPUT_STYLE_SET: 3607 if o_style in self.output_style: 3608 if o_style == 'trigger:sentence': 3609 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3610 if o_style == 'argument:sentence': 3611 output_template += ' \n {}'.format('somebody received a pardon from some adjudicator.') 3612 return ('\n'.join(output_template.split('\n')[1:])).strip() 3613 3614 def generate_input_str(self, query_trigger): 3615 input_str = self.passage 3616 for i_style in INPUT_STYLE_SET: 3617 if i_style in self.input_style: 3618 if i_style == 'event_type': 3619 input_str += ' \n {}'.format('justice event, pardon sub-type') 3620 if i_style == 'event_type_sent': 3621 input_str += ' \n {}'.format("The event is related to someone being pardoned.") 3622 if i_style == 'keywords': 3623 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3624 if i_style == 'triggers': 3625 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3626 if i_style == 'template': 3627 input_str += ' \n {}'.format(self.output_template) 3628 return input_str 3629 3630 def generate_output_str(self, query_trigger): 3631 assert self.gold_event is not None 3632 output_str = '' 3633 gold_sample = False 3634 for o_style in OUTPUT_STYLE_SET: 3635 if o_style in self.output_style: 3636 if o_style == 'trigger:sentence': 3637 if self.trigger_text != '': 3638 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3639 gold_sample = True 3640 else: 3641 output_str += ' \n Event trigger is <Trigger>' 3642 if o_style == 'argument:sentence': 3643 output_texts = [] 3644 for argu in self.arguments: 3645 filler = ( 3646 " and ".join([ a['argument text'] for a in argu['Defendant']]) if "Defendant" in argu.keys() else ROLE_PH_MAP['Defendant'], 3647 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3648 ) 3649 output_texts.append("{} received a pardon from {}.".format(*filler)) 3650 gold_sample = True 3651 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3652 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3653 return (output_str, gold_sample) 3654 3655 def decode(self, preds): 3656 output = [] 3657 for cnt, pred in enumerate(preds.split('\n')): 3658 used_o_cnt = 0 3659 full_pred = pred.strip() 3660 for o_style in OUTPUT_STYLE_SET: 3661 if o_style in self.output_style: 3662 if o_style == 'trigger:sentence': 3663 if used_o_cnt == cnt: 3664 try: 3665 triggers = full_pred.split('Event trigger is ', 1)[1] 3666 triggers = triggers.split(' and ') 3667 for t_cnt, t in enumerate(triggers): 3668 if t != '<Trigger>': 3669 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3670 except: 3671 pass 3672 used_o_cnt += 1 3673 if o_style == 'argument:sentence': 3674 if used_o_cnt == cnt: 3675 try: 3676 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3677 defendant = prediction.split(' received a pardon from ', 1)[0] 3678 defendant = defendant.split(' and ') 3679 remain = prediction.split(' received a pardon from ', 1)[1] 3680 3681 adjudicator = remain.rsplit('.',1)[0] 3682 adjudicator = adjudicator.split(' and ') 3683 for arg in defendant: 3684 if arg != ROLE_PH_MAP['Defendant']: 3685 output.append((arg, 'Defendant', {'cor tri cnt': a_cnt})) 3686 for arg in adjudicator: 3687 if arg != ROLE_PH_MAP['Adjudicator']: 3688 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3689 except: 3690 pass 3691 used_o_cnt += 1 3692 return output 3693 3694 class Justice_Appeal(event_template): 3695 def __init__(self, input_style, output_style, passage, event_type, gold_event=None): 3696 super().__init__(input_style, output_style, passage, event_type, gold_event) 3697 3698 @classmethod 3699 def get_keywords(self): 3700 return ['appeal', 'appealing', 'appeals'] 3701 3702 def get_output_template(self): 3703 output_template = '' 3704 for o_style in OUTPUT_STYLE_SET: 3705 if o_style in self.output_style: 3706 if o_style == 'trigger:sentence': 3707 output_template += ' \n {}'.format('Event trigger is <Trigger>') 3708 if o_style == 'argument:sentence': 3709 output_template += ' \n {}'.format('some other in somewhere appealed the adjudication from some adjudicator.') 3710 return ('\n'.join(output_template.split('\n')[1:])).strip() 3711 3712 def generate_input_str(self, query_trigger): 3713 input_str = self.passage 3714 for i_style in INPUT_STYLE_SET: 3715 if i_style in self.input_style: 3716 if i_style == 'event_type': 3717 input_str += ' \n {}'.format('justice event, appeal sub-type') 3718 if i_style == 'event_type_sent': 3719 input_str += ' \n {}'.format("The event is related to someone appealing the decision of a court.") 3720 if i_style == 'keywords': 3721 input_str += ' \n Similar triggers such as {}'.format(', '.join(self.get_keywords())) 3722 if i_style == 'triggers': 3723 input_str += ' \n The event trigger word is {}'.format(query_trigger) 3724 if i_style == 'template': 3725 input_str += ' \n {}'.format(self.output_template) 3726 return input_str 3727 3728 def generate_output_str(self, query_trigger): 3729 assert self.gold_event is not None 3730 output_str = '' 3731 gold_sample = False 3732 for o_style in OUTPUT_STYLE_SET: 3733 if o_style in self.output_style: 3734 if o_style == 'trigger:sentence': 3735 if self.trigger_text != '': 3736 output_str += ' \n Event trigger is {}'.format(self.trigger_text) 3737 gold_sample = True 3738 else: 3739 output_str += ' \n Event trigger is <Trigger>' 3740 if o_style == 'argument:sentence': 3741 output_texts = [] 3742 for argu in self.arguments: 3743 filler = ( 3744 " and ".join([ a['argument text'] for a in argu['Plaintiff']]) if "Plaintiff" in argu.keys() else ROLE_PH_MAP['Plaintiff'], 3745 " and ".join([ a['argument text'] for a in argu['Place']]) if "Place" in argu.keys() else ROLE_PH_MAP['Place'], 3746 " and ".join([ a['argument text'] for a in argu['Adjudicator']]) if "Adjudicator" in argu.keys() else ROLE_PH_MAP['Adjudicator'] 3747 ) 3748 output_texts.append("{} in {} appealed the adjudication from {}.".format(*filler)) 3749 gold_sample = True 3750 output_str += ' \n {}'.format(' <sep> '.join(output_texts)) 3751 output_str = ('\n'.join(output_str.split('\n')[1:])).strip() 3752 return (output_str, gold_sample) 3753 3754 def decode(self, preds): 3755 output = [] 3756 for cnt, pred in enumerate(preds.split('\n')): 3757 used_o_cnt = 0 3758 full_pred = pred.strip() 3759 for o_style in OUTPUT_STYLE_SET: 3760 if o_style in self.output_style: 3761 if o_style == 'trigger:sentence': 3762 if used_o_cnt == cnt: 3763 try: 3764 triggers = full_pred.split('Event trigger is ', 1)[1] 3765 triggers = triggers.split(' and ') 3766 for t_cnt, t in enumerate(triggers): 3767 if t != '<Trigger>': 3768 output.append((t, self.event_type, {'tri counter': t_cnt})) # (text, type, kwargs) 3769 except: 3770 pass 3771 used_o_cnt += 1 3772 if o_style == 'argument:sentence': 3773 if used_o_cnt == cnt: 3774 try: 3775 for a_cnt, prediction in enumerate(full_pred.split(' <sep> ')): 3776 plaintiff = prediction.split(' in ', 1)[0] 3777 plaintiff = plaintiff.split(' and ') 3778 remain = prediction.split(' in ', 1)[1] 3779 3780 place = remain.split(' appealed the adjudication from ', 1)[0] 3781 place = place.split(' and ') 3782 remain = remain.split(' appealed the adjudication from ', 1)[1] 3783 3784 adjudicator = remain.rsplit('.',1)[0] 3785 adjudicator = adjudicator.split(' and ') 3786 3787 for arg in plaintiff: 3788 if arg != ROLE_PH_MAP['Plaintiff']: 3789 output.append((arg, 'Plaintiff', {'cor tri cnt': a_cnt})) 3790 for arg in place: 3791 if arg != ROLE_PH_MAP['Place']: 3792 output.append((arg, 'Place', {'cor tri cnt': a_cnt})) 3793 for arg in adjudicator: 3794 if arg != ROLE_PH_MAP['Adjudicator']: 3795 output.append((arg, 'Adjudicator', {'cor tri cnt': a_cnt})) 3796 except: 3797 pass 3798 used_o_cnt += 1 3799 return output