Skip to content

Commit 24d261d

Browse files
committed
refactor(LanguageTranslator): add filename to translate_document and add tests for LT
1 parent d597080 commit 24d261d

File tree

3 files changed

+241
-5
lines changed

3 files changed

+241
-5
lines changed

lib/ibm_watson/language_translator_v3.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def list_documents
411411
end
412412

413413
##
414-
# @!method translate_document(file:, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil)
414+
# @!method translate_document(file:, filename: nil, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil)
415415
# Translate document.
416416
# Submit a document for translation. You can submit the document contents in the
417417
# `file` parameter, or you can reference a previously submitted document by document
@@ -422,6 +422,7 @@ def list_documents
422422
# types](https://cloud.ibm.com/docs/services/language-translator?topic=language-translator-document-translator-tutorial#supported-file-formats)
423423
#
424424
# Maximum file size: **20 MB**.
425+
# @param filename [String] The filename for file.
425426
# @param file_content_type [String] The content type of file.
426427
# @param model_id [String] The model to use for translation. `model_id` or both `source` and `target` are
427428
# required.
@@ -430,7 +431,7 @@ def list_documents
430431
# @param document_id [String] To use a previously submitted document as the source for a new translation, enter
431432
# the `document_id` of the document.
432433
# @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
433-
def translate_document(file:, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil)
434+
def translate_document(file:, filename: nil, file_content_type: nil, model_id: nil, source: nil, target: nil, document_id: nil)
434435
raise ArgumentError.new("file must be provided") if file.nil?
435436

436437
headers = {
@@ -447,7 +448,8 @@ def translate_document(file:, file_content_type: nil, model_id: nil, source: nil
447448
unless file.instance_of?(StringIO) || file.instance_of?(File)
448449
file = file.respond_to?(:to_json) ? StringIO.new(file.to_json) : StringIO.new(file)
449450
end
450-
form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: file.respond_to?(:path) ? file.path : nil)
451+
filename = file.path if filename.nil? && file.respond_to?(:path)
452+
form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: filename)
451453

452454
form_data[:model_id] = HTTP::FormData::Part.new(model_id.to_s, content_type: "text/plain") unless model_id.nil?
453455

