watson_developer_cloud.language_translator_v2 module

IBM Watson Language Translator translates text from one language to another. The service offers multiple domain-specific models that you can customize based on your unique terminology and language. Use Language Translator to take news from across the globe and present it in your language, communicate with your customers in their own language, and more.

class LanguageTranslatorV2(url='https://gateway.watsonplatform.net/language-translator/api', username=None, password=None)[source]

Bases: watson_developer_cloud.watson_service.WatsonService

The Language Translator V2 service.

default_url = 'https://gateway.watsonplatform.net/language-translator/api'
translate(text, model_id=None, source=None, target=None, **kwargs)[source]

Translates the input text from the source language to the target language.

Parameters:
  • text (list[str]) – Input text in UTF-8 encoding. Multiple entries will result in multiple translations in the response.
  • model_id (str) – Model ID of the translation model to use. If this is specified, the source and target parameters will be ignored. The method requires either a model ID or both the source and target parameters.
  • source (str) – Language code of the source text language. Use with target as an alternative way to select a translation model. When source and target are set, and a model ID is not set, the system chooses a default model for the language pair (usually the model based on the news domain).
  • target (str) – Language code of the translation target language. Use with source as an alternative way to select a translation model.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the TranslationResult response.

Return type:

dict

identify(text, **kwargs)[source]

Identifies the language of the input text.

Parameters:
  • text (str) – Input text in UTF-8 format.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the IdentifiedLanguages response.

Return type:

dict

list_identifiable_languages(**kwargs)[source]

Lists the languages that the service can identify. Returns the language code (for example, en for English or es for Spanish) and name of each language.

Parameters:headers (dict) – A dict containing the request headers
Returns:A dict containing the IdentifiableLanguages response.
Return type:dict
create_model(base_model_id, name=None, forced_glossary=None, parallel_corpus=None, monolingual_corpus=None, forced_glossary_filename=None, parallel_corpus_filename=None, monolingual_corpus_filename=None, **kwargs)[source]

Uploads a TMX glossary file on top of a domain to customize a translation model. Depending on the size of the file, training can range from minutes for a glossary to several hours for a large parallel corpus. Glossary files must be less than 10 MB. The cumulative file size of all uploaded glossary and corpus files is limited to 250 MB.

Parameters:
  • base_model_id (str) – The model ID of the model to use as the base for customization. To see available models, use the List models method.
  • name (str) – An optional model name that you can use to identify the model. Valid characters are letters, numbers, dashes, underscores, spaces and apostrophes. The maximum length is 32 characters.
  • forced_glossary (file) – A TMX file with your customizations. The customizations in the file completely overwrite the domain translaton data, including high frequency or high confidence phrase translations. You can upload only one glossary with a file size less than 10 MB per call.
  • parallel_corpus (file) – A TMX file that contains entries that are treated as a parallel corpus instead of a glossary.
  • monolingual_corpus (file) – A UTF-8 encoded plain text file that is used to customize the target language model.
  • forced_glossary_filename (str) – The filename for forced_glossary.
  • parallel_corpus_filename (str) – The filename for parallel_corpus.
  • monolingual_corpus_filename (str) – The filename for monolingual_corpus.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the TranslationModel response.

Return type:

dict

delete_model(model_id, **kwargs)[source]

Deletes a custom translation model.

Parameters:
  • model_id (str) – Model ID of the model to delete.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the DeleteModelResult response.

Return type:

dict

get_model(model_id, **kwargs)[source]

Gets information about a translation model, including training status for custom models.

Parameters:
  • model_id (str) – Model ID of the model to get.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the TranslationModel response.

Return type:

dict

list_models(source=None, target=None, default_models=None, **kwargs)[source]

Lists available translation models.

Parameters:
  • source (str) – Specify a language code to filter results by source language.
  • target (str) – Specify a language code to filter results by target language.
  • default_models (bool) – If the default parameter isn’t specified, the service will return all models (default and non-default) for each language pair. To return only default models, set this to true. To return only non-default models, set this to false.
  • headers (dict) – A dict containing the request headers
Returns:

A dict containing the TranslationModels response.

Return type:

dict

class DeleteModelResult(status)[source]

Bases: object

DeleteModelResult.

Attr str status:
 “OK” indicates that the model was successfully deleted.
class IdentifiableLanguage(language, name)[source]

Bases: object

IdentifiableLanguage.

Attr str language:
 The language code for an identifiable language.
Attr str name:The name of the identifiable language.
class IdentifiableLanguages(languages)[source]

Bases: object

IdentifiableLanguages.

Attr list[IdentifiableLanguage] languages:
 A list of all languages that the service can identify.
class IdentifiedLanguage(language, confidence)[source]

Bases: object

IdentifiedLanguage.

Attr str language:
 The language code for an identified language.
Attr float confidence:
 The confidence score for the identified language.
class IdentifiedLanguages(languages)[source]

Bases: object

IdentifiedLanguages.

Attr list[IdentifiedLanguage] languages:
 A ranking of identified languages with confidence scores.
class Translation(translation_output)[source]

Bases: object

Translation.

Attr str translation_output:
 Translation output in UTF-8.
class TranslationModel(model_id, name=None, source=None, target=None, base_model_id=None, domain=None, customizable=None, default_model=None, owner=None, status=None)[source]

Bases: object

Response payload for models.

Attr str model_id:
 A globally unique string that identifies the underlying model that is used for translation.
Attr str name:(optional) Optional name that can be specified when the model is created.
Attr str source:
 (optional) Translation source language code.
Attr str target:
 (optional) Translation target language code.
Attr str base_model_id:
 (optional) Model ID of the base model that was used to customize the model. If the model is not a custom model, this will be an empty string.
Attr str domain:
 (optional) The domain of the translation model.
Attr bool customizable:
 (optional) Whether this model can be used as a base for customization. Customized models are not further customizable, and some base models are not customizable.
Attr bool default_model:
 (optional) Whether or not the model is a default model. A default model is the model for a given language pair that will be used when that language pair is specified in the source and target parameters.
Attr str owner:(optional) Either an empty string, indicating the model is not a custom model, or the ID of the service instance that created the model.
Attr str status:
 (optional) Availability of a model.
class TranslationModels(models)[source]

Bases: object

The response type for listing existing translation models.

Attr list[TranslationModel] models:
 An array of available models.
class TranslationResult(word_count, character_count, translations)[source]

Bases: object

TranslationResult.

Attr int word_count:
 Number of words in the input text.
Attr int character_count:
 Number of characters in the input text.
Attr list[Translation] translations:
 List of translation output in UTF-8, corresponding to the input text entries.