mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 13:26:32 -04:00
e46202829f
# TextLoader auto detect encoding and enhanced exception handling - Add an option to enable encoding detection on `TextLoader`. - The detection is done using `chardet` - The loading is done by trying all detected encodings by order of confidence or raise an exception otherwise. ### New Dependencies: - `chardet` Fixes #4479 ## Before submitting <!-- If you're adding a new integration, include an integration test and an example notebook showing its use! --> ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: - @eyurtsev --------- Co-authored-by: blob42 <spike@w530>
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
"""Document loader helpers."""
|
|
|
|
import concurrent.futures
|
|
from typing import List, NamedTuple, Optional, cast
|
|
|
|
|
|
class FileEncoding(NamedTuple):
|
|
encoding: Optional[str]
|
|
confidence: float
|
|
language: Optional[str]
|
|
|
|
|
|
def detect_file_encodings(file_path: str, timeout: int = 5) -> List[FileEncoding]:
|
|
"""Try to detect the file encoding.
|
|
|
|
Returns a list of `FileEncoding` tuples with the detected encodings ordered
|
|
by confidence.
|
|
"""
|
|
import chardet
|
|
|
|
def read_and_detect(file_path: str) -> List[dict]:
|
|
with open(file_path, "rb") as f:
|
|
rawdata = f.read()
|
|
return cast(List[dict], chardet.detect_all(rawdata))
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
future = executor.submit(read_and_detect, file_path)
|
|
try:
|
|
encodings = future.result(timeout=timeout)
|
|
except concurrent.futures.TimeoutError:
|
|
raise TimeoutError(
|
|
f"Timeout reached while detecting encoding for {file_path}"
|
|
)
|
|
|
|
if all(encoding["encoding"] is None for encoding in encodings):
|
|
raise RuntimeError(f"Could not detect encoding for {file_path}")
|
|
return [FileEncoding(**enc) for enc in encodings if enc["encoding"] is not None]
|