-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.py
More file actions
149 lines (132 loc) · 4.62 KB
/
Copy pathhelper.py
File metadata and controls
149 lines (132 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from typing import List, Union
import numpy as np
import html
def get_config():
return {
"_num_labels": 14,
"architectures": ["BertForTokenClassification"],
"attention_probs_dropout_prob": 0.1,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"id2label": {
"0": "O",
"1": "OBJ",
"2": "TME",
"3": "ORG/PRS",
"4": "OBJ/ORG",
"5": "PRS/WRK",
"6": "WRK",
"7": "LOC",
"8": "ORG",
"9": "PER",
"10": "LOC/PRS",
"11": "LOC/ORG",
"12": "MSR",
"13": "EVN"
},
"initializer_range": 0.02,
"intermediate_size": 3072,
"label2id": {
"EVN": 13,
"LOC": 7,
"LOC/ORG": 11,
"LOC/PRS": 10,
"MSR": 12,
"O": 0,
"OBJ": 1,
"OBJ/ORG": 4,
"ORG": 8,
"ORG/PRS": 3,
"PER": 9,
"PRS/WRK": 5,
"TME": 2,
"WRK": 6
},
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"num_attention_heads": 12,
"num_hidden_layers": 12,
"output_past": True,
"pad_token_id": 0,
"type_vocab_size": 2,
"vocab_size": 50325
}
def group_sub_entities(entities: List[dict], tokenizer) -> dict:
"""
Group together the adjacent tokens with the same entity predicted.
Args:
entities (:obj:`dict`): The entities predicted by the pipeline.
"""
# Get the first entity in the entity group
entity = entities[0]["entity"].split("-")[-1]
scores = np.nanmean([entity["score"] for entity in entities])
tokens = [entity["word"] for entity in entities]
entity_group = {
"entity_group": entity,
"score": np.mean(scores),
"word": tokenizer.convert_tokens_to_string(tokens)
}
return entity_group
def group_entities(entities: List[dict], tokenizer) -> List[dict]:
"""
Find and group together the adjacent tokens with the same entity predicted.
Args:
entities (:obj:`dict`): The entities predicted by the pipeline.
"""
entity_groups = []
entity_group_disagg = []
if entities:
last_idx = entities[-1]["index"]
for entity in entities:
is_last_idx = entity["index"] == last_idx
if not entity_group_disagg:
entity_group_disagg += [entity]
if is_last_idx:
entity_groups += [group_sub_entities(entity_group_disagg, tokenizer)]
continue
# If the current entity is similar and adjacent to the previous entity, append it to the disaggregated entity group
# The split is meant to account for the "B" and "I" suffixes
if (
entity["entity"].split("-")[-1] == entity_group_disagg[-1]["entity"].split("-")[-1]
and entity["index"] == entity_group_disagg[-1]["index"] + 1
):
entity_group_disagg += [entity]
# Group the entities at the last entity
if is_last_idx:
entity_groups += [group_sub_entities(entity_group_disagg, tokenizer)]
# If the current entity is different from the previous entity, aggregate the disaggregated entity group
else:
entity_groups += [group_sub_entities(entity_group_disagg, tokenizer)]
entity_group_disagg = [entity]
# If it's the last entity, add it to the entity groups
if is_last_idx:
entity_groups += [group_sub_entities(entity_group_disagg, tokenizer)]
return entity_groups
TAGGED_ENTITY = """
<mark class="entity" style="background: {color}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 3; border-radius: 0.35em; box-decoration-break: clone; -webkit-box-decoration-break: clone">
{entity}
<span style="font-size: 0.8em; font-weight: bold; line-height: 3; border-radius: 0.35em; text-transform: uppercase; vertical-align: middle; margin-left: 0.5rem">{label}</span>
</mark>
"""
PARAGRAPH = """<p>{sentence}</p>"""
def render_ner_html_custom(
text: str,
predictions: List[dict],
colors={
"PER": "#F7FF53",
"ORG": "#E8902E",
"LOC": "#FF40A3",
"MISC": "#4647EB",
"O": "#ddd",
},
default_color: str = "#ddd"
) -> str:
escaped_text = html.escape(text).replace("\n", "<br/>")
for prediction in predictions:
tag = prediction['entity_group']
tag_text = TAGGED_ENTITY.format(entity=prediction['word'], label=tag, color=colors.get(tag, default_color))
escaped_text = escaped_text.replace(prediction['word'], tag_text)
line = PARAGRAPH.format(sentence="".join(escaped_text))
return line