Create a gist now

Instantly share code, notes, and snippets.

@tondrej /FastText.cs
Last active Apr 18, 2018

Embed
What would you like to do?
FastText wrapper class
public delegate bool WordCallback(string word, Single score);
class FastText : IDisposable
{
private IntPtr ft = IntPtr.Zero;
private WordCallback c = null;
private void FastTextCheck(bool result)
{
if (!result)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
#region IDisposable Support
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
if (ft != IntPtr.Zero)
{
FastTextWrapper.fasttext_release(ft);
ft = IntPtr.Zero;
}
}
public FastText() {
FastTextCheck(FastTextWrapper.fasttext_new(out ft));
}
~FastText() {
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
public bool NN2Callback(StringBuilder word, Single score, IntPtr data)
{
return c(word.ToString(), score);
}
public void NN2(IntPtr vectors, string[] positive, string[] negative, int count, WordCallback callback)
{
c = callback;
try
{
FastTextCheck(FastTextWrapper.fasttext_nn2(ft, vectors, positive, negative, count, new FastTextWrapper.FastTextNNCallback(NN2Callback), IntPtr.Zero));
}
finally
{
c = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment