forked from Blendman974/faceclaw
iOS porting: Work around missing punctuation between segments in multi-segment Siri voice input
This commit is contained in:
1 parent
319d3dc44b
commit
16764359bd
2 files changed
+51
-2
No files matched your search
@@ -8,15 +8,37 @@
|
|||||||
if ((self = [super init])) _utterances = [NSMutableArray new];
|
if ((self = [super init])) _utterances = [NSMutableArray new];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
- (NSString *)text {
|
// Only infer a sentence boundary between separate recognizer utterances.
|
||||||
|
// Keep the raw hypotheses unchanged so later revisions remain authoritative.
|
||||||
|
static NSString *sentenceJoin(NSString *previous, NSString *next) {
|
||||||
|
NSCharacterSet *openingQuotes = [NSCharacterSet characterSetWithCharactersInString:@"\"'“‘«"];
|
||||||
|
NSUInteger first = 0;
|
||||||
|
while (first < next.length && [openingQuotes characterIsMember:[next characterAtIndex:first]]) first++;
|
||||||
|
if (first == next.length || ![NSCharacterSet.uppercaseLetterCharacterSet characterIsMember:[next characterAtIndex:first]]) return previous;
|
||||||
|
NSCharacterSet *closingQuotes = [NSCharacterSet characterSetWithCharactersInString:@"\"'”’»"];
|
||||||
|
NSUInteger end = previous.length;
|
||||||
|
while (end && [closingQuotes characterIsMember:[previous characterAtIndex:end - 1]]) end--;
|
||||||
|
if (!end || [NSCharacterSet.punctuationCharacterSet characterIsMember:[previous characterAtIndex:end - 1]]) return previous;
|
||||||
|
return [NSString stringWithFormat:@"%@.%@", [previous substringToIndex:end], [previous substringFromIndex:end]];
|
||||||
|
}
|
||||||
|
- (NSString *)rawText {
|
||||||
NSMutableArray *parts = [NSMutableArray new];
|
NSMutableArray *parts = [NSMutableArray new];
|
||||||
for (NSDictionary *utterance in self.utterances) [parts addObject:utterance[@"text"]];
|
for (NSDictionary *utterance in self.utterances) [parts addObject:utterance[@"text"]];
|
||||||
return [parts componentsJoinedByString:@" "];
|
return [parts componentsJoinedByString:@" "];
|
||||||
}
|
}
|
||||||
|
- (NSString *)text {
|
||||||
|
NSMutableArray *parts = [NSMutableArray new];
|
||||||
|
for (NSUInteger i = 0; i < self.utterances.count; i++) {
|
||||||
|
NSString *part = self.utterances[i][@"text"];
|
||||||
|
if (i + 1 < self.utterances.count) part = sentenceJoin(part, self.utterances[i + 1][@"text"]);
|
||||||
|
[parts addObject:part];
|
||||||
|
}
|
||||||
|
return [parts componentsJoinedByString:@" "];
|
||||||
|
}
|
||||||
- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final {
|
- (NSString *)updateText:(NSString *)text start:(NSTimeInterval)start duration:(NSTimeInterval)duration settled:(BOOL)settled final:(BOOL)final {
|
||||||
text = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
text = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
||||||
// Apple's final callback can be empty after a pause. Keep the best result.
|
// Apple's final callback can be empty after a pause. Keep the best result.
|
||||||
if (!text.length || (final && [text isEqual:self.text])) return self.text;
|
if (!text.length || (final && ([text isEqual:self.text] || [text isEqual:self.rawText]))) return self.text;
|
||||||
BOOL timed = isfinite(start) && start >= 0 && isfinite(duration) && duration > 0;
|
BOOL timed = isfinite(start) && start >= 0 && isfinite(duration) && duration > 0;
|
||||||
NSUInteger index = self.utterances.count;
|
NSUInteger index = self.utterances.count;
|
||||||
if (timed) {
|
if (timed) {
|
||||||
|
|||||||
@@ -24,5 +24,32 @@ int main(void) { @autoreleasepool {
|
|||||||
check([t updateText:@"Ice cream is good." start:0.5 duration:2 settled:YES final:NO], @"Ice cream is good.");
|
check([t updateText:@"Ice cream is good." start:0.5 duration:2 settled:YES final:NO], @"Ice cream is good.");
|
||||||
check([t updateText:@"Next sentence" start:4 duration:1 settled:NO final:NO], @"Ice cream is good. Next sentence");
|
check([t updateText:@"Next sentence" start:4 duration:1 settled:NO final:NO], @"Ice cream is good. Next sentence");
|
||||||
check([t updateText:@"Next sentence." start:4 duration:1 settled:YES final:NO], @"Ice cream is good. Next sentence.");
|
check([t updateText:@"Next sentence." start:4 duration:1 settled:YES final:NO], @"Ice cream is good. Next sentence.");
|
||||||
|
t = [FaceclawSpeechTranscript new];
|
||||||
|
check([t updateText:@"First sentence" start:0 duration:1 settled:YES final:NO], @"First sentence");
|
||||||
|
check([t updateText:@"Second" start:0 duration:0 settled:NO final:NO], @"First sentence. Second");
|
||||||
|
check([t updateText:@"Second sentence" start:2 duration:1 settled:YES final:NO], @"First sentence. Second sentence");
|
||||||
|
check([t updateText:@"Third" start:0 duration:0 settled:NO final:NO], @"First sentence. Second sentence. Third");
|
||||||
|
check([t updateText:@"Third" start:4 duration:1 settled:YES final:NO], @"First sentence. Second sentence. Third");
|
||||||
|
// A cumulative final without punctuation must not erase inferred joins.
|
||||||
|
check([t updateText:@"First sentence Second sentence Third" start:0 duration:5 settled:YES final:YES], @"First sentence. Second sentence. Third");
|
||||||
|
// Lowercase continuation: a pause alone does not introduce a period.
|
||||||
|
t = [FaceclawSpeechTranscript new];
|
||||||
|
check([t updateText:@"We should" start:0 duration:1 settled:YES final:NO], @"We should");
|
||||||
|
check([t updateText:@"keep going" start:2 duration:1 settled:YES final:NO], @"We should keep going");
|
||||||
|
// Existing punctuation (including non-ASCII) always wins.
|
||||||
|
for (NSString *punctuation in @[@".", @"?", @"!", @",", @":", @";", @"—", @"…", @"。", @"!"]) {
|
||||||
|
t = [FaceclawSpeechTranscript new];
|
||||||
|
NSString *first = [@"First" stringByAppendingString:punctuation];
|
||||||
|
[t updateText:first start:0 duration:1 settled:YES final:NO];
|
||||||
|
check([t updateText:@"Next" start:2 duration:1 settled:YES final:NO], [first stringByAppendingString:@" Next"]);
|
||||||
|
}
|
||||||
|
t = [FaceclawSpeechTranscript new];
|
||||||
|
[t updateText:@"He said “yes”" start:0 duration:1 settled:YES final:NO];
|
||||||
|
check([t updateText:@"“Then go”" start:2 duration:1 settled:YES final:NO], @"He said “yes.” “Then go”");
|
||||||
|
// A revised lowercase start removes the inferred period; raw text survives.
|
||||||
|
t = [FaceclawSpeechTranscript new];
|
||||||
|
[t updateText:@"Keep" start:0 duration:1 settled:YES final:NO];
|
||||||
|
check([t updateText:@"Going" start:2 duration:1 settled:NO final:NO], @"Keep. Going");
|
||||||
|
check([t updateText:@"going" start:2 duration:1 settled:YES final:NO], @"Keep going");
|
||||||
puts("PASS: utterance accumulation, live revisions, repeated words, duplicate/cumulative results and empty final callbacks");
|
puts("PASS: utterance accumulation, live revisions, repeated words, duplicate/cumulative results and empty final callbacks");
|
||||||
} return 0; }
|
} return 0; }
|
||||||
Reference in new issue
Block a user