require 'faraday' require 'json' module Lingo24 # +PremiumMTAPI+ is an API wrapper for the Lingo24 Premium Machine Translation API # (https://developer.lingo24.com/premium-machine-translation-api). # # Machine Translation (MT) is a cost-effective, quick translation option for large volumes of content, or cases where # it is not feasible to use a professional human translator. # # The Lingo24 Premium Machine Translation API provides direct, secure, high volume access to our Premium Machine # Translation Engines. Encrypted using SSL and with no data storage at Lingo24, you can translate safely with no data # privacy concerns. # # We have developed a range of Premium MT engines focusing on specific language combinations and subject areas, # including Law, Information Technology, Pharmaceuticals, Business, IT and many more. This focused approach results in # much higher-quality output than generic engines from big-name translators, with the API selecting the best suited # engine based on the content of the text for translation. Ideal for those scenarios where no post-editing is required, such as gisting (getting a basic understanding of a source text), real-time translation (tweets, IM chat, etc) or sentiment analysis, the Premium MT API enables easy access to raw machine translation. # class PremiumMTAPI BASE_API_URL = "https://api.lingo24.com/mt/v1/" public # Creates a new API client to handle calls to the Lingo24 Premium Machine Translation API. # # Parameters: # user_key - a string containing the user_key issued by the Lingo24 developer portal for your application. def initialize(user_key) @user_key = user_key end # Synchronous call to translate text using the Lingo24 Premium Machine Translation API. When calling this method # the text sent is analysed and routed to the engine most likely to produce the highest quality output. # # NOTE: Customers who have custom engines for specific business domains will have these prioritised ahead of Lingo24 # vertical engines. # # Options: # text - a string for translation # source - a string containing the ISO-639-1 code for the source language # target - a string containing the ISO-639-1 code for the target language def translate(text, source, target) params = {:q => text, :source => source, :target => target, :user_key => @user_key} handle_request('translate', params, 'translation') end # Synchronous call to translate text using the Lingo24 Premium Machine Translation API. When calling this method # the text sent is analysed and routed to the engine most likely to produce the highest quality output. # # NOTE: Customers who have custom engines for specific business domains will have these prioritised ahead of Lingo24 # vertical engines. # # Options: # text - a string for translation # source - a +Lingo24::Language+ containing the source language # target - a +Lingo24::Language+ containing the target language def translate_language(text, source, target) params = {:q => text, :source => source.code, :target => target.code, :user_key => @user_key} handle_request('translate', params, 'translation') end # Synchronous call to translate text using a specific Lingo24 Premium Machine Translation engine. This method is # typically used by customers who have a custom engine and want to ignore the automatic classification.. # # Options: # text - a string for translation # source - a +Lingo24::Language+ containing the source language # target - a +Lingo24::Language+ containing the target language def translate_specific(text, id) params = {:q => text, :id => id, :user_key => @user_key} handle_request('translate', params, 'translation') end ## # Returns a list of the supported source languages for +PremiumMTAPI+. This method will return a list containing # +Lingo24::Language+ objects which provide access to both the ISO-639-1 code as well as a friendly name. # # If the +target+ is nil, the list will contain all possible source languages. # If the +target+ is set, the list will contain only the source languages that # can generate the target language. def get_source_langs(target = nil) if (target.nil?) params = { :user_key => @user_key } else params = { :user_key => @user_key, :target => target } end source_langs = handle_request('sourcelangs', params, 'source_langs') source_langs.map { |code, name| Lingo24::Language.new(code, name) } end ## # Returns a list of the supported target languages for +PremiumMTAPI+. This method will return a list containing # +Lingo24::Language+ objects which provide access to both the ISO-639-1 code as well as a friendly name. # # If +source+ is nil, the list will contain all possible target languages. # If +source+ is set, the list will contain only the target languages that # can generate the source language. def get_target_langs(source = nil) if (source.nil?) params = { :user_key => @user_key } else params = { :user_key => @user_key, :source => source } end target_langs = handle_request('targetlangs', params, 'target_langs') target_langs.map { |code, name| Lingo24::Language.new(code, name) } end private # Private method to handle the interaction between the client library and the Lingo24 API. def handle_request(method, params, field) response = Faraday.post BASE_API_URL + method, params do |request| request.options[:timeout] = 300 request.options[:open_timeout] = 120 end if (response.status.equal? '200') raise Lingo24::Exception.new(response.status, response.body) elsif (JSON.parse(response.body)['errors']) raise Lingo24::Exception.new('500', JSON.parse(response.body)['errors'][0]) else JSON.parse(response.body)[field] end end end end