File size: 11,453 Bytes
edede4c
ad89e4a
 
 
 
 
 
f2b7e07
 
ad89e4a
 
 
 
 
 
 
 
 
f2b7e07
 
 
 
 
 
 
 
edede4c
ad89e4a
f2b7e07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edede4c
 
b09b54e
 
 
c1f7e74
 
 
 
 
 
79c94e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b09b54e
edede4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79c94e9
 
 
 
 
 
 
d56d262
edede4c
d56d262
edede4c
 
d56d262
 
 
 
edede4c
d56d262
 
 
 
 
 
 
 
edede4c
d56d262
edede4c
d56d262
edede4c
d56d262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edede4c
d56d262
 
 
 
 
 
 
 
 
 
 
 
edede4c
d56d262
 
 
 
 
edede4c
d56d262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edede4c
d56d262
 
 
edede4c
 
 
d56d262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edede4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79c94e9
 
 
 
 
 
 
 
 
 
 
 
edede4c
 
 
8c9459c
 
 
 
 
 
 
 
edede4c
 
79c94e9
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

---
language:
- en

license: mit

library_name: custom

tags:
- tokenizer
- symbolic-ai
- mathematics
- llm
- reasoning
- ast
- compiler
- nlp
- deep-learning
- machine-learning
- mathematical-reasoning
- symbolic-reasoning
- tokenization
- parser
- transformers
- artificial-intelligence

pipeline_tag: text-generation

datasets:
- custom-mathematical-dataset

metrics:
- semantic-density
- structural-efficiency
- symbolic-compression-ratio

model-index:
- name: MathTok
  results:
  - task:
      type: tokenization
      name: Mathematical Tokenization
    dataset:
      name: Custom Mathematical Benchmark
      type: symbolic-math
    metrics:
    - type: semantic-density
      value: Improved
      name: Semantic Density
    - type: structural-efficiency
      value: Optimized
      name: Structural Efficiency
    - type: symbolic-compression-ratio
      value: Enhanced
      name: SCR

co2_eq_emissions:
  emissions: 0

license_name: mit

pretty_name: MathTok

thumbnail: assets/mathtok_architecture_improvements.svg
---

# MathTok

**A Hybrid Canonicalized AST-Based Tokenization Framework for Mathematical Language Modeling**

  ![Python](https://img.shields.io/badge/Python-3.10-blue)
  ![License](https://img.shields.io/badge/License-MIT-green)
  ![HuggingFace](https://img.shields.io/badge/HuggingFace-Live-yellow)
  ![Tests](https://img.shields.io/badge/Tests-110%2B-success)
  ![Research](https://img.shields.io/badge/Focus-Symbolic%20AI-purple)

---

## Why MathTok?

Traditional tokenizers such as BPE and SentencePiece treat mathematical
expressions as plain text sequences, fragmenting semantic structure and
discarding operator hierarchy.

MathTok introduces a structure-aware tokenization pipeline that:
- canonicalizes equivalent mathematical expressions,
- preserves AST hierarchy,
- encodes operator semantics explicitly,
- improves symbolic compression efficiency,
- and enables future tree-aware transformer architectures.

---

## Overview

MathTok is a research-grade tokenizer pipeline that converts raw mathematical expressions (LaTeX or ASCII) into a structured, semantically-rich token stream. Unlike standard BPE or SentencePiece tokenizers, MathTok is *structure-aware*: it builds an Abstract Syntax Tree (AST) from each expression and serializes it via DFS preorder traversal, preserving full mathematical structure.

```
Raw Mathematical Expression
          ↓
Canonicalization Layer       (sympy: simplify, expand, normalize)
          ↓
Hybrid Mathematical Lexer    (split TEXT / MATH spans)
          ↓
AST Generator                (SymPy tree β†’ typed ASTNode tree)
          ↓
Operator-Aware Semantic Encoder  (rich metadata per operator)
          ↓
Structural Serialization     (DFS preorder β†’ flat token stream)
          ↓
Structural Attention Metadata (per-token tree context)
          ↓
Vocabulary Mapping + BPE     (fixed math vocab + HF BPE for text)
          ↓
Compressed Token Stream
```

---

## Architecture

![MathTok Architecture](assets/mathtok_architecture_improvements.svg)

---

## Installation

Clone the repository and install the package in editable mode:

```bash
git clone https://github.com/SurweeshSP/mathtok.git

cd mathtok

pip install -e ".[eval,dev]"
```

---
## Quick Start

### Tokenize a Mathematical Expression

Run the tokenizer pipeline directly from the command line:

```bash
python -m mathtok.pipeline "The derivative of sin(x^2) + 3x"
```

Example output:

```text
[
  FUNCTION_SIN,
  VARIABLE_x,
  POWER,
  NUMBER_2,
  OP_ADD,
  NUMBER_3,
  VARIABLE_x
]
```

---

## Running the Test Suite

Execute the comprehensive unit and integration test suite:

```bash
pytest tests/ -v
```

Current coverage includes:

- AST generation
- Canonicalization
- Lexer validation
- Pipeline integration
- Serialization consistency
- Structural comparison metrics

---

## Comparative Tokenizer Evaluation

Run the full benchmark evaluation pipeline:

```bash
python -m evaluation.comparison
```

This benchmark compares:

- MathTok (Hybrid AST Tokenizer)
- GPT-2 BPE
- SentencePiece Unigram
- Character-Level Tokenization

Evaluation metrics include:

- Symbolic Compression Ratio (SCR)
- Semantic Density
- Structural Efficiency
- Token Fragmentation
- Sequence Compactness

---

## Visualization Dashboard

Generate benchmark plots and the unified evaluation dashboard:

```bash
python -m evaluation.visualize
```

Generated outputs include:

- Semantic Density Comparison
- SCR Comparison
- Structural Efficiency Comparison
- Token Count Analysis
- Unified Metrics Dashboard

All generated figures are stored in:

```text
evaluation/results/
```

---

## Repository Structure

```text
mathtok/
β”œβ”€β”€ mathtok/                 # Core tokenizer framework
β”œβ”€β”€ evaluation/              # Benchmarking and evaluation
β”œβ”€β”€ tests/                   # Comprehensive test suite
β”œβ”€β”€ assets/                  # Architecture diagrams
β”œβ”€β”€ README.md
β”œβ”€β”€ setup.py
└── pyproject.toml
```

---

## Python API

```python
from mathtok import MathTokPipeline

pipeline = MathTokPipeline()

# Encode mixed text + math (supporting LaTeX or ASCII syntax)
out = pipeline.encode("The derivative of $\\sin(x^2)$ is $2x\\cos(x^2)$.")
print(out.tokens)      # ['[MATH_START]', 'FUNC_SIN', 'OP_POW', 'VAR_X', 'CONST_2', '[MATH_END]', ...]
print(out.sexp)        # (FUNC_SIN (OP_POW VAR_X CONST_2))
print(out.input_ids)   # [4, 27, 10, 45, 12, 5, ...]

# Access structural metadata (for tree-aware attention masking)
for meta in out.metadata:
    print(meta.token, meta.depth, meta.tree_position_key)

# Pure math expression serialization
out = pipeline.encode_math_only("(x+1)^2")
print(out.sexp)        # (OP_POW (OP_ADD VAR_X CONST_1) CONST_2)

# HuggingFace-compatible tokenizer export
hf_tok = pipeline.get_hf_tokenizer()
hf_tok.save_pretrained("./mathtok-tokenizer")
result = hf_tok("x^2 + 2*x + 1", return_tensors="pt")
```

---

## Research Contributions

### 1. Hybrid Lexer
Separates natural language from mathematical content using LaTeX delimiter detection (`$...$`, `\(...\)`, `\[...\]`) and ASCII math heuristics.

### 2. Canonicalization Engine
Normalizes mathematically equivalent expressions via SymPy's `simplify()`, `expand()`, and internal representation (subtraction β†’ addition + negation, division β†’ multiplication + reciprocal).

### 3. AST-Based Structural Serialization
Maps SymPy's expression tree to a typed token vocabulary with semantic metadata per operator. Serializes via DFS preorder traversal.

### 4. Operator Semantic Registry
Every operator and function carries an explicit metadata record: `arity`, `precedence`, `associativity`, `semantic_role`. This is the primary novelty over standard tokenization.

### 5. Structural Attention Metadata
Per-token records encoding `depth`, `parent_id`, `children_ids`, `tree_position_key`, and `sibling_count` β€” enabling future structure-aware attention.

### 6. Two-Tier Vocabulary
- **Fixed math vocabulary**: deterministic IDs for all operators, functions, variables, constants.
- **BPE text vocabulary**: HuggingFace `tokenizers` BPE for natural language spans.

---

## Evaluation Metrics & Benchmarks

### Core Metrics

| Metric | Symbol | Meaning |
|--------|--------|---------|
| **Semantic Compression Ratio** | SCR | `structural_score / token_count` (Higher is better β€” measures parsed semantic content density) |
| **Semantic Density** | SD | `math_tokens / total_tokens` (Ratio of high-value math tokens, measures information density) |
| **Structural Efficiency** | SE | `parent_child_relations / token_count` (Ratio of hierarchy relationships encoded per token) |
| **Token Stability** | TS | `1 - CoV(token count across rewritings)` (Fidelity and stability across representations) |

### Empirical Benchmarks (4-Way Comparison)

Below are the empirical averages computed over our comprehensive suite of 70 mathematical test expressions:

| Tokenizer | Mean SCR (↑ Better) | Semantic Density (↑ Better) | Structural Efficiency (↑ Better) |
|:---|:---:|:---:|:---:|
| **MathTok (Ours)** | **0.8501** | **0.5285** | **0.2339** |
| **GPT-2 BPE** | 0.4251 | 0.1838 | 0.1491 |
| **SentencePiece Unigram** | 0.3696 | 0.1499 | 0.1403 |
| **Character-Level** | 0.3708 | 0.1518 | 0.1518 |

> [!NOTE]
> * MathTok achieves a **2.30x structural compression improvement** over SentencePiece.
> * MathTok packs **3.52x more math-centric information** per token stream compared to SentencePiece unigrams (**0.5285** vs **0.1499**), showing immense semantic density.
> * MathTok is **1.67x more efficient** at encoding hierarchical ast relationships directly into token structures (**0.2339** vs **0.1403**).

### High-Impact Visualizations

The visualization system runs via `python -m evaluation.visualize` and exports professional visual assets under [`evaluation/results/`](file:///c:/Users/surwe/Project/math_token/evaluation/results/):
- **Unified Evaluation Dashboard** (`metrics_dashboard.png`): 3-panel side-by-side display of SCR, Semantic Density, and Structural Efficiency.
- **Overall SCR Comparison** (`scr_comparison.png`): Comparative summary bar chart.
- **Category-Level Breakdowns** (`scr_by_category.png`): SCR analyzed by nested/standard categories.
- **Semantic Density Summary** (`semantic_density_comparison.png`): Ratio of math structure to total tokens.

---

## Project Structure

```
math_token/
β”œβ”€β”€ mathtok/
β”‚   β”œβ”€β”€ canonicalizer.py      # Layer 1: Canonicalization Engine
β”‚   β”œβ”€β”€ lexer.py              # Layer 2: Hybrid Mathematical Lexer
β”‚   β”œβ”€β”€ ast_generator.py      # Layer 3: AST Generator
β”‚   β”œβ”€β”€ operator_registry.py  # Layer 4: Operator Semantic Registry
β”‚   β”œβ”€β”€ serializer.py         # Layer 5: Structural Traversal & Serialization
β”‚   β”œβ”€β”€ metadata.py           # Layer 6: Structural Attention Metadata
β”‚   β”œβ”€β”€ vocabulary.py         # Layer 7: Two-Tier Vocabulary
β”‚   └── pipeline.py           # Orchestrator Pipeline
β”œβ”€β”€ evaluation/
β”‚   β”œβ”€β”€ metrics.py            # Definition of core evaluation metrics
β”‚   β”œβ”€β”€ benchmark.py          # Quick benchmarking scripts
β”‚   β”œβ”€β”€ comparison.py         # Full 4-way comparative framework (SentencePiece integrated)
β”‚   β”œβ”€β”€ visualize.py          # Custom dashboard visualization engine
β”‚   └── results/              # JSON/JSONL reports & visual plots
└── tests/                    # 110+ passing unit tests
```

---

## Future Work

- Tree-aware transformer attention integration
- Native mathematical pretraining corpus
- Symbolic reasoning benchmarks
- Neural theorem proving interfaces
- Equation graph embeddings
- Mathematical multimodal tokenization
- Integration with Lean/Coq theorem systems

---

## Citation

```bibtex
@article{surweesh2026mathtok,
  title   = {MathTok: A Hybrid Canonicalized AST-Based Tokenization Framework for Mathematical Language Modeling and Symbolic Reasoning},
  author  = {Surweesh SP},
  year    = {2026},
  journal = {Preprint},
  note    = {Open-source research framework available on GitHub and Hugging Face},
  keywords = {Mathematical Tokenization, Symbolic AI, Abstract Syntax Trees, LLMs, NLP, Mathematical Reasoning, Canonicalization},
  url     = {https://huggingface.co/Surweesh/MathTok}
}
```

---

## Links

- GitHub: https://github.com/SurweeshSP/mathtok
- Hugging Face: https://huggingface.co/Surweesh/MathTok