Skip to content

Commit b9a7d41

Browse files
feat(language-translator-v3): new method: listLanguages
1 parent 01d426c commit b9a7d41

File tree

2 files changed

+253
-38
lines changed

2 files changed

+253
-38
lines changed

language-translator/v3.ts

Lines changed: 201 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { getSdkHeaders } from '../lib/common';
2828

2929
class LanguageTranslatorV3 extends BaseService {
3030

31-
static DEFAULT_SERVICE_URL: string = 'https://gateway.watsonplatform.net/language-translator/api';
31+
static DEFAULT_SERVICE_URL: string = 'https://api.us-south.language-translator.watson.cloud.ibm.com';
3232
static DEFAULT_SERVICE_NAME: string = 'language_translator';
3333

3434
/**
@@ -41,7 +41,7 @@ class LanguageTranslatorV3 extends BaseService {
4141
* programmatically specify the current date at runtime, in case the API has been updated since your application's
4242
* release. Instead, specify a version date that is compatible with your application, and don't change it until your
4343
* application is ready for a later version.
44-
* @param {string} [options.serviceUrl] - The base url to use when contacting the service (e.g. 'https://gateway.watsonplatform.net/language-translator/api'). The base url may differ between IBM Cloud regions.
44+
* @param {string} [options.serviceUrl] - The base url to use when contacting the service (e.g. 'https://gateway.watsonplatform.net'). The base url may differ between IBM Cloud regions.
4545
* @param {OutgoingHttpHeaders} [options.headers] - Default headers that shall be included with every request to the service.
4646
* @param {string} [options.serviceName] - The name of the service to configure
4747
* @param {Authenticator} [options.authenticator] - The Authenticator object used to authenticate requests to the service. Defaults to environment if not set
@@ -69,6 +69,58 @@ class LanguageTranslatorV3 extends BaseService {
6969
this.baseOptions.qs.version = options.version;
7070
}
7171

72+
/*************************
73+
* languages
74+
************************/
75+
76+
/**
77+
* List supported languages.
78+
*
79+
* Lists all supported languages. The method returns an array of supported languages with information about each
80+
* language. Languages are listed in alphabetical order by language code (for example, `af`, `ar`).
81+
*
82+
* @param {Object} [params] - The parameters to send to the service.
83+
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
84+
* @param {Function} [callback] - The callback that handles the response
85+
* @returns {Promise<LanguageTranslatorV3.Response<LanguageTranslatorV3.Languages>>}
86+
*/
87+
public listLanguages(params?: LanguageTranslatorV3.ListLanguagesParams, callback?: LanguageTranslatorV3.Callback<LanguageTranslatorV3.Languages>): Promise<LanguageTranslatorV3.Response<LanguageTranslatorV3.Languages>> {
88+
const _params = (typeof params === 'function' && !callback) ? {} : extend({}, params);
89+
const _callback = (typeof params === 'function' && !callback) ? params : callback;
90+
91+
return new Promise((resolve, reject) => {
92+
const sdkHeaders = getSdkHeaders(LanguageTranslatorV3.DEFAULT_SERVICE_NAME, 'v3', 'listLanguages');
93+
94+
const parameters = {
95+
options: {
96+
url: '/v3/languages',
97+
method: 'GET',
98+
},
99+
defaultOptions: extend(true, {}, this.baseOptions, {
100+
headers: extend(true, sdkHeaders, {
101+
'Accept': 'application/json',
102+
}, _params.headers),
103+
}),
104+
};
105+
106+
return this.createRequest(parameters).then(
107+
res => {
108+
if (_callback) {
109+
_callback(null, res);
110+
}
111+
return resolve(res);
112+
},
113+
err => {
114+
if (_callback) {
115+
_callback(err)
116+
return resolve();
117+
}
118+
return reject(err);
119+
}
120+
);
121+
});
122+
};
123+
72124
/*************************
73125
* translation
74126
************************/
@@ -328,33 +380,89 @@ class LanguageTranslatorV3 extends BaseService {
328380
/**
329381
* Create model.
330382
*
331-
* Uploads Translation Memory eXchange (TMX) files to customize a translation model.
383+
* Uploads training files to customize a translation model. You can customize a model with a forced glossary or with a
384+
* parallel corpus:
385+
* * Use a *forced glossary* to force certain terms and phrases to be translated in a specific way. You can upload
386+
* only a single forced glossary file for a model. The size of a forced glossary file for a custom model is limited to
387+
* 10 MB.
388+
* * Use a *parallel corpus* when you want your custom model to learn from general translation patterns in parallel
389+
* sentences in your samples. What your model learns from a parallel corpus can improve translation results for input
390+
* text that the model has not been trained on. You can upload multiple parallel corpora files with a request. To
391+
* successfully train with parallel corpora, the corpora files must contain a cumulative total of at least 5000
392+
* parallel sentences. The cumulative size of all uploaded corpus files for a custom model is limited to 250 MB.
393+
*
394+
* Depending on the type of customization and the size of the uploaded files, training time can range from minutes for
395+
* a glossary to several hours for a large parallel corpus. To create a model that is customized with a parallel
396+
* corpus and a forced glossary, customize the model with a parallel corpus first and then customize the resulting
397+
* model with a forced glossary.
398+
*
399+
* You can create a maximum of 10 custom models per language pair. For more information about customizing a
400+
* translation model, including the formatting and character restrictions for data files, see [Customizing your
401+
* model](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing).
402+
*
403+
* #### Supported file formats
404+
*
405+
* You can provide your training data for customization in the following document formats:
406+
* * **TMX** (`.tmx`) - Translation Memory eXchange (TMX) is an XML specification for the exchange of translation
407+
* memories.
408+
* * **XLIFF** (`.xliff`) - XML Localization Interchange File Format (XLIFF) is an XML specification for the exchange
409+
* of translation memories.
410+
* * **CSV** (`.csv`) - Comma-separated values (CSV) file with two columns for aligned sentences and phrases. The
411+
* first row contains the language code.
412+
* * **TSV** (`.tsv` or `.tab`) - Tab-separated values (TSV) file with two columns for aligned sentences and phrases.
413+
* The first row contains the language code.
414+
* * **JSON** (`.json`) - Custom JSON format for specifying aligned sentences and phrases.
415+
* * **Microsoft Excel** (`.xls` or `.xlsx`) - Excel file with the first two columns for aligned sentences and
416+
* phrases. The first row contains the language code.
332417
*
333-
* You can either customize a model with a forced glossary or with a corpus that contains parallel sentences. To
334-
* create a model that is customized with a parallel corpus <b>and</b> a forced glossary, proceed in two steps:
335-
* customize with a parallel corpus first and then customize the resulting model with a glossary. Depending on the
336-
* type of customization and the size of the uploaded corpora, training can range from minutes for a glossary to
337-
* several hours for a large parallel corpus. You can upload a single forced glossary file and this file must be less
338-
* than <b>10 MB</b>. You can upload multiple parallel corpora tmx files. The cumulative file size of all uploaded
339-
* files is limited to <b>250 MB</b>. To successfully train with a parallel corpus you must have at least <b>5,000
340-
* parallel sentences</b> in your corpus.
418+
* You must encode all text data in UTF-8 format. For more information, see [Supported document formats for training
419+
* data](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing#supported-document-formats-for-training-data).
341420
*
342-
* You can have a <b>maximum of 10 custom models per language pair</b>.
421+
*
422+
* #### Specifying file formats
423+
*
424+
* You can indicate the format of a file by including the file extension with the file name. Use the file extensions
425+
* shown in **Supported file formats**.
426+
*
427+
* Alternatively, you can omit the file extension and specify one of the following `content-type` specifications for
428+
* the file:
429+
* * **TMX** - `application/x-tmx+xml`
430+
* * **XLIFF** - `application/xliff+xml`
431+
* * **CSV** - `text/csv`
432+
* * **TSV** - `text/tab-separated-values`
433+
* * **JSON** - `application/json`
434+
* * **Microsoft Excel** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`
435+
*
436+
* For example, with `curl`, use the following `content-type` specification to indicate the format of a CSV file named
437+
* **glossary**:
438+
*
439+
* `--form "forced_glossary=@glossary;type=text/csv"`.
343440
*
344441
* @param {Object} params - The parameters to send to the service.
345-
* @param {string} params.baseModelId - The model ID of the model to use as the base for customization. To see
346-
* available models, use the `List models` method. Usually all IBM provided models are customizable. In addition, all
347-
* your models that have been created via parallel corpus customization, can be further customized with a forced
348-
* glossary.
349-
* @param {NodeJS.ReadableStream|Buffer} [params.forcedGlossary] - A TMX file with your customizations. The
350-
* customizations in the file completely overwrite the domain translaton data, including high frequency or high
351-
* confidence phrase translations. You can upload only one glossary with a file size less than 10 MB per call. A
352-
* forced glossary should contain single words or short phrases.
353-
* @param {NodeJS.ReadableStream|Buffer} [params.parallelCorpus] - A TMX file with parallel sentences for source and
354-
* target language. You can upload multiple parallel_corpus files in one request. All uploaded parallel_corpus files
355-
* combined, your parallel corpus must contain at least 5,000 parallel sentences to train successfully.
442+
* @param {string} params.baseModelId - The ID of the translation model to use as the base for customization. To see
443+
* available models and IDs, use the `List models` method. Most models that are provided with the service are
444+
* customizable. In addition, all models that you create with parallel corpora customization can be further customized
445+
* with a forced glossary.
446+
* @param {NodeJS.ReadableStream|Buffer} [params.forcedGlossary] - A file with forced glossary terms for the source
447+
* and target languages. The customizations in the file completely overwrite the domain translation data, including
448+
* high frequency or high confidence phrase translations.
449+
*
450+
* You can upload only one glossary file for a custom model, and the glossary can have a maximum size of 10 MB. A
451+
* forced glossary must contain single words or short phrases. For more information, see **Supported file formats** in
452+
* the method description.
453+
*
454+
* *With `curl`, use `--form forced_glossary=@{filename}`.*.
455+
* @param {NodeJS.ReadableStream|Buffer} [params.parallelCorpus] - A file with parallel sentences for the source and
456+
* target languages. You can upload multiple parallel corpus files in one request by repeating the parameter. All
457+
* uploaded parallel corpus files combined must contain at least 5000 parallel sentences to train successfully. You
458+
* can provide a maximum of 500,000 parallel sentences across all corpora.
459+
*
460+
* A single entry in a corpus file can contain a maximum of 80 words. All corpora files for a custom model can have a
461+
* cumulative maximum size of 250 MB. For more information, see **Supported file formats** in the method description.
462+
*
463+
* *With `curl`, use `--form parallel_corpus=@{filename}`.*.
356464
* @param {string} [params.name] - An optional model name that you can use to identify the model. Valid characters are
357-
* letters, numbers, dashes, underscores, spaces and apostrophes. The maximum length is 32 characters.
465+
* letters, numbers, dashes, underscores, spaces, and apostrophes. The maximum length of the name is 32 characters.
358466
* @param {OutgoingHttpHeaders} [params.headers] - Custom request headers
359467
* @param {Function} [callback] - The callback that handles the response
360468
* @returns {Promise<LanguageTranslatorV3.Response<LanguageTranslatorV3.TranslationModel>>}
@@ -621,7 +729,8 @@ class LanguageTranslatorV3 extends BaseService {
621729
* @param {string} [params.modelId] - The model to use for translation. For example, `en-de` selects the IBM provided
622730
* base model for English to German translation. A model ID overrides the source and target parameters and is required
623731
* if you use a custom model. If no model ID is specified, you must specify a target language.
624-
* @param {string} [params.source] - Language code that specifies the language of the source document.
732+
* @param {string} [params.source] - Language code that specifies the language of the source document. If omitted, the
733+
* service derives the source language from the input text.
625734
* @param {string} [params.target] - Language code that specifies the target language for translation. Required if
626735
* model ID is not specified.
627736
* @param {string} [params.documentId] - To use a previously submitted document as the source for a new translation,
@@ -920,6 +1029,11 @@ namespace LanguageTranslatorV3 {
9201029
* request interfaces
9211030
************************/
9221031

1032+
/** Parameters for the `listLanguages` operation. */
1033+
export interface ListLanguagesParams {
1034+
headers?: OutgoingHttpHeaders;
1035+
}
1036+
9231037
/** Parameters for the `translate` operation. */
9241038
export interface TranslateParams {
9251039
/** Input text in UTF-8 encoding. Multiple entries will result in multiple translations in the response. */
@@ -964,24 +1078,36 @@ namespace LanguageTranslatorV3 {
9641078

9651079
/** Parameters for the `createModel` operation. */
9661080
export interface CreateModelParams {
967-
/** The model ID of the model to use as the base for customization. To see available models, use the `List
968-
* models` method. Usually all IBM provided models are customizable. In addition, all your models that have been
969-
* created via parallel corpus customization, can be further customized with a forced glossary.
1081+
/** The ID of the translation model to use as the base for customization. To see available models and IDs, use
1082+
* the `List models` method. Most models that are provided with the service are customizable. In addition, all
1083+
* models that you create with parallel corpora customization can be further customized with a forced glossary.
9701084
*/
9711085
baseModelId: string;
972-
/** A TMX file with your customizations. The customizations in the file completely overwrite the domain
973-
* translaton data, including high frequency or high confidence phrase translations. You can upload only one
974-
* glossary with a file size less than 10 MB per call. A forced glossary should contain single words or short
975-
* phrases.
1086+
/** A file with forced glossary terms for the source and target languages. The customizations in the file
1087+
* completely overwrite the domain translation data, including high frequency or high confidence phrase
1088+
* translations.
1089+
*
1090+
* You can upload only one glossary file for a custom model, and the glossary can have a maximum size of 10 MB. A
1091+
* forced glossary must contain single words or short phrases. For more information, see **Supported file formats**
1092+
* in the method description.
1093+
*
1094+
* *With `curl`, use `--form forced_glossary=@{filename}`.*.
9761095
*/
9771096
forcedGlossary?: NodeJS.ReadableStream|Buffer;
978-
/** A TMX file with parallel sentences for source and target language. You can upload multiple parallel_corpus
979-
* files in one request. All uploaded parallel_corpus files combined, your parallel corpus must contain at least
980-
* 5,000 parallel sentences to train successfully.
1097+
/** A file with parallel sentences for the source and target languages. You can upload multiple parallel corpus
1098+
* files in one request by repeating the parameter. All uploaded parallel corpus files combined must contain at
1099+
* least 5000 parallel sentences to train successfully. You can provide a maximum of 500,000 parallel sentences
1100+
* across all corpora.
1101+
*
1102+
* A single entry in a corpus file can contain a maximum of 80 words. All corpora files for a custom model can have
1103+
* a cumulative maximum size of 250 MB. For more information, see **Supported file formats** in the method
1104+
* description.
1105+
*
1106+
* *With `curl`, use `--form parallel_corpus=@{filename}`.*.
9811107
*/
9821108
parallelCorpus?: NodeJS.ReadableStream|Buffer;
9831109
/** An optional model name that you can use to identify the model. Valid characters are letters, numbers,
984-
* dashes, underscores, spaces and apostrophes. The maximum length is 32 characters.
1110+
* dashes, underscores, spaces, and apostrophes. The maximum length of the name is 32 characters.
9851111
*/
9861112
name?: string;
9871113
headers?: OutgoingHttpHeaders;
@@ -1025,7 +1151,9 @@ namespace LanguageTranslatorV3 {
10251151
* model. If no model ID is specified, you must specify a target language.
10261152
*/
10271153
modelId?: string;
1028-
/** Language code that specifies the language of the source document. */
1154+
/** Language code that specifies the language of the source document. If omitted, the service derives the source
1155+
* language from the input text.
1156+
*/
10291157
source?: string;
10301158
/** Language code that specifies the target language for translation. Required if model ID is not specified. */
10311159
target?: string;
@@ -1204,6 +1332,42 @@ namespace LanguageTranslatorV3 {
12041332
languages: IdentifiedLanguage[];
12051333
}
12061334

1335+
/** Response payload for languages. */
1336+
export interface Language {
1337+
/** The language code for the language (for example, `af`). */
1338+
language?: string;
1339+
/** The name of the language in English (for example, `Afrikaans`). */
1340+
language_name?: string;
1341+
/** The native name of the language (for example, `Afrikaans`). */
1342+
native_language_name?: string;
1343+
/** The country code for the language (for example, `ZA` for South Africa). */
1344+
country_code?: string;
1345+
/** Indicates whether words of the language are separated by whitespace: `true` if the words are separated;
1346+
* `false` otherwise.
1347+
*/
1348+
words_separated?: boolean;
1349+
/** Indicates the direction of the language: `right_to_left` or `left_to_right`. */
1350+
direction?: string;
1351+
/** Indicates whether the language can be used as the source for translation: `true` if the language can be used
1352+
* as the source; `false` otherwise.
1353+
*/
1354+
supported_as_source?: boolean;
1355+
/** Indicates whether the language can be used as the target for translation: `true` if the language can be used
1356+
* as the target; `false` otherwise.
1357+
*/
1358+
supported_as_target?: boolean;
1359+
/** Indicates whether the language supports automatic detection: `true` if the language can be detected
1360+
* automatically; `false` otherwise.
1361+
*/
1362+
identifiable?: boolean;
1363+
}
1364+
1365+
/** The response type for listing supported languages. */
1366+
export interface Languages {
1367+
/** An array of supported languages with information about each language. */
1368+
languages: Language[];
1369+
}
1370+
12071371
/** Translation. */
12081372
export interface Translation {
12091373
/** Translation output in UTF-8. */

0 commit comments

Comments
 (0)