Guest User

Untitled

a guest
Apr 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. function nn_callback(word: PAnsiChar; score: Single; data: Pointer): LongBool; cdecl;
  2. begin
  3. Result := False;
  4. Writeln(Format('''%s'': %f', [word, score]));
  5. end;
  6.  
  7. procedure TFastTextConsole.NN2(const ModelFileName: string);
  8. var
  9. ft: TFastText;
  10. mx: TMatrix;
  11. I: Int64;
  12. S: string;
  13. PositiveWords, NegativeWords: TStringList;
  14. Positive, Negative: array of PAnsiChar;
  15. begin
  16. PositiveWords := nil;
  17. NegativeWords := nil;
  18. Win32Check(fasttext_new(ft));
  19. try
  20. PositiveWords := TStringList.Create;
  21. PositiveWords.Delimiter := ' ';
  22. NegativeWords := TStringList.Create;
  23. NegativeWords.Delimiter := ' ';
  24.  
  25. Write(Format('Loading file ''%s''...', [ModelFileName]));
  26. Win32Check(fasttext_loadmodel(ft, PAnsiChar(ModelFileName)));
  27. Writeln('done.');
  28. Write('Computing vectors...');
  29. Win32Check(fasttext_precomputevectors(ft, mx));
  30. try
  31. Writeln('done.');
  32. repeat
  33. WriteLn('positive words:');
  34. ReadLn(S);
  35. PositiveWords.DelimitedText := S;
  36. if PositiveWords.Count = 0 then
  37. Break;
  38.  
  39. WriteLn('negative words:');
  40. ReadLn(S);
  41. if S = '' then
  42. Break;
  43. NegativeWords.DelimitedText := S;
  44. if NegativeWords.Count = 0 then
  45. Break;
  46.  
  47. SetLength(Positive, PositiveWords.Count + 1);
  48. for I := 0 to PositiveWords.Count - 1 do
  49. Positive[I] := PAnsiChar(PositiveWords[I]);
  50. Positive[PositiveWords.Count] := nil;
  51.  
  52. SetLength(Negative, NegativeWords.Count + 1);
  53. for I := 0 to NegativeWords.Count - 1 do
  54. Negative[I] := PAnsiChar(NegativeWords[I]);
  55. Negative[NegativeWords.Count] := nil;
  56.  
  57. Win32Check(fasttext_nn2(ft, mx, @Positive[0], @Negative[0], 25, @nn_callback, nil));
  58. Writeln;
  59. until False;
  60. finally
  61. Win32Check(matrix_release(mx));
  62. end;
  63. finally
  64. Win32Check(fasttext_release(ft));
  65. NegativeWords.Free;
  66. PositiveWords.Free;
  67. end;
  68. end;
Add Comment
Please, Sign In to add comment