soimort / translate-shell

:speech_balloon: Command-line translator using Google Translate, Bing Translator, Yandex.Translate, etc.
https://www.soimort.org/translate-shell
The Unlicense
7.01k stars 392 forks source link

command line parsing is bogus; auto-correction not always shown #524

Open sdaoden opened 1 month ago

sdaoden commented 1 month ago

Hello. It is good to have your tool around. I yesterday stumbled over a problem once i produced a typo:

trans -verbose -show-original Y -show-translation Y en=de unfulfilld
unfulfilld

unerfüllt

Translations of unfulfilld
[ English -> Deutsch ]

unfulfilld
    unerfüllt, nicht erfüllt

I wondered as the above looks as if "unfulfilld" is the correct word, and no "unfulfilled" in sight there. It only is with -dump:

trans -verbose -dump -show-original Y -show-translation Y en=de unfulfilld
1b9
[[["unerfüllt","unfulfilld",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]]],null,"en",null,null,[["unfulfilld",null,[["unerfüllt",null,true,false,[3],null,[[3]]],["nicht erfüllt",null,true,false,[8]]],[[0,10]],"unfulfilld",0,0]],1,["\u003cb\u003e\u003ci\u003eunfulfilled\u003c/i\u003e\u003c/b\u003e","unfulfilled",[1]],[["en"],null,[1],["en"]],null,null,null,null,null,null,null,null,null,[null,2]]
0

And now comes the fun part: mess up the command line and it works!

$ trans de=en -verbose -show-translation unfulfilld

^C  # < this hangs and needs interruption!
$ trans -verbose -show-translation de=en unfulfilld
unfulfilld

unfulfilld
$ trans de=en -verbose -show-translation de=en unfulfilld
Showing translation for:  (use -no-auto to disable autocorrect)
unfulfilled # << there it is!!

unfulfilled

Translations of unfulfilled
[ Deutsch -> English ]

unfulfilled
    unfulfilled, unfilled
$ trans en=de -verbose -show-translation en=de unfulfilld
unfulfilld

unerfüllt

Translations of unfulfilld
[ English -> Deutsch ]

unfulfilld
    unerfüllt, nicht erfüllt

With -dump it is sane:

a$ trans en=de -verbose -dump unfulfilld
1b9
[[["unerfüllt","unfulfilld",null,null,3,null,null,[[]],[[["84d48e73ebfa38d4d681515b81e0b72a","en_de_2023q1.md"]]]]],null,"en",null,null,[["unfulfilld",null,[["unerfüllt",null,true,false,[3],null,[[3]]],["nicht erfüllt",null,true,false,[8]]],[[0,10]],"unfulfilld",0,0]],1,["\u003cb\u003e\u003ci\u003eunfulfilled\u003c/i\u003e\u003c/b\u003e","unfulfilled",[1]],[["en"],null,[1],["en"]],null,null,null,null,null,null,null,null,null,[null,2]]
0
sdaoden commented 1 month ago

maybe it is interpretation of the response: i print the ast iter from googleTranslate() here, maybe you can get something out of that:

$ trans en=de unfulfilld
ITER i=0000
        VALUE 
HERE i<0000> XX<^075$> |||||| ELSE 
ITER i=0001
        VALUE 
HERE i<0001> XX<^075$> |||||| ELSE 
ITER i=0002
ITER i=0003
ITER i=0004
        VALUE <3>
HERE i<0004> XX<^075$> |||||| ELSE 
ITER i=0005
ITER i=0006
ITER i=0008000
        VALUE <84d48e73ebfa38d4d681515b81e0b72a>
HERE i<0008000> XX<^075$> |||||| ELSE 
ITER i=0008001
        VALUE 
HERE i<0008001> XX<^075$> |||||| ELSE 
ITER i=01
ITER i=02
        VALUE 
HERE i<02> XX<^075$> |||||| ELSE 
ITER i=03
ITER i=04
ITER i=0500
        VALUE 
HERE i<0500> XX<^075$> |||||| ELSE 
ITER i=0501
ITER i=050200
        VALUE 
HERE i<050200> XX<^075$> |||||| ELSE 
ITER i=050201
ITER i=050202
        VALUE 
HERE i<050202> XX<^075$> |||||| ELSE 
ITER i=050203
        VALUE 
HERE i<050203> XX<^075$> |||||| ELSE 
ITER i=0502040
        VALUE <3>
HERE i<0502040> XX<^075$> |||||| ELSE 
ITER i=050205
ITER i=05020600
        VALUE <3>
HERE i<05020600> XX<^075$> |||||| ELSE 
ITER i=050210
        VALUE 
