export_documents#
- scikitplot.corpus.export_documents(documents, output_path, format, *, include_embedding=True, json_indent=2, parquet_compression='snappy')[source]#
Export a list of documents to
output_pathin the given format.- Parameters:
- documentslist of CorpusDocument
Documents to export. May be empty (produces an empty file/dataset).
- output_pathpathlib.Path
Destination file or directory path.
File formats (CSV, JSON, JSONL, Pickle, Joblib, NumPy, pandas, Parquet, Polars): path to the output file.
Directory formats (HuggingFace, MLflow): path to the root directory / artifact path.
- formatExportFormat or str
Target export format, or its string value (
"jsonl").- include_embeddingbool, optional
When
True(default), embedding vectors are included in the output where the format supports them (JSONL, JSON, Pickle, Joblib, NumPy). Embeddings are always included for NumPy. For CSV and Parquet (tabular), embeddings are excluded regardless of this flag to avoid storing variable-length arrays in cells.- json_indentint or None, optional
Indentation for JSON output.
Noneproduces compact JSON. Default:2.- parquet_compressionstr, optional
Compression codec for Parquet output (
"snappy","gzip","brotli","zstd","none"). Default:"snappy".
- Returns:
- pathlib.Path
The path that was written to (same as
output_path).
- Raises:
- ValueError
If
formatisExportFormat.NUMPYand no documents have embeddings, or if the embedding dimensions are inconsistent; for a text format, if a value cannot be represented (an infinity in JSON,Nonein a TOML array or in metadata for TOML). The message names the document.- TypeError
For a text format, if a metadata value is not serialisable (a
set, adatetime, a custom object); the message names the document.- ImportError
If the required optional library for the format is not installed.
- OSError
If the output directory cannot be created or the file cannot be written.
- Parameters:
documents (list[CorpusDocument])
output_path (Path)
format (ExportFormat)
include_embedding (bool)
json_indent (int | None)
parquet_compression (str)
- Return type:
See also
scikitplot.corpus._schema.ExportFormatEnumeration of all formats.
Notes
Atomic writes: All file-based formats are written to a
.tmpsibling first, then renamed atomically. Interrupted exports leave no partial files at the final path.Embedding in tabular formats: CSV and Parquet omit embeddings because storing a float32 vector per row in a tabular cell is impractical. Use JSON, JSON Lines, YAML or TOML (safe to load back) or PICKLE/JOBLIB to preserve embeddings.
Text formats: metadata must hold JSON-like values (
str, numbers,bool,None, lists, string-keyed mappings; NumPy scalars are converted). Tuples come back as lists. JSON has noNaN: aNaNin metadata is written asnulland reads back asNone(YAML and TOML keepNaN; embeddings restore it in every format).Examples
>>> import tempfile >>> from pathlib import Path >>> import numpy as np >>> from scikitplot.corpus import CorpusDocument, ExportFormat >>> docs = [ ... CorpusDocument.create( ... "notes.txt", i, f"Paragraph {i}.", embedding=np.full(3, i, np.float32) ... ) ... for i in range(2) ... ] >>> out = Path(tempfile.mkdtemp())
CSV (zero dependencies; no embeddings):
>>> export_documents(docs, out / "corpus.csv", ExportFormat.CSV).name 'corpus.csv'
JSON Lines with embeddings, loadable back exactly:
>>> path = export_documents(docs, out / "corpus.jsonl", ExportFormat.JSONL) >>> load_documents(path)[1].embedding array([1., 1., 1.], dtype=float32)
NumPy embedding matrix:
>>> export_documents(docs, out / "embeddings.npy", ExportFormat.NUMPY).name 'embeddings.npy'
Gallery examples#
Write Your Own Corpus Components: a Hamlet Walkthrough