Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix character decoding issues with text-like files #19

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies = [
"youtube-transcript-api",
"SpeechRecognition",
"pathvalidate",
"charset-normalizer",
]

[project.urls]
Expand Down
5 changes: 2 additions & 3 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import puremagic
import requests
from bs4 import BeautifulSoup
from charset_normalizer import from_path

# Optional Transcription support
try:
Expand Down Expand Up @@ -161,9 +162,7 @@ def convert(
elif "text/" not in content_type.lower():
return None

text_content = ""
with open(local_path, "rt", encoding="utf-8") as fh:
text_content = fh.read()
text_content = str(from_path(local_path).best())
return DocumentConverterResult(
title=None,
text_content=text_content,
Expand Down
4 changes: 4 additions & 0 deletions tests/test_files/test_mskanji.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
–¼‘O,”N—î,ZŠ
²“¡‘¾˜Y,30,“Œ‹ž
ŽO–؉pŽq,25,‘åã
îà‹´~,35,–¼ŒÃ‰®
13 changes: 13 additions & 0 deletions tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
"data:image/svg+xml,%3Csvg%20width%3D",
]

CSV_CP932_TEST_STRINGS = [
"名前,年齢,住所",
"佐藤太郎,30,東京",
"三木英子,25,大阪",
"髙橋淳,35,名古屋",
]

brc-dd marked this conversation as resolved.
Show resolved Hide resolved

@pytest.mark.skipif(
skip_remote,
Expand Down Expand Up @@ -164,6 +171,12 @@ def test_markitdown_local() -> None:
for test_string in SERP_TEST_STRINGS:
assert test_string in text_content

## Test non-UTF-8 encoding
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test_mskanji.csv"))
text_content = result.text_content.replace("\\", "")
for test_string in CSV_CP932_TEST_STRINGS:
assert test_string in text_content


@pytest.mark.skipif(
skip_exiftool,
Expand Down
Loading