Skip to content

Commit 0570cdb

Browse files
committed
feat(LanguageTranslatorV3): add support for ListLanguages
1 parent 369e7fd commit 0570cdb

File tree

6 files changed

+333
-45
lines changed

6 files changed

+333
-45
lines changed

Scripts/Services/LanguageTranslator/V3/LanguageTranslatorService.cs

Lines changed: 175 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace IBM.Watson.LanguageTranslator.V3
3232
public partial class LanguageTranslatorService : BaseService
3333
{
3434
private const string serviceId = "language_translator";
35-
private const string defaultServiceUrl = "https://gateway.watsonplatform.net/language-translator/api";
35+
private const string defaultServiceUrl = "https://api.us-south.language-translator.watson.cloud.ibm.com";
3636

3737
#region VersionDate
3838
private string versionDate;
@@ -88,20 +88,91 @@ public LanguageTranslatorService(string versionDate, Authenticator authenticator
8888
}
8989
}
9090

91+
/// <summary>
92+
/// List supported languages.
93+
///
94+
/// Lists all supported languages. The method returns an array of supported languages with information about
95+
/// each language. Languages are listed in alphabetical order by language code (for example, `af`, `ar`).
96+
/// </summary>
97+
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
98+
/// <returns><see cref="Languages" />Languages</returns>
99+
public bool ListLanguages(Callback<Languages> callback)
100+
{
101+
if (callback == null)
102+
throw new ArgumentNullException("`callback` is required for `ListLanguages`");
103+
104+
RequestObject<Languages> req = new RequestObject<Languages>
105+
{
106+
Callback = callback,
107+
HttpMethod = UnityWebRequest.kHttpVerbGET,
108+
DisableSslVerification = DisableSslVerification
109+
};
110+
111+
foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
112+
{
113+
req.Headers.Add(kvp.Key, kvp.Value);
114+
}
115+
116+
ClearCustomRequestHeaders();
117+
118+
foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "ListLanguages"))
119+
{
120+
req.Headers.Add(kvp.Key, kvp.Value);
121+
}
122+
123+
req.Parameters["version"] = VersionDate;
124+
125+
req.OnResponse = OnListLanguagesResponse;
126+
127+
Connector.URL = GetServiceUrl() + "/v3/languages";
128+
Authenticator.Authenticate(Connector);
129+
130+
return Connector.Send(req);
131+
}
132+
133+
private void OnListLanguagesResponse(RESTConnector.Request req, RESTConnector.Response resp)
134+
{
135+
DetailedResponse<Languages> response = new DetailedResponse<Languages>();
136+
foreach (KeyValuePair<string, string> kvp in resp.Headers)
137+
{
138+
response.Headers.Add(kvp.Key, kvp.Value);
139+
}
140+
response.StatusCode = resp.HttpResponseCode;
141+
142+
try
143+
{
144+
string json = Encoding.UTF8.GetString(resp.Data);
145+
response.Result = JsonConvert.DeserializeObject<Languages>(json);
146+
response.Response = json;
147+
}
148+
catch (Exception e)
149+
{
150+
Log.Error("LanguageTranslatorService.OnListLanguagesResponse()", "Exception: {0}", e.ToString());
151+
resp.Success = false;
152+
}
153+
154+
if (((RequestObject<Languages>)req).Callback != null)
155+
((RequestObject<Languages>)req).Callback(response, resp.Error);
156+
}
91157
/// <summary>
92158
/// Translate.
93159
///
94-
/// Translates the input text from the source language to the target language. A target language or translation
95-
/// model ID is required. The service attempts to detect the language of the source text if it is not specified.
160+
/// Translates the input text from the source language to the target language. Specify a model ID that indicates
161+
/// the source and target languages, or specify the source and target languages individually. You can omit the
162+
/// source language to have the service attempt to detect the language from the input text. If you omit the
163+
/// source language, the request must contain sufficient input text for the service to identify the source
164+
/// language.
96165
/// </summary>
97166
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
98-
/// <param name="text">Input text in UTF-8 encoding. Multiple entries will result in multiple translations in
99-
/// the response.</param>
100-
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
101-
/// model for English to German translation. A model ID overrides the source and target parameters and is
102-
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
103-
/// (optional)</param>
104-
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
167+
/// <param name="text">Input text in UTF-8 encoding. Multiple entries result in multiple translations in the
168+
/// response.</param>
169+
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM-provided base
170+
/// model for English-to-German translation. A model ID overrides the `source` and `target` parameters and is
171+
/// required if you use a custom model. If no model ID is specified, you must specify at least a target
172+
/// language. (optional)</param>
173+
/// <param name="source">Language code that specifies the language of the input text. If omitted, the service
174+
/// derives the source language from the input text. The input must contain sufficient text for the service to
175+
/// identify the language reliably. (optional)</param>
105176
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
106177
/// is not specified. (optional)</param>
107178
/// <returns><see cref="TranslationResult" />TranslationResult</returns>
@@ -323,10 +394,10 @@ private void OnIdentifyResponse(RESTConnector.Request req, RESTConnector.Respons
323394
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
324395
/// <param name="source">Specify a language code to filter results by source language. (optional)</param>
325396
/// <param name="target">Specify a language code to filter results by target language. (optional)</param>
326-
/// <param name="_default">If the default parameter isn't specified, the service will return all models (default
327-
/// and non-default) for each language pair. To return only default models, set this to `true`. To return only
328-
/// non-default models, set this to `false`. There is exactly one default model per language pair, the IBM
329-
/// provided base model. (optional)</param>
397+
/// <param name="_default">If the `default` parameter isn't specified, the service returns all models (default
398+
/// and non-default) for each language pair. To return only default models, set this parameter to `true`. To
399+
/// return only non-default models, set this parameter to `false`. There is exactly one default model, the
400+
/// IBM-provided base model, per language pair. (optional)</param>
330401
/// <returns><see cref="TranslationModels" />TranslationModels</returns>
331402
public bool ListModels(Callback<TranslationModels> callback, string source = null, string target = null, bool? _default = null)
332403
{
@@ -401,34 +472,93 @@ private void OnListModelsResponse(RESTConnector.Request req, RESTConnector.Respo
401472
/// <summary>
402473
/// Create model.
403474
///
404-
/// Uploads Translation Memory eXchange (TMX) files to customize a translation model.
475+
/// Uploads training files to customize a translation model. You can customize a model with a forced glossary or
476+
/// with a parallel corpus:
477+
/// * Use a *forced glossary* to force certain terms and phrases to be translated in a specific way. You can
478+
/// upload only a single forced glossary file for a model. The size of a forced glossary file for a custom model
479+
/// is limited to 10 MB.
480+
/// * Use a *parallel corpus* when you want your custom model to learn from general translation patterns in
481+
/// parallel sentences in your samples. What your model learns from a parallel corpus can improve translation
482+
/// results for input text that the model has not been trained on. You can upload multiple parallel corpora
483+
/// files with a request. To successfully train with parallel corpora, the corpora files must contain a
484+
/// cumulative total of at least 5000 parallel sentences. The cumulative size of all uploaded corpus files for a
485+
/// custom model is limited to 250 MB.
486+
///
487+
/// Depending on the type of customization and the size of the uploaded files, training time can range from
488+
/// minutes for a glossary to several hours for a large parallel corpus. To create a model that is customized
489+
/// with a parallel corpus and a forced glossary, customize the model with a parallel corpus first and then
490+
/// customize the resulting model with a forced glossary.
491+
///
492+
/// You can create a maximum of 10 custom models per language pair. For more information about customizing a
493+
/// translation model, including the formatting and character restrictions for data files, see [Customizing your
494+
/// model](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing).
495+
///
496+
/// #### Supported file formats
497+
///
498+
/// You can provide your training data for customization in the following document formats:
499+
/// * **TMX** (`.tmx`) - Translation Memory eXchange (TMX) is an XML specification for the exchange of
500+
/// translation memories.
501+
/// * **XLIFF** (`.xliff`) - XML Localization Interchange File Format (XLIFF) is an XML specification for the
502+
/// exchange of translation memories.
503+
/// * **CSV** (`.csv`) - Comma-separated values (CSV) file with two columns for aligned sentences and phrases.
504+
/// The first row contains the language code.
505+
/// * **TSV** (`.tsv` or `.tab`) - Tab-separated values (TSV) file with two columns for aligned sentences and
506+
/// phrases. The first row contains the language code.
507+
/// * **JSON** (`.json`) - Custom JSON format for specifying aligned sentences and phrases.
508+
/// * **Microsoft Excel** (`.xls` or `.xlsx`) - Excel file with the first two columns for aligned sentences and
509+
/// phrases. The first row contains the language code.
510+
///
511+
/// You must encode all text data in UTF-8 format. For more information, see [Supported document formats for
512+
/// training
513+
/// data](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing#supported-document-formats-for-training-data).
514+
///
405515
///
406-
/// You can either customize a model with a forced glossary or with a corpus that contains parallel sentences.
407-
/// To create a model that is customized with a parallel corpus <b>and</b> a forced glossary, proceed in two
408-
/// steps: customize with a parallel corpus first and then customize the resulting model with a glossary.
409-
/// Depending on the type of customization and the size of the uploaded corpora, training can range from minutes
410-
/// for a glossary to several hours for a large parallel corpus. You can upload a single forced glossary file
411-
/// and this file must be less than <b>10 MB</b>. You can upload multiple parallel corpora tmx files. The
412-
/// cumulative file size of all uploaded files is limited to <b>250 MB</b>. To successfully train with a
413-
/// parallel corpus you must have at least <b>5,000 parallel sentences</b> in your corpus.
516+
/// #### Specifying file formats
414517
///
415-
/// You can have a <b>maximum of 10 custom models per language pair</b>.
518+
/// You can indicate the format of a file by including the file extension with the file name. Use the file
519+
/// extensions shown in **Supported file formats**.
520+
///
521+
/// Alternatively, you can omit the file extension and specify one of the following `content-type`
522+
/// specifications for the file:
523+
/// * **TMX** - `application/x-tmx+xml`
524+
/// * **XLIFF** - `application/xliff+xml`
525+
/// * **CSV** - `text/csv`
526+
/// * **TSV** - `text/tab-separated-values`
527+
/// * **JSON** - `application/json`
528+
/// * **Microsoft Excel** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`
529+
///
530+
/// For example, with `curl`, use the following `content-type` specification to indicate the format of a CSV
531+
/// file named **glossary**:
532+
///
533+
/// `--form "forced_glossary=@glossary;type=text/csv"`.
416534
/// </summary>
417535
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
418-
/// <param name="baseModelId">The model ID of the model to use as the base for customization. To see available
419-
/// models, use the `List models` method. Usually all IBM provided models are customizable. In addition, all
420-
/// your models that have been created via parallel corpus customization, can be further customized with a
421-
/// forced glossary.</param>
422-
/// <param name="forcedGlossary">A TMX file with your customizations. The customizations in the file completely
423-
/// overwrite the domain translaton data, including high frequency or high confidence phrase translations. You
424-
/// can upload only one glossary with a file size less than 10 MB per call. A forced glossary should contain
425-
/// single words or short phrases. (optional)</param>
426-
/// <param name="parallelCorpus">A TMX file with parallel sentences for source and target language. You can
427-
/// upload multiple parallel_corpus files in one request. All uploaded parallel_corpus files combined, your
428-
/// parallel corpus must contain at least 5,000 parallel sentences to train successfully. (optional)</param>
536+
/// <param name="baseModelId">The ID of the translation model to use as the base for customization. To see
537+
/// available models and IDs, use the `List models` method. Most models that are provided with the service are
538+
/// customizable. In addition, all models that you create with parallel corpora customization can be further
539+
/// customized with a forced glossary.</param>
540+
/// <param name="forcedGlossary">A file with forced glossary terms for the source and target languages. The
541+
/// customizations in the file completely overwrite the domain translation data, including high frequency or
542+
/// high confidence phrase translations.
543+
///
544+
/// You can upload only one glossary file for a custom model, and the glossary can have a maximum size of 10 MB.
545+
/// A forced glossary must contain single words or short phrases. For more information, see **Supported file
546+
/// formats** in the method description.
547+
///
548+
/// *With `curl`, use `--form forced_glossary=@{filename}`.*. (optional)</param>
549+
/// <param name="parallelCorpus">A file with parallel sentences for the source and target languages. You can
550+
/// upload multiple parallel corpus files in one request by repeating the parameter. All uploaded parallel
551+
/// corpus files combined must contain at least 5000 parallel sentences to train successfully. You can provide a
552+
/// maximum of 500,000 parallel sentences across all corpora.
553+
///
554+
/// A single entry in a corpus file can contain a maximum of 80 words. All corpora files for a custom model can
555+
/// have a cumulative maximum size of 250 MB. For more information, see **Supported file formats** in the method
556+
/// description.
557+
///
558+
/// *With `curl`, use `--form parallel_corpus=@{filename}`.*. (optional)</param>
429559
/// <param name="name">An optional model name that you can use to identify the model. Valid characters are
430-
/// letters, numbers, dashes, underscores, spaces and apostrophes. The maximum length is 32 characters.
431-
/// (optional)</param>
560+
/// letters, numbers, dashes, underscores, spaces, and apostrophes. The maximum length of the name is 32
561+
/// characters. (optional)</param>
432562
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
433563
public bool CreateModel(Callback<TranslationModel> callback, string baseModelId, System.IO.MemoryStream forcedGlossary = null, System.IO.MemoryStream parallelCorpus = null, string name = null)
434564
{
@@ -579,7 +709,7 @@ private void OnDeleteModelResponse(RESTConnector.Request req, RESTConnector.Resp
579709
/// Get model details.
580710
///
581711
/// Gets information about a translation model, including training status for custom models. Use this API call
582-
/// to poll the status of your customization request. A successfully completed training will have a status of
712+
/// to poll the status of your customization request. A successfully completed training has a status of
583713
/// `available`.
584714
/// </summary>
585715
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
@@ -725,11 +855,13 @@ private void OnListDocumentsResponse(RESTConnector.Request req, RESTConnector.Re
725855
/// Maximum file size: **20 MB**.</param>
726856
/// <param name="filename">The filename for file.</param>
727857
/// <param name="fileContentType">The content type of file. (optional)</param>
728-
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
729-
/// model for English to German translation. A model ID overrides the source and target parameters and is
730-
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
731-
/// (optional)</param>
732-
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
858+
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM-provided base
859+
/// model for English-to-German translation. A model ID overrides the `source` and `target` parameters and is
860+
/// required if you use a custom model. If no model ID is specified, you must specify at least a target
861+
/// language. (optional)</param>
862+
/// <param name="source">Language code that specifies the language of the source document. If omitted, the
863+
/// service derives the source language from the input text. The input must contain sufficient text for the
864+
/// service to identify the language reliably. (optional)</param>
733865
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
734866
/// is not specified. (optional)</param>
735867
/// <param name="documentId">To use a previously submitted document as the source for a new translation, enter

0 commit comments

Comments
 (0)