1#################################################################### 2# Apertium.awk # 3#################################################################### 4# 5# Last Updated: 14 Mar 2016 6BEGIN { provides("apertium") } 7 8function apertiumInit() { 9 HttpProtocol = "http://" 10 HttpHost = "www.apertium.org" 11 HttpPort = 80 12} 13 14function apertiumRequestUrl(text, sl, tl, hl) { 15 return HttpPathPrefix "/apy/translate?" \ 16 "langpair=" preprocess(sl) "|" preprocess(tl) \ 17 "&q=" preprocess(text) 18} 19 20function apertiumTTSUrl(text, tl, narrator) { 21 # Not implemented 22} 23 24function apertiumWebTranslateUrl(uri, sl, tl, hl) { 25 # Not implemented 26} 27 28# Get the translation of a string. 29function apertiumTranslate(text, sl, tl, hl, 30 isVerbose, toSpeech, returnPlaylist, returnIl, 31 #### 32 r, 33 content, tokens, ast, 34 _sl, _tl, _hl, il, 35 translation, 36 wShowOriginal, wShowTranslation, wShowLanguages, 37 group, temp) { 38 if (!getCode(tl)) { 39 # Check if target language is supported 40 w("[WARNING] Unknown target language code: " tl) 41 } else if (isRTL(tl)) { 42 # Check if target language is R-to-L 43 if (!FriBidi) 44 w("[WARNING] " getName(tl) " is a right-to-left language, but FriBidi is not found.") 45 } 46 _sl = getCode(sl); if (!_sl) _sl = sl 47 _tl = getCode(tl); if (!_tl) _tl = tl 48 _hl = getCode(hl); if (!_hl) _hl = hl 49 50 # Quick hack: Apertium doesn't have an "auto" language code 51 _sl = "auto" == _sl ? "en" : _sl 52 53 content = getResponse(text, _sl, _tl, _hl) 54 if (Option["dump"]) 55 return content 56 tokenize(tokens, content) 57 parseJson(ast, tokens) 58 59 l(content, "content", 1, 1) 60 l(tokens, "tokens", 1, 0, 1) 61 l(ast, "ast") 62 if (!isarray(ast) || !anything(ast)) { 63 e("[ERROR] Oops! Something went wrong and I can't translate it for you :(") 64 ExitCode = 1 65 return 66 } 67 68 translation = uprintf(unquote(unparameterize(ast[0 SUBSEP "responseData" SUBSEP "translatedText"]))) 69 70 returnIl[0] = il = _sl 71 if (Option["verbose"] < -1) 72 return il 73 else if (Option["verbose"] < 0) 74 return getList(il) 75 76 # Generate output 77 if (!isVerbose) { 78 # Brief mode 79 r = translation 80 81 } else { 82 # Verbose mode 83 wShowOriginal = Option["show-original"] 84 wShowTranslation = Option["show-translation"] 85 wShowLanguages = Option["show-languages"] 86 87 if (wShowOriginal) { 88 # Display: original text 89 if (r) r = r RS RS 90 r = r m("-- display original text") 91 r = r prettify("original", s(text, il)) 92 } 93 94 if (wShowTranslation) { 95 # Display: major translation 96 if (r) r = r RS RS 97 r = r m("-- display major translation") 98 r = r prettify("translation", s(translation, tl)) 99 } 100 101 if (wShowLanguages) { 102 # Display: source language -> target language 103 if (r) r = r RS RS 104 r = r m("-- display source language -> target language") 105 temp = Option["fmt-languages"] 106 if (!temp) temp = "[ %s -> %t ]" 107 split(temp, group, /(%s|%S|%t|%T)/) 108 r = r prettify("languages", group[1]) 109 if (temp ~ /%s/) 110 r = r prettify("languages-sl", getDisplay(il)) 111 if (temp ~ /%S/) 112 r = r prettify("languages-sl", getName(il)) 113 r = r prettify("languages", group[2]) 114 if (temp ~ /%t/) 115 r = r prettify("languages-tl", getDisplay(tl)) 116 if (temp ~ /%T/) 117 r = r prettify("languages-tl", getName(tl)) 118 r = r prettify("languages", group[3]) 119 } 120 } 121 122 if (toSpeech) { 123 returnPlaylist[0]["text"] = translation 124 returnPlaylist[0]["tl"] = _tl 125 } 126 127 return r 128} 129