KeenASR Framework v2.2 (c662ac9)
Keen Research
Loading...
Searching...
No Matches
KIOSTextAligner.h
1//
2// KIOSTextAligner.h
3// KeenASR
4//
5// Copyright © 2026 Keen Research LLC. All rights reserved.
6//
7
8#import <Foundation/Foundation.h>
9
10@class KIOSRecognizer;
11
12NS_ASSUME_NONNULL_BEGIN
13
21typedef NS_ENUM(NSInteger, KIOSAlignOp) {
23 KIOSAlignOpMatch = 0,
25 KIOSAlignOpSubstitution = 1,
27 KIOSAlignOpInsertion = 2,
29 KIOSAlignOpDeletion = 3,
30};
31
32#pragma mark - KIOSAlignmentConfig
33
42@interface KIOSAlignmentConfig : NSObject
43
49@property (nonatomic, assign) float insertCost;
50
56@property (nonatomic, assign) float deleteCost;
57
64@property (nonatomic, assign) float substituteCost;
65
73@property (nonatomic, assign) BOOL detectRepetitions;
74
82@property (nonatomic, assign) BOOL filterNoiseTokens;
83
84@end
85
86#pragma mark - KIOSAlignmentItem
87
99@interface KIOSAlignmentItem : NSObject
100
102@property (nonatomic, readonly) KIOSAlignOp op;
103
108@property (nonatomic, readonly) NSInteger refIndex;
109
114@property (nonatomic, readonly) NSInteger recIndex;
115
119@property (nonatomic, readonly, copy) NSString *refToken;
120
124@property (nonatomic, readonly, copy) NSString *recToken;
125
126@end
127
128#pragma mark - KIOSAlignmentResult
129
141@interface KIOSAlignmentResult : NSObject
142
144@property (nonatomic, readonly) NSArray<KIOSAlignmentItem *> *trace;
145
146#pragma mark Edit-distance counts
147
149@property (nonatomic, readonly) NSInteger matches;
150
152@property (nonatomic, readonly) NSInteger substitutions;
153
155@property (nonatomic, readonly) NSInteger insertions;
156
158@property (nonatomic, readonly) NSInteger deletions;
159
161@property (nonatomic, readonly) NSInteger refLength;
162
164@property (nonatomic, readonly) NSInteger recLength;
165
171@property (nonatomic, readonly) float wordErrorRate;
172
173#pragma mark Oral-reading views
174
182@property (nonatomic, readonly) NSArray<NSNumber *> *matchedRefIndices;
183
192@property (nonatomic, readonly) NSArray<NSNumber *> *skippedRefIndices;
193
198@property (nonatomic, readonly) NSInteger furthestMatchedIndex;
199
207@property (nonatomic, readonly) NSArray<NSNumber *> *repetitionRefIndices;
208
209@end
210
211#pragma mark - KIOSTextAligner
212
245@interface KIOSTextAligner : NSObject
246
259- (nullable instancetype)initWithReference:(NSString *)reference
260 langCode:(NSString *)langCode
261 NS_DESIGNATED_INITIALIZER;
262
275- (nullable instancetype)initWithReference:(NSString *)reference
276 recognizer:(KIOSRecognizer *)recognizer;
277
278- (instancetype)init NS_UNAVAILABLE;
279+ (instancetype)new NS_UNAVAILABLE;
280
281#pragma mark Alignment
282
292- (KIOSAlignmentResult *)align:(NSString *)recognized;
293
303- (KIOSAlignmentResult *)align:(NSString *)recognized
304 config:(nullable KIOSAlignmentConfig *)config;
305
306#pragma mark Incremental alignment
307
325- (KIOSAlignmentResult *)incrementalAlign:(NSString *)recognized;
326
337- (KIOSAlignmentResult *)incrementalAlign:(NSString *)recognized
338 config:(nullable KIOSAlignmentConfig *)config;
339
344- (void)reset;
345
346#pragma mark Accessors
347
352@property (nonatomic, readonly) NSArray<NSString *> *referenceTokens;
353
357@property (nonatomic, readonly, copy) NSString *langCode;
358
359@end
360
361NS_ASSUME_NONNULL_END
Per-call configuration for a text-alignment operation.
Definition KIOSTextAligner.h:43
float insertCost
Cost of inserting a recognized token (one not present in the reference).
Definition KIOSTextAligner.h:49
float deleteCost
Cost of deleting a reference token (one not present in the recognized text).
Definition KIOSTextAligner.h:56
BOOL detectRepetitions
When YES, the result populates repetitionRefIndices for words the reader appears to have stuttered or...
Definition KIOSTextAligner.h:73
float substituteCost
Cost of substituting one token for another.
Definition KIOSTextAligner.h:64
BOOL filterNoiseTokens
When YES, recognized tokens whose text begins with '<' (e.g. <SPOKEN_NOISE>, <UNK>) are dropped befor...
Definition KIOSTextAligner.h:82
A single step in an alignment trace.
Definition KIOSTextAligner.h:100
KIOSAlignOp op
The edit operation this item represents.
Definition KIOSTextAligner.h:102
NSInteger recIndex
Index of the recognized token participating in this operation, or NSNotFound for a deletion.
Definition KIOSTextAligner.h:114
NSString * recToken
Recognized token at recIndex, or empty string for a deletion.
Definition KIOSTextAligner.h:124
NSInteger refIndex
Index of the reference token participating in this operation, or NSNotFound for an insertion.
Definition KIOSTextAligner.h:108
NSString * refToken
Reference token at refIndex, or empty string for an insertion.
Definition KIOSTextAligner.h:119
Result of aligning a recognized token sequence against a reference.
Definition KIOSTextAligner.h:142
NSInteger recLength
Length of the post-normalization recognized token vector.
Definition KIOSTextAligner.h:164
NSArray< KIOSAlignmentItem * > * trace
Full alignment trace, left-to-right over the input sequences.
Definition KIOSTextAligner.h:144
NSInteger furthestMatchedIndex
Highest matched reference index, or NSNotFound if no reference token matched.
Definition KIOSTextAligner.h:198
float wordErrorRate
Word error rate: (substitutions + insertions + deletions) / refLength.
Definition KIOSTextAligner.h:171
NSArray< NSNumber * > * matchedRefIndices
Sorted array of reference indices that matched their corresponding recognized token (NSNumber-wrapped...
Definition KIOSTextAligner.h:182
NSInteger insertions
Number of insertions (extra recognized tokens).
Definition KIOSTextAligner.h:155
NSInteger matches
Number of matched token pairs.
Definition KIOSTextAligner.h:149
NSInteger refLength
Length of the post-normalization reference token vector.
Definition KIOSTextAligner.h:161
NSInteger deletions
Number of deletions (skipped reference tokens).
Definition KIOSTextAligner.h:158
NSArray< NSNumber * > * repetitionRefIndices
Reference indices flagged as repetitions in the recognized text (NSNumber-wrapped NSInteger).
Definition KIOSTextAligner.h:207
NSInteger substitutions
Number of substitutions in the alignment.
Definition KIOSTextAligner.h:152
NSArray< NSNumber * > * skippedRefIndices
Sorted array of reference indices that did not match but lie at or below furthestMatchedIndex (NSNumb...
Definition KIOSTextAligner.h:192
Definition KIOSRecognizer.h:347
Aligns recognized speech against a reference text using DP edit distance.
Definition KIOSTextAligner.h:246
NSString * langCode
Language code this aligner was constructed with.
Definition KIOSTextAligner.h:357
NSArray< NSString * > * referenceTokens
Normalized reference tokens, in order. Indices in alignment results refer to positions in this array.
Definition KIOSTextAligner.h:352
void reset()
Drop cached DP state. Call between utterances when using incremental alignment.