指定语音转文本的源语言Specify source language for speech to text

重要

需要语音 SDK 版本 1.11.0 或更高版本。Speech SDK version 1.11.0 or later is required.

本文介绍如何指定某个传递给语音 SDK 进行语音识别的音频输入的源语言。In this article, you'll learn how to specify the source language for an audio input passed to the Speech SDK for speech recognition. 另外还提供示例代码,用于指定自定义语音识别模型以改进识别。Additionally, example code is provided to specify a custom speech model for improved recognition.

如何在 C# 中指定源语言How to specify source language in C#

在以下示例中,源语言是使用 SpeechRecognizer 构造作为参数显式提供的。In the following example, the source language is provided explicitly as a parameter using SpeechRecognizer construct.

var recognizer = new SpeechRecognizer(speechConfig, "de-DE", audioConfig);

在以下示例中,源语言是使用 SourceLanguageConfig 提供的。In the following example, the source language is provided using SourceLanguageConfig. 然后,将 sourceLanguageConfig 作为参数传递给 SpeechRecognizer 构造。Then, the sourceLanguageConfig is passed as a parameter to SpeechRecognizer construct.

var sourceLanguageConfig = SourceLanguageConfig.FromLanguage("de-DE");
var recognizer = new SpeechRecognizer(speechConfig, sourceLanguageConfig, audioConfig);

在以下示例中,源语言和自定义终结点是使用 SourceLanguageConfig 提供的。In the following example, the source language and custom endpoint are provided using SourceLanguageConfig. 然后,将 sourceLanguageConfig 作为参数传递给 SpeechRecognizer 构造。Then, the sourceLanguageConfig is passed as a parameter to SpeechRecognizer construct.

var sourceLanguageConfig = SourceLanguageConfig.FromLanguage("de-DE", "The Endpoint ID for your custom model.");
var recognizer = new SpeechRecognizer(speechConfig, sourceLanguageConfig, audioConfig);

备注

在 C# 中,SpeechRecognitionLanguageEndpointId set 方法已从 SpeechConfig 类中弃用。SpeechRecognitionLanguage and EndpointId set methods are deprecated from the SpeechConfig class in C#. 建议不要使用这些方法,在构造 SpeechRecognizer 时不应使用它们。The use of these methods are discouraged, and shouldn't be used when constructing a SpeechRecognizer.

如何在 C++ 中指定源语言How to specify source language in C++

在以下示例中,源语言是使用 FromConfig 方法作为参数显式提供的。In the following example, the source language is provided explicitly as a parameter using the FromConfig method.

auto recognizer = SpeechRecognizer::FromConfig(speechConfig, "de-DE", audioConfig);

在以下示例中,源语言是使用 SourceLanguageConfig 提供的。In the following example, the source language is provided using SourceLanguageConfig. 然后,在创建 recognizer 时,sourceLanguageConfig 会作为参数传递给 FromConfigThen, the sourceLanguageConfig is passed as a parameter to FromConfig when creating the recognizer.

auto sourceLanguageConfig = SourceLanguageConfig::FromLanguage("de-DE");
auto recognizer = SpeechRecognizer::FromConfig(speechConfig, sourceLanguageConfig, audioConfig);

在以下示例中,源语言和自定义终结点是使用 SourceLanguageConfig 提供的。In the following example, the source language and custom endpoint are provided using SourceLanguageConfig. 在创建 recognizer 时,sourceLanguageConfig 作为参数传递给 FromConfigThe sourceLanguageConfig is passed as a parameter to FromConfig when creating the recognizer.

auto sourceLanguageConfig = SourceLanguageConfig::FromLanguage("de-DE", "The Endpoint ID for your custom model.");
auto recognizer = SpeechRecognizer::FromConfig(speechConfig, sourceLanguageConfig, audioConfig);

备注

在 C++ 和 Java 中,SetSpeechRecognitionLanguageSetEndpointIdSpeechConfig 类中弃用的方法。SetSpeechRecognitionLanguage and SetEndpointId are deprecated methods from the SpeechConfig class in C++ and Java. 建议不要使用这些方法,在构造 SpeechRecognizer 时不应使用它们。The use of these methods are discouraged, and shouldn't be used when constructing a SpeechRecognizer.

如何在 Java 中指定源语言How to specify source language in Java

在以下示例中,源语言是在创建新的 SpeechRecognizer 时显式提供的。In the following example, the source language is provided explicitly when creating a new SpeechRecognizer.

SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, "de-DE", audioConfig);

在以下示例中,源语言是使用 SourceLanguageConfig 提供的。In the following example, the source language is provided using SourceLanguageConfig. 然后,在创建新的 SpeechRecognizer 时,sourceLanguageConfig 会作为参数传递。Then, the sourceLanguageConfig is passed as a parameter when creating a new SpeechRecognizer.

SourceLanguageConfig sourceLanguageConfig = SourceLanguageConfig.fromLanguage("de-DE");
SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, sourceLanguageConfig, audioConfig);

在以下示例中,源语言和自定义终结点是使用 SourceLanguageConfig 提供的。In the following example, the source language and custom endpoint are provided using SourceLanguageConfig. 然后,在创建新的 SpeechRecognizer 时,sourceLanguageConfig 会作为参数传递。Then, the sourceLanguageConfig is passed as a parameter when creating a new SpeechRecognizer.

SourceLanguageConfig sourceLanguageConfig = SourceLanguageConfig.fromLanguage("de-DE", "The Endpoint ID for your custom model.");
SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, sourceLanguageConfig, audioConfig);

备注

