Use apertium locally for translations
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
#!/usr/bin/env ruby
|
||||
#!/usr/bin/ruby --disable-all
|
||||
|
||||
# A cgi-bin script that translates between languages, scraping apertium's API
|
||||
# to do so
|
||||
#
|
||||
# It's kind of bad manners; I'll transition it to the offline tools once I've
|
||||
# worked those out.
|
||||
# A cgi-bin script that translates between languages. It relies on apertium
|
||||
# being installed on the machine.
|
||||
#
|
||||
# CGI works with environment variables. Here are the ones that matter:
|
||||
#
|
||||
@@ -18,6 +15,8 @@
|
||||
|
||||
require 'cgi'
|
||||
|
||||
LANGUAGE_PAIRS = %w[en-es es-en]
|
||||
|
||||
class Object
|
||||
def blank?
|
||||
nil? || self&.empty?
|
||||
@@ -46,15 +45,17 @@ def not_found!(meta = 'Not Found')
|
||||
respond!(51, meta)
|
||||
end
|
||||
|
||||
def extract_languages(path)
|
||||
def extract_langpair(path)
|
||||
return if path.blank? || !path.start_with?('/')
|
||||
|
||||
_, src, dst, *rest = path.split('/')
|
||||
|
||||
return unless rest.empty?
|
||||
# return unless LANGUAGES.include?(src) && LANGUAGES.include?(dst)
|
||||
|
||||
[src, dst]
|
||||
pair = [src, dst].join('-')
|
||||
return unless LANGUAGE_PAIRS.include?(pair)
|
||||
|
||||
pair
|
||||
end
|
||||
|
||||
def extract_text(query)
|
||||
@@ -63,29 +64,19 @@ def extract_text(query)
|
||||
CGI.unescape(query)
|
||||
end
|
||||
|
||||
SRC_LANG, DST_LANG = extract_languages(ENV['PATH_INFO'])
|
||||
not_found! if SRC_LANG.blank? || DST_LANG.blank?
|
||||
LANG_PAIR = extract_langpair(ENV['PATH_INFO'])
|
||||
not_found! if LANG_PAIR.blank?
|
||||
|
||||
TRANSLATE = extract_text(ENV['QUERY_STRING'])
|
||||
|
||||
# TODO: we could detect a URL and translate the whole page sometime, perhaps
|
||||
ask!('Enter text to translate') if TRANSLATE.blank?
|
||||
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
require 'open3'
|
||||
|
||||
uri = URI.parse('https://www.apertium.org/apy/translate')
|
||||
uri.query = URI.encode_www_form(q: TRANSLATE, langpair: [SRC_LANG, DST_LANG].join('|'))
|
||||
translation, status = Open3.capture2("apertium", LANG_PAIR, stdin_data: TRANSLATE)
|
||||
|
||||
rsp = Net::HTTP.get(uri)
|
||||
temp_fail!("Couldn't get translation (1)") unless rsp
|
||||
temp_fail!("Couldn't get translation") unless status.success?
|
||||
|
||||
begin
|
||||
structure = JSON.parse(rsp)
|
||||
rescue
|
||||
temp_fail!("Couldn't get translation (2)")
|
||||
end
|
||||
ok!('text/plain; charset="utf-8"', translation)
|
||||
|
||||
temp_fail!("Couldn't get transaltion (3)") unless structure['responseStatus'] == 200
|
||||
|
||||
ok!('text/plain', structure.dig('responseData', 'translatedText'))
|
||||
|
@@ -9,7 +9,7 @@
|
||||
|
||||
## Translation service
|
||||
|
||||
=> /cgi-bin/translate/eng/spa English -> Español
|
||||
=> /cgi-bin/translate/spa/eng Español -> English
|
||||
=> /cgi-bin/translate/en/es English -> Español
|
||||
=> /cgi-bin/translate/es/en Español -> English
|
||||
|
||||
## About Me
|
||||
|
Reference in New Issue
Block a user