mojifix 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. mojifix-0.1.0/.gitignore +16 -0
  2. mojifix-0.1.0/CHANGELOG.md +19 -0
  3. mojifix-0.1.0/LICENSE +21 -0
  4. mojifix-0.1.0/PKG-INFO +235 -0
  5. mojifix-0.1.0/README.ja.md +122 -0
  6. mojifix-0.1.0/README.md +213 -0
  7. mojifix-0.1.0/docs/calibration.md +292 -0
  8. mojifix-0.1.0/pyproject.toml +57 -0
  9. mojifix-0.1.0/scripts/calibrate.py +619 -0
  10. mojifix-0.1.0/src/mojifix/__init__.py +10 -0
  11. mojifix-0.1.0/src/mojifix/__main__.py +8 -0
  12. mojifix-0.1.0/src/mojifix/cli.py +230 -0
  13. mojifix-0.1.0/src/mojifix/core.py +125 -0
  14. mojifix-0.1.0/src/mojifix/detect.py +52 -0
  15. mojifix-0.1.0/src/mojifix/names.py +155 -0
  16. mojifix-0.1.0/src/mojifix/paths.py +43 -0
  17. mojifix-0.1.0/src/mojifix/py.typed +0 -0
  18. mojifix-0.1.0/src/mojifix/repair.py +18 -0
  19. mojifix-0.1.0/src/mojifix/score.py +206 -0
  20. mojifix-0.1.0/src/mojifix/search.py +33 -0
  21. mojifix-0.1.0/src/mojifix/zipnames.py +273 -0
  22. mojifix-0.1.0/tests/corpus/filenames.txt +153 -0
  23. mojifix-0.1.0/tests/corpus/halfwidth_words.txt +96 -0
  24. mojifix-0.1.0/tests/corpus/normal_other.txt +61 -0
  25. mojifix-0.1.0/tests/corpus/sentences.txt +176 -0
  26. mojifix-0.1.0/tests/corpus/short.txt +154 -0
  27. mojifix-0.1.0/tests/corpus/zip_european_names.txt +82 -0
  28. mojifix-0.1.0/tests/corpus/zip_japanese_names.txt +55 -0
  29. mojifix-0.1.0/tests/corpus_util.py +123 -0
  30. mojifix-0.1.0/tests/test_cli.py +204 -0
  31. mojifix-0.1.0/tests/test_core.py +228 -0
  32. mojifix-0.1.0/tests/test_corpus.py +99 -0
  33. mojifix-0.1.0/tests/test_private.py +57 -0
  34. mojifix-0.1.0/tests/test_zip.py +224 -0
  35. mojifix-0.1.0/tests/zip_util.py +32 -0
  36. mojifix-0.1.0/tests/zips/windows-compress-archive-utf8.zip +0 -0
  37. mojifix-0.1.0/tests/zips/windows-dotnet-cp932.zip +0 -0
@@ -0,0 +1,16 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ build/
5
+ dist/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ htmlcov/
12
+ .DS_Store
13
+ Thumbs.db
14
+ .vscode/
15
+ .idea/
16
+ tests/corpus_private/
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [0.1.0] - 2026-12-03
8
+
9
+ ### Added
10
+
11
+ - `fix`, `candidates`, `explain`, `looks_garbled` and the `Candidate` dataclass.
12
+ - Ten wrong-read paths (CP932/Latin-1/CP1252/CP437 read instead of UTF-8, CP932, EUC-JP;
13
+ leftover ISO-2022-JP escapes) and any two of them in a row. Repairs are strict only.
14
+ - A calibrated character-class score and two margins (`THRESHOLD`, `AMBIG_MARGIN`):
15
+ 0 false repairs on the public and private normal-text samples (including 141,470 half-width katakana names); 94.5% of the public garbled samples restored.
16
+ - `mojifix text`, `mojifix names` (`--recursive`, `--apply`, `--log`, `--undo`) and
17
+ `mojifix zip` (`--extract`), all with `--json`. `python -m mojifix` works too.
18
+ - ZIP member names are read from their raw bytes, giving the same result on every OS.
19
+ - `scripts/calibrate.py` and `docs/calibration.md` (how the constants were chosen).
mojifix-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 takosasi-dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
mojifix-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,235 @@
1
+ Metadata-Version: 2.5
2
+ Name: mojifix
3
+ Version: 0.1.0
4
+ Summary: Repair Japanese mojibake (文字化け) strictly: undo wrong-codec reads like 縺薙s縺ォ縺。縺ッ → こんにちは, fix garbled file and ZIP member names.
5
+ Project-URL: Homepage, https://github.com/takosasi-dev/mojifix
6
+ Project-URL: Issues, https://github.com/takosasi-dev/mojifix/issues
7
+ Project-URL: Changelog, https://github.com/takosasi-dev/mojifix/blob/main/CHANGELOG.md
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: cp932,encoding,filename,japanese,mojibake,shift_jis,utf-8,zip
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Natural Language :: English
14
+ Classifier: Natural Language :: Japanese
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Topic :: Text Processing
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+
23
+ # MojiFix
24
+
25
+ [![CI](https://github.com/takosasi-dev/mojifix/actions/workflows/ci.yml/badge.svg)](https://github.com/takosasi-dev/mojifix/actions/workflows/ci.yml)
26
+ [![PyPI](https://img.shields.io/pypi/v/mojifix.svg)](https://pypi.org/project/mojifix/)
27
+ [![Python](https://img.shields.io/pypi/pyversions/mojifix.svg)](https://pypi.org/project/mojifix/)
28
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/takosasi-dev/mojifix/blob/main/LICENSE)
29
+
30
+ **Repair Japanese mojibake (文字化け) strictly — or say that it cannot be repaired.**
31
+ `縺薙s縺ォ縺。縺ッ` → `こんにちは`, `‚±‚ñ‚É‚¿‚Í` → `こんにちは`, garbled file names on disk, and garbled member names in ZIP files.
32
+
33
+ [日本語の README はこちら (README.ja.md)](https://github.com/takosasi-dev/mojifix/blob/main/README.ja.md)
34
+
35
+ ## What it does
36
+
37
+ - Undoes a **wrong-codec read** of Japanese text: the correct bytes were decoded with the wrong
38
+ character encoding (UTF-8 read as CP932, CP932 read as CP1252, a ZIP name read as CP437, …),
39
+ including **two such reads in a row**.
40
+ - Repairs only when re-encoding with the wrong codec gives back bytes that decode **exactly**
41
+ (`errors="strict"` both ways) with the right codec. If a single byte is missing, it does not
42
+ guess: the text is returned unchanged and `explain()` says it cannot be restored.
43
+ - Ranks the candidates with a small, calibrated "how Japanese is this?" score (no dictionary,
44
+ no model) and replaces the text **only when the best candidate is clearly better** than the
45
+ input and clearly better than the runner-up. Ordinary Japanese is left alone (0 false repairs
46
+ on every normal-text sample set, see [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md)).
47
+ - Lists garbled **file and folder names** in a folder and, only with `--apply`, renames them
48
+ (never overwriting, with a JSON Lines log and `--undo`).
49
+ - Lists the repaired **member names of a ZIP file** and, only with `--extract`, extracts with
50
+ the repaired names (never outside the destination, never overwriting).
51
+ - Standard library only. No network. Python 3.10+. Typed (`py.typed`).
52
+
53
+ ## What it does not do
54
+
55
+ - It does **not detect the encoding of raw bytes** (use charset-normalizer or chardet for that).
56
+ - It does **not rewrite file contents** — only strings, file names and ZIP member names.
57
+ - It does **not guess lost bytes**: text containing `U+FFFD` (�) is never "repaired".
58
+ - It does **not repair part of a string**: if only some of the text is garbled, the whole
59
+ string is left as is (`looks_garbled()` still reports it).
60
+ - It does **not handle Chinese or Korean** mojibake, and uses no dictionary or ML model.
61
+
62
+ ## Install
63
+
64
+ ```sh
65
+ pip install mojifix
66
+ ```
67
+
68
+ ## Quick start
69
+
70
+ ```python
71
+ import mojifix
72
+
73
+ mojifix.fix("縺薙s縺ォ縺。縺ッ") # 'こんにちは'
74
+ mojifix.fix("‚±‚ñ‚É‚¿‚Í") # 'こんにちは'
75
+ mojifix.fix("今日はいい天気ですね。") # unchanged: '今日はいい天気ですね。'
76
+
77
+ mojifix.explain("縺薙s縺ォ縺。縺ッ") # 'UTF-8 を CP932 として読んだ文字化け'
78
+ mojifix.explain("縺薙s縺ォ縺。縺ッ", lang="en") # 'Mojibake: UTF-8 bytes read as CP932'
79
+
80
+ for c in mojifix.candidates("πüôπéôπü½πüíπü»"): # a UTF-8 ZIP name read as CP437
81
+ print(c.text, c.path, round(c.score, 3))
82
+ # こんにちは (('cp437', 'utf-8'),) 1.0
83
+ # 縺薙s縺ォ縺。縺ッ (('cp437', 'cp932'),) 0.289
84
+
85
+ broken = "縺薙s縺ォ縺。" + chr(0xFFFD) + ".txt" # one byte was lost
86
+ mojifix.fix(broken) == broken # True: nothing is guessed
87
+ mojifix.explain(broken, "en") # 'Bytes were lost (contains U+FFFD); it cannot be restored'
88
+ mojifix.looks_garbled(broken) # True
89
+ ```
90
+
91
+ API (`from mojifix import ...`):
92
+
93
+ | Function | Returns |
94
+ |---|---|
95
+ | `fix(text, threshold=None)` | the repaired string, or `text` itself if nothing is clearly better |
96
+ | `candidates(text, max_depth=2)` | every strict repair as `Candidate(text, path, score)`, best first |
97
+ | `explain(text, lang="ja")` | a short sentence for the repair `fix()` makes (`"ja"` or `"en"`), or `None` |
98
+ | `looks_garbled(text)` | `True` if the text looks like mojibake (also for unrepairable or partly garbled text) |
99
+
100
+ `path` lists the steps in the order they are undone: `(("latin-1", "utf-8"), ("cp932", "utf-8"))`
101
+ means "read back as Latin-1 → UTF-8, then CP932 → UTF-8".
102
+
103
+ ### Command line
104
+
105
+ ```sh
106
+ mojifix text "縺薙s縺ォ縺。縺ッ" [--all] [--json]
107
+ mojifix names <folder> [--recursive] [--apply] [--log <file>] [--json]
108
+ mojifix names --undo <log>
109
+ mojifix zip <file.zip> [--extract <folder>] [--json]
110
+ ```
111
+
112
+ `python -m mojifix ...` works too. Nothing is written without `--apply` / `--extract`.
113
+ Output never crashes on a console that cannot show a character (it is written as an escape).
114
+
115
+ | Exit code | Meaning |
116
+ |---|---|
117
+ | 0 | success (including "nothing to repair") |
118
+ | 1 | usage error |
119
+ | 2 | some items were skipped (name already exists, not allowed on Windows, file in use, unsafe ZIP member, …), or extraction stopped because a name already exists |
120
+ | 3 | the input cannot be read (no such folder, broken ZIP, password-protected ZIP with `--extract`) |
121
+
122
+ ## Shapes it repairs
123
+
124
+ What `こんにちは` looks like after each wrong read (the rows MojiFix tries, in this order):
125
+
126
+ | Read as (wrong) | Actually (right) | `こんにちは` looks like |
127
+ |---|---|---|
128
+ | CP932 | UTF-8 | `縺薙s縺ォ縺。縺ッ` |
129
+ | Latin-1 | UTF-8 | `ã` followed by control characters (`ã\x81\x93ã\x82\x93…`) |
130
+ | CP1252 | UTF-8 | not possible for `こんにちは` (byte 0x81); `ん` alone looks like `ã‚“` |
131
+ | Latin-1 | CP932 | `\x82±\x82ñ\x82É…` (with control characters) |
132
+ | CP1252 | CP932 | `‚±‚ñ‚É‚¿‚Í` |
133
+ | CP437 | CP932 | `é▒é±é╔é┐é═` (ZIP member names) |
134
+ | CP437 | UTF-8 | `πüôπéôπü½πüíπü»` (ZIP member names) |
135
+ | CP932 | EUC-JP | `、ウ、`(private-use character)`ヒ、チ、マ` |
136
+ | Latin-1 | EUC-JP | `¤³¤ó¤Ë¤Á¤Ï` |
137
+ | ASCII | ISO-2022-JP | the escape sequences left as text (`ESC $B$3$s$K$A$O ESC (B`) |
138
+
139
+ Any two of these in a row (double mojibake) are tried as well.
140
+
141
+ ## How it decides
142
+
143
+ 1. **Strict repair only.** Each row is `text.encode(wrong, "strict").decode(right, "strict")`.
144
+ 2. **Score.** Every character is put in a class based on CP932 (hiragana, katakana, JIS level-1
145
+ kanji, JIS level-2 kanji, half-width katakana, ASCII, symbols, Greek/Cyrillic in CP932, IBM
146
+ extension kanji, other European letters, other letters, other symbols, controls / private use /
147
+ U+FFFD). The score is a weighted average. Level-2 kanji such as `縺 繧 繝` are typical of
148
+ mojibake and weigh 0.
149
+ 3. **Replace only when clearly better.** The best candidate must beat the input by `THRESHOLD`
150
+ and the runner-up by `AMBIG_MARGIN`, and must not itself look like mojibake. Otherwise `fix()`
151
+ returns the input unchanged.
152
+ 4. **Half-width katakana is never read as EUC-JP by `fix()`.** A string of only half-width
153
+ katakana and ASCII (`オオサカ・タナカ.txt`) is left alone even when it decodes as EUC-JP, because
154
+ `・` and `、` are also EUC-JP lead bytes. `candidates()` still lists that reading.
155
+
156
+ The weights and both margins were fitted on the public samples in `tests/corpus/` with
157
+ [`scripts/calibrate.py`](https://github.com/takosasi-dev/mojifix/blob/main/scripts/calibrate.py); the numbers (and what they do and do not prove)
158
+ are in [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md). Strings longer than 4,096 characters are
159
+ scored on their first 4,096 characters; the repair itself must still succeed on the whole string.
160
+
161
+ ## File names and ZIP files
162
+
163
+ - `mojifix names` lists names for which `looks_garbled()` is true, with the repaired name or the
164
+ reason it cannot be repaired. `--apply` renames deepest entries first and skips, one by one,
165
+ names that already exist, names Windows does not allow (`\ / : * ? " < > |`, `CON`, `NUL`, …)
166
+ and files in use. `--log` records `{"old": ..., "new": ...}` (absolute paths) per line; the log
167
+ file must not exist yet and is created before anything is renamed (an existing name is a usage
168
+ error, exit code 1; a log that cannot be created stops with exit code 3). `--undo` renames back
169
+ in reverse order and skips entries whose original name exists again.
170
+ - `mojifix zip` reads each member's **raw name bytes** (it re-encodes `ZipInfo.orig_filename`
171
+ with CP437, the codec `zipfile` used to decode them), so the result is the same on Windows,
172
+ Linux and macOS. ZIP names are decided differently from `fix()`: since the bytes are known to
173
+ have been read as CP437, the strict UTF-8 and CP932 readings are compared with the CP437 reading
174
+ itself (with ZIP-specific weights calibrated so that real CP437 names like `café.txt` or
175
+ `Größe.doc` stay as they are), and short names such as `予`, `表.xlsx` or `十月.txt` are restored. This also handles the CP932 characters whose second byte is `0x5C` (`\`), such
176
+ as 予 表 ソ: a `0x5C` that is the second byte of a character stays part of it, any other `0x5C`
177
+ is a path separator. This holds for names that cannot be repaired too: such a name keeps the
178
+ `\` and is skipped on `--extract`, since Windows does not allow it. Members with the UTF-8 flag
179
+ (or an Info-ZIP Unicode Path field) are not touched. MojiFix does not use `metadata_encoding` (Python 3.11+), which needs the encoding to
180
+ be known in advance.
181
+ - `--extract` refuses members with `..`, absolute paths, drive letters, UNC paths, NUL or
182
+ names Windows does not allow (skipped one by one, exit code 2), and stops **before writing
183
+ anything** if a name already exists in the destination, if two members would get the same
184
+ name, or if the ZIP is password-protected. A member that fails while being written (bad CRC,
185
+ unsupported compression, …) is removed again and reported; the others are still extracted
186
+ (exit code 2). `--json` lists every member as extracted, skipped or failed, with the reason.
187
+ - Names repaired from macOS-style ZIPs (UTF-8 without the flag) come back in **NFD**
188
+ (decomposed, e.g. `テ` + combining `゙`), exactly as stored. MojiFix does not normalize them to NFC.
189
+
190
+ ## Compared with ftfy
191
+
192
+ [ftfy](https://github.com/rspeer/python-ftfy) is the standard tool for mojibake in Western text and
193
+ documents that it does not handle legacy CJK encodings. On MojiFix's public samples (2,222 garbled
194
+ strings, one wrong read each), ftfy 6.3.1 restored 672 (30%): it handles the Latin-1/CP1252/CP437 →
195
+ UTF-8 rows and none of the CP932, EUC-JP or ISO-2022-JP rows. MojiFix restored 2,099 (94%) with no
196
+ false repair. Use ftfy for Western text; use MojiFix for Japanese.
197
+
198
+ ## Limitations
199
+
200
+ - **Partly garbled text is not repaired** (a garbled part joined to a normal part does not decode
201
+ strictly as a whole). `looks_garbled()` reports it.
202
+ - **Lost bytes are not restored.** Converters often replace invalid sequences with `?`, `・` or
203
+ `U+FFFD` while garbling a name; such names can only be reported as unrecoverable.
204
+ - **Very short kanji-only strings are ambiguous.** A garbled single kanji often reads back as
205
+ several different kanji with similar scores; MojiFix usually leaves it alone rather than guess.
206
+ Occasionally a short garbled input is "repaired" to a different wrong string (346 of 6,808
207
+ garbled short words in the samples; 0 for normal text). Repair rates by length are in
208
+ [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md).
209
+ - **EUC-JP mojibake made only of half-width katakana is not repaired by `fix()`** (see "How it
210
+ decides", 4). This costs 13 of the 132 CP932 → EUC-JP samples; the reading is still in
211
+ `candidates()`.
212
+ - **Mojibake of other languages is out of scope** and can be turned into a different wrong
213
+ string: Greek UTF-8 read as CP437 (`Ελλ...`) or Latin-1 becomes half-width katakana, because MojiFix
214
+ only scores how Japanese a string looks.
215
+ - **About 1 in 9 short ZIP names stays unrepaired** (on the ZIP name samples: 25,066 of 28,238
216
+ restored), mostly a single kanji plus an extension, such as `暖.txt`, whose CP437 reading
217
+ `Æg.txt` could also be a European name. They are listed as they are, and never wrongly renamed.
218
+ - CP932 has a few characters with two byte codes (NEC/IBM duplicates) and maps `〜` to `~`;
219
+ text that went through those cannot always be restored character for character.
220
+ - Python's `cp1252` cannot decode 0x81, 0x8D, 0x8F, 0x90, 0x9D. Text garbled by a decoder that maps
221
+ them to control characters is only repaired when the Latin-1 row fits.
222
+ - File names stored as raw bytes on a Linux file system (shown with surrogate escapes) are not
223
+ handled: that is byte-encoding detection, which is out of scope.
224
+
225
+ ## Development
226
+
227
+ ```sh
228
+ python -m pip install -e . pytest ruff mypy build
229
+ python -m ruff check . && python -m mypy && python -m pytest
230
+ python scripts/calibrate.py # re-check the calibration (add --search to refit)
231
+ ```
232
+
233
+ ## License
234
+
235
+ MIT — see [LICENSE](https://github.com/takosasi-dev/mojifix/blob/main/LICENSE).
@@ -0,0 +1,122 @@
1
+ # MojiFix(日本語)
2
+
3
+ [English README](README.md)
4
+
5
+ **日本語の文字化けを、厳密に元へ戻す。戻せない物は「戻せない」と返す。**
6
+ `縺薙s縺ォ縺。縺ッ` → `こんにちは`、`‚±‚ñ‚É‚¿‚Í` → `こんにちは`。ディスクの上の化けたファイル名・フォルダ名と、ZIP の中の化けた名前も扱います。
7
+
8
+ ## 何をするか
9
+
10
+ - 正しいバイトを違う文字コードで読んでしまった文字列(UTF-8 を CP932 で読んだ・CP932 を CP1252 で読んだ・ZIP の名前を CP437 で読んだ など)を、逆の操作で戻します。読み違いが2回重なった物も試します。
11
+ - 戻すのは「誤った文字コードで符号化し直すと、正しい文字コードで**厳密に**(`errors="strict"`)読めるバイトに戻る」時だけです。1バイトでも欠けていれば推測で埋めず、元のまま返し、`explain()` が「戻せない」と言います。
12
+ - 候補は、辞書もモデルも使わない小さな「日本語らしさ」の点数で並べ、**最上位が元より明らかに高く、次点よりも明らかに高い時だけ**置き換えます。普通の日本語は置き換えません(普通の文字列の見本で誤修復は 0 件。[docs/calibration.md](docs/calibration.md))。
13
+ - フォルダの中の化けた名前の一覧を出し、`--apply` を付けた時だけ名前を変えます(上書きしない・JSON Lines のログ・`--undo` で戻せる)。
14
+ - ZIP の中の名前を直した一覧を出し、`--extract` を付けた時だけ直した名前で展開します(展開先の外に書かない・上書きしない)。
15
+ - 標準ライブラリだけ。ネットワークを使いません。Python 3.10 以上。型つき。
16
+
17
+ ## しないこと
18
+
19
+ - バイト列の文字コードの判定(charset-normalizer や chardet の役割)
20
+ - ファイルの中身(本文)の書き換え
21
+ - 失われたバイトの推測(U+FFFD「�」を含む文字列は直しません)
22
+ - 一部だけ化けた文字列の部分的な修復(文字列全体で判定します。`looks_garbled()` は化けていると答えます)
23
+ - 中国語・韓国語の文字化け、辞書・機械学習の利用
24
+
25
+ ## 入れ方
26
+
27
+ ```sh
28
+ pip install mojifix
29
+ ```
30
+
31
+ ## 使い方
32
+
33
+ ```python
34
+ import mojifix
35
+
36
+ mojifix.fix("縺薙s縺ォ縺。縺ッ") # 'こんにちは'
37
+ mojifix.fix("‚±‚ñ‚É‚¿‚Í") # 'こんにちは'
38
+ mojifix.fix("今日はいい天気ですね。") # そのまま
39
+ mojifix.explain("縺薙s縺ォ縺。縺ッ") # 'UTF-8 を CP932 として読んだ文字化け'
40
+ mojifix.candidates("πüôπéôπü½πüíπü»") # 候補を点数の高い順に(Candidate(text, path, score))
41
+ mojifix.looks_garbled("縺薙s縺ォ縺。" + chr(0xFFFD)) # True(バイトが失われていて戻せない化け)
42
+ ```
43
+
44
+ | 関数 | 返す物 |
45
+ |---|---|
46
+ | `fix(text, threshold=None)` | 直した文字列。明らかに良い候補が無ければ `text` そのもの |
47
+ | `candidates(text, max_depth=2)` | 厳密に戻せた候補(`Candidate(text, path, score)`)を点数の高い順に |
48
+ | `explain(text, lang="ja")` | `fix()` が行う修復の道を短い文で(`"ja"`・`"en"`)。置き換えない時は `None`、U+FFFD を含む時は「戻せない」 |
49
+ | `looks_garbled(text)` | 文字化けらしければ `True`(戻せない物・一部だけ化けた物も) |
50
+
51
+ ### コマンド
52
+
53
+ ```sh
54
+ mojifix text "縺薙s縺ォ縺。縺ッ" [--all] [--json]
55
+ mojifix names <フォルダ> [--recursive] [--apply] [--log <ファイル>] [--json]
56
+ mojifix names --undo <ログ>
57
+ mojifix zip <ファイル> [--extract <フォルダ>] [--json]
58
+ ```
59
+
60
+ `python -m mojifix` でも動きます。`--apply`・`--extract` が無ければ何も書きません。出力の文字コード(cp932 のコンソールなど)で表せない字は、落ちずにエスケープして出します。
61
+
62
+ | 終了コード | 意味 |
63
+ |---|---|
64
+ | 0 | 成功(直す物が無かった場合を含む) |
65
+ | 1 | 使い方の誤り |
66
+ | 2 | 一部を飛ばした(名前の衝突・Windows で使えない名前・開いているファイル・危ない ZIP のメンバー)、または展開先に同じ名前があって展開を止めた |
67
+ | 3 | 入力が読めない(フォルダが無い・ZIP が壊れている・パスワードつきの ZIP を `--extract`) |
68
+
69
+ ## 直せる形
70
+
71
+ 「こんにちは」が、それぞれの読み違いでどう見えるか(MojiFix が試す順):
72
+
73
+ | 誤って読んだ文字コード | 本来の文字コード | 「こんにちは」の見た目 |
74
+ |---|---|---|
75
+ | CP932 | UTF-8 | `縺薙s縺ォ縺。縺ッ` |
76
+ | Latin-1 | UTF-8 | `ã` と制御文字の並び |
77
+ | CP1252 | UTF-8 | 「こんにちは」では起きない(0x81 を読めない)。「ん」だけなら `ã‚“` |
78
+ | Latin-1 | CP932 | `\x82±\x82ñ\x82É…`(制御文字を含む) |
79
+ | CP1252 | CP932 | `‚±‚ñ‚É‚¿‚Í` |
80
+ | CP437 | CP932 | `é▒é±é╔é┐é═`(ZIP のメンバー名) |
81
+ | CP437 | UTF-8 | `πüôπéôπü½πüíπü»`(ZIP のメンバー名) |
82
+ | CP932 | EUC-JP | `、ウ、`(私用領域の文字)`ヒ、チ、マ` |
83
+ | Latin-1 | EUC-JP | `¤³¤ó¤Ë¤Á¤Ï` |
84
+ | ASCII | ISO-2022-JP | エスケープ(ESC `$B` …)が文字として残った物 |
85
+
86
+ この中の2つが重なった物(二重の文字化け)も試します。
87
+
88
+ ## 判定のしかた
89
+
90
+ 1. 修復は `text.encode(誤, "strict").decode(本来, "strict")` だけ。
91
+ 2. 各文字を CP932 の区分(ひらがな・カタカナ・第一水準・第二水準・半角カナ・ASCII・記号・ギリシャ/キリル・IBM 拡張漢字・CP932 に無い欧文の字・その他の字・その他の記号・制御文字/私用領域/U+FFFD)に分け、重みの平均を点数にします。化けによく出る第二水準の漢字(縺 繧 繝 …)の重みは 0 です。
92
+ 3. 最上位の候補が元より `THRESHOLD` 以上、次点より `AMBIG_MARGIN` 以上高く、候補そのものが文字化けらしく見えない時だけ置き換えます。
93
+ 4. 半角カナと ASCII だけの文字列(`オオサカ・タナカ.txt`)は、`fix()` では EUC-JP として読み直しません。`・`・`、` が EUC-JP のかなの1バイト目でもあり、普通の名前が化けた漢字に変わるためです。その読みは `candidates()` には出ます。
94
+
95
+ 重みと閾値は `tests/corpus/` の公開の見本から [`scripts/calibrate.py`](scripts/calibrate.py) で決めました。数字と、その数字が何を示し何を示さないかは [docs/calibration.md](docs/calibration.md) にあります。4,096 文字を超える文字列は先頭の 4,096 文字で点数をつけ、修復は全体で厳密に通った時だけ行います。
96
+
97
+ ## ファイル名と ZIP
98
+
99
+ - `mojifix names` は `looks_garbled()` が真の名前について、直した名前か戻せない理由を出します。`--apply` は深い方から名前を変え、既にある名前・Windows で使えない名前(`\ / : * ? " < > |`・`CON`・`NUL` など)・開いているファイルは1件ずつ飛ばします。`--log` に `{"old": ..., "new": ...}`(絶対パス)を1行ずつ残し、`--undo` で後ろから戻します(元の名前が既にあればその1件を飛ばします)。ログは名前を変える前に新しく作ります。既にある名前は使い方の誤り(終了コード 1)、作れなければ終了コード 3 で、どちらも何も変えません。
100
+ - `mojifix zip` は、`ZipInfo.orig_filename` を CP437 で符号化し直して**名前の生のバイト**を取ります。このため Windows・Linux・macOS で同じ結果になります。ZIP の名前は `fix()` と別の決め方をします。CP437 で読まれたことが確かなので、生のバイトを UTF-8・CP932 で厳密に読んだ物と CP437 の読みそのものを、ZIP 用の重み(`café.txt`・`Größe.doc` のような本当の CP437 の名前は変えないように較正)で比べ、`予`・`表.xlsx`・`十月.txt` のような短い名前も戻します。2バイト目が `0x5C`(`\`)の CP932 の字(予・表・ソ など)も、字の2バイト目の `0x5C` はその字の一部、それ以外の `0x5C` は区切り、と見分けます。直せなかった名前でもこの見分け方は同じで、字の中の `\` が残った名前は Windows で作れないので展開では飛ばします。UTF-8 の印(または Info-ZIP の Unicode Path)があるメンバーは対象外です。文字コードを前もって知る必要がある `metadata_encoding`(Python 3.11 以上)は使いません。
101
+ - `--extract` は `..`・絶対パス・ドライブ名・UNC・NUL・Windows で使えない名前を含むメンバーを1件ずつ飛ばし(終了コード 2)、展開先に同じ名前がある・2つのメンバーが同じ名前になる・パスワードつき、の時は**何も書かずに**止めます。書く途中で失敗したメンバー(CRC の不一致・対応していない圧縮など)は書きかけを消して次へ進み(終了コード 2)、`--json` には全メンバーの結果(書けた・飛ばした・失敗と理由)を出します。
102
+ - macOS 型の ZIP(UTF-8・印なし)から直した名前は、入っていたとおり **NFD**(「テ」+ 結合文字の濁点)で返します。NFC にはそろえません。
103
+
104
+ ## ftfy との違い
105
+
106
+ 英語圏の定番 ftfy は、CJK の古い文字コードは扱わないと書いています。MojiFix の公開の見本(1回読み違えた 2,222 件)では、ftfy 6.3.1 が戻せたのは 672 件(30%)で、Latin-1/CP1252/CP437 → UTF-8 の道だけでした。MojiFix は 2,099 件(94%)を戻し、誤修復は 0 件でした。
107
+
108
+ ## 限界
109
+
110
+ - 一部だけ化けた文字列は直しません(`looks_garbled()` は真を返します)。
111
+ - 失われたバイトは戻しません。名前が化ける時、変換で `?`・`・`・U+FFFD に置き換わっていることが多く、その名前は「戻せない」としか言えません。
112
+ - 漢字だけのとても短い文字列は、候補が拮抗しやすく、たいていは推測せずにそのままにします。ただ、短い化けた文字列が別の誤った文字列に置き換わることがまれにあります(見本の化けた短い語 6,808 件中 346 件。普通の文字列では 0 件)。長さ別の修復率は docs/calibration.md にあります。
113
+ - 半角カナだけになる EUC-JP の化けは `fix()` では直しません(上の「判定のしかた」4)。見本の CP932→EUC-JP 132 件のうち 13 件がこれに当たります。
114
+ - 日本語以外の文字化けは範囲の外で、別の誤った文字列になることがあります(CP437 や Latin-1 で読んだギリシャ語の UTF-8 `╬ò╬╗╬╗...` が半角カナになる、など)。
115
+ - 短い ZIP の名前の約 9 件に 1 件は直りません(ZIP の名前の見本で 28,238 件中 25,066 件が戻った)。多くは漢字1字+拡張子で、`暖.txt` の CP437 の読み `Æg.txt` が欧文の名前でもありうる物です。そのまま一覧に出し、誤った名前にはしません。
116
+ - CP932 には符号が2つある字(NEC・IBM の重複)があり、`〜` を `~` に対応づけます。これらを通った文字列は1字ずつ同じには戻らないことがあります。
117
+ - Python の `cp1252` は 0x81・0x8D・0x8F・0x90・0x9D を読めません。これらを制御文字として読む変換で化けた物は、Latin-1 の道で戻せる時だけ直します。
118
+ - Linux のファイルシステムに生のバイトのまま置かれた名前(サロゲートで表される物)は扱いません(バイト列の文字コードの判定になるため、範囲の外)。
119
+
120
+ ## ライセンス
121
+
122
+ MIT([LICENSE](LICENSE))
@@ -0,0 +1,213 @@
1
+ # MojiFix
2
+
3
+ [![CI](https://github.com/takosasi-dev/mojifix/actions/workflows/ci.yml/badge.svg)](https://github.com/takosasi-dev/mojifix/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/mojifix.svg)](https://pypi.org/project/mojifix/)
5
+ [![Python](https://img.shields.io/pypi/pyversions/mojifix.svg)](https://pypi.org/project/mojifix/)
6
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/takosasi-dev/mojifix/blob/main/LICENSE)
7
+
8
+ **Repair Japanese mojibake (文字化け) strictly — or say that it cannot be repaired.**
9
+ `縺薙s縺ォ縺。縺ッ` → `こんにちは`, `‚±‚ñ‚É‚¿‚Í` → `こんにちは`, garbled file names on disk, and garbled member names in ZIP files.
10
+
11
+ [日本語の README はこちら (README.ja.md)](https://github.com/takosasi-dev/mojifix/blob/main/README.ja.md)
12
+
13
+ ## What it does
14
+
15
+ - Undoes a **wrong-codec read** of Japanese text: the correct bytes were decoded with the wrong
16
+ character encoding (UTF-8 read as CP932, CP932 read as CP1252, a ZIP name read as CP437, …),
17
+ including **two such reads in a row**.
18
+ - Repairs only when re-encoding with the wrong codec gives back bytes that decode **exactly**
19
+ (`errors="strict"` both ways) with the right codec. If a single byte is missing, it does not
20
+ guess: the text is returned unchanged and `explain()` says it cannot be restored.
21
+ - Ranks the candidates with a small, calibrated "how Japanese is this?" score (no dictionary,
22
+ no model) and replaces the text **only when the best candidate is clearly better** than the
23
+ input and clearly better than the runner-up. Ordinary Japanese is left alone (0 false repairs
24
+ on every normal-text sample set, see [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md)).
25
+ - Lists garbled **file and folder names** in a folder and, only with `--apply`, renames them
26
+ (never overwriting, with a JSON Lines log and `--undo`).
27
+ - Lists the repaired **member names of a ZIP file** and, only with `--extract`, extracts with
28
+ the repaired names (never outside the destination, never overwriting).
29
+ - Standard library only. No network. Python 3.10+. Typed (`py.typed`).
30
+
31
+ ## What it does not do
32
+
33
+ - It does **not detect the encoding of raw bytes** (use charset-normalizer or chardet for that).
34
+ - It does **not rewrite file contents** — only strings, file names and ZIP member names.
35
+ - It does **not guess lost bytes**: text containing `U+FFFD` (�) is never "repaired".
36
+ - It does **not repair part of a string**: if only some of the text is garbled, the whole
37
+ string is left as is (`looks_garbled()` still reports it).
38
+ - It does **not handle Chinese or Korean** mojibake, and uses no dictionary or ML model.
39
+
40
+ ## Install
41
+
42
+ ```sh
43
+ pip install mojifix
44
+ ```
45
+
46
+ ## Quick start
47
+
48
+ ```python
49
+ import mojifix
50
+
51
+ mojifix.fix("縺薙s縺ォ縺。縺ッ") # 'こんにちは'
52
+ mojifix.fix("‚±‚ñ‚É‚¿‚Í") # 'こんにちは'
53
+ mojifix.fix("今日はいい天気ですね。") # unchanged: '今日はいい天気ですね。'
54
+
55
+ mojifix.explain("縺薙s縺ォ縺。縺ッ") # 'UTF-8 を CP932 として読んだ文字化け'
56
+ mojifix.explain("縺薙s縺ォ縺。縺ッ", lang="en") # 'Mojibake: UTF-8 bytes read as CP932'
57
+
58
+ for c in mojifix.candidates("πüôπéôπü½πüíπü»"): # a UTF-8 ZIP name read as CP437
59
+ print(c.text, c.path, round(c.score, 3))
60
+ # こんにちは (('cp437', 'utf-8'),) 1.0
61
+ # 縺薙s縺ォ縺。縺ッ (('cp437', 'cp932'),) 0.289
62
+
63
+ broken = "縺薙s縺ォ縺。" + chr(0xFFFD) + ".txt" # one byte was lost
64
+ mojifix.fix(broken) == broken # True: nothing is guessed
65
+ mojifix.explain(broken, "en") # 'Bytes were lost (contains U+FFFD); it cannot be restored'
66
+ mojifix.looks_garbled(broken) # True
67
+ ```
68
+
69
+ API (`from mojifix import ...`):
70
+
71
+ | Function | Returns |
72
+ |---|---|
73
+ | `fix(text, threshold=None)` | the repaired string, or `text` itself if nothing is clearly better |
74
+ | `candidates(text, max_depth=2)` | every strict repair as `Candidate(text, path, score)`, best first |
75
+ | `explain(text, lang="ja")` | a short sentence for the repair `fix()` makes (`"ja"` or `"en"`), or `None` |
76
+ | `looks_garbled(text)` | `True` if the text looks like mojibake (also for unrepairable or partly garbled text) |
77
+
78
+ `path` lists the steps in the order they are undone: `(("latin-1", "utf-8"), ("cp932", "utf-8"))`
79
+ means "read back as Latin-1 → UTF-8, then CP932 → UTF-8".
80
+
81
+ ### Command line
82
+
83
+ ```sh
84
+ mojifix text "縺薙s縺ォ縺。縺ッ" [--all] [--json]
85
+ mojifix names <folder> [--recursive] [--apply] [--log <file>] [--json]
86
+ mojifix names --undo <log>
87
+ mojifix zip <file.zip> [--extract <folder>] [--json]
88
+ ```
89
+
90
+ `python -m mojifix ...` works too. Nothing is written without `--apply` / `--extract`.
91
+ Output never crashes on a console that cannot show a character (it is written as an escape).
92
+
93
+ | Exit code | Meaning |
94
+ |---|---|
95
+ | 0 | success (including "nothing to repair") |
96
+ | 1 | usage error |
97
+ | 2 | some items were skipped (name already exists, not allowed on Windows, file in use, unsafe ZIP member, …), or extraction stopped because a name already exists |
98
+ | 3 | the input cannot be read (no such folder, broken ZIP, password-protected ZIP with `--extract`) |
99
+
100
+ ## Shapes it repairs
101
+
102
+ What `こんにちは` looks like after each wrong read (the rows MojiFix tries, in this order):
103
+
104
+ | Read as (wrong) | Actually (right) | `こんにちは` looks like |
105
+ |---|---|---|
106
+ | CP932 | UTF-8 | `縺薙s縺ォ縺。縺ッ` |
107
+ | Latin-1 | UTF-8 | `ã` followed by control characters (`ã\x81\x93ã\x82\x93…`) |
108
+ | CP1252 | UTF-8 | not possible for `こんにちは` (byte 0x81); `ん` alone looks like `ã‚“` |
109
+ | Latin-1 | CP932 | `\x82±\x82ñ\x82É…` (with control characters) |
110
+ | CP1252 | CP932 | `‚±‚ñ‚É‚¿‚Í` |
111
+ | CP437 | CP932 | `é▒é±é╔é┐é═` (ZIP member names) |
112
+ | CP437 | UTF-8 | `πüôπéôπü½πüíπü»` (ZIP member names) |
113
+ | CP932 | EUC-JP | `、ウ、`(private-use character)`ヒ、チ、マ` |
114
+ | Latin-1 | EUC-JP | `¤³¤ó¤Ë¤Á¤Ï` |
115
+ | ASCII | ISO-2022-JP | the escape sequences left as text (`ESC $B$3$s$K$A$O ESC (B`) |
116
+
117
+ Any two of these in a row (double mojibake) are tried as well.
118
+
119
+ ## How it decides
120
+
121
+ 1. **Strict repair only.** Each row is `text.encode(wrong, "strict").decode(right, "strict")`.
122
+ 2. **Score.** Every character is put in a class based on CP932 (hiragana, katakana, JIS level-1
123
+ kanji, JIS level-2 kanji, half-width katakana, ASCII, symbols, Greek/Cyrillic in CP932, IBM
124
+ extension kanji, other European letters, other letters, other symbols, controls / private use /
125
+ U+FFFD). The score is a weighted average. Level-2 kanji such as `縺 繧 繝` are typical of
126
+ mojibake and weigh 0.
127
+ 3. **Replace only when clearly better.** The best candidate must beat the input by `THRESHOLD`
128
+ and the runner-up by `AMBIG_MARGIN`, and must not itself look like mojibake. Otherwise `fix()`
129
+ returns the input unchanged.
130
+ 4. **Half-width katakana is never read as EUC-JP by `fix()`.** A string of only half-width
131
+ katakana and ASCII (`オオサカ・タナカ.txt`) is left alone even when it decodes as EUC-JP, because
132
+ `・` and `、` are also EUC-JP lead bytes. `candidates()` still lists that reading.
133
+
134
+ The weights and both margins were fitted on the public samples in `tests/corpus/` with
135
+ [`scripts/calibrate.py`](https://github.com/takosasi-dev/mojifix/blob/main/scripts/calibrate.py); the numbers (and what they do and do not prove)
136
+ are in [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md). Strings longer than 4,096 characters are
137
+ scored on their first 4,096 characters; the repair itself must still succeed on the whole string.
138
+
139
+ ## File names and ZIP files
140
+
141
+ - `mojifix names` lists names for which `looks_garbled()` is true, with the repaired name or the
142
+ reason it cannot be repaired. `--apply` renames deepest entries first and skips, one by one,
143
+ names that already exist, names Windows does not allow (`\ / : * ? " < > |`, `CON`, `NUL`, …)
144
+ and files in use. `--log` records `{"old": ..., "new": ...}` (absolute paths) per line; the log
145
+ file must not exist yet and is created before anything is renamed (an existing name is a usage
146
+ error, exit code 1; a log that cannot be created stops with exit code 3). `--undo` renames back
147
+ in reverse order and skips entries whose original name exists again.
148
+ - `mojifix zip` reads each member's **raw name bytes** (it re-encodes `ZipInfo.orig_filename`
149
+ with CP437, the codec `zipfile` used to decode them), so the result is the same on Windows,
150
+ Linux and macOS. ZIP names are decided differently from `fix()`: since the bytes are known to
151
+ have been read as CP437, the strict UTF-8 and CP932 readings are compared with the CP437 reading
152
+ itself (with ZIP-specific weights calibrated so that real CP437 names like `café.txt` or
153
+ `Größe.doc` stay as they are), and short names such as `予`, `表.xlsx` or `十月.txt` are restored. This also handles the CP932 characters whose second byte is `0x5C` (`\`), such
154
+ as 予 表 ソ: a `0x5C` that is the second byte of a character stays part of it, any other `0x5C`
155
+ is a path separator. This holds for names that cannot be repaired too: such a name keeps the
156
+ `\` and is skipped on `--extract`, since Windows does not allow it. Members with the UTF-8 flag
157
+ (or an Info-ZIP Unicode Path field) are not touched. MojiFix does not use `metadata_encoding` (Python 3.11+), which needs the encoding to
158
+ be known in advance.
159
+ - `--extract` refuses members with `..`, absolute paths, drive letters, UNC paths, NUL or
160
+ names Windows does not allow (skipped one by one, exit code 2), and stops **before writing
161
+ anything** if a name already exists in the destination, if two members would get the same
162
+ name, or if the ZIP is password-protected. A member that fails while being written (bad CRC,
163
+ unsupported compression, …) is removed again and reported; the others are still extracted
164
+ (exit code 2). `--json` lists every member as extracted, skipped or failed, with the reason.
165
+ - Names repaired from macOS-style ZIPs (UTF-8 without the flag) come back in **NFD**
166
+ (decomposed, e.g. `テ` + combining `゙`), exactly as stored. MojiFix does not normalize them to NFC.
167
+
168
+ ## Compared with ftfy
169
+
170
+ [ftfy](https://github.com/rspeer/python-ftfy) is the standard tool for mojibake in Western text and
171
+ documents that it does not handle legacy CJK encodings. On MojiFix's public samples (2,222 garbled
172
+ strings, one wrong read each), ftfy 6.3.1 restored 672 (30%): it handles the Latin-1/CP1252/CP437 →
173
+ UTF-8 rows and none of the CP932, EUC-JP or ISO-2022-JP rows. MojiFix restored 2,099 (94%) with no
174
+ false repair. Use ftfy for Western text; use MojiFix for Japanese.
175
+
176
+ ## Limitations
177
+
178
+ - **Partly garbled text is not repaired** (a garbled part joined to a normal part does not decode
179
+ strictly as a whole). `looks_garbled()` reports it.
180
+ - **Lost bytes are not restored.** Converters often replace invalid sequences with `?`, `・` or
181
+ `U+FFFD` while garbling a name; such names can only be reported as unrecoverable.
182
+ - **Very short kanji-only strings are ambiguous.** A garbled single kanji often reads back as
183
+ several different kanji with similar scores; MojiFix usually leaves it alone rather than guess.
184
+ Occasionally a short garbled input is "repaired" to a different wrong string (346 of 6,808
185
+ garbled short words in the samples; 0 for normal text). Repair rates by length are in
186
+ [docs/calibration.md](https://github.com/takosasi-dev/mojifix/blob/main/docs/calibration.md).
187
+ - **EUC-JP mojibake made only of half-width katakana is not repaired by `fix()`** (see "How it
188
+ decides", 4). This costs 13 of the 132 CP932 → EUC-JP samples; the reading is still in
189
+ `candidates()`.
190
+ - **Mojibake of other languages is out of scope** and can be turned into a different wrong
191
+ string: Greek UTF-8 read as CP437 (`Ελλ...`) or Latin-1 becomes half-width katakana, because MojiFix
192
+ only scores how Japanese a string looks.
193
+ - **About 1 in 9 short ZIP names stays unrepaired** (on the ZIP name samples: 25,066 of 28,238
194
+ restored), mostly a single kanji plus an extension, such as `暖.txt`, whose CP437 reading
195
+ `Æg.txt` could also be a European name. They are listed as they are, and never wrongly renamed.
196
+ - CP932 has a few characters with two byte codes (NEC/IBM duplicates) and maps `〜` to `~`;
197
+ text that went through those cannot always be restored character for character.
198
+ - Python's `cp1252` cannot decode 0x81, 0x8D, 0x8F, 0x90, 0x9D. Text garbled by a decoder that maps
199
+ them to control characters is only repaired when the Latin-1 row fits.
200
+ - File names stored as raw bytes on a Linux file system (shown with surrogate escapes) are not
201
+ handled: that is byte-encoding detection, which is out of scope.
202
+
203
+ ## Development
204
+
205
+ ```sh
206
+ python -m pip install -e . pytest ruff mypy build
207
+ python -m ruff check . && python -m mypy && python -m pytest
208
+ python scripts/calibrate.py # re-check the calibration (add --search to refit)
209
+ ```
210
+
211
+ ## License
212
+
213
+ MIT — see [LICENSE](https://github.com/takosasi-dev/mojifix/blob/main/LICENSE).