class AwsEngine

Provides Language services using Amazon Translate

Module can be intialized using multiple options

Credential Referencing Order

Constants

DEFAULT_REGION

Public Class Methods

new(options) click to toggle source

Arguments

options can carry the following details

  • :access_key_id
    • access key id

  • :secret_access_key
    • Secret access key

  • :env
    • true for using credentials from environment variables

  • :profile
    • profile name for using shared credentials setup

  • :region
    • If not provided defaults to us-east-1

raises

  • EngineInitializationException if credentials cannot be setup due to lack of details

  • Aws Exceptions if profile name is invalid or invalid credentials are passed

# File lib/engines/aws.rb, line 39
def initialize(options)
  access_key_id = nil
  secret_access_key = nil
  @region = options[:region] || DEFAULT_REGION
  if options[:env]
    access_key_id = ENV["AWS_ACCESS_KEY_ID"]
    secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]
  elsif options[:access_key_id] && options[:secret_access_key]
    access_key_id = options[:access_key_id]
    secret_access_key = options[:secret_access_key]
  end
  if access_key_id && secret_access_key
    Aws.config.update({
      region: options[:region] || DEFAULT_REGION,
      credentials: Aws::Credentials.new(access_key_id, secret_access_key)
    })
  elsif options[:profile]
    credentials = Aws::SharedCredentials.new(profile_name: options[:profile])
    Aws.config.update({
      region: @region,
      credentials: credentials.credentials
    })
  else
    raise Translator::EngineInitializationException.new(
      "Failed to initialize Aws Engine. Credentials are missing / not provided")
  end
  @translate_service  = Aws::Translate::Client.new(region: @region)
  @comprehend_service = Aws::Comprehend::Client.new(region: @region)
end

Public Instance Methods

infer_language(text) click to toggle source

Invokes the language detection API of AWS and returns only the language of the highest score and returns the ISO 639-1 code

  • text - The text for which the language is to be inferred

# File lib/engines/aws.rb, line 75
def infer_language(text)
  response = @comprehend_service.detect_dominant_language({ text: "#{text}" })
  response[:languages][0][:language_code]
end
translate(input_text, src_lang, target_lang) click to toggle source

Invokes the translation API of AWS and returns the translated text as per the arguments provided. Will Raise exception if a translation cannot be made between the source and target language codes or if the lang code is invalid

  • input_text - The text that needs to be translated

  • src_lang - The source language of the text

  • target_lang - The target language to which the input_text needs to be translated to

# File lib/engines/aws.rb, line 90
def translate(input_text, src_lang, target_lang)
  response = @translate_service.translate_text({ :text => "#{input_text}" , 
    :source_language_code => "#{src_lang}", :target_language_code => "#{target_lang}"})
  response.translated_text
end