File size: 6,321 Bytes
588a02c
 
 
 
 
eb4710d
f9dd31c
eb4710d
 
 
 
 
588a02c
 
eb4710d
 
 
 
 
 
588a02c
eb4710d
 
 
 
588a02c
 
eb4710d
588a02c
 
eb4710d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
588a02c
eb4710d
 
 
588a02c
eb4710d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f38dbc0
 
eb4710d
 
 
 
 
 
 
 
 
 
f38dbc0
eb4710d
 
 
 
 
 
f38dbc0
 
 
eb4710d
f38dbc0
 
eb4710d
 
 
 
 
 
 
 
f38dbc0
eb4710d
f38dbc0
eb4710d
 
 
f38dbc0
 
 
 
 
 
 
 
 
f9dd31c
 
 
 
 
 
 
 
 
 
f38dbc0
 
 
32fbbc2
f38dbc0
 
32fbbc2
f38dbc0
0fde90f
f38dbc0
f9dd31c
 
 
eb4710d
 
 
 
 
f38dbc0
eb4710d
f38dbc0
 
 
eb4710d
f9dd31c
 
 
f38dbc0
 
 
 
 
 
32fbbc2
f38dbc0
 
 
 
 
 
 
 
 
 
32fbbc2
 
 
 
 
 
 
 
 
 
f38dbc0
 
eb4710d
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Visualizer for TAPAS

Implementation heavily based on
`EncodingVisualizer` from `tokenizers.tools`.
"""
import os
from typing import Any, List, Dict

from collections import defaultdict

import pandas as pd

from transformers import TapasTokenizer

dirname = os.path.dirname(__file__)
css_filename = os.path.join(dirname, "tapas-styles.css")
with open(css_filename) as f:
    css = f.read()


def HTMLBody(table_html: str, css_styles: str = css) -> str:
    """
    Generates the full html with css from a list of html spans

    Args:
        table_html (str):
            The html string of the table

        css_styles (str):
            CSS styling to be embedded inline

    Returns:
        :obj:`str`: An HTML string with style markup
    """
    return f"""
    <html>
        <head>
            <style>
                {css_styles}
            </style>
        </head>
        <body>
            <div class="tokenized-text" dir=auto>
            {table_html}
            </div>
        </body>
    </html>
    """


class TapasVisualizer:
    def __init__(self, tokenizer: TapasTokenizer) -> None:
        self.tokenizer = tokenizer

    def normalize_token_str(self, token_str: str) -> str:
        # Normalize subword tokens to org subword str
        return token_str.replace("##", "")

    def style_span(self, span_text: str, css_classes: List[str]) -> str:
        css = f'''class="{' '.join(css_classes)}"'''
        return f"<span {css} >{span_text}</span>"

    def text_to_html(self, org_text: str, tokens: List[str]) -> str:
        """Create html based on the original text and its tokens.

        Note: The tokens need to be in same order as in the original text

        Args:
            org_text (str): Original string before tokenization
            tokens (List[str]): The tokens of org_text

        Returns:
            str: html with styling for the tokens
        """
        if len(tokens) == 0:
            print(f"Empty tokens for: {org_text}")
            return ""

        cur_token_id = 0
        cur_token = self.normalize_token_str(tokens[cur_token_id])

        # Loop through each character
        next_start = 0
        last_end = 0
        spans = []

        while next_start < len(org_text):
            candidate = org_text[next_start : next_start + len(cur_token)]

            # The tokenizer performs lowercasing; so check against lowercase
            if candidate.lower() == cur_token:
                if last_end != next_start:
                    # There was token-less text (probably whitespace)
                    # in the middle
                    spans.append(
                        self.style_span(org_text[last_end:next_start], ["non-token"])
                    )

                odd_or_even = "even-token" if cur_token_id % 2 == 0 else "odd-token"
                spans.append(self.style_span(candidate, ["token", odd_or_even]))
                next_start += len(cur_token)
                last_end = next_start
                cur_token_id += 1
                if cur_token_id >= len(tokens):
                    break
                cur_token = self.normalize_token_str(tokens[cur_token_id])
            else:
                next_start += 1

        if last_end != len(org_text):
            spans.append(self.style_span(org_text[last_end:next_start], ["non-token"]))

        return spans

    def cells_to_html(
        self,
        cell_vals: List[List[str]],
        cell_tokens: Dict,
        row_id_start: int = 0,
        cell_element: str = "td",
        cumulative_cnt: int = 0,
        table_html: str = "",
    ) -> str:
        for row_id, row in enumerate(cell_vals, start=row_id_start):
            row_html = ""
            row_token_cnt = 0
            for col_id, cell in enumerate(row, start=1):
                cur_cell_tokens = cell_tokens[(row_id, col_id)]
                span_htmls = self.text_to_html(cell, cur_cell_tokens)
                cell_html = "".join(span_htmls)
                row_html += f"<{cell_element}>{cell_html}</{cell_element}>"
                row_token_cnt += len(cur_cell_tokens)
            cumulative_cnt += row_token_cnt
            cnt_html = (
                f'<td style="border: none;" align="right">'
                f'{self.style_span(str(cumulative_cnt), ["non-token", "count"])}'
                "</td>"
                f'<td style="border: none;" align="right">'
                f'{self.style_span(f"<+{row_token_cnt}", ["non-token", "count"])}'
                "</td>"
            )
            row_html = cnt_html + row_html
            table_html += f"<tr>{row_html}</tr>"

        return table_html, cumulative_cnt

    def __call__(self, table: pd.DataFrame) -> Any:
        tokenized = self.tokenizer(table)

        cell_tokens = defaultdict(list)

        for id_ind, input_id in enumerate(tokenized["input_ids"]):
            input_id = int(input_id)
            # 'prev_label', 'column_rank', 'inv_column_rank', 'numeric_relation'
            # not required
            segment_id, col_id, row_id, *_ = tokenized["token_type_ids"][id_ind]
            token_text = self.tokenizer._convert_id_to_token(input_id)
            if int(segment_id) == 1:
                cell_tokens[(row_id, col_id)].append(token_text)

        table_html, cumulative_cnt = self.cells_to_html(
            cell_vals=[table.columns],
            cell_tokens=cell_tokens,
            row_id_start=0,
            cell_element="th",
            cumulative_cnt=0,
            table_html="",
        )

        table_html, cumulative_cnt = self.cells_to_html(
            cell_vals=table.values,
            cell_tokens=cell_tokens,
            row_id_start=1,
            cell_element="td",
            cumulative_cnt=cumulative_cnt,
            table_html=table_html,
        )
        top_label = self.style_span("#Tokens", ["count"])
        top_label_cnt = self.style_span(f"(Total: {cumulative_cnt})", ["count"])

        table_html = (
            '<tr style="line-height: 2rem">'
            f'<td style="border: none;" colspan="2" align="left">{top_label}</td>'
            f'<td style="border: none;" colspan="1" align="left">{top_label_cnt}</td>'
            "</tr>"
            f"{table_html}"
        )

        table_html = f"<table>{table_html}</table>"
        return HTMLBody(table_html)