ibm_watson.language_translator_v3 module

IBM Watson™ Language Translator translates text from one language to another. The service offers multiple IBM provided translation 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 LanguageTranslatorV3(version, authenticator=None)[source]

Bases: ibm_cloud_sdk_core.base_service.BaseService

The Language Translator V3 service.

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

Translate.

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) – (optional) A globally unique string that identifies the underlying model that is used for translation.

  • source (str) – (optional) Translation source language code.

  • target (str) – (optional) Translation target language code.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

list_identifiable_languages(**kwargs)[source]

List identifiable languages.

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 DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

identify(text, **kwargs)[source]

Identify language.

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 DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

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

List models.

Lists available translation models.

Parameters
  • source (str) – (optional) Specify a language code to filter results by source language.

  • target (str) – (optional) Specify a language code to filter results by target language.

  • default (bool) – (optional) 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. There is exactly one default model per language pair, the IBM provided base model.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

create_model(base_model_id, *, forced_glossary=None, parallel_corpus=None, name=None, **kwargs)[source]

Create model.

Uploads Translation Memory eXchange (TMX) files to customize a translation model. You can either customize a model with a forced glossary or with a corpus that contains parallel sentences. To create a model that is customized with a parallel corpus <b>and</b> a forced glossary, proceed in two steps: customize with a parallel corpus first and then customize the resulting model with a glossary. Depending on the type of customization and the size of the uploaded corpora, training can range from minutes for a glossary to several hours for a large parallel corpus. You can upload a single forced glossary file and this file must be less than <b>10 MB</b>. You can upload multiple parallel corpora tmx files. The cumulative file size of all uploaded files is limited to <b>250 MB</b>. To successfully train with a parallel corpus you must have at least <b>5,000 parallel sentences</b> in your corpus. You can have a <b>maximum of 10 custom models per language pair</b>.

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. Usually all IBM provided models are customizable. In addition, all your models that have been created via parallel corpus customization, can be further customized with a forced glossary.

  • forced_glossary (file) – (optional) 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. A forced glossary should contain single words or short phrases.

  • parallel_corpus (file) – (optional) A TMX file with parallel sentences for source and target language. You can upload multiple parallel_corpus files in one request. All uploaded parallel_corpus files combined, your parallel corpus must contain at least 5,000 parallel sentences to train successfully.

  • name (str) – (optional) 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.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

delete_model(model_id, **kwargs)[source]

Delete model.

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 DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

get_model(model_id, **kwargs)[source]

Get model details.

Gets information about a translation model, including training status for custom models. Use this API call to poll the status of your customization request. A successfully completed training will have a status of available.

Parameters
  • model_id (str) – Model ID of the model to get.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

list_documents(**kwargs)[source]

List documents.

Lists documents that have been submitted for translation.

Parameters

headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

translate_document(file, *, filename=None, file_content_type=None, model_id=None, source=None, target=None, document_id=None, **kwargs)[source]

Translate document.

Submit a document for translation. You can submit the document contents in the file parameter, or you can reference a previously submitted document by document ID.

