watson_developer_cloud.language_translator_v2 module

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)[source]

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

Parameters:
  • text (list[str]) – Input text in UTF-8 encoding. It is a list so that multiple paragraphs can be submitted. Also accept a single string, instead of an array, as valid input.
  • model_id (str) – The unique model_id of the translation model being used to translate text. The model_id inherently specifies source language, target language, and domain. If the model_id is specified, there is no need for the source and target parameters and the values are ignored.
  • source (str) – Used in combination with target as an alternative way to select the model for translation. When target and source are set, and model_id is not set, the system chooses a default model with the right language pair to translate (usually the model based on the news domain).
  • target (str) – Used in combination with source as an alternative way to select the model for translation. When target and source are set, and model_id is not set, the system chooses a default model with the right language pair to translate (usually the model based on the news domain).
Returns:

A dict containing the TranslationResult response.

Return type:

dict

identify(text)[source]

Identifies the language of the input text.

Parameters:text (str) – Input text in UTF-8 format.
Returns:A dict containing the IdentifiedLanguages response.
Return type:dict
list_identifiable_languages()[source]

Lists all languages that can be identified by the API.

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

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)[source]

Uploads a TMX glossary file on top of a domain to customize a translation model.

Parameters:
  • base_model_id (str) – Specifies the domain model that is used as the base for the training. To see current supported domain models, use the GET /v2/models parameter.
  • name (str) – The model name. Valid characters are letters, numbers, -, and _. No spaces.
  • forced_glossary (file) – A TMX file with your customizations. The customizations in the file completely overwrite the domain data translation, 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.
Returns:

A dict containing the TranslationModel response.

Return type:

dict

delete_model(model_id)[source]

Deletes a custom translation model.

Parameters:model_id (str) – The model identifier.
Returns:A dict containing the DeleteModelResult response.
Return type:dict
get_model(model_id)[source]

Get information about the given translation model, including training status.

Parameters:model_id (str) – Model ID to use.
Returns:A dict containing the TranslationModel response.
Return type:dict
list_models(source=None, target=None, default_models=None)[source]

Lists available standard and custom models by source or target language.

Parameters:
  • source (str) – Filter models by source language.
  • target (str) – Filter models by target language.
  • default_models (bool) – Valid values are leaving it unset, true, and false. When true, it filters models to return the default_models model or models. When false, it returns the non-default_models model or models. If not set, it returns all models, default_models and non-default_models.
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 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 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. This string contains all the information about source language, target language, domain, and various other related configurations.
Attr str name:(optional) If a model is trained by a user, there might be an optional “name” parameter attached during training to help the user identify the model.
Attr str source:
 (optional) Source language in two letter language code. Use the five letter code when clarifying between multiple supported languages. When model_id is used directly, it will override the source-target language combination. Also, when a two letter language code is used, but no suitable default is found, it returns an error.
Attr str target:
 (optional) Target language in two letter language code.
Attr str base_model_id:
 (optional) If this model is a custom model, this returns the base model that it is trained on. For a base model, this response value is empty.
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 we don’t allow the customization of certain base models.
Attr bool default_model:
 (optional) Whether this model is considered a default model and is used when the source and target languages are specified without the model_id.
Attr str owner:(optional) Returns the Bluemix ID of the instance that created the model, or an empty string if it is a model that is trained by IBM.
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 of the complete input text.
Attr int character_count:
 Number of characters of the complete input text.
Attr list[Translation] translations:
 List of translation output in UTF-8, corresponding to the list of input text.