translate: added defaults, detection, and temp store last used in memory
This commit is contained in:
parent
f9bde50717
commit
07cc6d48ad
4 changed files with 66 additions and 26 deletions
0
compile_all.sh
Normal file → Executable file
0
compile_all.sh
Normal file → Executable file
0
fuzzytime.nim
Normal file → Executable file
0
fuzzytime.nim
Normal file → Executable file
|
@ -1,14 +1,17 @@
|
||||||
import base
|
import base
|
||||||
import std/[re,httpclient,json,strutils]
|
import std/[re,httpclient,json,strutils,tables]
|
||||||
|
|
||||||
|
|
||||||
# TODO: Add language detection and translate into HOME language
|
# TODO:
|
||||||
# Once one translation is done, stay in that translation until told otherwise
|
# 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 HOME = "en"
|
||||||
const FIRST_CHOICE = "de"
|
|
||||||
let LANG_RE = re"\w+>\w+"
|
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
|
type
|
||||||
Request = object
|
Request = object
|
||||||
|
@ -17,36 +20,35 @@ type
|
||||||
q: string
|
q: string
|
||||||
format: string
|
format: string
|
||||||
|
|
||||||
proc main(message: varargs[string] = @[])
|
proc main(messages: varargs[string] = @[])
|
||||||
|
|
||||||
proc newRequest(): Request =
|
proc newRequest(): Request =
|
||||||
return Request(source:FIRST_CHOICE,
|
return Request(source:current[0],
|
||||||
target:HOME,
|
target:current[1],
|
||||||
q: "",
|
q: "",
|
||||||
format: "text",)
|
format: "text",)
|
||||||
|
|
||||||
proc parseReq(req: string): Request =
|
proc parseReq(req: string): Request =
|
||||||
var r = newRequest()
|
|
||||||
var langs = findAll(req,LANG_RE)
|
var langs = findAll(req,LANG_RE)
|
||||||
|
var r = newRequest()
|
||||||
|
let query = replace(req,LANG_RE,"")
|
||||||
|
r.q = query
|
||||||
if len(langs) > 0:
|
if len(langs) > 0:
|
||||||
let lang = langs[0]
|
let lang = langs[0]
|
||||||
langs = lang.split(re">")
|
langs = lang.split(re">")
|
||||||
let query = replace(req,LANG_RE,"")
|
current[0] = langs[0].toLowerAscii()
|
||||||
if query != "":
|
current[1] = langs[1].toLowerAscii()
|
||||||
r.source = langs[0].toLowerAscii()
|
r.source = current[0].toLowerAscii()
|
||||||
r.target = langs[1].toLowerAscii()
|
r.target = current[1].toLowerAscii()
|
||||||
r.q = query
|
if query == "":
|
||||||
else:
|
main()
|
||||||
main()
|
|
||||||
else:
|
|
||||||
main("No language selections made. i.e. en>de, de>en, etc.")
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
proc answerTranslate(response: string, req: Request) =
|
proc answerTranslate(response: string, req: Request) =
|
||||||
let r = parseJson(response)
|
let r = parseJson(response)
|
||||||
try:
|
try:
|
||||||
let ans = r["translatedText"].getStr()
|
let ans = r["translatedText"].getStr()
|
||||||
main(ans, req.source & " > " & req.target)
|
main(ans)
|
||||||
except:
|
except:
|
||||||
main("Error : " & r["error"].getStr())
|
main("Error : " & r["error"].getStr())
|
||||||
|
|
||||||
|
@ -56,19 +58,52 @@ proc goTranslate(req: string) =
|
||||||
var client = newHTTPClient()
|
var client = newHTTPClient()
|
||||||
client.headers = newHttpHeaders({"Content-Type":"application/json"})
|
client.headers = newHttpHeaders({"Content-Type":"application/json"})
|
||||||
let body = %*r
|
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 != "":
|
if response.status != "":
|
||||||
answerTranslate(response.body, r)
|
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")
|
var info = newInfo("Translate")
|
||||||
info.selected_bg = green
|
info.selected_bg = green
|
||||||
var args: seq[string] = @[]
|
var args: seq[string] = @[]
|
||||||
if len(message) > 0:
|
for msg in messages:
|
||||||
for msg in message:
|
if msg != "":
|
||||||
if msg != "":
|
args.add(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("back")
|
||||||
args.add("exit")
|
args.add("exit")
|
||||||
let output = outputData(info,args)
|
let output = outputData(info,args)
|
||||||
|
@ -76,7 +111,11 @@ proc main(message:varargs[string] = @[]) =
|
||||||
return
|
return
|
||||||
elif output == "back":
|
elif output == "back":
|
||||||
main()
|
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)
|
copyToClipboard(output)
|
||||||
else:
|
else:
|
||||||
goTranslate(output)
|
goTranslate(output)
|
||||||
|
@ -84,3 +123,4 @@ proc main(message:varargs[string] = @[]) =
|
||||||
|
|
||||||
if isMainModule:
|
if isMainModule:
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
0
volume.nim
Normal file → Executable file
0
volume.nim
Normal file → Executable file
Loading…
Reference in a new issue