This page describes how to enable language recognition for audio transcription requests sent to Speech-to-Text.
In some situations, you don't know for certain what language your audio recordings contain. For example, if you publish your service, app, or product in a country with multiple official languages, you can potentially receive audio input from users in a variety of languages. This can make specifying a single language code for transcription requests significantly more difficult.
Multiple language recognition
Speech-to-Text offers a way for you to specify a set of alternative languages that your audio data might contain. When you send an audio transcription request to Speech-to-Text, you can provide a list of additional languages that the audio data might include. If you include a list of languages in your request, Speech-to-Text attempts to transcribe the audio based upon the language that best fits the sample from the alternates you provide. Speech-to-Text then labels the transcription results with the predicted language code.
This feature is ideal for apps that need to transcribe short statements like voice commands or search. You can list up to three alternative languages from among those that Speech-to-Text supports in addition to your primary language (for four languages total).
Even though you can specify alternative languages for your speech
transcription request, you must still provide a primary language code
in the languageCode
field. Also, you should constrain the number
of languages you request to a bare minimum. The fewer alternative
language codes that you request helps Speech-to-Text more
successfully select the correct one. Specifying just a single language
produces the best results.
Enable language recognition in audio transcription requests
To specify alternative languages in your audio transcription,
you must set the alternativeLanguageCodes
field to a list of
language codes in the RecognitionConfig
parameters for the request. Speech-to-Text supports
alternative language codes for all speech recognition methods:
speech:recognize
,
speech:longrunningrecognize
,
and Streaming.
Use a local file
Protocol
Refer to the speech:recognize
API endpoint for complete details.
To perform synchronous speech recognition, make a POST
request and provide the
appropriate request body. The following shows an example of a POST
request using
curl
. The example uses the Google Cloud CLI to generate an access
token. For instructions on installing the gcloud CLI,
see the quickstart.
The following example shows how to request transcription of an audio file that may include speech in English, French, or German.
curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ https://speech.googleapis.com/v1p1beta1/speech:recognize \ --data '{ "config": { "encoding": "LINEAR16", "languageCode": "en-US", "alternativeLanguageCodes": ["fr-FR", "de-DE"], "model": "command_and_search" }, "audio": { "uri": "gs://cloud-samples-tests/speech/commercial_mono.wav" } }' > multi-language.txt
If the request is successful, the server returns a 200 OK
HTTP
status code and the response in JSON format, saved to a file
named multi-language.txt
.
{ "results": [ { "alternatives": [ { "transcript": "hi I'd like to buy a Chromecast I'm ..." "confidence": 0.9466864 } ], "languageCode": "en-us" }, { "alternatives": [ { "transcript": " let's go with the black one", "confidence": 0.9829583 } ], "languageCode": "en-us" }, ] }
Java
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Java API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Node.js API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Python API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Use a remote file
Java
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Java API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Node.js API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries. For more information, see the Speech-to-Text Python API reference documentation.
To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.