Class: Lara::Translator
- Inherits:
-
Object
- Object
- Lara::Translator
- Defined in:
- lib/lara/translator.rb
Constant Summary collapse
- VALID_CONTENT_TYPES =
%w[text/plain text/html text/xml application/xliff+xml].freeze
Instance Attribute Summary collapse
-
#audio ⇒ Object
readonly
Returns the value of attribute audio.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
-
#glossaries ⇒ Object
readonly
Returns the value of attribute glossaries.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#memories ⇒ Object
readonly
Returns the value of attribute memories.
-
#styleguides ⇒ Object
readonly
Returns the value of attribute styleguides.
Instance Method Summary collapse
-
#detect(text, hint: nil, passlist: nil) ⇒ Lara::Models::DetectResult
Detects the language of the given text.
-
#detect_profanities(text, language:, content_type: "text/plain") ⇒ Lara::Models::ProfanityDetectResult
Detects profanities in the given text.
-
#get_languages ⇒ Object
Lists supported language codes.
-
#initialize(credentials: nil, auth_token: nil, access_key_id: nil, access_key_secret: nil, base_url: nil, connection_timeout: nil, read_timeout: nil) ⇒ Translator
constructor
A new instance of Translator.
-
#quality_estimation(source:, target:, sentence:, translation:) ⇒ Lara::Models::QualityEstimationResult+
Estimates translation quality for a sentence pair (or batch of pairs).
-
#translate(text, target:, source: nil, source_hint: nil, adapt_to: nil, glossaries: nil, instructions: nil, content_type: nil, multiline: true, timeout_ms: nil, priority: nil, use_cache: nil, cache_ttl_s: nil, no_trace: false, verbose: false, style: nil, reasoning: false, headers: nil, metadata: nil, profanity_filter: nil, styleguide_id: nil, styleguide_reasoning: nil, styleguide_explanation_language: nil) {|Lara::Models::TextResult| ... } ⇒ Lara::Models::TextResult
Translates text with optional tuning parameters.
Constructor Details
#initialize(credentials: nil, auth_token: nil, access_key_id: nil, access_key_secret: nil, base_url: nil, connection_timeout: nil, read_timeout: nil) ⇒ Translator
Returns a new instance of Translator.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lara/translator.rb', line 14 def initialize(credentials: nil, auth_token: nil, access_key_id: nil, access_key_secret: nil, base_url: nil, connection_timeout: nil, read_timeout: nil) auth_method = if auth_token auth_token elsif credentials credentials elsif access_key_id && access_key_secret Credentials.new(access_key_id, access_key_secret) else raise ArgumentError, "either credentials, auth_token, or access_key_id and access_key_secret must be provided" end @client = Client.new(auth_method, base_url: base_url, connection_timeout: connection_timeout, read_timeout: read_timeout) @memories = Memories.new(@client) @glossaries = Glossaries.new(@client) @styleguides = Styleguides.new(@client) @documents = Documents.new(@client) @images = Images.new(@client) @audio = AudioTranslator.new(@client) end |
Instance Attribute Details
#audio ⇒ Object (readonly)
Returns the value of attribute audio.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def audio @audio end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def client @client end |
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def documents @documents end |
#glossaries ⇒ Object (readonly)
Returns the value of attribute glossaries.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def glossaries @glossaries end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def images @images end |
#memories ⇒ Object (readonly)
Returns the value of attribute memories.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def memories @memories end |
#styleguides ⇒ Object (readonly)
Returns the value of attribute styleguides.
37 38 39 |
# File 'lib/lara/translator.rb', line 37 def styleguides @styleguides end |
Instance Method Details
#detect(text, hint: nil, passlist: nil) ⇒ Lara::Models::DetectResult
Detects the language of the given text.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/lara/translator.rb', line 118 def detect(text, hint: nil, passlist: nil) body = { q: text } body[:hint] = hint if hint body[:passlist] = passlist if passlist&.any? body = body.compact result = @client.post("/v2/detect/language", body: body) Lara::Models::DetectResult.new( language: result["language"], content_type: result["content_type"], predictions: result["predictions"] || [] ) end |
#detect_profanities(text, language:, content_type: "text/plain") ⇒ Lara::Models::ProfanityDetectResult
Detects profanities in the given text.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/lara/translator.rb', line 139 def detect_profanities(text, language:, content_type: "text/plain") unless VALID_CONTENT_TYPES.include?(content_type) raise ArgumentError, "Invalid content_type '#{content_type}'. Must be one of: #{VALID_CONTENT_TYPES.join(', ')}" end body = { text: text, language: language, content_type: content_type } result = @client.post("/v2/detect/profanities", body: body) Lara::Models::ProfanityDetectResult.new( masked_text: result["masked_text"], profanities: result["profanities"] || [] ) end |
#get_languages ⇒ Object
Lists supported language codes.
175 176 177 |
# File 'lib/lara/translator.rb', line 175 def get_languages @client.get("/v2/languages") end |
#quality_estimation(source:, target:, sentence:, translation:) ⇒ Lara::Models::QualityEstimationResult+
Estimates translation quality for a sentence pair (or batch of pairs).
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/lara/translator.rb', line 158 def quality_estimation(source:, target:, sentence:, translation:) body = { source: source, target: target, sentence: sentence, translation: translation } result = @client.post("/v2/detect/quality-estimation", body: body) if result.is_a?(Array) result.map { |r| Lara::Models::QualityEstimationResult.new(score: r["score"] || r[:score]) } else Lara::Models::QualityEstimationResult.new(score: result["score"] || result[:score]) end end |
#translate(text, target:, source: nil, source_hint: nil, adapt_to: nil, glossaries: nil, instructions: nil, content_type: nil, multiline: true, timeout_ms: nil, priority: nil, use_cache: nil, cache_ttl_s: nil, no_trace: false, verbose: false, style: nil, reasoning: false, headers: nil, metadata: nil, profanity_filter: nil, styleguide_id: nil, styleguide_reasoning: nil, styleguide_explanation_language: nil) {|Lara::Models::TextResult| ... } ⇒ Lara::Models::TextResult
Translates text with optional tuning parameters.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lara/translator.rb', line 62 def translate(text, target:, source: nil, source_hint: nil, adapt_to: nil, glossaries: nil, instructions: nil, content_type: nil, multiline: true, timeout_ms: nil, priority: nil, use_cache: nil, cache_ttl_s: nil, no_trace: false, verbose: false, style: nil, reasoning: false, headers: nil, metadata: nil, profanity_filter: nil, styleguide_id: nil, styleguide_reasoning: nil, styleguide_explanation_language: nil, &callback) q = normalize_text_input(text) use_cache_value = case use_cache when true then "yes" when false then "no" else use_cache end body = { q: q, source: source, target: target, source_hint: source_hint, content_type: content_type, multiline: multiline, adapt_to: adapt_to, glossaries: glossaries, instructions: instructions, timeout: timeout_ms, priority: priority, use_cache: use_cache_value, cache_ttl: cache_ttl_s, verbose: verbose, style: style, reasoning: reasoning, metadata: , profanity_filter: profanity_filter, styleguide_id: styleguide_id, styleguide_reasoning: styleguide_reasoning, styleguide_explanation_language: styleguide_explanation_language }.compact request_headers = {} request_headers.merge!(headers) if headers.is_a?(Hash) request_headers["X-No-Trace"] = "true" if no_trace stream_callback = if callback && reasoning ->(partial) { callback.call(Lara::Models::TextResult.from_hash(partial)) } end result = @client.post("/translate", body: body, headers: request_headers, &stream_callback) Lara::Models::TextResult.from_hash(result) if result end |