Text tfidfvectorizer loadasync - CyrilB1531/lodestar GitHub Wiki
Development build. This page describes
main, not a released package. The latest published Lodestar.Text is 0.6.0 — read its documentation.
Home › Text › Vectorization
Read a fitted vectorizer back without blocking the caller.
public static Task<TfidfVectorizer> LoadAsync(Stream source, ArtifactLoadOptions options = null, CancellationToken cancellationToken = default)Parameters — source is a readable stream, left open. options bounds what will be accepted.
cancellationToken cancels the read.
Returns — Task<TfidfVectorizer>, completing with a fitted vectorizer.
Exceptions — ArgumentNullException for a null source. InvalidDataException for content that
is not a saved vectorizer, holds options the vectorizer refuses, or 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(); // => 4The 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 also — TfidfVectorizer.Load,
TfidfVectorizer.SaveAsync,
ArtifactLoadOptions.