KeywordSpotting.ets
2.0 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
import {
createKeywordSpotter,
createKeywordStream,
isKeywordStreamReady,
decodeKeywordStream,
resetKeywordStream,
getKeywordResultAsJson,
} from 'libsherpa_onnx.so';
import { FeatureConfig } from './NonStreamingAsr';
import { OnlineModelConfig, OnlineStream } from './StreamingAsr';
export class KeywordSpotterConfig {
public featConfig: FeatureConfig = new FeatureConfig();
public modelConfig: OnlineModelConfig = new OnlineModelConfig();
public maxActivePaths: number = 4;
public numTrailingBlanks: number = 1;
public keywordsScore: number = 1;
public keywordsThreshold: number = 0.25;
public keywordsFile: string = '';
}
interface KeywordSpotterResultJson {
keyword: string;
timestamps: number[];
tokens: string[];
}
export class KeywordSpotterResult {
public keyword: string = '';
public tokens: string[] = [];
public timestamps: number[] = [];
public json: string = '';
}
export class KeywordSpotter {
public handle: object;
public config: KeywordSpotterConfig;
constructor(config: KeywordSpotterConfig, mgr?: object) {
this.handle = createKeywordSpotter(config, mgr);
this.config = config
}
createStream(keywords?: string): OnlineStream {
if (typeof keywords !== "undefined") {
return new OnlineStream(createKeywordStream(this.handle, keywords));
} else {
return new OnlineStream(createKeywordStream(this.handle));
}
}
isReady(stream: OnlineStream): boolean {
return isKeywordStreamReady(this.handle, stream.handle);
}
decode(stream: OnlineStream) {
decodeKeywordStream(this.handle, stream.handle);
}
reset(stream: OnlineStream) {
resetKeywordStream(this.handle, stream.handle);
}
getResult(stream: OnlineStream): KeywordSpotterResult {
const jsonStr: string = getKeywordResultAsJson(this.handle, stream.handle);
let o = JSON.parse(jsonStr) as KeywordSpotterResultJson;
const r = new KeywordSpotterResult()
r.keyword = o.keyword
r.timestamps = o.timestamps;
r.tokens = o.tokens;
r.json = jsonStr;
return r;
}
}