Find Role's offset using closest matched with trigger work. :param instance: :return:
(instance)
| 92 | return matched_list |
| 93 | |
| 94 | def record_to_offset(instance): |
| 95 | """ |
| 96 | Find Role's offset using closest matched with trigger work. |
| 97 | :param instance: |
| 98 | :return: |
| 99 | """ |
| 100 | trigger_list = list() |
| 101 | role_list = list() |
| 102 | |
| 103 | token_list = instance['text'].split() |
| 104 | |
| 105 | trigger_matched_set = set() |
| 106 | for record in instance['pred_record']: |
| 107 | event_type = record['type'] |
| 108 | trigger = record['trigger'] |
| 109 | matched_list = match_sublist(token_list, trigger.split()) |
| 110 | |
| 111 | trigger_offset = None |
| 112 | for matched in matched_list: |
| 113 | if matched not in trigger_matched_set: |
| 114 | trigger_list += [(event_type, matched)] |
| 115 | trigger_offset = matched |
| 116 | trigger_matched_set.add(matched) |
| 117 | break |
| 118 | |
| 119 | # No trigger word, skip the record |
| 120 | if trigger_offset is None: |
| 121 | break |
| 122 | |
| 123 | for _, role_type, text_str in record['roles']: |
| 124 | matched_list = match_sublist(token_list, text_str.split()) |
| 125 | if len(matched_list) == 1: |
| 126 | role_list += [(event_type, role_type, matched_list[0])] |
| 127 | elif len(matched_list) == 0: |
| 128 | sys.stderr.write("[Cannot reconstruct]: %s %s\n" % |
| 129 | (text_str, token_list)) |
| 130 | else: |
| 131 | abs_distances = [abs(match[0] - trigger_offset[0]) |
| 132 | for match in matched_list] |
| 133 | closest_index = np.argmin(abs_distances) |
| 134 | role_list += [(event_type, role_type, |
| 135 | matched_list[closest_index])] |
| 136 | |
| 137 | return instance['text'], trigger_list, role_list |
| 138 | |
| 139 | class Metric: |
| 140 | def __init__(self): |
nothing calls this directly
no test coverage detected