Class: Lara::Translator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • credentials (Lara::Credentials, nil) (defaults to: nil)
  • auth_token (Lara::AuthToken, nil) (defaults to: nil)
  • access_key_id (String, nil) (defaults to: nil)
  • access_key_secret (String, nil) (defaults to: nil)
  • base_url (String, nil) (defaults to: nil)
  • connection_timeout (Integer, nil) (defaults to: nil)
  • read_timeout (Integer, nil) (defaults to: nil)


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

#audioObject (readonly)

Returns the value of attribute audio.



37
38
39
# File 'lib/lara/translator.rb', line 37

def audio
  @audio
end

#clientObject (readonly)

Returns the value of attribute client.



37
38
39
# File 'lib/lara/translator.rb', line 37

def client
  @client
end

#documentsObject (readonly)

Returns the value of attribute documents.



37
38
39
# File 'lib/lara/translator.rb', line 37

def documents
  @documents
end

#glossariesObject (readonly)

Returns the value of attribute glossaries.



37
38
39
# File 'lib/lara/translator.rb', line 37

def glossaries
  @glossaries
end

#imagesObject (readonly)

Returns the value of attribute images.



37
38
39
# File 'lib/lara/translator.rb', line 37

def images
  @images
end

#memoriesObject (readonly)

Returns the value of attribute memories.



37
38
39
# File 'lib/lara/translator.rb', line 37

def memories
  @memories
end

#styleguidesObject (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.

Parameters:

  • text (String, Array<String>)

    Text to detect language for

  • hint (String, nil) (defaults to: nil)

    Language hint

  • passlist (Array<String>, nil) (defaults to: nil)

    List of allowed languages

Returns:



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.

Parameters:

  • text (String)

    Text to check for profanities

  • language (String)

    Language code (e.g. “en”)

  • content_type (String) (defaults to: "text/plain")

    One of “text/plain”, “text/html”, “text/xml”, “application/xliff+xml”

Returns:



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_languagesObject

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).

Parameters:

  • source (String)
  • target (String)
  • sentence (String, Array<String>)
  • translation (String, Array<String>)

Returns:



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.

Parameters:

  • text (String, Array<String>, Array<Lara::Models::TextBlock>)
  • source (String, nil) (defaults to: nil)
  • source_hint (String, nil) (defaults to: nil)
  • target (String)
  • adapt_to (Array<String>, nil) (defaults to: nil)
  • glossaries (Array<String>, nil) (defaults to: nil)
  • instructions (Array<String>, nil) (defaults to: nil)
  • content_type (String, nil) (defaults to: nil)
  • multiline (Boolean) (defaults to: true)
  • timeout_ms (Integer, nil) (defaults to: nil)
  • priority (String, nil) (defaults to: nil)
  • use_cache (String, Boolean, nil) (defaults to: nil)
  • cache_ttl_s (Integer, nil) (defaults to: nil)
  • no_trace (Boolean) (defaults to: false)
  • verbose (Boolean) (defaults to: false)
  • style (String, nil) (defaults to: nil)
  • reasoning (Boolean) (defaults to: false)

    When true with a block, yields partial results during reasoning

  • headers (Hash, nil) (defaults to: nil)
  • metadata (String, Hash, nil) (defaults to: nil)
  • profanity_filter (String, nil) (defaults to: nil)

    One of “detect”, “avoid”, “hide”

Yields:

Returns:



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