Parameters
  • file (file) – The source file to translate. [Supported file types](https://cloud.ibm.com/docs/services/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats) Maximum file size: 20 MB.

  • filename (str) – (optional) The filename for file.

  • file_content_type (str) – (optional) The content type of file.

  • model_id (str) – (optional) The model to use for translation. model_id or both source and target are required.

  • source (str) – (optional) Language code that specifies the language of the source document.

  • target (str) – (optional) Language code that specifies the target language for translation.

  • document_id (str) – (optional) To use a previously submitted document as the source for a new translation, enter the document_id of the document.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

get_document_status(document_id, **kwargs)[source]

Get document status.

Gets the translation status of a document.

Parameters
  • document_id (str) – The document ID of the document.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

delete_document(document_id, **kwargs)[source]

Delete document.

Deletes a document.

Parameters
  • document_id (str) – Document ID of the document to delete.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

get_translated_document(document_id, *, accept=None, **kwargs)[source]

Get translated document.

Gets the translated document associated with the given document ID.

Parameters
  • document_id (str) – The document ID of the document that was submitted for translation.

  • accept (str) – (optional) The type of the response: application/powerpoint, application/mspowerpoint, application/x-rtf, application/json, application/xml, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.oasis.opendocument.spreadsheet, application/vnd.oasis.opendocument.presentation, application/vnd.oasis.opendocument.text, application/pdf, application/rtf, text/html, text/json, text/plain, text/richtext, text/rtf, or text/xml. A character encoding can be specified by including a charset parameter. For example, ‘text/html;charset=utf-8’.

  • headers (dict) – A dict containing the request headers

Returns

A DetailedResponse containing the result, headers and HTTP status code.

Return type

DetailedResponse

class TranslateDocumentEnums[source]

Bases: object

class FileContentType[source]

Bases: enum.Enum

The content type of file.

APPLICATION_POWERPOINT = 'application/powerpoint'
APPLICATION_MSPOWERPOINT = 'application/mspowerpoint'
APPLICATION_X_RTF = 'application/x-rtf'
APPLICATION_JSON = 'application/json'
APPLICATION_XML = 'application/xml'
APPLICATION_VND_MS_EXCEL = 'application/vnd.ms-excel'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
APPLICATION_VND_MS_POWERPOINT = 'application/vnd.ms-powerpoint'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
APPLICATION_MSWORD = 'application/msword'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
APPLICATION_VND_OASIS_OPENDOCUMENT_SPREADSHEET = 'application/vnd.oasis.opendocument.spreadsheet'
APPLICATION_VND_OASIS_OPENDOCUMENT_PRESENTATION = 'application/vnd.oasis.opendocument.presentation'
APPLICATION_VND_OASIS_OPENDOCUMENT_TEXT = 'application/vnd.oasis.opendocument.text'
APPLICATION_PDF = 'application/pdf'
APPLICATION_RTF = 'application/rtf'
TEXT_HTML = 'text/html'
TEXT_JSON = 'text/json'
TEXT_PLAIN = 'text/plain'
TEXT_RICHTEXT = 'text/richtext'
TEXT_RTF = 'text/rtf'
TEXT_XML = 'text/xml'
class GetTranslatedDocumentEnums[source]

Bases: object

class Accept[source]

Bases: enum.Enum

The type of the response: application/powerpoint, application/mspowerpoint, application/x-rtf, application/json, application/xml, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.oasis.opendocument.spreadsheet, application/vnd.oasis.opendocument.presentation, application/vnd.oasis.opendocument.text, application/pdf, application/rtf, text/html, text/json, text/plain, text/richtext, text/rtf, or text/xml. A character encoding can be specified by including a charset parameter. For example, ‘text/html;charset=utf-8’.

APPLICATION_POWERPOINT = 'application/powerpoint'
APPLICATION_MSPOWERPOINT = 'application/mspowerpoint'
APPLICATION_X_RTF = 'application/x-rtf'
APPLICATION_JSON = 'application/json'
APPLICATION_XML = 'application/xml'
APPLICATION_VND_MS_EXCEL = 'application/vnd.ms-excel'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_SPREADSHEETML_SHEET = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
APPLICATION_VND_MS_POWERPOINT = 'application/vnd.ms-powerpoint'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_PRESENTATIONML_PRESENTATION = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
APPLICATION_MSWORD = 'application/msword'
APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
APPLICATION_VND_OASIS_OPENDOCUMENT_SPREADSHEET = 'application/vnd.oasis.opendocument.spreadsheet'
APPLICATION_VND_OASIS_OPENDOCUMENT_PRESENTATION = 'application/vnd.oasis.opendocument.presentation'
APPLICATION_VND_OASIS_OPENDOCUMENT_TEXT = 'application/vnd.oasis.opendocument.text'
APPLICATION_PDF = 'application/pdf'
APPLICATION_RTF = 'application/rtf'
TEXT_HTML = 'text/html'
TEXT_JSON = 'text/json'
TEXT_PLAIN = 'text/plain'
TEXT_RICHTEXT = 'text/richtext'
TEXT_RTF = 'text/rtf'
TEXT_XML = 'text/xml'
class DeleteModelResult(status)[source]

Bases: object

DeleteModelResult.

Attr str status

“OK” indicates that the model was successfully deleted.

class DocumentList(documents)[source]

Bases: object

DocumentList.

Attr list[DocumentStatus] documents

An array of all previously submitted documents.

class DocumentStatus(document_id, filename, status, model_id, source, target, created, *, base_model_id=None, completed=None, word_count=None, character_count=None)[source]

Bases: object

Document information, including translation status.

Attr str document_id

System generated ID identifying a document being translated using one specific translation model.

Attr str filename

filename from the submission (if it was missing in the multipart-form, ‘noname.<ext matching content type>’ is used.

Attr str status

The status of the translation job associated with a submitted document.

Attr str model_id

A globally unique string that identifies the underlying model that is used for translation.

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 absent or an empty string.

Attr str source

Translation source language code.

Attr str target

Translation target language code.

Attr datetime created

The time when the document was submitted.

Attr datetime completed

(optional) The time when the translation completed.

Attr int word_count

(optional) The number of words in the source document, present only if status=available.

Attr int character_count

(optional) The number of characters in the source document, present only if status=available.

class StatusEnum[source]

Bases: enum.Enum

The status of the translation job associated with a submitted document.

PROCESSING = 'processing'
AVAILABLE = 'available'
FAILED = 'failed'
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)[source]

Bases: object

Translation.

Attr str translation

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

Bases: enum.Enum

Availability of a model.

UPLOADING = 'uploading'
UPLOADED = 'uploaded'
DISPATCHING = 'dispatching'
QUEUED = 'queued'
TRAINING = 'training'
TRAINED = 'trained'
PUBLISHING = 'publishing'
AVAILABLE = 'available'
DELETED = 'deleted'
ERROR = 'error'
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.