-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoskModel.pas
66 lines (56 loc) · 1.52 KB
/
VoskModel.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
unit VoskModel;
interface
uses
SysUtils, Classes, vosk_api;
type
TVoskModel = class(TComponent)
private
{ Private declarations }
FModel: PVoskModel;
FModelPath: String;
protected
{ Protected declarations }
public
{ Public declarations }
//constructor Create(AOwner: TComponent); overload; override;
constructor Create(AOwner: TComponent; AModelPath: String); reintroduce;
destructor Destroy; override;
function FindWord(AWord: String): Integer;
property Model: PVoskModel read FModel;
published
{ Published declarations }
property ModelPath: String read FModelPath;
end;
procedure Register;
implementation
constructor TVoskModel.Create(AOwner: TComponent; AModelPath: String);
begin
inherited Create(AOwner);
FModelPath := AModelPath;
FModel := vosk_model_new(PAnsiChar(FModelPath));
end;
(** Check if a word can be recognized by the model
* @param word: the word
* @returns the word symbol if @param word exists inside the model
* or -1 otherwise.
* Reminding that word symbol 0 is for <epsilon>
* Will return -2 if model is not initialized yet. *)
function TVoskModel.FindWord(AWord: String): Integer;
begin
if FModel = nil then
begin
Result := -2;
Exit;
end;
Result := vosk_model_find_word(FModel, PAnsiChar(AWord));
end;
destructor TVoskModel.Destroy;
begin
vosk_model_free(FModel);
inherited Destroy;
end;
procedure Register;
begin
RegisterComponents('Vosk', [TVoskModel]);
end;
end.