Class: IBMWatson::LanguageTranslatorV3
- Inherits:
-
IBMCloudSdkCore::BaseService
- Object
- IBMCloudSdkCore::BaseService
- IBMWatson::LanguageTranslatorV3
- Includes:
- Concurrent::Async
- Defined in:
- lib/ibm_watson/language_translator_v3.rb
Overview
The Language Translator V3 service.
Constant Summary collapse
- DEFAULT_SERVICE_NAME =
"language_translator"
- DEFAULT_SERVICE_URL =
"https://api.us-south.language-translator.watson.cloud.ibm.com"
Instance Attribute Summary collapse
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#create_model(base_model_id: , forced_glossary: nil, parallel_corpus: nil, name: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Create model.
-
#delete_document(document_id: ) ⇒ nil
Delete document.
-
#delete_model(model_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
Delete model.
-
#get_document_status(document_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
Get document status.
-
#get_model(model_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
Get model details.
-
#get_translated_document(document_id: , accept: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Get translated document.
-
#identify(text: ) ⇒ IBMCloudSdkCore::DetailedResponse
Identify language.
-
#initialize(args) ⇒ LanguageTranslatorV3
constructor
Construct a new client for the Language Translator service.
-
#list_documents ⇒ IBMCloudSdkCore::DetailedResponse
List documents.
-
#list_identifiable_languages ⇒ IBMCloudSdkCore::DetailedResponse
List identifiable languages.
-
#list_languages ⇒ IBMCloudSdkCore::DetailedResponse
List supported languages.
-
#list_models(source: nil, target: nil, default: nil) ⇒ IBMCloudSdkCore::DetailedResponse
List models.
-
#translate(text: , model_id: nil, source: nil, target: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Translate.
-
#translate_document(file: , filename: nil, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Translate document.
Constructor Details
#initialize(args) ⇒ LanguageTranslatorV3
Construct a new client for the Language Translator service.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 51 def initialize(args = {}) @__async_initialized__ = false defaults = {} defaults[:service_url] = DEFAULT_SERVICE_URL defaults[:service_name] = DEFAULT_SERVICE_NAME defaults[:authenticator] = nil defaults[:version] = nil user_service_url = args[:service_url] unless args[:service_url].nil? args = defaults.merge(args) @version = args[:version] raise ArgumentError.new("version must be provided") if @version.nil? args[:authenticator] = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: args[:service_name]) if args[:authenticator].nil? super @service_url = user_service_url unless user_service_url.nil? end |
Instance Attribute Details
#version ⇒ Object
Returns the value of attribute version.
38 39 40 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 38 def version @version end |
Instance Method Details
#create_model(base_model_id: , forced_glossary: nil, parallel_corpus: nil, name: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Create 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 must have two language codes. The
first column is for the source language code, and the second column is for the
target language code.
* **TSV** (`.tsv` or `.tab`) - Tab-separated values (TSV) file with two columns
for aligned sentences and phrases. The first row must have two language codes. The
first column is for the source language code, and the second column is for the
target 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).
#### Specifying file formats
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"`.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 382 def create_model(base_model_id:, forced_glossary: nil, parallel_corpus: nil, name: nil) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("base_model_id must be provided") if base_model_id.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "create_model") headers.merge!(sdk_headers) params = { "version" => @version, "base_model_id" => base_model_id, "name" => name } form_data = {} unless forced_glossary.nil? unless forced_glossary.instance_of?(StringIO) || forced_glossary.instance_of?(File) forced_glossary = forced_glossary.respond_to?(:to_json) ? StringIO.new(forced_glossary.to_json) : StringIO.new(forced_glossary) end form_data[:forced_glossary] = HTTP::FormData::File.new(forced_glossary, content_type: "application/octet-stream", filename: forced_glossary.respond_to?(:path) ? forced_glossary.path : nil) end unless parallel_corpus.nil? unless parallel_corpus.instance_of?(StringIO) || parallel_corpus.instance_of?(File) parallel_corpus = parallel_corpus.respond_to?(:to_json) ? StringIO.new(parallel_corpus.to_json) : StringIO.new(parallel_corpus) end form_data[:parallel_corpus] = HTTP::FormData::File.new(parallel_corpus, content_type: "application/octet-stream", filename: parallel_corpus.respond_to?(:path) ? parallel_corpus.path : nil) end method_url = "/v3/models" response = request( method: "POST", url: method_url, headers: headers, params: params, form: form_data, accept_json: true ) response end |
#delete_document(document_id: ) ⇒ nil
Delete document. Deletes a document.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 633 def delete_document(document_id:) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("document_id must be provided") if document_id.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "delete_document") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/documents/%s" % [ERB::Util.url_encode(document_id)] request( method: "DELETE", url: method_url, headers: headers, params: params, accept_json: false ) nil end |
#delete_model(model_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
Delete model. Deletes a custom translation model.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 433 def delete_model(model_id:) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("model_id must be provided") if model_id.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "delete_model") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/models/%s" % [ERB::Util.url_encode(model_id)] response = request( method: "DELETE", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#get_document_status(document_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
Get document status. Gets the translation status of a document.
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 601 def get_document_status(document_id:) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("document_id must be provided") if document_id.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "get_document_status") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/documents/%s" % [ERB::Util.url_encode(document_id)] response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#get_model(model_id: ) ⇒ IBMCloudSdkCore::DetailedResponse
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 has a status of `available`.
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 467 def get_model(model_id:) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("model_id must be provided") if model_id.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "get_model") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/models/%s" % [ERB::Util.url_encode(model_id)] response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#get_translated_document(document_id: , accept: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Get translated document. Gets the translated document associated with the given document ID.
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 678 def get_translated_document(document_id:, accept: nil) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("document_id must be provided") if document_id.nil? headers = { "Accept" => accept } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "get_translated_document") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/documents/%s/translated_document" % [ERB::Util.url_encode(document_id)] response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#identify(text: ) ⇒ IBMCloudSdkCore::DetailedResponse
Identify language. Identifies the language of the input text.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 207 def identify(text:) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("text must be provided") if text.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "identify") headers.merge!(sdk_headers) params = { "version" => @version } data = text headers["Content-Type"] = "text/plain" method_url = "/v3/identify" response = request( method: "POST", url: method_url, headers: headers, params: params, data: data, accept_json: true ) response end |
#list_documents ⇒ IBMCloudSdkCore::DetailedResponse
List documents. Lists documents that have been submitted for translation.
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 501 def list_documents raise ArgumentError.new("version must be provided") if version.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "list_documents") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/documents" response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#list_identifiable_languages ⇒ IBMCloudSdkCore::DetailedResponse
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.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 177 def list_identifiable_languages raise ArgumentError.new("version must be provided") if version.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "list_identifiable_languages") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/identifiable_languages" response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#list_languages ⇒ IBMCloudSdkCore::DetailedResponse
List supported languages. Lists all supported languages for translation. 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`). In addition to
basic information about each language, the response indicates whether the language
is `supported_as_source` for translation and `supported_as_target` for
translation. It also lists whether the language is `identifiable`.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 82 def list_languages raise ArgumentError.new("version must be provided") if version.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "list_languages") headers.merge!(sdk_headers) params = { "version" => @version } method_url = "/v3/languages" response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#list_models(source: nil, target: nil, default: nil) ⇒ IBMCloudSdkCore::DetailedResponse
List models. Lists available translation models.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 252 def list_models(source: nil, target: nil, default: nil) raise ArgumentError.new("version must be provided") if version.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "list_models") headers.merge!(sdk_headers) params = { "version" => @version, "source" => source, "target" => target, "default" => default } method_url = "/v3/models" response = request( method: "GET", url: method_url, headers: headers, params: params, accept_json: true ) response end |
#translate(text: , model_id: nil, source: nil, target: nil) ⇒ IBMCloudSdkCore::DetailedResponse
Translate. 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.
You can translate a maximum of 50 KB (51,200 bytes) of text with a single request.
All input text must be encoded in UTF-8 format.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 134 def translate(text:, model_id: nil, source: nil, target: nil) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("text must be provided") if text.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "translate") headers.merge!(sdk_headers) params = { "version" => @version } data = { "text" => text, "model_id" => model_id, "source" => source, "target" => target } method_url = "/v3/translate" response = request( method: "POST", url: method_url, headers: headers, params: params, json: data, accept_json: true ) response end |
#translate_document(file: , filename: nil, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil) ⇒ IBMCloudSdkCore::DetailedResponse
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. The maximum file size for document translation is
* 20 MB for service instances on the Standard, Advanced, and Premium plans
* 2 MB for service instances on the Lite plan.
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/ibm_watson/language_translator_v3.rb', line 552 def translate_document(file:, filename: nil, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil) raise ArgumentError.new("version must be provided") if version.nil? raise ArgumentError.new("file must be provided") if file.nil? headers = { } sdk_headers = Common.new.get_sdk_headers("language_translator", "V3", "translate_document") headers.merge!(sdk_headers) params = { "version" => @version } form_data = {} unless file.instance_of?(StringIO) || file.instance_of?(File) file = file.respond_to?(:to_json) ? StringIO.new(file.to_json) : StringIO.new(file) end filename = file.path if filename.nil? && file.respond_to?(:path) form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: filename) form_data[:model_id] = HTTP::FormData::Part.new(model_id.to_s, content_type: "text/plain") unless model_id.nil? form_data[:source] = HTTP::FormData::Part.new(source.to_s, content_type: "text/plain") unless source.nil? form_data[:target] = HTTP::FormData::Part.new(target.to_s, content_type: "text/plain") unless target.nil? form_data[:document_id] = HTTP::FormData::Part.new(document_id.to_s, content_type: "text/plain") unless document_id.nil? method_url = "/v3/documents" response = request( method: "POST", url: method_url, headers: headers, params: params, form: form_data, accept_json: true ) response end |