Skip to content

Text 0.4.0 countvectorizer save

github-actions[bot] edited this page Aug 21, 2026 · 1 revision

Lodestar.Text 0.4.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

CountVectorizer.Save

Write a fitted vectorizer out, so the vocabulary survives the process.

public void Save(Stream destination)
public void Save(string path)

Parametersdestination is a writable stream, left open for the caller to dispose; path is a file to create or overwrite.

ExceptionsInvalidOperationException when nothing has been fitted yet. ArgumentNullException for a null stream or path. IOException from the stream or file system.

Example — round-tripping through memory.

using Lodestar.Text.Vectorization;

var cv = new CountVectorizer();
cv.Fit(["the cat eats", "the dog eats"]);

using var buffer = new MemoryStream();
cv.Save(buffer);
buffer.Position = 0;

CountVectorizer restored = CountVectorizer.Load(buffer);
int columns = restored.Transform(["the cat"]).ColumnCount;  // => 4

Remarks — what is written is the fit: the vocabulary and the options that produced it. A vectorizer restored from it counts a corpus exactly as the original would, which is the point — the alternative is refitting on training data that may no longer be around.

Saving before fitting throws rather than writing an empty vocabulary, because a file that loads into a vectorizer which drops every term is worse than no file.

The stream overload leaves destination open. That is deliberate: it lets a vectorizer be one part of a larger archive.

Applies to — net10.0, netstandard2.0.

See alsoCountVectorizer.Load, CountVectorizer.SaveAsync.

Lodestar

Project

Clone this wiki locally