-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fffd.py
More file actions
32 lines (27 loc) · 1.1 KB
/
Copy pathtest_fffd.py
File metadata and controls
32 lines (27 loc) · 1.1 KB
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
#!/usr/bin/env python3
"""Test handling of U+FFFD replacement character."""
from transformers import BertTokenizerFast
tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased")
backend = tokenizer.backend_tokenizer
normalizer = backend.normalizer
test_cases = [
"state\uFFFDs", # replacement char
"Rond\uFFFDnia",
"\uFFFD", # standalone
"hello\uFFFDworld",
"1080×1920", # multiplication sign
"×", # standalone mult sign
]
print("HF Normalizer behavior with special chars:\n")
for text in test_cases:
if normalizer:
normalized = normalizer.normalize_str(text)
else:
normalized = text
tokens = tokenizer.encode(text, add_special_tokens=False)
token_strs = tokenizer.convert_ids_to_tokens(tokens)
print(f"Input: {repr(text):25} -> Normalized: {repr(normalized):25} -> Tokens: {token_strs}")
# Check Unicode category
import unicodedata
print(f"\nU+FFFD category: {unicodedata.category(chr(0xFFFD))} ({unicodedata.name(chr(0xFFFD), 'UNKNOWN')})")
print(f"U+00D7 category: {unicodedata.category(chr(0x00D7))} ({unicodedata.name(chr(0x00D7), 'UNKNOWN')})")