translate: added defaults, detection, and temp store last used in memory

This commit is contained in:
Paul Wilde 2022-05-11 16:16:07 +01:00
parent f9bde50717
commit 07cc6d48ad
4 changed files with 66 additions and 26 deletions

0
compile_all.sh Normal file → Executable file
View file

0
fuzzytime.nim Normal file → Executable file
View file

View file

@ -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()

0
volume.nim Normal file → Executable file
View file