Class: TranslationDiff::Providers::ModernMT

Inherits:
HTTPProvider
  • Object
show all
Defined in:
lib/translation_diff/providers/modernmt.rb

Overview

ModernMT: adaptive translation with translation memories.

Constant Summary collapse

HOST =
"https://api.modernmt.com".freeze
DEFAULT_FORMAT =

ModernMT spells its formats as MIME types.

"text/html".freeze
MODERNMT_HONOURS_NOTRANSLATE =

Unverified, not observed: no key was available to probe it; false is the safe assumption either way.

false

Constants inherited from HTTPProvider

HTTPProvider::RETRY_STATUSES, HTTPProvider::TRANSPORT_FAILURES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HTTPProvider

#connection, #translate

Class Method Details

.capabilities ⇒ Object

128 texts is documented; the character limit is not, so Google's 5,000 recommendation is borrowed.



12
13
14
15
16
17
18
# File 'lib/translation_diff/providers/modernmt.rb', line 12

def self.capabilities
  TranslationDiff::Capabilities.new(
    max_request_size: 5_000, max_batch_size: 128, max_text_size: nil,
    html: :format, notranslate: MODERNMT_HONOURS_NOTRANSLATE,
    detects_language: true, reports_billing: true
  )
end

.configuration_options ⇒ Object



20
# File 'lib/translation_diff/providers/modernmt.rb', line 20

def self.configuration_options = %i[modernmt_api_key modernmt_api_base]

.configuration_requirements ⇒ Object



21
# File 'lib/translation_diff/providers/modernmt.rb', line 21

def self.configuration_requirements = %i[modernmt_api_key]

Instance Method Details

#api_base ⇒ Object



23
# File 'lib/translation_diff/providers/modernmt.rb', line 23

def api_base = config.modernmt_api_base || HOST

#detect(text) ⇒ Object



46
47
48
49
# File 'lib/translation_diff/providers/modernmt.rb', line 46

def detect(text)
  request = TranslationDiff::Translation::Request.new(texts: [text], from: nil, to: "en")
  translate(request).detected_source
end

#headers ⇒ Object



24
# File 'lib/translation_diff/providers/modernmt.rb', line 24

def headers = { "MMT-ApiKey" => config.modernmt_api_key.to_s }

#languages ⇒ Object



51
52
53
54
55
# File 'lib/translation_diff/providers/modernmt.rb', line 51

def languages
  codes = Array(get("translate/languages").body["data"])

  { source: codes, target: codes }
end

#languages_endpoint ⇒ Object



57
# File 'lib/translation_diff/providers/modernmt.rb', line 57

def languages_endpoint = "#{api_base}/translate/languages"

#parse_translate_response(body, _headers, request) ⇒ Object

One text comes back as an object rather than a one-element array, so the envelope is always coerced.



35
36
37
38
39
40
41
42
43
44
# File 'lib/translation_diff/providers/modernmt.rb', line 35

def parse_translate_response(body, _headers, request)
  results = results_from(body)

  TranslationDiff::Translation::Response.build(
    request: request,
    texts: results.map { |r| r["translation"] },
    detected_source: results.first&.dig("detectedLanguage")&.downcase,
    usage: usage_for(request, results)
  )
end

#render_translate_payload(request) ⇒ Object



27
28
29
30
31
32
# File 'lib/translation_diff/providers/modernmt.rb', line 27

def render_translate_payload(request)
  { format: DEFAULT_FORMAT }
    .merge(request.options)
    .merge(q: request.texts, target: language(request.to))
    .tap { |payload| payload[:source] = language(request.from) unless request.from.nil? }
end

#translate_url ⇒ Object



25
# File 'lib/translation_diff/providers/modernmt.rb', line 25

def translate_url = "translate"