HERE i<050210> XX<^075$> |||||| ELSE                                                                                                                           20:21:52 [20/15831]
ITER i=050211
ITER i=050212
        VALUE 
HERE i<050212> XX<^075$> |||||| ELSE 
ITER i=050213
        VALUE 
HERE i<050213> XX<^075$> |||||| ELSE 
ITER i=0502140
        VALUE <8>
HERE i<0502140> XX<^075$> |||||| ELSE 
ITER i=050300
        VALUE <0>
HERE i<050300> XX<^075$> |||||| ELSE 
ITER i=050301
        VALUE <10>
HERE i<050301> XX<^075$> |||||| ELSE 
ITER i=0504
        VALUE 
HERE i<0504> XX<^075$> |||||| ELSE 
ITER i=0505
        VALUE <0>
HERE i<0505> XX<^075$> |||||| ELSE 
ITER i=0506
        VALUE <0>
HERE i<0506> XX<^075$> |||||| ELSE 
ITER i=06
        VALUE <1>
HERE i<06> XX<^075$> |||||| ELSE 
ITER i=070
        VALUE <unfulfilled>
HERE i<070> XX<^075$> |||||| ELSE 
ITER i=071
        VALUE 
HERE i<071> XX<^075$> |||||| ELSE 
ITER i=0720
        VALUE <1>
HERE i<0720> XX<^075$> |||||| ELSE 
ITER i=0800
        VALUE 
HERE i<0800> XX<^075$> |||||| ELSE 
ITER i=081
ITER i=0820
        VALUE <1>
HERE i<0820> XX<^075$> |||||| ELSE 
ITER i=0830
        VALUE 
HERE i<0830> XX<^075$> |||||| ELSE 
ITER i=09
ITER i=010
ITER i=011
ITER i=012
ITER i=013
ITER i=014
ITER i=015
ITER i=016
ITER i=017
ITER i=0180
ITER i=0181
        VALUE <2>
HERE i<0181> XX<^075$> |||||| ELSE 
ITER i=0
        VALUE <0>
HERE i<0> XX<^075$> |||||| ELSE 
unfulfilld

unerfüllt

Translations of unfulfilld
[ English -> Deutsch ]

unfulfilld
    unerfüllt, nicht erfüllt

Silent now, ciao!

sdaoden commented 1 month ago

(one more: the ELSE is followed by <unfulfilled> here, the textbox swallowed the "unknown tag". Ciao.)

sdaoden commented 1 month ago

Ok i tell you what, one final addition. I locally change in include/Translators/GoogleTranslate.awk function googleTranslate() from

        # 7 - autocorrection
        if (i ~ "^0" SUBSEP "7" SUBSEP "5$") {

to

        # 7 - autocorrection
        if (i ~ "^0" SUBSEP "7" SUBSEP "5$" || i ~ "^0" SUBSEP "7" SUBSEP "2" SUBSEP "0$") {

and the software does what i want:

$ trans en=de unfulfilld
Did you mean: unfulfilled
unfulfilld

unerfüllt

Translations of unfulfilld
[ English -> Deutsch ]

unfulfilld
    unerfüllt, nicht erfüllt

but i have no idea what i am doing!

But now really: Ciao!

sdaoden commented 1 month ago

I will update my port with that, but really, i have no idea

diff --git a/include/Translators/GoogleTranslate.awk b/include/Translators/GoogleTranslate.awk
index 8f506e2fb5..456e0e6aa8 100644
--- a/include/Translators/GoogleTranslate.awk
+++ b/include/Translators/GoogleTranslate.awk
@@ -155,12 +155,12 @@ function googleTranslate(text, sl, tl, hl,
             altTranslations[group[1]][group[2]] = literal(ast[i])

         # 7 - autocorrection
-        if (i ~ "^0" SUBSEP "7" SUBSEP "5$") {
-            if (ast[i] == "true")
-                w("Showing translation for:  (use -no-auto to disable autocorrect)")
+        if (i ~ "^0" SUBSEP "7" SUBSEP "5$" || i ~ "^0" SUBSEP "7" SUBSEP "2" SUBSEP "0$") {
+            if (ast[i] == "true" || ast[i] == "1")
+                msg = "Showing translation for (use -no-auto to disable autocorrect): "
             else
-                w("Did you mean: "                                      \
-                  ansi("bold", unparameterize(ast["0" SUBSEP "7" SUBSEP "1"])))
+                msg = "Did you mean: "
+            w(msg ansi("bold", unparameterize(ast["0" SUBSEP "7" SUBSEP "1"])))
         }

         # 8 - identified source languages