Skip to content

Instantly share code, notes, and snippets.

View tondrej's full-sized avatar

Ondrej Kelle tondrej

View GitHub Profile
@tondrej
tondrej / mint_extras.sh
Last active June 8, 2019 13:18
Linux Mint extras
#!/bin/bash
# Extras for Linux Mint 19
# VirtualBox
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
echo "deb [arch=amd64] http://download.virtualbox.org/virtualbox/debian bionic contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
# git
sudo add-apt-repository ppa:git-core/ppa
@tondrej
tondrej / analogies.sql
Created June 23, 2018 09:55
SQL cosine similarity, "king - man + woman = queen"
-- king - man + woman
with q (
id,
w
) as (
select w.id, +1.0 from fasttext.word as w where (w.word = 'king')
union
select w.id, -1.0 from fasttext.word as w where (w.word = 'man')
union
@tondrej
tondrej / ft.js
Last active April 16, 2018 08:46
FastText library JavaScript usage
model = "C:\\Data\\fasttext\\wiki\\enwik9.bin";
console.log("Loading file \"%s\"...", model);
fasttext.loadModel(model);
console.log("done.");
console.log("Computing vectors...");
vectors = fasttext.precomputeVectors();
try {
console.log("done.");
while (true) {
positive = prompt('positive words:');
@tondrej
tondrej / FastTextObj.pas
Created April 15, 2018 16:26
FastText Pascal wrapper class for ChakraCore host
type
TFastTextObject = class(TNativeObject)
private
FCallback: JsValueRef;
FHandle: TFastText;
procedure NN2Callback(const Word: string; Score: Single);
protected
class procedure RegisterMethods(AInstance: JsHandle); override;
public
@tondrej
tondrej / ftc.lpr
Created April 15, 2018 16:22
FastText library Pascal usage
function nn_callback(word: PAnsiChar; score: Single; data: Pointer): LongBool; cdecl;
begin
Result := False;
Writeln(Format('''%s'': %f', [word, score]));
end;
procedure TFastTextConsole.NN2(const ModelFileName: string);
var
ft: TFastText;
mx: TMatrix;
@tondrej
tondrej / Program.cs
Last active April 16, 2018 07:53
FastText library C# usage
class Program
{
static bool NearestNeighborsCallback(string word, Single score)
{
Console.WriteLine($"\"{word}\": {score}");
return false;
}
static void NearestNeighbors(string modelFileName)
{
@tondrej
tondrej / FastText.cs
Last active April 18, 2018 11:27
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)
@tondrej
tondrej / FastTextWrapper.cs
Last active April 16, 2018 08:38
FastText C-style bindings for .NET
public static class FastTextWrapper
{
[return: MarshalAs(UnmanagedType.Bool)]
[UnmanagedFunctionPointer(callingConvention: CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate bool FastTextNNCallback(StringBuilder word, Single score, IntPtr data);
[DllImport(dllName: "fasttext.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool fasttext_new(out IntPtr ft);
@tondrej
tondrej / fasttext.pas
Created April 15, 2018 15:12
FastText C-style API bindings for Delphi and Free Pascal
const
FastTextLib = 'fasttext.dll';
type
TFastTextNNCallback = function(word: PAnsiChar; score: Single; data: Pointer): LongBool; cdecl;
function fasttext_new(out ft: TFastText): LongBool; cdecl; external FastTextLib;
function fasttext_release(ft: TFastText): LongBool; cdecl; external FastTextLib;
function fasttext_nn2(ft: TFastText; mx: TMatrix; positive, negative: PPAnsiChar; count: Integer;
@tondrej
tondrej / fasttext_api.cpp
Last active April 16, 2018 08:44
FastText flattened C-style API examples (DLL)
FASTTEXT_API BOOL FASTTEXT_CC fasttext_new(FastText** ft)
{
return ExceptionBoundary([&]() -> BOOL
{
*ft = new FastText();
return true;
});
}
FASTTEXT_API BOOL FASTTEXT_CC fasttext_release(FastText* ft)