Windows-1252 text is decoded with the wrong charset ("Café crème" becomes "Cafﻠ crﻟme")
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
Start with _get_stream_info_guesses and the plain text and CSV fallbacks, where charset_normalizer.from_bytes(data).best() picks the charset. Pull request #2679 is already open against this issue, so review it before writing any code. Done means cp1252 wins on a chaos and coherence tie unless the data contains the bytes 0x8A, 0x8E, 0x9A or 0x9E, with the Bosnian sample without š/ž decoding correctly and the test cases in the issue passing.
索引モデルが issue の本文から書いたものです。
説明
Bug
Text files in Windows-1252 (or Latin-1) with French, Spanish, Portuguese or Nordic text come out garbled. There is no error, so the wrong text goes straight into the Markdown. The same happens to .csv files and to text files inside a .zip.
import io
from markitdown import MarkItDown
md = MarkItDown()
for text in [
"Café crème",
"Le café crème coûte 3 € à la boulangerie près de l'église.",
"Señor Muñoz, año próximo",
"Blåbærgrød og rødgrød med fløde på ø-hoppet i Århus.",
]:
data = text.encode("cp1252")
print(md.convert_stream(io.BytesIO(data), file_extension=".txt").markdown)
Output on main (4cc9fa1), charset_normalizer 3.5.2:
Cafﻠ crﻟme
Le café crčme coűte 3 € ŕ la boulangerie prčs de l'église.
Seńor Muńoz, ańo próximo
Blĺbćrgrřd og rřdgrřd med flřde pĺ ř-hoppet i Ĺrhus.
German text is not affected, because its letters are the same in cp1250 and cp1252.
Cause
_get_stream_info_guesses (and the plain text and CSV fallbacks) use charset_normalizer.from_bytes(data).best(). For these samples, cp1252 is in the match list with exactly the same chaos and coherence as the first match, and best() returns the first one, which is cp1250 (or cp1006 for "Café crème"). For example, for the French sentence:
cp1250 chaos=0.000 coherence=0.524 decodes correctly: no
cp1252 chaos=0.000 coherence=0.524 decodes correctly: yes
The charset_normalizer maintainer has said such close calls can't be fixed in the detector, and advised callers to choose among the top matches themselves (jawah/charset_normalizer#524, jawah/charset_normalizer#571).
Proposed fix
When cp1252 ties with the best match in both chaos and coherence, use cp1252, unless the data contains S or Z with caron (bytes 0x8A 0x8E 0x9A 0x9E). Those bytes are the same in cp1250 and cp1252, are common in Croatian and Slovene, and are rare in Western European text.
I compared the charset picked with and without this rule on 26 samples in 20 languages:
| Result | Samples |
|---|---|
| Fixed | French (3), Spanish (2), Portuguese, Danish, Icelandic |
| Correct before and after | German, Polish, Czech, Hungarian, Croatian, Slovene, Turkish, Greek, Russian |
| Wrong before and after | Italian, Dutch, Romanian, Finnish, Lithuanian, Croatian without š/ž, short Turkish and Russian samples |
| Correct before, wrong after | Bosnian sentence without š/ž ("Ćevapi u Sarajevu su čuveni, a đaci ih vole.") |
The last row is the trade-off: Croatian, Slovene and Bosnian text without š or ž is byte-for-byte as ambiguous as the French sample, and today it only decodes correctly because cp1250 sorts first. I think preferring cp1252 on a tie is the better default, but I'm happy to narrow or change the rule if you see it differently.
Update (after review in #2679): the trade-off is wider than the table shows. Czech and Hungarian text without š or ž is affected the same way: "Česká republika" now decodes as "Èeská republika". The PR now also prefers cp1250 on a tie when the bytes point to it. That happens when cp1252 would read ą, ł, Ľ, Ł, Ą or Ż as a symbol right before a letter, or ő as õ outside the Portuguese "õe". So "egyenlő", "Pociąg był pełny" and "Ľudia" decode correctly again. What remains ambiguous is cp1250 text whose only non-ASCII letters are also Western letters (č/è, Č/È, ř/ø, ű/û), or ą only at the end of a word. Some short samples like that move from correct to wrong.
Related
- #1956, #2015, #2280, #2337 and #2346 handle a guessed charset that fails to decode. Here the decode succeeds with the wrong charset.
- #2674 handles short UTF-8 text guessed as UTF-16. It doesn't cover single-byte encodings.
I have a fix with tests and will open a PR.
- 主要言語
- Python
- スター
- 198k
- フォーク
- 15k
- 平均マージ
- 2時間 16分
- マージ済み PR(30日)
- 9
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/markitdown のほかの issue
-
IpynbConverter uses a comment inside a Markdown code fence as the notebook title対応中かも @Primaverah が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
microsoft/markitdown#2964 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
PPTX image alt text ending in a backslash breaks the Markdown image対応中かも @dongyikuan919 が 3 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
microsoft/markitdown#2943 ·
メンテナーはふだん 1 日以内に返信
-
PPTX chart labels with pipes or line breaks produce malformed Markdown tables対応中かも @dongyikuan919 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
microsoft/markitdown#2937 ·
メンテナーはふだん 1 日以内に返信
-
DOCX: math pre-processing re-parses every document.xml even without equations (32 s -> 17.5 s on a 12.5 MB document)対応中かも @houyunsun が 26 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 89/100
microsoft/markitdown#2834 ·
メンテナーはふだん 1 日以内に返信
-
URL fetch sends default python-requests User-Agent, Wikipedia returns 403対応中かも @sushan5140 が 31 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/markitdown#2820 ·
メンテナーはふだん 1 日以内に返信
microsoft/markitdown の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
hugohe3/ppt-master#366 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 89/100
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
521xueweihan/HelloGitHub#4716 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 84/100
pcqpcq/open-source-android-apps#619 ·
メンテナーはふだん 1 日以内に返信
-
contribution Windows 主机
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
Lee-xeo/Claude-Cage#1519 ·
メンテナーはふだん 1 日以内に返信