From 07cc6d48ad0aa12be8f7dc5aab4015bab39cb05c Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 11 May 2022 16:16:07 +0100 Subject: [PATCH] translate: added defaults, detection, and temp store last used in memory --- compile_all.sh | 0 fuzzytime.nim | 0 translate.nim | 92 ++++++++++++++++++++++++++++++++++++-------------- volume.nim | 0 4 files changed, 66 insertions(+), 26 deletions(-) mode change 100644 => 100755 compile_all.sh mode change 100644 => 100755 fuzzytime.nim mode change 100644 => 100755 volume.nim diff --git a/compile_all.sh b/compile_all.sh old mode 100644 new mode 100755 diff --git a/fuzzytime.nim b/fuzzytime.nim old mode 100644 new mode 100755 diff --git a/translate.nim b/translate.nim index 1102c9e..5be6247 100644 --- a/translate.nim +++ b/translate.nim @@ -1,14 +1,17 @@ import base -import std/[re,httpclient,json,strutils] +import std/[re,httpclient,json,strutils,tables] -# TODO: Add language detection and translate into HOME language -# Once one translation is done, stay in that translation until told otherwise +# TODO: +# Query available languages, maybe have selection panel +# Maybe store last used so future translations are more fluent -const LIBRETRANSLATE_URL = "https://libretranslate.pussthecat.org/translate" +const LIBRETRANSLATE_URL = "https://libretranslate.pussthecat.org/" const HOME = "en" -const FIRST_CHOICE = "de" let LANG_RE = re"\w+>\w+" +let DETECT_RE = re"\bd(etect)?\b" +var current = @["de",HOME] +var detected = {"detected":"0","confidence":"0","language":"en"}.toTable type Request = object @@ -17,36 +20,35 @@ type q: string format: string -proc main(message: varargs[string] = @[]) +proc main(messages: varargs[string] = @[]) proc newRequest(): Request = - return Request(source:FIRST_CHOICE, - target:HOME, + return Request(source:current[0], + target:current[1], q: "", format: "text",) proc parseReq(req: string): Request = - var r = newRequest() var langs = findAll(req,LANG_RE) + var r = newRequest() + let query = replace(req,LANG_RE,"") + r.q = query if len(langs) > 0: let lang = langs[0] langs = lang.split(re">") - let query = replace(req,LANG_RE,"") - if query != "": - r.source = langs[0].toLowerAscii() - r.target = langs[1].toLowerAscii() - r.q = query - else: - main() - else: - main("No language selections made. i.e. en>de, de>en, etc.") + current[0] = langs[0].toLowerAscii() + current[1] = langs[1].toLowerAscii() + r.source = current[0].toLowerAscii() + r.target = current[1].toLowerAscii() + if query == "": + main() return r proc answerTranslate(response: string, req: Request) = let r = parseJson(response) try: let ans = r["translatedText"].getStr() - main(ans, req.source & " > " & req.target) + main(ans) except: main("Error : " & r["error"].getStr()) @@ -56,19 +58,52 @@ proc goTranslate(req: string) = var client = newHTTPClient() client.headers = newHttpHeaders({"Content-Type":"application/json"}) let body = %*r - let response = client.request(LIBRETRANSLATE_URL, httpMethod = HttpPost, body = $body) + let response = client.request(LIBRETRANSLATE_URL & "translate", httpMethod = HttpPost, body = $body) if response.status != "": answerTranslate(response.body, r) +proc flipCurrent() = + current = @[current[1],current[0]] + main() -proc main(message:varargs[string] = @[]) = +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] = @[]) = var info = newInfo("Translate") info.selected_bg = green var args: seq[string] = @[] - if len(message) > 0: - for msg in message: - if msg != "": - args.add(msg) + 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: args.add("back") args.add("exit") let output = outputData(info,args) @@ -76,7 +111,11 @@ proc main(message:varargs[string] = @[]) = return elif output == "back": main() - elif output in message: + elif output == current[0] & " > " & current[1]: + flipCurrent() + elif re.startsWith(output,DETECT_RE): + detectLanguage(output) + elif output in messages: copyToClipboard(output) else: goTranslate(output) @@ -84,3 +123,4 @@ proc main(message:varargs[string] = @[]) = if isMainModule: main() + diff --git a/volume.nim b/volume.nim old mode 100644 new mode 100755