partition() re-classifies a .txt file as CSV or JSON from its content

Aperta Adatta ai principianti
#4,876 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 6 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend, data

Direzione di ricerca

Leggi unstructured/file_utils/filetype.py, in particolare _TextFileDifferentiator.file_type, e segui come partition() usa detect_filetype() per i file .txt. Aggiungi o esamina i test per contenuti delimitati da virgole e contenuti JSON nei file .txt, quindi conferma che i file di testo con nome continuino a essere gestiti come testo semplice, mentre i file senza estensione continuino a essere rilevati in base al contenuto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

partition() and detect_filetype() re-classify a file named *.txt from its content. A plain text file whose lines all contain the same number of commas is detected as CSV, so the commas are dropped and the lines are fused into a single Table.

notes.txt:  "Dear Ann,\nThank you for the gift,\nSee you soon,\n"
names.txt:  "Smith, John\nDoe, Jane\nRoe, Richard\n"
from unstructured.partition.auto import partition
partition("names.txt")
# [('Table', 'Smith John Doe Jane Roe Richard')]   metadata.filetype == "text/csv"
partition("notes.txt")
# [('Table', 'Dear Ann Thank you for the gift See you soon')]

partition_text("names.txt") gives three Title elements, so only the type detection is affected. A .txt file that contains a JSON object is likewise routed to the JSON partitioner and its text is rewritten.

Cause

In _TextFileDifferentiator.file_type (unstructured/file_utils/filetype.py), .csv, .json, .md, .eml, ... are trusted by extension first, but the .txt/.text check comes after _is_json, _is_csv and _is_eml.

Expected

A file the user named .txt is plain text. The content sniffs stay for files without an extension.

Proposed fix

Return FileType.TXT for .txt/.text before the content sniffs. I have a small PR with tests ready; happy to adjust if you want to keep the sniffing for some case I missed.

Version: main at d9c80bdb, Python 3.12, Windows 11.

Lingua principale
HTML
Stelle
15.8k
Fork
1.4k
Merge medio
8g 4h
PR unite (30g)
40

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Unstructured-IO/unstructured

Tutte le issue di Unstructured-IO/unstructured

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.