Skip to content

Commit 7a30269

Browse files
committed
refactor: move longCommonInfPrefix to utils
1 parent b48f0e2 commit 7a30269

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

src/controllers/SpeechToTextController.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,10 @@ import {
88
SECOND,
99
} from '../constants/sttDefaults';
1010
import { _SpeechToTextModule } from '../native/RnExecutorchModules';
11+
import { TokenDecoder } from '../tokenizers/tokenDecoder';
1112
import { ResourceSource } from '../types/common';
1213
import { fetchResource } from '../utils/fetchResource';
13-
import { TokenDecoder } from '../tokenizers/tokenDecoder';
14-
15-
const longCommonInfPref = (seq1: number[], seq2: number[]) => {
16-
let maxInd = 0;
17-
let maxLength = 0;
18-
19-
for (let i = 0; i < seq1.length; i++) {
20-
let j = 0;
21-
let hammingDist = 0;
22-
while (
23-
j < seq2.length &&
24-
i + j < seq1.length &&
25-
(seq1[i + j] === seq2[j] || hammingDist < HAMMING_DIST_THRESHOLD)
26-
) {
27-
if (seq1[i + j] !== seq2[j]) {
28-
hammingDist++;
29-
}
30-
j++;
31-
}
32-
if (j >= maxLength) {
33-
maxLength = j;
34-
maxInd = i;
35-
}
36-
}
37-
return maxInd;
38-
};
14+
import { longCommonInfPref } from '../utils/tokenizerUtils';
3915

4016
export class SpeechToTextController {
4117
private nativeModule: _SpeechToTextModule;
@@ -139,7 +115,6 @@ export class SpeechToTextController {
139115
this.onErrorCallback?.(
140116
new Error(`Error when loading the SpeechToTextController! ${e}`)
141117
);
142-
console.error('Error when loading the SpeechToTextController!', e);
143118
}
144119
}
145120

@@ -223,7 +198,6 @@ export class SpeechToTextController {
223198
let output;
224199
try {
225200
output = await this.nativeModule.decode(seq, [encoderOutput]);
226-
console.log(output[0]);
227201
} catch (error) {
228202
this.onErrorCallback?.(`Decode ${error}`);
229203
return '';
@@ -264,7 +238,11 @@ export class SpeechToTextController {
264238
continue;
265239
}
266240

267-
const maxInd = longCommonInfPref(seqs.at(-2)!, seqs.at(-1)!);
241+
const maxInd = longCommonInfPref(
242+
seqs.at(-2)!,
243+
seqs.at(-1)!,
244+
HAMMING_DIST_THRESHOLD
245+
);
268246
finalSeq = [...this.sequence, ...seqs.at(-2)!.slice(0, maxInd)];
269247
this.sequence = finalSeq;
270248
this.decodedTranscribeCallback(finalSeq);

src/utils/tokenizerUtils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ export function unicodeToBytes() {
4040

4141
return unicodeToByteMap;
4242
}
43+
44+
export const longCommonInfPref = (
45+
seq1: number[],
46+
seq2: number[],
47+
hammingDistThreshold: number
48+
) => {
49+
let maxInd = 0;
50+
let maxLength = 0;
51+
52+
for (let i = 0; i < seq1.length; i++) {
53+
let j = 0;
54+
let hammingDist = 0;
55+
while (
56+
j < seq2.length &&
57+
i + j < seq1.length &&
58+
(seq1[i + j] === seq2[j] || hammingDist < hammingDistThreshold)
59+
) {
60+
if (seq1[i + j] !== seq2[j]) {
61+
hammingDist++;
62+
}
63+
j++;
64+
}
65+
if (j >= maxLength) {
66+
maxLength = j;
67+
maxInd = i;
68+
}
69+
}
70+
return maxInd;
71+
};

0 commit comments

Comments
 (0)