在 C++ 和 Java 中,setSpeechRecognitionLanguagesetEndpointIdSpeechConfig 类中弃用的方法。setSpeechRecognitionLanguage and setEndpointId are deprecated methods from the SpeechConfig class in C++ and Java. 建议不要使用这些方法,在构造 SpeechRecognizer 时不应使用它们。The use of these methods are discouraged, and shouldn't be used when constructing a SpeechRecognizer.

如何在 Python 中指定源语言How to specify source language in Python

在以下示例中,源语言是使用 SpeechRecognizer 构造作为参数显式提供的。In the following example, the source language is provided explicitly as a parameter using SpeechRecognizer construct.

speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config, language="de-DE", audio_config=audio_config)

在以下示例中,源语言是使用 SourceLanguageConfig 提供的。In the following example, the source language is provided using SourceLanguageConfig. 然后,将 SourceLanguageConfig 作为参数传递给 SpeechRecognizer 构造。Then, the SourceLanguageConfig is passed as a parameter to SpeechRecognizer construct.

source_language_config = speechsdk.languageconfig.SourceLanguageConfig("de-DE")
speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config, source_language_config=source_language_config, audio_config=audio_config)

在以下示例中,源语言和自定义终结点是使用 SourceLanguageConfig 提供的。In the following example, the source language and custom endpoint are provided using SourceLanguageConfig. 然后,将 SourceLanguageConfig 作为参数传递给 SpeechRecognizer 构造。Then, the SourceLanguageConfig is passed as a parameter to SpeechRecognizer construct.

source_language_config = speechsdk.languageconfig.SourceLanguageConfig("de-DE", "The Endpoint ID for your custom model.")
speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config, source_language_config=source_language_config, audio_config=audio_config)

备注

在 Python 中,SpeechConfig 类中的 speech_recognition_languageendpoint_id 属性已弃用。speech_recognition_language and endpoint_id properties are deprecated from the SpeechConfig class in Python. 建议不要使用这些属性,在构造 SpeechRecognizer 时不应使用它们。The use of these properties is discouraged, and they shouldn't be used when constructing a SpeechRecognizer.

如何在 Javascript 中指定源语言How to specify source language in Javascript

第一步是创建 SpeechConfigThe first step is to create a SpeechConfig:

var speechConfig = sdk.SpeechConfig.fromSubscription("YourSubscriptionkey", "YourRegion");

接下来,使用 speechRecognitionLanguage 指定音频的源语言:Next, specify the source language of your audio with speechRecognitionLanguage:

speechConfig.speechRecognitionLanguage = "de-DE";

如果使用自定义模型进行识别,则可使用 endpointId 指定终结点:If you're using a custom model for recognition, you can specify the endpoint with endpointId:

speechConfig.endpointId = "The Endpoint ID for your custom model.";

如何在 Objective-C 中指定源语言How to specify source language in Objective-C

在以下示例中,源语言是使用 SPXSpeechRecognizer 构造作为参数显式提供的。In the following example, the source language is provided explicitly as a parameter using SPXSpeechRecognizer construct.

SPXSpeechRecognizer* speechRecognizer = \
    [[SPXSpeechRecognizer alloc] initWithSpeechConfiguration:speechConfig language:@"de-DE" audioConfiguration:audioConfig];

在以下示例中,源语言是使用 SPXSourceLanguageConfiguration 提供的。In the following example, the source language is provided using SPXSourceLanguageConfiguration. 然后,将 SPXSourceLanguageConfiguration 作为参数传递给 SPXSpeechRecognizer 构造。Then, the SPXSourceLanguageConfiguration is passed as a parameter to SPXSpeechRecognizer construct.

SPXSourceLanguageConfiguration* sourceLanguageConfig = [[SPXSourceLanguageConfiguration alloc]init:@"de-DE"];
SPXSpeechRecognizer* speechRecognizer = [[SPXSpeechRecognizer alloc] initWithSpeechConfiguration:speechConfig
                                                                     sourceLanguageConfiguration:sourceLanguageConfig
                                                                              audioConfiguration:audioConfig];

在以下示例中,源语言和自定义终结点是使用 SPXSourceLanguageConfiguration 提供的。In the following example, the source language and custom endpoint are provided using SPXSourceLanguageConfiguration. 然后,将 SPXSourceLanguageConfiguration 作为参数传递给 SPXSpeechRecognizer 构造。Then, the SPXSourceLanguageConfiguration is passed as a parameter to SPXSpeechRecognizer construct.

SPXSourceLanguageConfiguration* sourceLanguageConfig = \
        [[SPXSourceLanguageConfiguration alloc]initWithLanguage:@"de-DE"
                                                     endpointId:@"The Endpoint ID for your custom model."];
SPXSpeechRecognizer* speechRecognizer = [[SPXSpeechRecognizer alloc] initWithSpeechConfiguration:speechConfig
                                                                     sourceLanguageConfiguration:sourceLanguageConfig
                                                                              audioConfiguration:audioConfig];

备注

在 Objective-C 中,SPXSpeechConfiguration 类中的 speechRecognitionLanguageendpointId 属性已弃用。speechRecognitionLanguage and endpointId properties are deprecated from the SPXSpeechConfiguration class in Objective-C. 建议不要使用这些属性,在构造 SPXSpeechRecognizer 时不应使用它们。The use of these properties is discouraged, and they shouldn't be used when constructing a SPXSpeechRecognizer.

另请参阅See also

  • 如需语音转文本支持的语言和区域设置的列表,请参阅语言支持For a list of supported languages and locales for speech to text, see Language support.

后续步骤Next steps