Skip to content

Commit

Permalink
feat(LanguageTranslatorV3): add support for ListLanguages
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Aug 12, 2020
1 parent 369e7fd commit 0570cdb
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 45 deletions.
218 changes: 175 additions & 43 deletions Scripts/Services/LanguageTranslator/V3/LanguageTranslatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace IBM.Watson.LanguageTranslator.V3
public partial class LanguageTranslatorService : BaseService
{
private const string serviceId = "language_translator";
private const string defaultServiceUrl = "https://gateway.watsonplatform.net/language-translator/api";
private const string defaultServiceUrl = "https://api.us-south.language-translator.watson.cloud.ibm.com";

#region VersionDate
private string versionDate;
Expand Down Expand Up @@ -88,20 +88,91 @@ public LanguageTranslatorService(string versionDate, Authenticator authenticator
}
}

/// <summary>
/// List supported languages.
///
/// Lists all supported languages. The method returns an array of supported languages with information about
/// each language. Languages are listed in alphabetical order by language code (for example, `af`, `ar`).
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <returns><see cref="Languages" />Languages</returns>
public bool ListLanguages(Callback<Languages> callback)
{
if (callback == null)
throw new ArgumentNullException("`callback` is required for `ListLanguages`");

RequestObject<Languages> req = new RequestObject<Languages>
{
Callback = callback,
HttpMethod = UnityWebRequest.kHttpVerbGET,
DisableSslVerification = DisableSslVerification
};

foreach (KeyValuePair<string, string> kvp in customRequestHeaders)
{
req.Headers.Add(kvp.Key, kvp.Value);
}

ClearCustomRequestHeaders();

foreach (KeyValuePair<string, string> kvp in Common.GetSdkHeaders("language_translator", "V3", "ListLanguages"))
{
req.Headers.Add(kvp.Key, kvp.Value);
}

req.Parameters["version"] = VersionDate;

req.OnResponse = OnListLanguagesResponse;

Connector.URL = GetServiceUrl() + "/v3/languages";
Authenticator.Authenticate(Connector);

return Connector.Send(req);
}

