2022-05-10 23:18:37 +02:00
|
|
|
import base
|
2022-05-11 17:16:07 +02:00
|
|
|
import std/[re,httpclient,json,strutils,tables]
|
2022-05-10 23:18:37 +02:00
|
|
|
|
2022-05-10 23:27:54 +02:00
|
|
|
|
2022-05-11 17:16:07 +02:00
|
|
|
# TODO:
|
|
|
|
# Query available languages, maybe have selection panel
|
|
|
|
# Maybe store last used so future translations are more fluent
|
2022-05-10 23:27:54 +02:00
|
|
|
|
2022-05-11 17:16:07 +02:00
|
|
|
const LIBRETRANSLATE_URL = "https://libretranslate.pussthecat.org/"
|
2022-05-10 23:27:54 +02:00
|
|
|
const HOME = "en"
|
2022-05-10 23:18:37 +02:00
|
|
|
let LANG_RE = re"\w+>\w+"
|
2022-05-11 17:16:07 +02:00
|
|
|
let DETECT_RE = re"\bd(etect)?\b"
|
|
|
|
var current = @["de",HOME]
|
|
|
|
var detected = {"detected":"0","confidence":"0","language":"en"}.toTable
|
2022-05-10 23:18:37 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
Request = object
|
|
|
|
source: string
|
|
|
|
target: string
|
|
|
|
q: string
|
|
|
|
format: string
|
|
|
|
|
2022-05-11 17:16:07 +02:00
|
|
|
proc main(messages: varargs[string] = @[])
|
2022-05-10 23:18:37 +02:00
|
|
|
|
|
|
|
proc newRequest(): Request =
|
2022-05-11 17:16:07 +02:00
|
|
|
return Request(source:current[0],
|
|
|
|
target:current[1],
|
2022-05-10 23:18:37 +02:00
|
|
|
q: "",
|
|
|
|
format: "text",)
|
|
|
|
|
|
|
|
proc parseReq(req: string): Request =
|
|
|
|
var langs = findAll(req,LANG_RE)
|
2022-05-11 17:16:07 +02:00
|
|
|
var r = newRequest()
|
|
|
|
let query = replace(req,LANG_RE,"")
|
|
|
|
r.q = query
|
2022-05-10 23:18:37 +02:00
|
|
|
if len(langs) > 0:
|
|
|
|
let lang = langs[0]
|
|
|
|
langs = lang.split(re">")
|
2022-05-11 17:16:07 +02:00
|
|
|
current[0] = langs[0].toLowerAscii()
|
|
|
|
current[1] = langs[1].toLowerAscii()
|
|
|
|
r.source = current[0].toLowerAscii()
|
|
|
|
r.target = current[1].toLowerAscii()
|
|
|
|
if query == "":
|
|
|
|
main()
|
2022-05-10 23:18:37 +02:00
|
|
|
return r
|
|
|
|
|
|
|
|
proc answerTranslate(response: string, req: Request) =
|
|
|
|
let r = parseJson(response)
|
|
|
|
try:
|
|
|
|
let ans = r["translatedText"].getStr()
|
2022-05-11 17:16:07 +02:00
|
|
|
main(ans)
|
2022-05-10 23:18:37 +02:00
|
|
|
except:
|
|
|
|
main("Error : " & r["error"].getStr())
|
|
|
|
|
|
|
|
proc goTranslate(req: string) =
|
|
|
|
let r = parseReq(req)
|
|
|
|
if r.q != "":
|
|
|
|
var client = newHTTPClient()
|
|
|
|
client.headers = newHttpHeaders({"Content-Type":"application/json"})
|
|
|
|
let body = %*r
|
2022-05-11 17:16:07 +02:00
|
|
|
let response = client.request(LIBRETRANSLATE_URL & "translate", httpMethod = HttpPost, body = $body)
|
2022-05-10 23:18:37 +02:00
|
|
|
if response.status != "":
|
|
|
|
answerTranslate(response.body, r)
|
|
|
|
|
2022-05-11 17:16:07 +02:00
|
|
|
proc flipCurrent() =
|
|
|
|
current = @[current[1],current[0]]
|
|
|
|
main()
|
2022-05-10 23:18:37 +02:00
|
|
|
|
2022-05-11 17:16:07 +02:00
|
|
|
proc detectLanguage(str: string) =
|
|
|
|
let term = replace(str,DETECT_RE,"")
|
|
|
|
echo "detecting ", term
|
|
|
|
if term != "":
|
|
|
|
var client = newHTTPClient()
|
|
|
|
client.headers = newHttpHeaders({"Content-Type":"application/json"})
|
|
|
|
let body = %*Request(q:term)
|
|
|
|
let response = client.request(LIBRETRANSLATE_URL & "detect", httpMethod = HttpPost, body = $body)
|
|
|
|
if response.status != "":
|
|
|
|
let r = parseJson(response.body)
|
|
|
|
echo r
|
|
|
|
try:
|
|
|
|
detected["confidence"] = $r[0]["confidence"].getFloat()
|
|
|
|
detected["language"] = r[0]["language"].getStr()
|
|
|
|
detected["detected"] = "1"
|
|
|
|
current[0] = detected["language"]
|
|
|
|
current[1] = HOME
|
|
|
|
goTranslate(term)
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
main("Error : " & r["error"].getStr())
|
|
|
|
except:
|
|
|
|
main("Error Parsing Json")
|
|
|
|
return
|
|
|
|
|
|
|
|
proc main(messages:varargs[string] = @[]) =
|
2022-05-10 23:18:37 +02:00
|
|
|
var info = newInfo("Translate")
|
|
|
|
info.selected_bg = green
|
|
|
|
var args: seq[string] = @[]
|
2022-05-11 17:16:07 +02:00
|
|
|
for msg in messages:
|
|
|
|
if msg != "":
|
|
|
|
args.add(msg)
|
|
|
|
args.add(current[0] & " > " & current[1])
|
|
|
|
if detected["detected"] == "1":
|
|
|
|
args.add("detected language : " & detected["language"])
|
|
|
|
args.add("detected confidence : " & detected["confidence"])
|
|
|
|
detected["detected"] = "0"
|
|
|
|
if len(messages) > 0:
|
2022-05-10 23:34:47 +02:00
|
|
|
args.add("back")
|
2022-05-10 23:18:37 +02:00
|
|
|
args.add("exit")
|
|
|
|
let output = outputData(info,args)
|
|
|
|
if output == "exit" or output == "":
|
|
|
|
return
|
2022-05-10 23:34:47 +02:00
|
|
|
elif output == "back":
|
|
|
|
main()
|
2022-05-11 17:16:07 +02:00
|
|
|
elif output == current[0] & " > " & current[1]:
|
|
|
|
flipCurrent()
|
|
|
|
elif re.startsWith(output,DETECT_RE):
|
|
|
|
detectLanguage(output)
|
|
|
|
elif output in messages:
|
2022-05-10 23:18:37 +02:00
|
|
|
copyToClipboard(output)
|
|
|
|
else:
|
|
|
|
goTranslate(output)
|
|
|
|
return
|
|
|
|
|
|
|
|
if isMainModule:
|
|
|
|
main()
|
2022-05-11 17:16:07 +02:00
|
|
|
|