lib/ibm_watson/speech_to_text_v1.rb

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,45 @@ def recognize(audio:, model: nil, language_customization_id: nil, acoustic_custo
458458
# @param profanity_filter [Boolean] If `true` (the default), filters profanity from all output except for keyword results by replacing inappropriate words with a series of asterisks. Set the parameter to `false` to return results with no censoring. Applies to US English transcription only.
459459
# @param smart_formatting [Boolean] If `true`, converts dates, times, series of digits and numbers, phone numbers, currency values, and Internet addresses into more readable, conventional representations in the final transcript of a recognition request. If `false` (the default), no formatting is performed. Applies to US English transcription only.
460460
# @param speaker_labels [Boolean] Indicates whether labels that identify which words were spoken by which participants in a multi-person exchange are to be included in the response. The default is `false`; no speaker labels are returned. Setting `speaker_labels` to `true` forces the `timestamps` parameter to be `true`, regardless of whether you specify `false` for the parameter. To determine whether a language model supports speaker labels, use the `GET /v1/models` method and check that the attribute `speaker_labels` is set to `true`. You can also refer to [Speaker labels](https://console.bluemix.net/docs/services/speech-to-text/output.html#speaker_labels).
461+
# @param grammar_name [String] The name of a grammar that is to be used with the recognition request. If you
462+
# specify a grammar, you must also use the `language_customization_id` parameter to
463+
# specify the name of the custom language model for which the grammar is defined.
464+
# The service recognizes only strings that are recognized by the specified grammar;
465+
# it does not recognize other custom words from the model's words resource. See
466+
# [Grammars](https://cloud.ibm.com/docs/services/speech-to-text/output.html).
467+
# @param redaction [Boolean] If `true`, the service redacts, or masks, numeric data from final transcripts. The
468+
# feature redacts any number that has three or more consecutive digits by replacing
469+
# each digit with an `X` character. It is intended to redact sensitive numeric data,
470+
# such as credit card numbers. By default, the service performs no redaction.
471+
#
472+
# When you enable redaction, the service automatically enables smart formatting,
473+
# regardless of whether you explicitly disable that feature. To ensure maximum
474+
# security, the service also disables keyword spotting (ignores the `keywords` and
475+
# `keywords_threshold` parameters) and returns only a single final transcript
476+
# (forces the `max_alternatives` parameter to be `1`).
477+
#
478+
# **Note:** Applies to US English, Japanese, and Korean transcription only.
479+
#
480+
# See [Numeric
481+
# redaction](https://cloud.ibm.com/docs/services/speech-to-text/output.html#redaction).
482+
#
483+
# @param processing_metrics [Boolean] If `true`, requests processing metrics about the service's transcription of the
484+
# input audio. The service returns processing metrics at the interval specified by
485+
# the `processing_metrics_interval` parameter. It also returns processing metrics
486+
# for transcription events, for example, for final and interim results. By default,
487+
# the service returns no processing metrics.
488+
# @param processing_metrics_interval [Float] Specifies the interval in real wall-clock seconds at which the service is to
489+
# return processing metrics. The parameter is ignored unless the
490+
# `processing_metrics` parameter is set to `true`. # The parameter accepts a minimum value of 0.1 seconds. The level of precision is
491+
# not restricted, so you can specify values such as 0.25 and 0.125.
492+
#
493+
# The service does not impose a maximum value. If you want to receive processing
494+
# metrics only for transcription events instead of at periodic intervals, set the
495+
# value to a large number. If the value is larger than the duration of the audio,
496+
# the service returns processing metrics only for transcription events.
497+
# @param audio_metrics [Boolean] If `true`, requests detailed information about the signal characteristics of the
498+
# input audio. The service returns audio metrics with the final transcription
499+
# results. By default, the service returns no audio metrics.
461500
# @return [WebSocketClient] Returns a new WebSocketClient object
462501
def recognize_using_websocket(
463502
content_type:,
@@ -479,7 +518,12 @@ def recognize_using_websocket(
479518
timestamps: nil,
480519
profanity_filter: nil,
481520
smart_formatting: nil,
482-
speaker_labels: nil
521+
speaker_labels: nil,
522+
grammar_name: nil,
523+
redaction: nil,
524+
processing_metrics: nil,
525+
processing_metrics_interval: nil,
526+
audio_metrics: nil
483527
)
484528
raise ArgumentError("Audio must be provided") if audio.nil? && !chunk_data
485529
raise ArgumentError("Recognize callback must be provided") if recognize_callback.nil?
@@ -516,7 +560,12 @@ def recognize_using_websocket(
516560
"timestamps" => timestamps,
517561
"profanity_filter" => profanity_filter,
518562
"smart_formatting" => smart_formatting,
519-
"speaker_labels" => speaker_labels
563+
"speaker_labels" => speaker_labels,
564+
"grammar_name" => grammar_name,
565+
"redaction" => redaction,
566+
"processing_metrics" => processing_metrics,
567+
"processing_metrics_interval" => processing_metrics_interval,
568+
"audio_metrics" => audio_metrics
520569
}
521570
options.delete_if { |_, v| v.nil? }
522571
WebSocketClient.new(audio: audio, chunk_data: chunk_data, options: options, recognize_callback: recognize_callback, url: url, headers: headers)

test/unit/test_language_translator_v3.rb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,189 @@ def test_list_models
456456
service_response = service.list_models
457457
assert_equal(expected, service_response.result)
458458
end
459+
460+
def test_lis_documents
461+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
462+
.with(
463+
body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
464+
headers: {
465+
"Accept" => "application/json",
466+
"Authorization" => "Basic Yng6Yng=",
467+
"Content-Type" => "application/x-www-form-urlencoded",
468+
"Host" => "iam.cloud.ibm.com"
469+
}
470+
).to_return(
471+
status: 200,
472+
body: {
473+
"access_token" => "oAeisG8yqPY7sFR_x66Z15",
474+
"token_type" => "Bearer",
475+
"expires_in" => 3600,
476+
"expiration" => 1_524_167_011,
477+
"refresh_token" => "jy4gl91BQ"
478+
}.to_json,
479+
headers: {}
480+
)
481+
service = IBMWatson::LanguageTranslatorV3.new(
482+
version: "2018-05-01",
483+
iam_apikey: "iam_apikey"
484+
)
485+
expected = {}
486+
stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/documents?version=2018-05-01")
487+
.with(
488+
headers: {
489+
"Accept" => "application/json",
490+
"Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
491+
"Host" => "gateway.watsonplatform.net"
492+
}
493+
).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
494+
service_response = service.list_documents
495+
assert_equal(expected, service_response.result)
496+
end
497+
498+
def test_translate_document
499+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
500+
.with(
501+
body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
502+
headers: {
503+
"Accept" => "application/json",
504+
"Authorization" => "Basic Yng6Yng=",
505+
"Content-Type" => "application/x-www-form-urlencoded",
506+
"Host" => "iam.cloud.ibm.com"
507+
}
508+
).to_return(
509+
status: 200,
510+
body: {
511+
"access_token" => "oAeisG8yqPY7sFR_x66Z15",
512+
"token_type" => "Bearer",
513+
"expires_in" => 3600,
514+
"expiration" => 1_524_167_011,
515+
"refresh_token" => "jy4gl91BQ"
516+
}.to_json,
517+
headers: {}
518+
)
519+
service = IBMWatson::LanguageTranslatorV3.new(
520+
version: "2018-05-01",
521+
iam_apikey: "iam_apikey"
522+
)
523+
stub_request(:post, "https://gateway.watsonplatform.net/language-translator/api/v3/documents?version=2018-05-01")
524+
.with do |req|
525+
assert_equal req.headers["Accept"], "application/json"
526+
assert_match %r{\Amultipart/form-data}, req.headers["Content-Type"]
527+
end
528+
file = File.open(Dir.getwd + "/resources/cnc_test.pdf")
529+
service.translate_document(file: file, filename: "file")
530+
end
531+
532+
def test_get_document_status
533+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
534+
.with(
535+
body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
536+
headers: {
537+
"Accept" => "application/json",
538+
"Authorization" => "Basic Yng6Yng=",
539+
"Content-Type" => "application/x-www-form-urlencoded",
540+
"Host" => "iam.cloud.ibm.com"
541+
}
542+
).to_return(
543+
status: 200,
544+
body: {
545+
"access_token" => "oAeisG8yqPY7sFR_x66Z15",
546+
"token_type" => "Bearer",
547+
"expires_in" => 3600,
548+
"expiration" => 1_524_167_011,
549+
"refresh_token" => "jy4gl91BQ"
550+
}.to_json,
551+
headers: {}
552+
)
553+
service = IBMWatson::LanguageTranslatorV3.new(
554+
version: "2018-05-01",
555+
iam_apikey: "iam_apikey"
556+
)
557+
expected = {}
558+
stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/documents/id?version=2018-05-01")
559+
.with(
560+
headers: {
561+
"Accept" => "application/json",
562+
"Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
563+
"Host" => "gateway.watsonplatform.net"
564+
}
565+
).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
566+
service_response = service.get_document_status(document_id: "id")
567+
assert_equal(expected, service_response.result)
568+
end
569+
570+
def test_get_translated_document
571+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
572+
.with(
573+
body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
574+
headers: {
575+
"Accept" => "application/json",
576+
"Authorization" => "Basic Yng6Yng=",
577+
"Content-Type" => "application/x-www-form-urlencoded",
578+
"Host" => "iam.cloud.ibm.com"
579+
}
580+
).to_return(
581+
status: 200,
582+
body: {
583+
"access_token" => "oAeisG8yqPY7sFR_x66Z15",
584+
"token_type" => "Bearer",
585+
"expires_in" => 3600,
586+
"expiration" => 1_524_167_011,
587+
"refresh_token" => "jy4gl91BQ"
588+
}.to_json,
589+
headers: {}
590+
)
591+
service = IBMWatson::LanguageTranslatorV3.new(
592+
version: "2018-05-01",
593+
iam_apikey: "iam_apikey"
594+
)
595+
expected = {}
596+
stub_request(:get, "https://gateway.watsonplatform.net/language-translator/api/v3/documents/id/translated_document?version=2018-05-01")
597+
.with(
598+
headers: {
599+
"Accept" => "application/json",
600+
"Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
601+
"Host" => "gateway.watsonplatform.net"
602+
}
603+
).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
604+
service_response = service.get_translated_document(document_id: "id")
605+
assert_equal(expected, service_response.result)
606+
end
607+
608+
def test_delete_document
609+
stub_request(:post, "https://iam.cloud.ibm.com/identity/token")
610+
.with(
611+
body: { "apikey" => "iam_apikey", "grant_type" => "urn:ibm:params:oauth:grant-type:apikey", "response_type" => "cloud_iam" },
612+
headers: {
613+
"Accept" => "application/json",
614+
"Authorization" => "Basic Yng6Yng=",
615+
"Content-Type" => "application/x-www-form-urlencoded",
616+
"Host" => "iam.cloud.ibm.com"
617+
}
618+
).to_return(
619+
status: 200,
620+
body: {
621+
"access_token" => "oAeisG8yqPY7sFR_x66Z15",
622+
"token_type" => "Bearer",
623+
"expires_in" => 3600,
624+
"expiration" => 1_524_167_011,
625+
"refresh_token" => "jy4gl91BQ"
626+
}.to_json,
627+
headers: {}
628+
)
629+
service = IBMWatson::LanguageTranslatorV3.new(
630+
version: "2018-05-01",
631+
iam_apikey: "iam_apikey"
632+
)
633+
expected = {}
634+
stub_request(:delete, "https://gateway.watsonplatform.net/language-translator/api/v3/documents/id?version=2018-05-01")
635+
.with(
636+
headers: {
637+
"Authorization" => "Bearer oAeisG8yqPY7sFR_x66Z15",
638+
"Host" => "gateway.watsonplatform.net"
639+
}
640+
).to_return(status: 200, body: expected.to_json, headers: { "Content-Type" => "application/json" })
641+
service_response = service.delete_document(document_id: "id")
642+
assert_nil(service_response)
643+
end
459644
end

0 commit comments

Comments
 (0)