Skip to content

Commit

Permalink
Add Translate model argument
Browse files Browse the repository at this point in the history
Add model optional argument to Api#translate, and Translation#model attribute.

[closes #1048]
  • Loading branch information
blowmage committed Nov 12, 2016
1 parent 436116c commit 16bea7f
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 30 deletions.
17 changes: 17 additions & 0 deletions google-cloud-translate/acceptance/translate/translate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
translations.last.text.must_equal "¿Cómo estás hoy?"
end

it "translates input with model attribute" do
translation = translate.translate "Hello", to: "es", model: ""
translation.text.must_include "Hola"
translation.model.must_be :nil?

translation = translate.translate "How are you today?", to: "es", model: "base"
translation.text.must_equal "¿Cómo estás hoy?"
translation.model.must_be :nil?

translations = translate.translate "Hello", "How are you today?", to: :es, model: :nmt
translations.count.must_equal 2
translations.first.text.must_include "Hola"
translations.first.model.must_equal "nmt"
translations.last.text.must_equal "¿Cómo estás hoy?"
translations.last.model.must_equal "nmt"
end

it "lists supported languages" do
languages = translate.languages
languages.count.must_be :>, 0
Expand Down
6 changes: 4 additions & 2 deletions google-cloud-translate/lib/google/cloud/translate/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def self.default_project
# @param [String] format The format of the text. Possible values include
# `:text` and `:html`. This is optional. The Translate API default is
# `:html`.
# @param [String] model The translation model.
# @param [String] cid The customization id for translate. This is
# optional.
#
Expand Down Expand Up @@ -160,15 +161,16 @@ def self.default_project
# to: :la
# translation.text #=> "<strong>Salve</strong> mundi!"
#
def translate *text, to: nil, from: nil, format: nil, cid: nil
def translate *text, to: nil, from: nil, format: nil, model: nil,
cid: nil
return nil if text.empty?
fail ArgumentError, "to is required" if to.nil?
to = to.to_s
from = from.to_s if from
format = format.to_s if format
text = Array(text).flatten
gapi = service.translate text, to: to, from: from,
format: format, cid: cid
format: format, model: model, cid: cid
Translation.from_gapi_list gapi, text, to, from
end

Expand Down
6 changes: 4 additions & 2 deletions google-cloud-translate/lib/google/cloud/translate/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def initialize project, credentials, retries: nil, timeout: nil,

##
# Returns Hash of ListTranslationsResponse JSON
def translate text, to: nil, from: nil, format: nil, cid: nil
def translate text, to: nil, from: nil, format: nil, model: nil,
cid: nil
body = {
q: Array(text), target: to, source: from, format: format, cid: cid
q: Array(text), target: to, source: from, format: format,
model: model, cid: cid
}.delete_if { |_k, v| v.nil? }.to_json

post "/language/translate/v2", body
Expand Down
13 changes: 11 additions & 2 deletions google-cloud-translate/lib/google/cloud/translate/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,25 @@ class Translation

##
# The source language from which the text was translated.
#
# @return [String]
attr_reader :from
alias_method :source, :from

##
# The translation model.
#
# @return [String]
attr_reader :model

##
# @private Create a new object.
def initialize text, to, origin, from, detected
def initialize text, to, origin, from, model, detected
@text = text
@to = to
@origin = origin
@from = from
@model = model
@detected = detected
end

Expand Down Expand Up @@ -105,7 +114,7 @@ def self.from_gapi_list gapi, text, to, from
def self.from_gapi gapi, to, origin, from
from ||= gapi["detectedSourceLanguage"]
detected = !gapi["detectedSourceLanguage"].nil?
new gapi["translatedText"], to, origin, from, detected
new gapi["translatedText"], to, origin, from, gapi["model"], detected
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions google-cloud-translate/support/doctest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,49 @@ def mock_translate
doctest.before "Google::Cloud#translate" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud#translate@Using API Key from the environment variable." do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud.translate" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud.translate@Using API Key from the environment variable." do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate.new" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate.new@Using API Key from the environment variable." do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate::Api" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

Expand All @@ -97,29 +97,29 @@ def mock_translate
doctest.before "Google::Cloud::Translate::Api#translate" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate::Api#translate@Setting the `from` language." do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: nil, translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: "en", format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: "en", format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate::Api#translate@Retrieving multiple translations." do
mock_translate do |mock|
res_attrs_1 = { detectedSourceLanguage: nil, translatedText: "Salve amice." }
res_attrs_2 = { detectedSourceLanguage: nil, translatedText: "Vide te mox." }
mock.expect :translate, list_translations_response([res_attrs_1, res_attrs_2]), [["Hello my friend.", "See you soon."], to: "la", from: "en", format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs_1, res_attrs_2]), [["Hello my friend.", "See you soon."], to: "la", from: "en", format: nil, model: nil, cid: nil]
end
end

doctest.before "Google::Cloud::Translate::Api#translate@Preserving HTML tags." do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: nil, translatedText: "<strong>Salve</strong> mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["<strong>Hello</strong> world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["<strong>Hello</strong> world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end

Expand Down Expand Up @@ -170,7 +170,7 @@ def mock_translate
doctest.before "Google::Cloud::Translate::Translation" do
mock_translate do |mock|
res_attrs = { detectedSourceLanguage: "en", translatedText: "Salve mundi!" }
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, cid: nil]
mock.expect :translate, list_translations_response([res_attrs]), [["Hello world!"], to: "la", from: nil, format: nil, model: nil, cid: nil]
end
end
end
Expand Down
Loading

0 comments on commit 16bea7f

Please sign in to comment.