UNPKG

microsoft-cognitiveservices-speech-sdk

Version:
1 lines 4.02 kB
{"version":3,"sources":["src/common.speech/ServiceMessages/TranslationHypothesis.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAIhE,MAAM,WAAW,sBAAsB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,CAAC,EAAE,gBAAgB,CAAC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,aAAa,CAAC;CAC9B;AAED,qBAAa,qBAAsB,YAAW,sBAAsB;IAChE,OAAO,CAAC,yBAAyB,CAAyB;IAE1D,OAAO;WAMO,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,qBAAqB;WAIjE,uBAAuB,CAAC,qBAAqB,EAAE;QAAE,gBAAgB,EAAE,sBAAsB,CAAA;KAAE,EAAE,UAAU,EAAE,MAAM,GAAG,qBAAqB;IAQrJ,IAAW,QAAQ,IAAI,MAAM,CAE5B;IAED,IAAW,MAAM,IAAI,MAAM,CAE1B;IAED,IAAW,IAAI,IAAI,MAAM,CAExB;IAED,IAAW,WAAW,IAAI,aAAa,CAEtC;IAED,IAAW,QAAQ,IAAI,MAAM,CAE5B;IAEM,MAAM,IAAI,MAAM;IAUvB,OAAO,CAAC,oBAAoB;CAO/B","file":"TranslationHypothesis.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\nimport { Contracts } from \"../../sdk/Contracts.js\";\r\nimport { IPrimaryLanguage, ITranslations } from \"../Exports.js\";\r\nimport { TranslationStatus } from \"../TranslationStatus.js\";\r\n\r\n// translation.hypothesis\r\nexport interface ITranslationHypothesis {\r\n Duration: number;\r\n Offset: number;\r\n PrimaryLanguage?: IPrimaryLanguage;\r\n Text: string;\r\n Translation: ITranslations;\r\n}\r\n\r\nexport class TranslationHypothesis implements ITranslationHypothesis {\r\n private privTranslationHypothesis: ITranslationHypothesis;\r\n\r\n private constructor(hypothesis: ITranslationHypothesis, baseOffset: number) {\r\n this.privTranslationHypothesis = hypothesis;\r\n this.privTranslationHypothesis.Offset += baseOffset;\r\n this.privTranslationHypothesis.Translation.TranslationStatus = this.mapTranslationStatus(this.privTranslationHypothesis.Translation.TranslationStatus);\r\n }\r\n\r\n public static fromJSON(json: string, baseOffset: number): TranslationHypothesis {\r\n return new TranslationHypothesis(JSON.parse(json) as ITranslationHypothesis, baseOffset);\r\n }\r\n\r\n public static fromTranslationResponse(translationHypothesis: { SpeechHypothesis: ITranslationHypothesis }, baseOffset: number): TranslationHypothesis {\r\n Contracts.throwIfNullOrUndefined(translationHypothesis, \"translationHypothesis\");\r\n const hypothesis: ITranslationHypothesis = translationHypothesis.SpeechHypothesis;\r\n translationHypothesis.SpeechHypothesis = undefined;\r\n hypothesis.Translation = (translationHypothesis as unknown as ITranslations);\r\n return new TranslationHypothesis(hypothesis, baseOffset);\r\n }\r\n\r\n public get Duration(): number {\r\n return this.privTranslationHypothesis.Duration;\r\n }\r\n\r\n public get Offset(): number {\r\n return this.privTranslationHypothesis.Offset;\r\n }\r\n\r\n public get Text(): string {\r\n return this.privTranslationHypothesis.Text;\r\n }\r\n\r\n public get Translation(): ITranslations {\r\n return this.privTranslationHypothesis.Translation;\r\n }\r\n\r\n public get Language(): string {\r\n return this.privTranslationHypothesis.PrimaryLanguage?.Language;\r\n }\r\n\r\n public asJson(): string {\r\n const jsonObj = { ...this.privTranslationHypothesis };\r\n // Convert the enum value to its string representation for serialization purposes.\r\n\r\n return jsonObj.Translation !== undefined ? JSON.stringify({\r\n ...jsonObj,\r\n TranslationStatus: TranslationStatus[jsonObj.Translation.TranslationStatus] as keyof typeof TranslationStatus\r\n }) : JSON.stringify(jsonObj);\r\n }\r\n\r\n private mapTranslationStatus(status: any): TranslationStatus {\r\n if (typeof status === \"string\") {\r\n return TranslationStatus[status as keyof typeof TranslationStatus];\r\n } else if (typeof status === \"number\") {\r\n return status;\r\n }\r\n }\r\n}\r\n"]}