File size: 6,478 Bytes
d8d7490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>mbpe-dyn</title>
    <script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
    <script src="wasm_exec.js"></script>
    <script>
        const go = new Go();

        WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(result => {
            go.run(result.instance);
        });

        async function tokenizeText() {
            const input = document.getElementById("textInput").value.trim();
            const modelChoice = document.getElementById("modelSelect").value;
            const vocabSize = parseInt(document.getElementById("vocabSize").value, 10) || -1;

            if (!input) {
                alert("Please enter text to tokenize.");
                return;
            }

            if (typeof tokenizeWeb !== "undefined") {
                const resultJSON = tokenizeWeb(input, modelChoice, vocabSize);

                try {
                    const result = JSON.parse(resultJSON);
                    console.log(result);

                    if (Array.isArray(result) && result.length > 0) {
                        displayTabs(result);
                        showResult(0); // Show first tab by default
                    } else {
                        document.getElementById("code").innerText = "No tokens found.";
                        document.getElementById("mermaidContainer").innerHTML = "";
                    }
                } catch (e) {
                    console.error("Error parsing result:", e);
                }
            }
        }

        function displayTabs(results) {
            const tabsContainer = document.getElementById("wordTabs");
            tabsContainer.innerHTML = "";

            results.forEach((entry, index) => {
                const finalSegmentation = entry.Segmentations[entry.Segmentations.length - 1];
                const label = finalSegmentation.map(t => t.Token).join("");

                const tabButton = document.createElement("button");
                tabButton.textContent = label;
                tabButton.classList.add("word-tab");
                tabButton.dataset.index = index;
                tabButton.onclick = () => showResult(index);

                tabsContainer.appendChild(tabButton);
            });
        }

        function showResult(index) {
            const results = JSON.parse(tokenizeWeb(
                document.getElementById("textInput").value.trim(),
                document.getElementById("modelSelect").value,
                parseInt(document.getElementById("vocabSize").value, 10) || -1
            ));

            const selectedResult = results[index];

            // Update Mermaid code
            document.getElementById("code").innerText = selectedResult.Mermaid;
            renderMermaid();

            // Update active tab
            document.querySelectorAll(".word-tab").forEach(tab => tab.classList.remove("active"));
            document.querySelector(`.word-tab[data-index="${index}"]`).classList.add("active");
        }

        function renderMermaid() {
            const diagramCode = document.getElementById("code").innerText.trim();
            if (diagramCode) {
                document.getElementById("mermaidContainer").innerHTML = `<div class="mermaid">${diagramCode}</div>`;
                mermaid.init(undefined, document.querySelectorAll(".mermaid"));
            }
        }
    </script>
    <style>
        /* General Styles */
        body {
            font-family: Arial, sans-serif;
            background: #f8f9fa;
            color: #333;
            text-align: center;
            padding: 20px;
        }

        h1 {
            color: #007bff;
        }

        /* Input and Select */
        .input-group {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-bottom: 20px;
        }

        input, select, button {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }

        input {
            width: 300px;
        }

        select {
            width: 150px;
            background: white;
            cursor: pointer;
        }

        button {
            background: #007bff;
            color: white;
            font-weight: bold;
            cursor: pointer;
            transition: 0.3s;
            border: none;
        }

        button:hover {
            background: #0056b3;
        }

        #modelSelect {
            width: 80px;
        }

        #vocabSize {
            width: 110px;
        }

        /* Word Tabs */
        .word-tabs {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

        .word-tab {
            padding: 8px 15px;
            background: #e1ecf4;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-weight: bold;
            transition: 0.3s;
        }

        .word-tab.active {
            background: #007bff;
            color: white;
        }
    </style>
</head>
<body>

<h1>mbpe-dyn</h1>

<div class="input-group">
    <select id="modelSelect">
        <option value="m000">m000</option>
        <option value="m010">m010</option>
        <option value="m020">m020</option>
        <option value="m030">m030</option>
        <option value="m040">m040</option>
        <option value="m050">m050</option>
        <option value="m060">m060</option>
        <option value="m070">m070</option>
        <option value="m080">m080</option>
        <option value="m090">m090</option>
        <option value="m100" selected>m100</option>
    </select>
    <select id="vocabSize">
        <option value="16384">2¹⁴ (16K)</option>
        <option value="32768">2¹⁵ (32K)</option>
        <option value="65536">2¹⁶ (64K)</option>
        <option value="131072" selected>2¹⁷ (128K)</option>
    </select>
    <input type="text" id="textInput" placeholder="Enter text here" value="airsickness">
    <button onclick="tokenizeText()">Tokenize</button>
</div>
<div id="wordTabs" class="word-tabs"></div>
<code id="code" style="display: none;"></code>
<div id="mermaidContainer"></div>
</body>
</html>