tinycodinglm / i18n /i18n-0010.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.46 kB
opencode trace
System You are an expert NLP engineer. You explain text-encoding failures precisely and give correct, minimal Python examples.

You are an expert NLP engineer. You explain text-encoding failures precisely and give correct, minimal Python examples.

User

What is mojibake, where does the term come from, and how do I fix it when I see it?

Assistant

Mojibake (文字化け, Japanese for "character transformation") is garbled text produced by decoding bytes with the wrong character encoding. It is not a font problem and not missing glyphs — the bytes are intact, they were just interpreted under the wrong codec.

Origins: the term comes from Japanese computing in the 1980s-90s, when Shift_JIS, EUC-JP and ISO-2022-JP coexisted and mail/news readers routinely guessed wrong, turning Japanese into garbage. It is now the general term for any such mis-decode, in any language.

How to recognize it: the corruption has signatures of the (wrong, right) codec pair:

you see actual text what happened
Nguyá»…n Nguyễn UTF-8 bytes read as Windows-1252
Lê Lê UTF-8 bytes read as Windows-1252
��� 你好 non-UTF-8 bytes read as UTF-8 with replacement

Repair, reversible case: decoders like cp1252 never fail on any byte, so no information is lost — re-encode with the wrong codec and decode with the right one:

python
garbled = "Nguyá»…n"  # UTF-8 bytes misread as cp1252
fixed = garbled.encode("cp1252").decode("utf-8")
assert fixed == "Nguyễn"
print(fixed)

Repair, lossy case: if the wrong decode used errors="replace", every bad byte became U+FFFD and the original is unrecoverable from the string — go back to the raw bytes:

python
raw = "你好".encode("gbk")                    # b'\xc4\xe3\xba\xc3'
seen = raw.decode("utf-8", errors="replace")  # full of U+FFFD
assert "\ufffd" in seen  # information lost; re-read the source bytes
correct = raw.decode("gbk")
assert correct == "你好"

Prevention beats repair: always pass encoding explicitly when opening text (UTF-8 by default, utf-8-sig for unknown sources), and treat a sudden burst of á», Ã, � as an encoding bug, never as content to train on.