-
Notifications
You must be signed in to change notification settings - Fork 1
/
vis.py
70 lines (57 loc) · 1.87 KB
/
vis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import imgkit
from pathlib import Path # maybe needed; used in
from spacy import displacy
def color_str(string, clr):
return clr + string + Color.END
class Color:
""" Source: https://stackoverflow.com/a/17303428 """
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
def save_heatmap_as_image(hm, filename):
ents = []
colors = {}
ii = 0
for color_token in hm:
ff = ii + len(color_token.token)
# One entity in displaCy contains start and end markers (character index) and optionally a label
# The label can be added by setting "attribution_labels" to True
ent = {
'start': ii,
'end': ff,
'label': str(color_token.score),
}
ents.append(ent)
# A "colors" dict takes care of the mapping between attribution labels and hex colors
colors[str(color_token.score)] = color_token.hex()
ii = ff
to_render = {
'text': ''.join([t.token for t in hm]),
'ents': ents,
}
template = """
<mark class="entity" style="background: {bg}; padding: 0.15em 0.3em; margin: 0 0.2em; line-height: 2.2;
border-radius: 0.25em; box-decoration-break: clone; -webkit-box-decoration-break: clone">
{text}
</mark>
"""
html = displacy.render(
to_render,
style='ent',
manual=True,
jupyter=False,
page=True,
options={'template': template,
'colors': colors,
}
)
#output_path = Path("./heatmap_renders/" + filename)
# output_path.open("w", encoding="utf-8").write(html)
image = imgkit.from_string(html, "heatmap_renders/" + filename)