Only use specific Reco types in specific ServiceReco classes. (#625)

This commit is contained in:
Glenn Harper 2023-02-08 14:02:36 -07:00 коммит произвёл GitHub
Родитель b7d396a29f
Коммит 912f1ac2ac
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 82 добавлений и 66 удалений

Просмотреть файл

@ -5,12 +5,9 @@ import {
OutputFormat,
PropertyCollection,
PropertyId,
Recognizer,
ResultReason,
SpeechRecognitionEventArgs,
SpeechRecognitionResult,
TranslationRecognitionEventArgs,
TranslationRecognitionResult,
TranslationRecognizer
SpeechRecognitionResult
} from "../sdk/Exports";
import {
DetailedSpeechPhrase,
@ -22,8 +19,7 @@ import {
RecognizerConfig,
ServiceRecognizerBase,
SimpleSpeechPhrase,
SpeechHypothesis,
TranscriberRecognizer
SpeechHypothesis
} from "./Exports";
import { SpeechConnectionMessage } from "./SpeechConnectionMessage.Internal";
@ -34,7 +30,7 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
connectionFactory: IConnectionFactory,
audioSource: IAudioSource,
recognizerConfig: RecognizerConfig,
recognizer: TranslationRecognizer | TranscriberRecognizer) {
recognizer: Recognizer) {
super(authentication, connectionFactory, audioSource, recognizerConfig, recognizer);
this.handleSpeechPhraseMessage = async (textBody: string): Promise<void> => this.handleSpeechPhrase(textBody);
this.handleSpeechHypothesisMessage = (textBody: string): void => this.handleSpeechHypothesis(textBody);
@ -45,6 +41,20 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
return;
}
protected handleRecognizedCallback(result: SpeechRecognitionResult, offset: number, sessionId: string): void {
void result;
void offset;
void sessionId;
return;
}
protected handleRecognizingCallback(result: SpeechRecognitionResult, duration: number, sessionId: string): void {
void result;
void duration;
void sessionId;
return;
}
protected async processSpeechMessages(connectionMessage: SpeechConnectionMessage): Promise<boolean> {
let processed: boolean = false;
switch (connectionMessage.path.toLowerCase()) {
@ -89,6 +99,7 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
const resultProps: PropertyCollection = new PropertyCollection();
resultProps.setProperty(PropertyId.SpeechServiceResponse_JsonResult, textBody);
const simpleOffset = simple.Offset + this.privRequestSession.currentTurnAudioOffset;
let offset = simpleOffset;
this.privRequestSession.onPhraseRecognized(this.privRequestSession.currentTurnAudioOffset + simple.Offset + simple.Duration);
@ -116,17 +127,6 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
undefined,
textBody,
resultProps);
if (this.privRecognizer instanceof TranslationRecognizer) {
try {
const ev = new TranslationRecognitionEventArgs(TranslationRecognitionResult.fromSpeechRecognitionResult(result), simpleOffset, this.privRequestSession.sessionId);
this.privRecognizer.recognized(this.privRecognizer, ev);
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
return;
}
} else {
const detailed: DetailedSpeechPhrase = DetailedSpeechPhrase.fromJSON(textBody);
const totalOffset: number = detailed.Offset + this.privRequestSession.currentTurnAudioOffset;
@ -144,32 +144,11 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
undefined,
offsetCorrectedJson,
resultProps);
offset = result.offset;
}
if (this.privRecognizer instanceof TranscriberRecognizer) {
try {
const event: SpeechRecognitionEventArgs = new SpeechRecognitionEventArgs(result, result.offset, this.privRequestSession.sessionId);
this.privRecognizer.recognized(this.privRecognizer, event);
if (!!this.privSuccessCallback) {
try {
this.privSuccessCallback(result);
} catch (e) {
if (!!this.privErrorCallback) {
this.privErrorCallback(e as string);
}
}
// Only invoke the call back once.
// and if it's successful don't invoke the
// error after that.
this.privSuccessCallback = undefined;
this.privErrorCallback = undefined;
}
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
this.handleRecognizedCallback(result, offset, this.privRequestSession.sessionId);
}
}
}
@ -195,28 +174,6 @@ export class ConversationServiceRecognizer extends ServiceRecognizerBase {
this.privRequestSession.onHypothesis(offset);
if (this.privRecognizer instanceof TranscriberRecognizer) {
if (!!this.privRecognizer.recognizing) {
try {
const ev = new SpeechRecognitionEventArgs(result, hypothesis.Duration, this.privRequestSession.sessionId);
this.privRecognizer.recognizing(this.privRecognizer, ev);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
} else {
if (this.privRecognizer instanceof TranslationRecognizer) {
try {
const ev = new TranslationRecognitionEventArgs(TranslationRecognitionResult.fromSpeechRecognitionResult(result), hypothesis.Duration, this.privRequestSession.sessionId);
this.privRecognizer.recognizing(this.privRecognizer, ev);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
}
this.handleRecognizingCallback(result, hypothesis.Duration, this.privRequestSession.sessionId);
}
}

Просмотреть файл

@ -13,6 +13,7 @@ import {
PropertyCollection,
PropertyId,
ResultReason,
SpeechRecognitionEventArgs,
SpeechRecognitionResult,
} from "../sdk/Exports";
import { ConversationInfo } from "../sdk/Transcription/Exports";
@ -57,6 +58,42 @@ export class TranscriptionServiceRecognizer extends ConversationServiceRecognize
return this.processSpeechMessages(connectionMessage);
}
protected handleRecognizedCallback(result: SpeechRecognitionResult, offset: number, sessionId: string): void {
try {
const event: SpeechRecognitionEventArgs = new SpeechRecognitionEventArgs(result, offset, sessionId);
this.privTranscriberRecognizer.recognized(this.privTranscriberRecognizer, event);
if (!!this.privSuccessCallback) {
try {
this.privSuccessCallback(result);
} catch (e) {
if (!!this.privErrorCallback) {
this.privErrorCallback(e as string);
}
}
// Only invoke the call back once.
// and if it's successful don't invoke the
// error after that.
this.privSuccessCallback = undefined;
this.privErrorCallback = undefined;
}
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
protected handleRecognizingCallback(result: SpeechRecognitionResult, duration: number, sessionId: string): void {
try {
const ev = new SpeechRecognitionEventArgs(result, duration, sessionId);
this.privTranscriberRecognizer.recognizing(this.privTranscriberRecognizer, ev);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
// Cancels recognition.
protected cancelRecognition(
sessionId: string,

Просмотреть файл

@ -13,6 +13,7 @@ import {
PropertyCollection,
PropertyId,
ResultReason,
SpeechRecognitionResult,
TranslationRecognitionCanceledEventArgs,
TranslationRecognitionEventArgs,
TranslationRecognitionResult,
@ -291,6 +292,27 @@ export class TranslationServiceRecognizer extends ConversationServiceRecognizer
}
}
protected handleRecognizingCallback(result: SpeechRecognitionResult, duration: number, sessionId: string): void {
try {
const ev = new TranslationRecognitionEventArgs(TranslationRecognitionResult.fromSpeechRecognitionResult(result), duration, sessionId);
this.privTranslationRecognizer.recognizing(this.privTranslationRecognizer, ev);
/* eslint-disable no-empty */
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
protected handleRecognizedCallback(result: SpeechRecognitionResult, offset: number, sessionId: string): void {
try {
const ev = new TranslationRecognitionEventArgs(TranslationRecognitionResult.fromSpeechRecognitionResult(result), offset, sessionId);
this.privTranslationRecognizer.recognized(this.privTranslationRecognizer, ev);
} catch (error) {
// Not going to let errors in the event handler
// trip things up.
}
}
private fireEventForResult(serviceResult: TranslationHypothesis | TranslationPhrase, properties: PropertyCollection): TranslationRecognitionEventArgs {
let translations: Translations;