Use apertium locally for translations

This commit is contained in:
2020-11-27 00:11:26 +00:00
parent 9c909f42c5
commit e5d6686c66
2 changed files with 18 additions and 27 deletions

View File

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

View File

@@ -9,7 +9,7 @@
## Translation service ## Translation service
=> /cgi-bin/translate/eng/spa English -> Español => /cgi-bin/translate/en/es English -> Español
=> /cgi-bin/translate/spa/eng Español -> English => /cgi-bin/translate/es/en Español -> English
## About Me ## About Me