FastText library Pascal usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
I: Int64; | |
S: string; | |
PositiveWords, NegativeWords: TStringList; | |
Positive, Negative: array of PAnsiChar; | |
begin | |
PositiveWords := nil; | |
NegativeWords := nil; | |
Win32Check(fasttext_new(ft)); | |
try | |
PositiveWords := TStringList.Create; | |
PositiveWords.Delimiter := ' '; | |
NegativeWords := TStringList.Create; | |
NegativeWords.Delimiter := ' '; | |
Write(Format('Loading file ''%s''...', [ModelFileName])); | |
Win32Check(fasttext_loadmodel(ft, PAnsiChar(ModelFileName))); | |
Writeln('done.'); | |
Write('Computing vectors...'); | |
Win32Check(fasttext_precomputevectors(ft, mx)); | |
try | |
Writeln('done.'); | |
repeat | |
WriteLn('positive words:'); | |
ReadLn(S); | |
PositiveWords.DelimitedText := S; | |
if PositiveWords.Count = 0 then | |
Break; | |
WriteLn('negative words:'); | |
ReadLn(S); | |
if S = '' then | |
Break; | |
NegativeWords.DelimitedText := S; | |
if NegativeWords.Count = 0 then | |
Break; | |
SetLength(Positive, PositiveWords.Count + 1); | |
for I := 0 to PositiveWords.Count - 1 do | |
Positive[I] := PAnsiChar(PositiveWords[I]); | |
Positive[PositiveWords.Count] := nil; | |
SetLength(Negative, NegativeWords.Count + 1); | |
for I := 0 to NegativeWords.Count - 1 do | |
Negative[I] := PAnsiChar(NegativeWords[I]); | |
Negative[NegativeWords.Count] := nil; | |
Win32Check(fasttext_nn2(ft, mx, @Positive[0], @Negative[0], 25, @nn_callback, nil)); | |
Writeln; | |
until False; | |
finally | |
Win32Check(matrix_release(mx)); | |
end; | |
finally | |
Win32Check(fasttext_release(ft)); | |
NegativeWords.Free; | |
PositiveWords.Free; | |
end; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment