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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Million-scale comprehensive test suite for deeplatent-nlp.
|
| 4 |
+
|
| 5 |
+
Tests:
|
| 6 |
+
1. Roundtrip accuracy on 1M+ samples from /root/.cache/deeplatent/base_data/
|
| 7 |
+
2. All 12 edge case categories from test_edge_cases.py
|
| 8 |
+
3. Performance metrics (throughput, memory)
|
| 9 |
+
4. PyPI vs Local tokenizer comparison
|
| 10 |
+
|
| 11 |
+
Usage:
|
| 12 |
+
python test_comprehensive_million.py [--samples 1000000] [--report]
|
| 13 |
+
|
| 14 |
+
# Quick test with 10k samples
|
| 15 |
+
python test_comprehensive_million.py --samples 10000
|
| 16 |
+
|
| 17 |
+
# Full million-scale test
|
| 18 |
+
python test_comprehensive_million.py --samples 1000000 --report
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import argparse
|
| 22 |
+
import json
|
| 23 |
+
import os
|
| 24 |
+
import sys
|
| 25 |
+
import time
|
| 26 |
+
import tracemalloc
|
| 27 |
+
from collections import defaultdict
|
| 28 |
+
from pathlib import Path
|
| 29 |
+
from typing import Dict, List, Optional, Tuple
|
| 30 |
+
|
| 31 |
+
import pyarrow.parquet as pq
|
| 32 |
+
|
| 33 |
+
# Add parent to path for imports
|
| 34 |
+
sys.path.insert(0, str(Path(__file__).parent))
|
| 35 |
+
|
| 36 |
+
from deeplatent import SARFTokenizer, version, RUST_AVAILABLE
|
| 37 |
+
from deeplatent.config import (
|
| 38 |
+
NormalizationConfig,
|
| 39 |
+
UnicodeNormalizationForm,
|
| 40 |
+
WhitespaceNormalization,
|
| 41 |
+
ControlCharStrategy,
|
| 42 |
+
ZeroWidthStrategy,
|
| 43 |
+
)
|
| 44 |
+
from deeplatent.utils import (
|
| 45 |
+
# Character classification
|
| 46 |
+
is_arabic,
|
| 47 |
+
is_arabic_diacritic,
|
| 48 |
+
is_pua,
|
| 49 |
+
is_zero_width,
|
| 50 |
+
is_unicode_whitespace,
|
| 51 |
+
is_control_char,
|
| 52 |
+
is_emoji,
|
| 53 |
+
is_emoji_sequence,
|
| 54 |
+
is_skin_tone_modifier,
|
| 55 |
+
is_regional_indicator,
|
| 56 |
+
# Normalization
|
| 57 |
+
normalize_nfc,
|
| 58 |
+
normalize_nfkc,
|
| 59 |
+
normalize_apostrophes,
|
| 60 |
+
normalize_dashes,
|
| 61 |
+
normalize_whitespace,
|
| 62 |
+
normalize_unicode_whitespace,
|
| 63 |
+
remove_zero_width,
|
| 64 |
+
remove_zero_width_all,
|
| 65 |
+
remove_zero_width_preserve_zwj,
|
| 66 |
+
remove_control_chars,
|
| 67 |
+
strip_diacritics,
|
| 68 |
+
normalize_alef,
|
| 69 |
+
remove_tatweel,
|
| 70 |
+
full_normalize_extended,
|
| 71 |
+
# Pattern detection
|
| 72 |
+
contains_url,
|
| 73 |
+
contains_email,
|
| 74 |
+
contains_path,
|
| 75 |
+
extract_urls,
|
| 76 |
+
extract_emails,
|
| 77 |
+
is_valid_url,
|
| 78 |
+
is_valid_email,
|
| 79 |
+
# Grapheme handling
|
| 80 |
+
grapheme_count,
|
| 81 |
+
# Input validation
|
| 82 |
+
validate_input,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 87 |
+
# Configuration
|
| 88 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 89 |
+
|
| 90 |
+
DATA_DIR = "/root/.cache/deeplatent/base_data/"
|
| 91 |
+
HF_REPO = "almaghrabima/SARFTokenizer"
|
| 92 |
+
HF_TOKENIZER_PATH = os.path.expanduser("~/.cache/deeplatent/tokenizers/SARFTokenizer")
|
| 93 |
+
LOCAL_TOKENIZER = "/root/.cache/DeepLatent/SARFTokenizer/SARF-65k-v2-fixed/"
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def download_tokenizer_from_hf(repo_id: str, cache_dir: Optional[str] = None) -> str:
|
| 97 |
+
"""
|
| 98 |
+
Download tokenizer files from HuggingFace Hub.
|
| 99 |
+
|
| 100 |
+
Args:
|
| 101 |
+
repo_id: HuggingFace repo ID (e.g., "almaghrabima/SARFTokenizer")
|
| 102 |
+
cache_dir: Optional cache directory
|
| 103 |
+
|
| 104 |
+
Returns:
|
| 105 |
+
Local path to downloaded tokenizer directory
|
| 106 |
+
"""
|
| 107 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 108 |
+
|
| 109 |
+
if cache_dir is None:
|
| 110 |
+
cache_dir = os.path.expanduser("~/.cache/deeplatent/tokenizers")
|
| 111 |
+
|
| 112 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 113 |
+
|
| 114 |
+
# Download the entire repo snapshot
|
| 115 |
+
local_dir = os.path.join(cache_dir, repo_id.replace("/", "_"))
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
# Try to download the full repo
|
| 119 |
+
local_dir = snapshot_download(
|
| 120 |
+
repo_id=repo_id,
|
| 121 |
+
local_dir=local_dir,
|
| 122 |
+
repo_type="model",
|
| 123 |
+
)
|
| 124 |
+
print(f" Downloaded tokenizer to: {local_dir}")
|
| 125 |
+
return local_dir
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f" Warning: Could not download from HF Hub: {e}")
|
| 128 |
+
raise
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 132 |
+
# Data Loading
|
| 133 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 134 |
+
|
| 135 |
+
def load_base_data(data_dir: str, num_samples: int = 1000000) -> Tuple[List[str], List[str], List[str]]:
|
| 136 |
+
"""
|
| 137 |
+
Load samples from base_data parquet shards.
|
| 138 |
+
|
| 139 |
+
Returns:
|
| 140 |
+
Tuple of (arabic_samples, english_samples, mixed_samples)
|
| 141 |
+
"""
|
| 142 |
+
import re
|
| 143 |
+
AR_DETECT = re.compile(r'[\u0600-\u06FF]')
|
| 144 |
+
|
| 145 |
+
parquet_files = sorted(Path(data_dir).glob("shard_*.parquet"))
|
| 146 |
+
if not parquet_files:
|
| 147 |
+
raise FileNotFoundError(f"No parquet files found in {data_dir}")
|
| 148 |
+
|
| 149 |
+
print(f"Found {len(parquet_files)} parquet shards")
|
| 150 |
+
|
| 151 |
+
arabic_samples = []
|
| 152 |
+
english_samples = []
|
| 153 |
+
mixed_samples = []
|
| 154 |
+
|
| 155 |
+
target_per_category = num_samples // 3
|
| 156 |
+
|
| 157 |
+
for pq_file in parquet_files:
|
| 158 |
+
# Check if we've collected enough samples in ALL categories
|
| 159 |
+
if (len(arabic_samples) >= target_per_category and
|
| 160 |
+
len(english_samples) >= target_per_category and
|
| 161 |
+
len(mixed_samples) >= target_per_category):
|
| 162 |
+
break
|
| 163 |
+
|
| 164 |
+
table = pq.read_table(pq_file, columns=["text", "language"])
|
| 165 |
+
texts = table.column("text").to_pylist()
|
| 166 |
+
languages = table.column("language").to_pylist() if "language" in table.column_names else [None] * len(texts)
|
| 167 |
+
|
| 168 |
+
for text, lang in zip(texts, languages):
|
| 169 |
+
# Check again inside the loop
|
| 170 |
+
if (len(arabic_samples) >= target_per_category and
|
| 171 |
+
len(english_samples) >= target_per_category and
|
| 172 |
+
len(mixed_samples) >= target_per_category):
|
| 173 |
+
break
|
| 174 |
+
|
| 175 |
+
if not text or not isinstance(text, str):
|
| 176 |
+
continue
|
| 177 |
+
|
| 178 |
+
# Classify by content
|
| 179 |
+
ar_chars = len(AR_DETECT.findall(text))
|
| 180 |
+
total_chars = len(text)
|
| 181 |
+
ar_ratio = ar_chars / total_chars if total_chars > 0 else 0
|
| 182 |
+
|
| 183 |
+
if ar_ratio > 0.5 and len(arabic_samples) < target_per_category:
|
| 184 |
+
arabic_samples.append(text)
|
| 185 |
+
elif ar_ratio < 0.1 and len(english_samples) < target_per_category:
|
| 186 |
+
english_samples.append(text)
|
| 187 |
+
elif 0.1 <= ar_ratio <= 0.5 and len(mixed_samples) < target_per_category:
|
| 188 |
+
mixed_samples.append(text)
|
| 189 |
+
|
| 190 |
+
print(f" {pq_file.name}: AR={len(arabic_samples):,}, EN={len(english_samples):,}, Mixed={len(mixed_samples):,}")
|
| 191 |
+
|
| 192 |
+
total_loaded = len(arabic_samples) + len(english_samples) + len(mixed_samples)
|
| 193 |
+
print(f"\nTotal loaded: {total_loaded:,} samples")
|
| 194 |
+
print(f" Arabic: {len(arabic_samples):,}")
|
| 195 |
+
print(f" English: {len(english_samples):,}")
|
| 196 |
+
print(f" Mixed: {len(mixed_samples):,}")
|
| 197 |
+
|
| 198 |
+
return arabic_samples, english_samples, mixed_samples
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 202 |
+
# Roundtrip Tests
|
| 203 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 204 |
+
|
| 205 |
+
def test_roundtrip_batch(
|
| 206 |
+
tokenizer: SARFTokenizer,
|
| 207 |
+
samples: List[str],
|
| 208 |
+
category: str,
|
| 209 |
+
max_failures: int = 100,
|
| 210 |
+
) -> Dict:
|
| 211 |
+
"""
|
| 212 |
+
Test roundtrip on a batch of samples.
|
| 213 |
+
|
| 214 |
+
Returns:
|
| 215 |
+
Dict with success count, failures, accuracy, timing
|
| 216 |
+
"""
|
| 217 |
+
success = 0
|
| 218 |
+
failures = []
|
| 219 |
+
total_encode_time = 0
|
| 220 |
+
total_decode_time = 0
|
| 221 |
+
|
| 222 |
+
for i, text in enumerate(samples):
|
| 223 |
+
try:
|
| 224 |
+
# Encode
|
| 225 |
+
t0 = time.perf_counter()
|
| 226 |
+
ids = tokenizer.encode(text)
|
| 227 |
+
total_encode_time += time.perf_counter() - t0
|
| 228 |
+
|
| 229 |
+
# Decode
|
| 230 |
+
t0 = time.perf_counter()
|
| 231 |
+
decoded = tokenizer.decode(ids)
|
| 232 |
+
total_decode_time += time.perf_counter() - t0
|
| 233 |
+
|
| 234 |
+
# The tokenizer normalizes text, so compare normalized versions
|
| 235 |
+
# For SARFTokenizer, decode(encode(text)) should return normalized text
|
| 236 |
+
if decoded == tokenizer.normalize(text) if hasattr(tokenizer, 'normalize') else True:
|
| 237 |
+
success += 1
|
| 238 |
+
else:
|
| 239 |
+
# Also accept if decoded matches original (no normalization case)
|
| 240 |
+
if decoded == text:
|
| 241 |
+
success += 1
|
| 242 |
+
elif len(failures) < max_failures:
|
| 243 |
+
failures.append({
|
| 244 |
+
"index": i,
|
| 245 |
+
"original": text[:100],
|
| 246 |
+
"decoded": decoded[:100],
|
| 247 |
+
})
|
| 248 |
+
except Exception as e:
|
| 249 |
+
if len(failures) < max_failures:
|
| 250 |
+
failures.append({
|
| 251 |
+
"index": i,
|
| 252 |
+
"original": text[:100] if text else "",
|
| 253 |
+
"error": str(e),
|
| 254 |
+
})
|
| 255 |
+
|
| 256 |
+
total = len(samples)
|
| 257 |
+
accuracy = success / total if total > 0 else 0
|
| 258 |
+
|
| 259 |
+
return {
|
| 260 |
+
"category": category,
|
| 261 |
+
"total": total,
|
| 262 |
+
"success": success,
|
| 263 |
+
"failed": total - success,
|
| 264 |
+
"accuracy": accuracy,
|
| 265 |
+
"accuracy_pct": f"{accuracy * 100:.2f}%",
|
| 266 |
+
"encode_time": total_encode_time,
|
| 267 |
+
"decode_time": total_decode_time,
|
| 268 |
+
"failures": failures,
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def run_roundtrip_tests(
|
| 273 |
+
tokenizer: SARFTokenizer,
|
| 274 |
+
arabic_samples: List[str],
|
| 275 |
+
english_samples: List[str],
|
| 276 |
+
mixed_samples: List[str],
|
| 277 |
+
) -> Dict:
|
| 278 |
+
"""Run roundtrip tests on all categories."""
|
| 279 |
+
results = {}
|
| 280 |
+
|
| 281 |
+
categories = [
|
| 282 |
+
("Arabic", arabic_samples),
|
| 283 |
+
("English", english_samples),
|
| 284 |
+
("Mixed", mixed_samples),
|
| 285 |
+
]
|
| 286 |
+
|
| 287 |
+
for name, samples in categories:
|
| 288 |
+
if samples:
|
| 289 |
+
print(f" Testing {name} ({len(samples):,} samples)...", end=" ", flush=True)
|
| 290 |
+
result = test_roundtrip_batch(tokenizer, samples, name)
|
| 291 |
+
results[name] = result
|
| 292 |
+
print(f"Accuracy: {result['accuracy_pct']}")
|
| 293 |
+
|
| 294 |
+
# Compute totals
|
| 295 |
+
total_success = sum(r["success"] for r in results.values())
|
| 296 |
+
total_samples = sum(r["total"] for r in results.values())
|
| 297 |
+
total_failed = sum(r["failed"] for r in results.values())
|
| 298 |
+
total_accuracy = total_success / total_samples if total_samples > 0 else 0
|
| 299 |
+
|
| 300 |
+
results["TOTAL"] = {
|
| 301 |
+
"category": "TOTAL",
|
| 302 |
+
"total": total_samples,
|
| 303 |
+
"success": total_success,
|
| 304 |
+
"failed": total_failed,
|
| 305 |
+
"accuracy": total_accuracy,
|
| 306 |
+
"accuracy_pct": f"{total_accuracy * 100:.2f}%",
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
return results
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 313 |
+
# Edge Case Tests (12 Categories)
|
| 314 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 315 |
+
|
| 316 |
+
EDGE_CASE_TESTS = {
|
| 317 |
+
"Unicode Normalization": [
|
| 318 |
+
("cafe\u0301", "cafรฉ", "NFC: combining acute"),
|
| 319 |
+
("n\u0303", "รฑ", "NFC: combining tilde"),
|
| 320 |
+
("e\u0308", "รซ", "NFC: combining diaeresis"),
|
| 321 |
+
("\uFB01", "fi", "NFKC: fi ligature"),
|
| 322 |
+
("\uFF21", "A", "NFKC: fullwidth A"),
|
| 323 |
+
("ู\u0651", None, "Arabic shadda combining"),
|
| 324 |
+
],
|
| 325 |
+
"Zero-Width Characters": [
|
| 326 |
+
("a\u200Bb", "ab", "ZWSP removal"),
|
| 327 |
+
("a\u200C\u200Db", None, "ZWNJ + ZWJ"),
|
| 328 |
+
("a\u200Eb", None, "LRM"),
|
| 329 |
+
("a\u200Fb", None, "RLM"),
|
| 330 |
+
("a\u2060b", None, "Word Joiner"),
|
| 331 |
+
("a\uFEFFb", None, "BOM"),
|
| 332 |
+
],
|
| 333 |
+
"Unicode Whitespace": [
|
| 334 |
+
("a\u00A0b", "a b", "NBSP"),
|
| 335 |
+
("a\u2003b", "a b", "Em Space"),
|
| 336 |
+
("a\u2009b", "a b", "Thin Space"),
|
| 337 |
+
("a\u202Fb", None, "Narrow NBSP"),
|
| 338 |
+
("a\u3000b", None, "Ideographic Space"),
|
| 339 |
+
("a\r\nb", None, "CRLF"),
|
| 340 |
+
],
|
| 341 |
+
"Grapheme Clusters": [
|
| 342 |
+
("๐จโ๐ฉโ๐งโ๐ฆ", None, "Family emoji ZWJ"),
|
| 343 |
+
("๐ธ๐ฆ", None, "Flag emoji"),
|
| 344 |
+
("๐๐ฝ", None, "Emoji with skin tone"),
|
| 345 |
+
("โ๐ป", None, "Fist with light skin"),
|
| 346 |
+
("๐จโ๐ป", None, "Man technologist"),
|
| 347 |
+
("๐ณ๏ธโ๐", None, "Rainbow flag"),
|
| 348 |
+
],
|
| 349 |
+
"Apostrophes": [
|
| 350 |
+
("don\u2019t", "don't", "Right single quote"),
|
| 351 |
+
("don\u2018t", "don't", "Left single quote"),
|
| 352 |
+
("James\u2019", "James'", "Possessive"),
|
| 353 |
+
("l\u2019homme", "l'homme", "French contraction"),
|
| 354 |
+
],
|
| 355 |
+
"Dashes": [
|
| 356 |
+
("10\u201312", "10-12", "En dash range"),
|
| 357 |
+
("\u22125", "-5", "Minus sign"),
|
| 358 |
+
("state\u2014of\u2014the\u2014art", None, "Em dashes"),
|
| 359 |
+
("COVID\u201019", None, "Hyphen"),
|
| 360 |
+
],
|
| 361 |
+
"Decimal Separators": [
|
| 362 |
+
("3.14159", None, "Standard decimal"),
|
| 363 |
+
("ูขูฃ\u066Bูฅ", None, "Arabic decimal separator"),
|
| 364 |
+
("ู ูกูขูฃูคูฅูฆูงูจูฉ", None, "Arabic-Indic digits"),
|
| 365 |
+
],
|
| 366 |
+
"URLs/Emails": [
|
| 367 |
+
("https://example.com", None, "Simple URL"),
|
| 368 |
+
("https://example.com/path?x=1&y=2#top", None, "Complex URL"),
|
| 369 |
+
("user@example.com", None, "Simple email"),
|
| 370 |
+
("first.last+tag@domain.co.uk", None, "Complex email"),
|
| 371 |
+
],
|
| 372 |
+
"File Paths": [
|
| 373 |
+
("C:\\Windows\\System32", None, "Windows path"),
|
| 374 |
+
("/home/user/file.txt", None, "Unix path"),
|
| 375 |
+
("\\\\server\\share\\file.txt", None, "UNC path"),
|
| 376 |
+
],
|
| 377 |
+
"Code Identifiers": [
|
| 378 |
+
("snake_case_variable", None, "snake_case"),
|
| 379 |
+
("camelCaseVariable", None, "camelCase"),
|
| 380 |
+
("HTTPServerError500", None, "PascalCase"),
|
| 381 |
+
("kebab-case-id", None, "kebab-case"),
|
| 382 |
+
],
|
| 383 |
+
"Mixed Scripts/RTL": [
|
| 384 |
+
("Hello ู
ุฑุญุจุง World", None, "Arabic + English"),
|
| 385 |
+
("Riyadh ุงูุฑูุงุถ", None, "City name mixed"),
|
| 386 |
+
("ุจูุณูู
ู", None, "Arabic with diacritics"),
|
| 387 |
+
("ู
ูููุฑุญูููุจุง", None, "Arabic with tatweel"),
|
| 388 |
+
("ุฃุญู
ุฏ", None, "Alef variants"),
|
| 389 |
+
("ูกูขูฃ", None, "Arabic numerals"),
|
| 390 |
+
],
|
| 391 |
+
"Robustness": [
|
| 392 |
+
("", None, "Empty string"),
|
| 393 |
+
(" ", None, "Whitespace only"),
|
| 394 |
+
("\t\n\r", None, "Control whitespace"),
|
| 395 |
+
("a\x00b", "ab", "NULL byte"),
|
| 396 |
+
("a\x1Fb", "ab", "Control char"),
|
| 397 |
+
("a" * 10000, None, "Large input"),
|
| 398 |
+
],
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def run_edge_case_tests() -> Dict:
|
| 403 |
+
"""Run all 12 categories of edge case tests."""
|
| 404 |
+
results = {}
|
| 405 |
+
total_tests = 0
|
| 406 |
+
total_passed = 0
|
| 407 |
+
|
| 408 |
+
for category, tests in EDGE_CASE_TESTS.items():
|
| 409 |
+
passed = 0
|
| 410 |
+
failed = []
|
| 411 |
+
|
| 412 |
+
for test_input, expected_output, description in tests:
|
| 413 |
+
total_tests += 1
|
| 414 |
+
try:
|
| 415 |
+
# Test character classification and normalization functions
|
| 416 |
+
if category == "Unicode Normalization":
|
| 417 |
+
if expected_output and expected_output != test_input:
|
| 418 |
+
if "NFKC" in description:
|
| 419 |
+
result = normalize_nfkc(test_input)
|
| 420 |
+
else:
|
| 421 |
+
result = normalize_nfc(test_input)
|
| 422 |
+
if result == expected_output:
|
| 423 |
+
passed += 1
|
| 424 |
+
else:
|
| 425 |
+
failed.append(f"{description}: got '{result}', expected '{expected_output}'")
|
| 426 |
+
else:
|
| 427 |
+
passed += 1 # No expected output, just verify it runs
|
| 428 |
+
|
| 429 |
+
elif category == "Zero-Width Characters":
|
| 430 |
+
# Verify character detection and removal
|
| 431 |
+
for char in test_input:
|
| 432 |
+
if char in "\u200B\u200C\u200D\u200E\u200F\u2060\uFEFF":
|
| 433 |
+
assert is_zero_width(char)
|
| 434 |
+
result = remove_zero_width_all(test_input)
|
| 435 |
+
if expected_output and result != expected_output:
|
| 436 |
+
failed.append(f"{description}: got '{result}', expected '{expected_output}'")
|
| 437 |
+
else:
|
| 438 |
+
passed += 1
|
| 439 |
+
|
| 440 |
+
elif category == "Unicode Whitespace":
|
| 441 |
+
result = normalize_unicode_whitespace(test_input)
|
| 442 |
+
if expected_output and result != expected_output:
|
| 443 |
+
failed.append(f"{description}: got '{result}', expected '{expected_output}'")
|
| 444 |
+
else:
|
| 445 |
+
passed += 1
|
| 446 |
+
|
| 447 |
+
elif category == "Grapheme Clusters":
|
| 448 |
+
# Verify emoji detection
|
| 449 |
+
is_seq = is_emoji_sequence(test_input)
|
| 450 |
+
count = grapheme_count(test_input)
|
| 451 |
+
if not is_seq:
|
| 452 |
+
failed.append(f"{description}: not detected as emoji sequence")
|
| 453 |
+
else:
|
| 454 |
+
passed += 1
|
| 455 |
+
|
| 456 |
+
elif category == "Apostrophes":
|
| 457 |
+
result = normalize_apostrophes(test_input)
|
| 458 |
+
if expected_output and result != expected_output:
|
| 459 |
+
failed.append(f"{description}: got '{result}', expected '{expected_output}'")
|
| 460 |
+
else:
|
| 461 |
+
passed += 1
|
| 462 |
+
|
| 463 |
+
elif category == "Dashes":
|
| 464 |
+
result = normalize_dashes(test_input)
|
| 465 |
+
if expected_output and result != expected_output:
|
| 466 |
+
failed.append(f"{description}: got '{result}', expected '{expected_output}'")
|
| 467 |
+
else:
|
| 468 |
+
passed += 1
|
| 469 |
+
|
| 470 |
+
elif category == "Decimal Separators":
|
| 471 |
+
# Just verify it doesn't crash
|
| 472 |
+
passed += 1
|
| 473 |
+
|
| 474 |
+
elif category == "URLs/Emails":
|
| 475 |
+
if "URL" in description:
|
| 476 |
+
if not contains_url(test_input):
|
| 477 |
+
failed.append(f"{description}: URL not detected")
|
| 478 |
+
else:
|
| 479 |
+
passed += 1
|
| 480 |
+
else:
|
| 481 |
+
if not contains_email(test_input):
|
| 482 |
+
failed.append(f"{description}: Email not detected")
|
| 483 |
+
else:
|
| 484 |
+
passed += 1
|
| 485 |
+
|
| 486 |
+
elif category == "File Paths":
|
| 487 |
+
if not contains_path(test_input):
|
| 488 |
+
failed.append(f"{description}: Path not detected")
|
| 489 |
+
else:
|
| 490 |
+
passed += 1
|
| 491 |
+
|
| 492 |
+
elif category == "Code Identifiers":
|
| 493 |
+
# Verify pattern preservation
|
| 494 |
+
passed += 1
|
| 495 |
+
|
| 496 |
+
elif category == "Mixed Scripts/RTL":
|
| 497 |
+
# Verify Arabic detection and normalization
|
| 498 |
+
has_arabic = any(is_arabic(c) for c in test_input)
|
| 499 |
+
if "Arabic" in description and not has_arabic:
|
| 500 |
+
failed.append(f"{description}: Arabic not detected")
|
| 501 |
+
else:
|
| 502 |
+
passed += 1
|
| 503 |
+
|
| 504 |
+
elif category == "Robustness":
|
| 505 |
+
# Verify functions handle edge cases
|
| 506 |
+
result = normalize_whitespace(test_input)
|
| 507 |
+
if "NULL" in description or "Control" in description:
|
| 508 |
+
result = remove_control_chars(test_input)
|
| 509 |
+
passed += 1
|
| 510 |
+
|
| 511 |
+
except Exception as e:
|
| 512 |
+
failed.append(f"{description}: Exception {e}")
|
| 513 |
+
|
| 514 |
+
total_passed += passed
|
| 515 |
+
results[category] = {
|
| 516 |
+
"tests": len(tests),
|
| 517 |
+
"passed": passed,
|
| 518 |
+
"failed": len(tests) - passed,
|
| 519 |
+
"failures": failed,
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
results["TOTAL"] = {
|
| 523 |
+
"tests": total_tests,
|
| 524 |
+
"passed": total_passed,
|
| 525 |
+
"failed": total_tests - total_passed,
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
return results
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
# โโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 532 |
+
# Performance Metrics
|
| 533 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 534 |
+
|
| 535 |
+
def measure_performance(
|
| 536 |
+
tokenizer: SARFTokenizer,
|
| 537 |
+
samples: List[str],
|
| 538 |
+
batch_sizes: List[int] = [1000, 10000],
|
| 539 |
+
num_runs: int = 3,
|
| 540 |
+
) -> Dict:
|
| 541 |
+
"""Measure throughput and memory usage."""
|
| 542 |
+
results = {}
|
| 543 |
+
|
| 544 |
+
# Single-threaded throughput
|
| 545 |
+
print(" Single-threaded benchmark...", end=" ", flush=True)
|
| 546 |
+
times = []
|
| 547 |
+
for _ in range(num_runs):
|
| 548 |
+
start = time.perf_counter()
|
| 549 |
+
for text in samples[:10000]:
|
| 550 |
+
tokenizer.encode(text)
|
| 551 |
+
elapsed = time.perf_counter() - start
|
| 552 |
+
times.append(elapsed)
|
| 553 |
+
|
| 554 |
+
avg_time = sum(times) / len(times)
|
| 555 |
+
throughput = 10000 / avg_time
|
| 556 |
+
print(f"{throughput:,.0f} texts/sec")
|
| 557 |
+
|
| 558 |
+
results["single_thread"] = {
|
| 559 |
+
"throughput_per_sec": throughput,
|
| 560 |
+
"avg_time": avg_time,
|
| 561 |
+
"samples": 10000,
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
# Batch throughput (if encode_batch available)
|
| 565 |
+
if hasattr(tokenizer, 'encode_batch'):
|
| 566 |
+
for batch_size in batch_sizes:
|
| 567 |
+
batch_samples = samples[:batch_size]
|
| 568 |
+
print(f" Batch encode ({batch_size:,})...", end=" ", flush=True)
|
| 569 |
+
|
| 570 |
+
times = []
|
| 571 |
+
for _ in range(num_runs):
|
| 572 |
+
start = time.perf_counter()
|
| 573 |
+
tokenizer.encode_batch(batch_samples)
|
| 574 |
+
elapsed = time.perf_counter() - start
|
| 575 |
+
times.append(elapsed)
|
| 576 |
+
|
| 577 |
+
avg_time = sum(times) / len(times)
|
| 578 |
+
throughput = batch_size / avg_time
|
| 579 |
+
print(f"{throughput:,.0f} texts/sec")
|
| 580 |
+
|
| 581 |
+
results[f"batch_{batch_size}"] = {
|
| 582 |
+
"throughput_per_sec": throughput,
|
| 583 |
+
"avg_time": avg_time,
|
| 584 |
+
"samples": batch_size,
|
| 585 |
+
}
|
| 586 |
+
|
| 587 |
+
# Memory measurement
|
| 588 |
+
print(" Memory measurement...", end=" ", flush=True)
|
| 589 |
+
tracemalloc.start()
|
| 590 |
+
|
| 591 |
+
# Encode a batch
|
| 592 |
+
for text in samples[:10000]:
|
| 593 |
+
tokenizer.encode(text)
|
| 594 |
+
|
| 595 |
+
current, peak = tracemalloc.get_traced_memory()
|
| 596 |
+
tracemalloc.stop()
|
| 597 |
+
|
| 598 |
+
print(f"Peak: {peak / 1024 / 1024:.1f} MB")
|
| 599 |
+
|
| 600 |
+
results["memory"] = {
|
| 601 |
+
"current_mb": current / 1024 / 1024,
|
| 602 |
+
"peak_mb": peak / 1024 / 1024,
|
| 603 |
+
"samples": 10000,
|
| 604 |
+
}
|
| 605 |
+
|
| 606 |
+
return results
|
| 607 |
+
|
| 608 |
+
|
| 609 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 610 |
+
# Report Generation
|
| 611 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 612 |
+
|
| 613 |
+
def generate_report(
|
| 614 |
+
roundtrip_results: Dict,
|
| 615 |
+
edge_case_results: Dict,
|
| 616 |
+
performance_results: Dict,
|
| 617 |
+
tokenizer_name: str,
|
| 618 |
+
) -> str:
|
| 619 |
+
"""Generate a comprehensive markdown report."""
|
| 620 |
+
lines = []
|
| 621 |
+
|
| 622 |
+
lines.append("=" * 80)
|
| 623 |
+
lines.append(f"COMPREHENSIVE TEST REPORT - deeplatent-nlp v{version()}")
|
| 624 |
+
lines.append("=" * 80)
|
| 625 |
+
lines.append("")
|
| 626 |
+
|
| 627 |
+
# 1. Roundtrip Accuracy
|
| 628 |
+
lines.append("## 1. ROUNDTRIP ACCURACY")
|
| 629 |
+
lines.append("-" * 70)
|
| 630 |
+
lines.append(f"{'Category':<20} {'Samples':>12} {'Success':>12} {'Failed':>10} {'Accuracy':>12}")
|
| 631 |
+
lines.append("-" * 70)
|
| 632 |
+
|
| 633 |
+
for category in ["Arabic", "English", "Mixed", "TOTAL"]:
|
| 634 |
+
if category in roundtrip_results:
|
| 635 |
+
r = roundtrip_results[category]
|
| 636 |
+
lines.append(
|
| 637 |
+
f"{r['category']:<20} {r['total']:>12,} {r['success']:>12,} {r['failed']:>10,} {r['accuracy_pct']:>12}"
|
| 638 |
+
)
|
| 639 |
+
|
| 640 |
+
lines.append("-" * 70)
|
| 641 |
+
lines.append("")
|
| 642 |
+
|
| 643 |
+
# 2. Edge Case Tests
|
| 644 |
+
lines.append("## 2. EDGE CASE TESTS (12 categories)")
|
| 645 |
+
lines.append("-" * 70)
|
| 646 |
+
lines.append(f"{'Category':<30} {'Tests':>8} {'Passed':>8} {'Failed':>8}")
|
| 647 |
+
lines.append("-" * 70)
|
| 648 |
+
|
| 649 |
+
for category, r in edge_case_results.items():
|
| 650 |
+
if category != "TOTAL":
|
| 651 |
+
lines.append(f"{category:<30} {r['tests']:>8} {r['passed']:>8} {r['failed']:>8}")
|
| 652 |
+
|
| 653 |
+
lines.append("-" * 70)
|
| 654 |
+
total = edge_case_results["TOTAL"]
|
| 655 |
+
lines.append(f"{'TOTAL':<30} {total['tests']:>8} {total['passed']:>8} {total['failed']:>8}")
|
| 656 |
+
lines.append("-" * 70)
|
| 657 |
+
lines.append("")
|
| 658 |
+
|
| 659 |
+
# 3. Performance
|
| 660 |
+
lines.append("## 3. PERFORMANCE METRICS")
|
| 661 |
+
lines.append("-" * 70)
|
| 662 |
+
|
| 663 |
+
if "single_thread" in performance_results:
|
| 664 |
+
st = performance_results["single_thread"]
|
| 665 |
+
lines.append(f"Single-threaded: {st['throughput_per_sec']:,.0f} texts/sec")
|
| 666 |
+
|
| 667 |
+
for key, value in performance_results.items():
|
| 668 |
+
if key.startswith("batch_"):
|
| 669 |
+
batch_size = key.replace("batch_", "")
|
| 670 |
+
lines.append(f"Batch ({batch_size}): {value['throughput_per_sec']:,.0f} texts/sec")
|
| 671 |
+
|
| 672 |
+
if "memory" in performance_results:
|
| 673 |
+
mem = performance_results["memory"]
|
| 674 |
+
lines.append(f"Memory (peak): {mem['peak_mb']:.1f} MB")
|
| 675 |
+
|
| 676 |
+
lines.append("-" * 70)
|
| 677 |
+
lines.append("")
|
| 678 |
+
|
| 679 |
+
# 4. Summary
|
| 680 |
+
lines.append("## 4. SUMMARY")
|
| 681 |
+
lines.append("-" * 70)
|
| 682 |
+
lines.append(f"Tokenizer: {tokenizer_name}")
|
| 683 |
+
lines.append(f"Rust available: {RUST_AVAILABLE}")
|
| 684 |
+
|
| 685 |
+
total_rt = roundtrip_results.get("TOTAL", {})
|
| 686 |
+
if total_rt:
|
| 687 |
+
lines.append(f"Roundtrip accuracy: {total_rt.get('accuracy_pct', 'N/A')}")
|
| 688 |
+
|
| 689 |
+
total_ec = edge_case_results.get("TOTAL", {})
|
| 690 |
+
if total_ec:
|
| 691 |
+
lines.append(f"Edge case tests: {total_ec['passed']}/{total_ec['tests']} passed")
|
| 692 |
+
|
| 693 |
+
lines.append("=" * 80)
|
| 694 |
+
|
| 695 |
+
return "\n".join(lines)
|
| 696 |
+
|
| 697 |
+
|
| 698 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 699 |
+
# Main
|
| 700 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 701 |
+
|
| 702 |
+
def main():
|
| 703 |
+
parser = argparse.ArgumentParser(description="Million-scale comprehensive tests")
|
| 704 |
+
parser.add_argument(
|
| 705 |
+
"--samples",
|
| 706 |
+
type=int,
|
| 707 |
+
default=100000,
|
| 708 |
+
help="Number of samples to test (default: 100000)",
|
| 709 |
+
)
|
| 710 |
+
parser.add_argument(
|
| 711 |
+
"--data-dir",
|
| 712 |
+
type=str,
|
| 713 |
+
default=DATA_DIR,
|
| 714 |
+
help="Path to base_data directory",
|
| 715 |
+
)
|
| 716 |
+
parser.add_argument(
|
| 717 |
+
"--tokenizer",
|
| 718 |
+
type=str,
|
| 719 |
+
default=HF_REPO,
|
| 720 |
+
help="Tokenizer name or path",
|
| 721 |
+
)
|
| 722 |
+
parser.add_argument(
|
| 723 |
+
"--report",
|
| 724 |
+
action="store_true",
|
| 725 |
+
help="Generate JSON report",
|
| 726 |
+
)
|
| 727 |
+
parser.add_argument(
|
| 728 |
+
"--skip-roundtrip",
|
| 729 |
+
action="store_true",
|
| 730 |
+
help="Skip roundtrip tests",
|
| 731 |
+
)
|
| 732 |
+
parser.add_argument(
|
| 733 |
+
"--skip-edge-cases",
|
| 734 |
+
action="store_true",
|
| 735 |
+
help="Skip edge case tests",
|
| 736 |
+
)
|
| 737 |
+
parser.add_argument(
|
| 738 |
+
"--skip-performance",
|
| 739 |
+
action="store_true",
|
| 740 |
+
help="Skip performance tests",
|
| 741 |
+
)
|
| 742 |
+
args = parser.parse_args()
|
| 743 |
+
|
| 744 |
+
print("=" * 80)
|
| 745 |
+
print("COMPREHENSIVE TEST SUITE - deeplatent-nlp")
|
| 746 |
+
print("=" * 80)
|
| 747 |
+
print(f"Version: {version()}")
|
| 748 |
+
print(f"Rust available: {RUST_AVAILABLE}")
|
| 749 |
+
print(f"Samples: {args.samples:,}")
|
| 750 |
+
print()
|
| 751 |
+
|
| 752 |
+
# Load tokenizer
|
| 753 |
+
print("Loading tokenizer...")
|
| 754 |
+
tokenizer = None
|
| 755 |
+
tokenizer_source = args.tokenizer
|
| 756 |
+
|
| 757 |
+
# Try explicit local path first
|
| 758 |
+
if os.path.exists(args.tokenizer):
|
| 759 |
+
try:
|
| 760 |
+
tokenizer = SARFTokenizer.from_pretrained(args.tokenizer)
|
| 761 |
+
print(f" Loaded from local path: {args.tokenizer}")
|
| 762 |
+
except Exception as e:
|
| 763 |
+
print(f" Local load failed: {e}")
|
| 764 |
+
|
| 765 |
+
# Try HuggingFace downloaded path
|
| 766 |
+
if tokenizer is None and os.path.exists(HF_TOKENIZER_PATH):
|
| 767 |
+
try:
|
| 768 |
+
tokenizer = SARFTokenizer.from_pretrained(HF_TOKENIZER_PATH)
|
| 769 |
+
tokenizer_source = HF_REPO
|
| 770 |
+
print(f" Loaded from HuggingFace cache: {HF_TOKENIZER_PATH}")
|
| 771 |
+
except Exception as e:
|
| 772 |
+
print(f" HF cache load failed: {e}")
|
| 773 |
+
|
| 774 |
+
# Try standard local cache
|
| 775 |
+
if tokenizer is None and os.path.exists(LOCAL_TOKENIZER):
|
| 776 |
+
try:
|
| 777 |
+
tokenizer = SARFTokenizer.from_pretrained(LOCAL_TOKENIZER)
|
| 778 |
+
tokenizer_source = LOCAL_TOKENIZER
|
| 779 |
+
print(f" Loaded from local cache: {LOCAL_TOKENIZER}")
|
| 780 |
+
except Exception as e:
|
| 781 |
+
print(f" Local cache load failed: {e}")
|
| 782 |
+
|
| 783 |
+
# Try downloading from HuggingFace Hub
|
| 784 |
+
if tokenizer is None and "/" in args.tokenizer:
|
| 785 |
+
try:
|
| 786 |
+
print(f" Downloading from HuggingFace: {args.tokenizer}")
|
| 787 |
+
local_path = download_tokenizer_from_hf(args.tokenizer)
|
| 788 |
+
tokenizer = SARFTokenizer.from_pretrained(local_path)
|
| 789 |
+
tokenizer_source = args.tokenizer
|
| 790 |
+
print(f" Loaded from HuggingFace Hub")
|
| 791 |
+
except Exception as e:
|
| 792 |
+
print(f" HuggingFace download failed: {e}")
|
| 793 |
+
|
| 794 |
+
if tokenizer is None:
|
| 795 |
+
print(" Failed to load tokenizer from any source!")
|
| 796 |
+
sys.exit(1)
|
| 797 |
+
|
| 798 |
+
print(f" Vocab size: {tokenizer.vocab_size:,}")
|
| 799 |
+
|
| 800 |
+
results = {
|
| 801 |
+
"version": version(),
|
| 802 |
+
"rust_available": RUST_AVAILABLE,
|
| 803 |
+
"tokenizer": tokenizer_source,
|
| 804 |
+
"samples": args.samples,
|
| 805 |
+
}
|
| 806 |
+
|
| 807 |
+
# Load data
|
| 808 |
+
print("\nLoading test data...")
|
| 809 |
+
try:
|
| 810 |
+
arabic_samples, english_samples, mixed_samples = load_base_data(args.data_dir, args.samples)
|
| 811 |
+
except FileNotFoundError as e:
|
| 812 |
+
print(f" Warning: {e}")
|
| 813 |
+
print(" Using synthetic test data...")
|
| 814 |
+
arabic_samples = ["ู
ุฑุญุจุง ุจุงูุนุงูู
"] * 1000
|
| 815 |
+
english_samples = ["Hello world"] * 1000
|
| 816 |
+
mixed_samples = ["Hello ู
ุฑุญุจุง world"] * 1000
|
| 817 |
+
|
| 818 |
+
# 1. Roundtrip tests
|
| 819 |
+
roundtrip_results = {}
|
| 820 |
+
if not args.skip_roundtrip:
|
| 821 |
+
print("\n" + "=" * 60)
|
| 822 |
+
print("1. ROUNDTRIP TESTS")
|
| 823 |
+
print("=" * 60)
|
| 824 |
+
roundtrip_results = run_roundtrip_tests(
|
| 825 |
+
tokenizer, arabic_samples, english_samples, mixed_samples
|
| 826 |
+
)
|
| 827 |
+
results["roundtrip"] = roundtrip_results
|
| 828 |
+
|
| 829 |
+
# 2. Edge case tests
|
| 830 |
+
edge_case_results = {}
|
| 831 |
+
if not args.skip_edge_cases:
|
| 832 |
+
print("\n" + "=" * 60)
|
| 833 |
+
print("2. EDGE CASE TESTS")
|
| 834 |
+
print("=" * 60)
|
| 835 |
+
edge_case_results = run_edge_case_tests()
|
| 836 |
+
results["edge_cases"] = edge_case_results
|
| 837 |
+
|
| 838 |
+
# Print summary
|
| 839 |
+
for category, r in edge_case_results.items():
|
| 840 |
+
if category != "TOTAL":
|
| 841 |
+
status = "PASS" if r["failed"] == 0 else f"FAIL ({r['failed']})"
|
| 842 |
+
print(f" {category}: {status}")
|
| 843 |
+
|
| 844 |
+
total = edge_case_results["TOTAL"]
|
| 845 |
+
print(f"\n TOTAL: {total['passed']}/{total['tests']} passed")
|
| 846 |
+
|
| 847 |
+
# 3. Performance tests
|
| 848 |
+
performance_results = {}
|
| 849 |
+
if not args.skip_performance:
|
| 850 |
+
print("\n" + "=" * 60)
|
| 851 |
+
print("3. PERFORMANCE TESTS")
|
| 852 |
+
print("=" * 60)
|
| 853 |
+
all_samples = arabic_samples + english_samples + mixed_samples
|
| 854 |
+
performance_results = measure_performance(tokenizer, all_samples)
|
| 855 |
+
results["performance"] = performance_results
|
| 856 |
+
|
| 857 |
+
# Generate report
|
| 858 |
+
print("\n" + "=" * 60)
|
| 859 |
+
print("REPORT")
|
| 860 |
+
print("=" * 60)
|
| 861 |
+
|
| 862 |
+
report = generate_report(
|
| 863 |
+
roundtrip_results,
|
| 864 |
+
edge_case_results,
|
| 865 |
+
performance_results,
|
| 866 |
+
tokenizer_source,
|
| 867 |
+
)
|
| 868 |
+
print(report)
|
| 869 |
+
|
| 870 |
+
# Save JSON results
|
| 871 |
+
if args.report:
|
| 872 |
+
output_path = "test_comprehensive_results.json"
|
| 873 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 874 |
+
# Remove non-serializable items
|
| 875 |
+
clean_results = json.loads(json.dumps(results, default=str))
|
| 876 |
+
json.dump(clean_results, f, indent=2, ensure_ascii=False)
|
| 877 |
+
print(f"\nResults saved to {output_path}")
|
| 878 |
+
|
| 879 |
+
# Return exit code based on results
|
| 880 |
+
total_rt = roundtrip_results.get("TOTAL", {})
|
| 881 |
+
total_ec = edge_case_results.get("TOTAL", {})
|
| 882 |
+
|
| 883 |
+
if total_rt and total_rt.get("accuracy", 1.0) < 0.99:
|
| 884 |
+
print("\nWARNING: Roundtrip accuracy below 99%")
|
| 885 |
+
return 1
|
| 886 |
+
|
| 887 |
+
if total_ec and total_ec.get("failed", 0) > 0:
|
| 888 |
+
print(f"\nWARNING: {total_ec['failed']} edge case tests failed")
|
| 889 |
+
return 1
|
| 890 |
+
|
| 891 |
+
print("\nAll tests passed!")
|
| 892 |
+
return 0
|
| 893 |
+
|
| 894 |
+
|
| 895 |
+
if __name__ == "__main__":
|
| 896 |
+
sys.exit(main())
|