load_documents#

scikitplot.corpus.load_documents(path, format=None, *, trusted=False, expected_sha256=None)[source]#

Load CorpusDocument instances from a previously exported file.

Parameters:
pathpathlib.Path or str

Path to the exported file.

formatExportFormat, str or None, optional

The export format, or its string value ("jsonl"). When None, it is inferred from the suffix: .json, .jsonl/.ndjson, .yaml/.yml, .toml, .pkl/.pickle, .joblib.

trustedbool, default=False

Required for pickle and joblib, whose loading can execute code. JSON, JSON Lines, YAML and TOML never need it.

expected_sha256str or None, optional

When provided, the file’s SHA-256 is verified before it is parsed or deserialized; a mismatch raises ValueError.

Returns:
list of CorpusDocument

In file order. Embeddings come back as NumPy arrays of the recorded embedding_dtype (float64 when a record names none).

Raises:
ValueError

If the format cannot be inferred or cannot be loaded back (CSV, pandas, Parquet, polars, NumPy, HuggingFace; for MLflow, load the downloaded corpus_documents.jsonl); if loading pickle or joblib is not trusted; if expected_sha256 does not match; if the file is not valid in its format (the message names the file); or if a record breaks the record contract: an unknown key, a field of the wrong type, a YAML or TOML date where a string belongs, an embedding value that overflows its dtype (the message names the record).

IncompatibleSchemaError

A subclass of ValueError, if the file was written by another major generation of the document schema: a text record or a pickle or joblib mapping with another schema_version, or a pickle or joblib bare list (written before schema 3.0). Rebuild and export again.

ImportError

If the library a format needs is not installed.

OSError

If the file cannot be read.

TypeError

If a trusted pickle or joblib file does not hold a list of CorpusDocument.

Parameters:
Return type:

list[CorpusDocument]

See also

export_documents

The writer.

Notes

User note. Text formats are the safe way to keep a corpus: loading runs no code, the files are diffable, and every field the exporter wrote comes back. Tabular and directory formats cannot rebuild every field (metadata is flattened, embeddings are dropped), so loading them is refused instead of returning a partial or empty result.

Examples

>>> import tempfile
>>> from pathlib import Path
>>> from scikitplot.corpus import CorpusDocument, ExportFormat, export_documents
>>> doc = CorpusDocument.create("notes.txt", 0, "Hello.")
>>> out = export_documents(
...     [doc], Path(tempfile.mkdtemp()) / "corpus.jsonl", ExportFormat.JSONL
... )
>>> [d.text for d in load_documents(out)]
['Hello.']

Loading a pickle/joblib export requires an explicit trust decision, because deserialization can execute arbitrary code. Pin a known digest when you have one:

>>> docs = load_documents(
...     Path("corpus.pkl"),
...     trusted=True,
...     expected_sha256="e3b0c44298fc1c149afbf4c8996fb924...",
... )