private void OnListLanguagesResponse(RESTConnector.Request req, RESTConnector.Response resp)
{
DetailedResponse<Languages> response = new DetailedResponse<Languages>();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;

try
{
string json = Encoding.UTF8.GetString(resp.Data);
response.Result = JsonConvert.DeserializeObject<Languages>(json);
response.Response = json;
}
catch (Exception e)
{
Log.Error("LanguageTranslatorService.OnListLanguagesResponse()", "Exception: {0}", e.ToString());
resp.Success = false;
}

if (((RequestObject<Languages>)req).Callback != null)
((RequestObject<Languages>)req).Callback(response, resp.Error);
}
/// <summary>
/// Translate.
///
/// Translates the input text from the source language to the target language. A target language or translation
/// model ID is required. The service attempts to detect the language of the source text if it is not specified.
/// Translates the input text from the source language to the target language. Specify a model ID that indicates
/// the source and target languages, or specify the source and target languages individually. You can omit the
/// source language to have the service attempt to detect the language from the input text. If you omit the
/// source language, the request must contain sufficient input text for the service to identify the source
/// language.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="text">Input text in UTF-8 encoding. Multiple entries will result in multiple translations in
/// the response.</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
/// model for English to German translation. A model ID overrides the source and target parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
/// (optional)</param>
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
/// <param name="text">Input text in UTF-8 encoding. Multiple entries result in multiple translations in the
/// response.</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM-provided base
/// model for English-to-German translation. A model ID overrides the `source` and `target` parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify at least a target
/// language. (optional)</param>
/// <param name="source">Language code that specifies the language of the input text. If omitted, the service
/// derives the source language from the input text. The input must contain sufficient text for the service to
/// identify the language reliably. (optional)</param>
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
/// is not specified. (optional)</param>
/// <returns><see cref="TranslationResult" />TranslationResult</returns>
Expand Down Expand Up @@ -323,10 +394,10 @@ private void OnIdentifyResponse(RESTConnector.Request req, RESTConnector.Respons
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="source">Specify a language code to filter results by source language. (optional)</param>
/// <param name="target">Specify a language code to filter results by target language. (optional)</param>
/// <param name="_default">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. (optional)</param>
/// <param name="_default">If the `default` parameter isn't specified, the service returns all models (default
/// and non-default) for each language pair. To return only default models, set this parameter to `true`. To
/// return only non-default models, set this parameter to `false`. There is exactly one default model, the
/// IBM-provided base model, per language pair. (optional)</param>
/// <returns><see cref="TranslationModels" />TranslationModels</returns>
public bool ListModels(Callback<TranslationModels> callback, string source = null, string target = null, bool? _default = null)
{
Expand Down Expand Up @@ -401,34 +472,93 @@ private void OnListModelsResponse(RESTConnector.Request req, RESTConnector.Respo
/// <summary>
/// Create model.
///
/// Uploads Translation Memory eXchange (TMX) files to customize a translation model.
/// Uploads training files to customize a translation model. You can customize a model with a forced glossary or
/// with a parallel corpus:
/// * Use a *forced glossary* to force certain terms and phrases to be translated in a specific way. You can
/// upload only a single forced glossary file for a model. The size of a forced glossary file for a custom model
/// is limited to 10 MB.
/// * Use a *parallel corpus* when you want your custom model to learn from general translation patterns in
/// parallel sentences in your samples. What your model learns from a parallel corpus can improve translation
/// results for input text that the model has not been trained on. You can upload multiple parallel corpora
/// files with a request. To successfully train with parallel corpora, the corpora files must contain a
/// cumulative total of at least 5000 parallel sentences. The cumulative size of all uploaded corpus files for a
/// custom model is limited to 250 MB.
///
/// Depending on the type of customization and the size of the uploaded files, training time can range from
/// minutes for a glossary to several hours for a large parallel corpus. To create a model that is customized
/// with a parallel corpus and a forced glossary, customize the model with a parallel corpus first and then
/// customize the resulting model with a forced glossary.
///
/// You can create a maximum of 10 custom models per language pair. For more information about customizing a
/// translation model, including the formatting and character restrictions for data files, see [Customizing your
/// model](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing).
///
/// #### Supported file formats
///
/// You can provide your training data for customization in the following document formats:
/// * **TMX** (`.tmx`) - Translation Memory eXchange (TMX) is an XML specification for the exchange of
/// translation memories.
/// * **XLIFF** (`.xliff`) - XML Localization Interchange File Format (XLIFF) is an XML specification for the
/// exchange of translation memories.
/// * **CSV** (`.csv`) - Comma-separated values (CSV) file with two columns for aligned sentences and phrases.
/// The first row contains the language code.
/// * **TSV** (`.tsv` or `.tab`) - Tab-separated values (TSV) file with two columns for aligned sentences and
/// phrases. The first row contains the language code.
/// * **JSON** (`.json`) - Custom JSON format for specifying aligned sentences and phrases.
/// * **Microsoft Excel** (`.xls` or `.xlsx`) - Excel file with the first two columns for aligned sentences and
/// phrases. The first row contains the language code.
///
/// You must encode all text data in UTF-8 format. For more information, see [Supported document formats for
/// training
/// data](https://cloud.ibm.com/docs/language-translator?topic=language-translator-customizing#supported-document-formats-for-training-data).
///
///
/// 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.
/// #### Specifying file formats
///
/// You can have a <b>maximum of 10 custom models per language pair</b>.
/// You can indicate the format of a file by including the file extension with the file name. Use the file
/// extensions shown in **Supported file formats**.
///
/// Alternatively, you can omit the file extension and specify one of the following `content-type`
/// specifications for the file:
/// * **TMX** - `application/x-tmx+xml`
/// * **XLIFF** - `application/xliff+xml`
/// * **CSV** - `text/csv`
/// * **TSV** - `text/tab-separated-values`
/// * **JSON** - `application/json`
/// * **Microsoft Excel** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`
///
/// For example, with `curl`, use the following `content-type` specification to indicate the format of a CSV
/// file named **glossary**:
///
/// `--form "forced_glossary=@glossary;type=text/csv"`.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
/// <param name="baseModelId">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.</param>
/// <param name="forcedGlossary">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. (optional)</param>
/// <param name="parallelCorpus">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. (optional)</param>
/// <param name="baseModelId">The ID of the translation model to use as the base for customization. To see
/// available models and IDs, use the `List models` method. Most models that are provided with the service are
/// customizable. In addition, all models that you create with parallel corpora customization can be further
/// customized with a forced glossary.</param>
/// <param name="forcedGlossary">A file with forced glossary terms for the source and target languages. The
/// customizations in the file completely overwrite the domain translation data, including high frequency or
/// high confidence phrase translations.
///
/// You can upload only one glossary file for a custom model, and the glossary can have a maximum size of 10 MB.
/// A forced glossary must contain single words or short phrases. For more information, see **Supported file
/// formats** in the method description.
///
/// *With `curl`, use `--form forced_glossary=@{filename}`.*. (optional)</param>
/// <param name="parallelCorpus">A file with parallel sentences for the source and target languages. You can
/// upload multiple parallel corpus files in one request by repeating the parameter. All uploaded parallel
/// corpus files combined must contain at least 5000 parallel sentences to train successfully. You can provide a
/// maximum of 500,000 parallel sentences across all corpora.
///
/// A single entry in a corpus file can contain a maximum of 80 words. All corpora files for a custom model can
/// have a cumulative maximum size of 250 MB. For more information, see **Supported file formats** in the method
/// description.
///
/// *With `curl`, use `--form parallel_corpus=@{filename}`.*. (optional)</param>
/// <param name="name">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.
/// (optional)</param>
/// letters, numbers, dashes, underscores, spaces, and apostrophes. The maximum length of the name is 32
/// characters. (optional)</param>
/// <returns><see cref="TranslationModel" />TranslationModel</returns>
public bool CreateModel(Callback<TranslationModel> callback, string baseModelId, System.IO.MemoryStream forcedGlossary = null, System.IO.MemoryStream parallelCorpus = null, string name = null)
{
Expand Down Expand Up @@ -579,7 +709,7 @@ private void OnDeleteModelResponse(RESTConnector.Request req, RESTConnector.Resp
/// 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
/// to poll the status of your customization request. A successfully completed training has a status of
/// `available`.
/// </summary>
/// <param name="callback">The callback function that is invoked when the operation completes.</param>
Expand Down Expand Up @@ -725,11 +855,13 @@ private void OnListDocumentsResponse(RESTConnector.Request req, RESTConnector.Re
/// Maximum file size: **20 MB**.</param>
/// <param name="filename">The filename for file.</param>
/// <param name="fileContentType">The content type of file. (optional)</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM provided base
/// model for English to German translation. A model ID overrides the source and target parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify a target language.
/// (optional)</param>
/// <param name="source">Language code that specifies the language of the source document. (optional)</param>
/// <param name="modelId">The model to use for translation. For example, `en-de` selects the IBM-provided base
/// model for English-to-German translation. A model ID overrides the `source` and `target` parameters and is
/// required if you use a custom model. If no model ID is specified, you must specify at least a target
/// language. (optional)</param>
/// <param name="source">Language code that specifies the language of the source document. If omitted, the
/// service derives the source language from the input text. The input must contain sufficient text for the
/// service to identify the language reliably. (optional)</param>
/// <param name="target">Language code that specifies the target language for translation. Required if model ID
/// is not specified. (optional)</param>
/// <param name="documentId">To use a previously submitted document as the source for a new translation, enter
Expand Down
Loading

0 comments on commit 0570cdb

Please sign in to comment.