feat: Add support for CSV input with new backend to transform CSV files to DoclingDocument (#945)
* feat: Implement csv backend and format detection Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com> * test: Implement csv parsing and format tests Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com> * docs: Add example and CSV format documentation Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com> * feat: Add support for various CSV dialects and update documentation Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com> * feat: Add validation for delimiters and tests for inconsistent csv files Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com> --------- Signed-off-by: Tobias Strebitzer <tobias.strebitzer@magloft.com>
This commit is contained in:
committed by
GitHub
parent
7493d5b01f
commit
00d9405b0a
@@ -39,6 +39,7 @@ class InputFormat(str, Enum):
|
||||
PDF = "pdf"
|
||||
ASCIIDOC = "asciidoc"
|
||||
MD = "md"
|
||||
CSV = "csv"
|
||||
XLSX = "xlsx"
|
||||
XML_USPTO = "xml_uspto"
|
||||
JSON_DOCLING = "json_docling"
|
||||
@@ -61,6 +62,7 @@ FormatToExtensions: Dict[InputFormat, List[str]] = {
|
||||
InputFormat.XML_PUBMED: ["xml", "nxml"],
|
||||
InputFormat.IMAGE: ["jpg", "jpeg", "png", "tif", "tiff", "bmp"],
|
||||
InputFormat.ASCIIDOC: ["adoc", "asciidoc", "asc"],
|
||||
InputFormat.CSV: ["csv"],
|
||||
InputFormat.XLSX: ["xlsx"],
|
||||
InputFormat.XML_USPTO: ["xml", "txt"],
|
||||
InputFormat.JSON_DOCLING: ["json"],
|
||||
@@ -88,6 +90,7 @@ FormatToMimeType: Dict[InputFormat, List[str]] = {
|
||||
InputFormat.PDF: ["application/pdf"],
|
||||
InputFormat.ASCIIDOC: ["text/asciidoc"],
|
||||
InputFormat.MD: ["text/markdown", "text/x-markdown"],
|
||||
InputFormat.CSV: ["text/csv"],
|
||||
InputFormat.XLSX: [
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import csv
|
||||
import logging
|
||||
import re
|
||||
from enum import Enum
|
||||
@@ -296,6 +297,7 @@ class _DocumentConversionInput(BaseModel):
|
||||
mime = _DocumentConversionInput._mime_from_extension(ext)
|
||||
|
||||
mime = mime or _DocumentConversionInput._detect_html_xhtml(content)
|
||||
mime = mime or _DocumentConversionInput._detect_csv(content)
|
||||
mime = mime or "text/plain"
|
||||
formats = MimeTypeToFormat.get(mime, [])
|
||||
if formats:
|
||||
@@ -352,6 +354,8 @@ class _DocumentConversionInput(BaseModel):
|
||||
mime = FormatToMimeType[InputFormat.HTML][0]
|
||||
elif ext in FormatToExtensions[InputFormat.MD]:
|
||||
mime = FormatToMimeType[InputFormat.MD][0]
|
||||
elif ext in FormatToExtensions[InputFormat.CSV]:
|
||||
mime = FormatToMimeType[InputFormat.CSV][0]
|
||||
elif ext in FormatToExtensions[InputFormat.JSON_DOCLING]:
|
||||
mime = FormatToMimeType[InputFormat.JSON_DOCLING][0]
|
||||
elif ext in FormatToExtensions[InputFormat.PDF]:
|
||||
@@ -392,3 +396,32 @@ class _DocumentConversionInput(BaseModel):
|
||||
return "application/xml"
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _detect_csv(
|
||||
content: bytes,
|
||||
) -> Optional[Literal["text/csv"]]:
|
||||
"""Guess the mime type of a CSV file from its content.
|
||||
|
||||
Args:
|
||||
content: A short piece of a document from its beginning.
|
||||
|
||||
Returns:
|
||||
The mime type of a CSV file, or None if the content does
|
||||
not match any of the format.
|
||||
"""
|
||||
content_str = content.decode("ascii", errors="ignore").strip()
|
||||
|
||||
# Ensure there's at least one newline (CSV is usually multi-line)
|
||||
if "\n" not in content_str:
|
||||
return None
|
||||
|
||||
# Use csv.Sniffer to detect CSV characteristics
|
||||
try:
|
||||
dialect = csv.Sniffer().sniff(content_str)
|
||||
if dialect.delimiter in {",", ";", "\t", "|"}: # Common delimiters
|
||||
return "text/csv"
|
||||
except csv.Error:
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user