|
| 1 | +""" |
| 2 | +################################################################################################## |
| 3 | +# Copyright Info : Copyright (c) Davar Lab @ Hikvision Research Institute. All rights reserved. |
| 4 | +# Filename : span_converter.py |
| 5 | +# Abstract : |
| 6 | +
|
| 7 | +# Current Version: 1.0.0 |
| 8 | +# Date : 2022-05-06 |
| 9 | +################################################################################################## |
| 10 | +""" |
| 11 | +from seqeval.scheme import Tokens, IOBES |
| 12 | +from davarocr.davar_common.core import CONVERTERS |
| 13 | +from .transformers_converter import TransformersConverter |
| 14 | + |
| 15 | + |
| 16 | +@CONVERTERS.register_module() |
| 17 | +class SpanConverter(TransformersConverter): |
| 18 | + """Span converter, converter for span model. |
| 19 | + """ |
| 20 | + def _generate_labelid_dict(self): |
| 21 | + label2id_dict = {label: i for i, label in enumerate(['O'] + self.label_list)} |
| 22 | + id2label_dict = {value: key for key, value in label2id_dict.items()} |
| 23 | + return label2id_dict, id2label_dict |
| 24 | + |
| 25 | + |
| 26 | + def _extract_subjects(self, seq): |
| 27 | + """Get entities from label sequence |
| 28 | + """ |
| 29 | + entities = [(t.to_tuple()[1], t.to_tuple()[2], t.to_tuple()[3]) for t in Tokens(seq, IOBES).entities] |
| 30 | + return entities |
| 31 | + |
| 32 | + |
| 33 | + def convert_entity2label(self, labels): |
| 34 | + """Convert labeled entities to ids. |
| 35 | +
|
| 36 | + Args: |
| 37 | + labels (list): eg:['B-PER', 'I-PER'] |
| 38 | +
|
| 39 | + Returns: |
| 40 | + dict: corresponding ids |
| 41 | + """ |
| 42 | + labels = self._labels_convert(labels, self.only_label_first_subword) |
| 43 | + cls_token_at_end=self.cls_token_at_end |
| 44 | + pad_on_left = self.pad_on_left |
| 45 | + label2id = self.label2id_dict |
| 46 | + subjects = self._extract_subjects(labels)#get entities |
| 47 | + start_ids = [0] * len(labels) |
| 48 | + end_ids = [0] * len(labels) |
| 49 | + subjects_id = [] |
| 50 | + for subject in subjects: |
| 51 | + label = subject[0] |
| 52 | + start = subject[1] |
| 53 | + end = subject[2] |
| 54 | + |
| 55 | + #set label for span |
| 56 | + start_ids[start] = label2id[label] |
| 57 | + end_ids[end-1] = label2id[label]#the true position is end-1 |
| 58 | + subjects_id.append((label2id[label], start, end)) |
| 59 | + |
| 60 | + # Account for [CLS] and [SEP] with "- 2". |
| 61 | + special_tokens_count = 2 |
| 62 | + if len(labels) > self.max_len - special_tokens_count: |
| 63 | + start_ids = start_ids[: (self.max_len - special_tokens_count)] |
| 64 | + end_ids = end_ids[: (self.max_len - special_tokens_count)] |
| 65 | + |
| 66 | + #add sep |
| 67 | + start_ids += [0] |
| 68 | + end_ids += [0] |
| 69 | + if cls_token_at_end: |
| 70 | + #add [CLS] at end |
| 71 | + start_ids += [0] |
| 72 | + end_ids += [0] |
| 73 | + else: |
| 74 | + #add [CLS] at begin |
| 75 | + start_ids = [0]+ start_ids |
| 76 | + end_ids = [0]+ end_ids |
| 77 | + padding_length = self.max_len - len(labels) - 2 |
| 78 | + if pad_on_left: |
| 79 | + #pad on left |
| 80 | + start_ids = ([0] * padding_length) + start_ids |
| 81 | + end_ids = ([0] * padding_length) + end_ids |
| 82 | + else: |
| 83 | + #pad on right |
| 84 | + start_ids += ([0] * padding_length) |
| 85 | + end_ids += ([0] * padding_length) |
| 86 | + res = dict(start_positions=start_ids, end_positions=end_ids) |
| 87 | + return res |
| 88 | + |
| 89 | + def convert_pred2entities(self, preds, masks, **kwargs): |
| 90 | + """Gets entities from preds. |
| 91 | +
|
| 92 | + Args: |
| 93 | + preds (list): Sequence of preds. |
| 94 | + masks (tensor): The valid part is 1 and the invalid part is 0. |
| 95 | + Returns: |
| 96 | + list: List of [[[entity_type, |
| 97 | + entity_start, entity_end]]]. |
| 98 | + """ |
| 99 | + id2label = self.id2label |
| 100 | + pred_entities = [] |
| 101 | + for pred in preds: |
| 102 | + entities = [] |
| 103 | + entity = [0, 0, 0] |
| 104 | + for tag in pred: |
| 105 | + entity[0] = id2label[tag[0]] |
| 106 | + entity[1] = tag[1] - 1 |
| 107 | + entity[2] = tag[2] - 1 |
| 108 | + entities.append(entity.copy()) |
| 109 | + pred_entities.append(entities.copy()) |
| 110 | + tokens_index = [index.cpu().numpy().tolist()[0] for index in kwargs['tokens_index']] |
| 111 | + pred_entities = [self._labels_convert_ori(pred_entity, tokens_index) for pred_entity in pred_entities] |
| 112 | + return pred_entities |
0 commit comments