Skip to content

Text tfidfvectorizer loadasync

github-actions[bot] edited this page Aug 22, 2026 · 23 revisions

TfidfVectorizer.LoadAsync

Read a fitted vectorizer back without blocking the caller.

public static Task<TfidfVectorizer> LoadAsync(Stream source, ArtifactLoadOptions options = null, CancellationToken cancellationToken = default)

Parameterssource is a readable stream, left open. options bounds what will be accepted. cancellationToken cancels the read.

ReturnsTask<TfidfVectorizer>, completing with a fitted vectorizer.

ExceptionsArgumentNullException for a null source. InvalidDataException for content that is not a saved vectorizer or that exceeds a bound. OperationCanceledException when cancelled.

Example — restoring, then weighting with the frequencies that were saved.

using Lodestar.Text.Vectorization;

static async Task<int> RestoreAsync()
{
    var original = new TfidfVectorizer();
    original.Fit(["the cat eats", "the dog eats"]);

    using var buffer = new MemoryStream();
    await original.SaveAsync(buffer);
    buffer.Position = 0;

    TfidfVectorizer restored = await TfidfVectorizer.LoadAsync(buffer);
    return restored.Transform(["the cat"]).ColumnCount;
}

int columns = RestoreAsync().GetAwaiter().GetResult();  // => 4

The GetAwaiter().GetResult() is only what lets a synchronous example drive an async one; in async code the call is simply await.

Remarks — the bounds in options are checked as the content is read rather than afterwards, so an oversized file is refused before it is allocated.

Applies to — net10.0, netstandard2.0.

See alsoTfidfVectorizer.Load, TfidfVectorizer.SaveAsync, ArtifactLoadOptions.

Lodestar

Project

Clone this wiki locally