Completed "Command line google translation tool"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Completed "Command line google translation tool"
# 1  
Old 08-12-2012
Completed "Command line google translation tool"

This tool for access translate.google.com from terminal and English dictionary.

main mirror https://github.com/Anoncheg1/Command-line-translator
mirror [Bash] Google translate from command line and some more features - Pastebin.com

requirements: bash, cURL, SpiderMonkey, forvo.com account for audio

FEATURES:
- translated text, fixed text with highlight, language detection, dictionary, translit
for english:
- phrases, ideom, transcription, audio pronunciation
- cache for words
- saving english words to file for learning

for convenience:
~/.bash_aliases
alias t="/home/user/translate"
alias ts="/home/user/translate -s"

Image

Image

Image

Image
old version:
Code:
#!/bin/bash
# This tool for access translate.google.com from terminal and to have English dictionary.
# mirror https://github.com/Anoncheg1/Command-line-translator
# mirror http://pastebin.com/kPTYjY6W
# requirements: curl, Mozilla js shell, forvo.com account
# + output: translated text, fixed text with highlight, dictionary, phrases, transcription, audio pronunciation
# + cache for all words
# + saving words to file for learning

trap bashtrap INT
bashtrap()
{
    echo "CTRL+C Detected"
    rm "$cachefile" 2> /dev/null
    exit
}

help='translate [-s] <text>
if text is english, target language is ENGLISH_TARGET_LANG
otherwise, "target" language is english
-s, --sound Enable sound for one word'

# adjust to taste
ENGLISH_TARGET_LANG=ru        #target language for english request
target=en             #target for all languages except english
flogin=121212             #forvo.com login and pass REQUIRED!
fpass=121212
words_buffer=3000
PR_DIR="/home/$USER/.translate"
timeout=5

jsout_dictionary="var googlearr = eval(JSON.stringify(myJSONObject));
var rsum=\"\"; //translated text
if(typeof googlearr[0] !== 'undefined' && googlearr[0] !== null){  //summing sentences
    for (var i = 0; i < googlearr[0].length; i++){
        if(typeof googlearr[0][i][0] !== 'undefined' && googlearr[0][i][0] !== null){
            rsum=rsum+googlearr[0][i][0];
        }
    }
}
print(\"\033[1;32m\"+rsum+\"\033[0m\");
/*arr=googlearr;
for (var c = 0; c < arr.length; c++){ //testing
    if(typeof arr[c] !== 'undefined' && arr[c] !== null){ //dictionary output
        for (var i = 0; i < arr[c].length; i++){
            if(typeof arr[c][i] !== 'undefined' && arr[c][i] !== null){
                for (var e = 0; e < arr[c][i].length; e++){
                    print(arr[c][i][e]);
                }
            }
        }
    }
}*/
function dictionary(arr){ //dictionary output
    if(typeof arr[1] !== 'undefined' && arr[1] !== null){
        for (var i = 0; i < arr[1].length; i++){
            if(typeof arr[1][i][0] !== 'undefined' && arr[1][i][0] !== null){
                print(\"\033[1;34m\"+arr[1][i][0]+\"\033[0m\");
                for (var e = 0; e < arr[1][i][2].length; e++){
                    print(arr[1][i][2][e]);
                }
            }
        }
    }
}"

request=($*)
sound=0
if [[ $1 = -h || $1 = --help ]]; then
    echo "$help"
    exit
fi
if [[ $1 == -s || $1 == --sound ]];then
    sound=1
    request[0]=""
fi
request=${request[*]}

source=auto
tchar=${request:0:1}
tcharnum=$(printf "%d" "'${tchar}")
if [[ $tcharnum -ge 65 && $tcharnum -le 122 ]]; then #if request is english
    source=en
    target="$ENGLISH_TARGET_LANG" #if text is english, "target" language is russian
fi

[ ! -d "$PR_DIR" ] && mkdir "$PR_DIR"
[ ! -d "$PR_DIR"/cache ] && mkdir "$PR_DIR"/cache

r_words_count=$(echo "$request"  |wc -w)

if [[ ${#request} -gt 300 ]]; then
    result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$request" http://translate.google.com)
    encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result")
    #iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8
    echo -e "\033[32;1m"$(iconv -f $encoding <<<"$result" |  awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8)"\033[0m"

else
    cachefile="$PR_DIR/cache/$request"
    if [[ ! -e "$cachefile" || (( $sound == 1 && ! -e "$cachefile".mp3 )) ]]; then
        grespond=$(curl -s -i --user-agent "" --data-urlencode "text=$request" "http://translate.google.com/translate_a/t?client=t&hl=$target&sl=$source&tl=$target&ie=UTF-8&oe=UTF-8&multires=1&ssel=0&tsel=0&sc=1") #getting google respond for short sentence

        #echo -n "var myJSONObject = " > "$PR_DIR"/tmpjsobj2
        #echo [$(echo "$grespond" | grep -o '"[^"]*/i[^"]*"')"];" >> "$PR_DIR"/tmpjsobj2
        #echo [$(echo "$grespond" | grep -o '[^"]*/i[^"]*')"];"
        #echo -e "var googlearr = eval(JSON.stringify(myJSONObject));\n print(googlearr);" >> "$PR_DIR"/tmpjsobj2
        #js "$PR_DIR"/tmpjsobj2
        if [[ $r_words_count -le 2 ]];then #getting google fixed text from $grespond
            fl=$(echo "$grespond" | grep -o '[^"]*/i[^"]*' | sed 's/\\u003cb\\u003e\\u003ci\\u003e//g' | sed 's/\\u003c\/i\\u003e\\u003c\/b\\u003e//g' | sed 's/\\u0026//g' | sed "s/\#39;/'/g")
            diffnum=$(cmp -l <(echo -n $request) <(echo -n $fl) 2>/dev/null | head -n 1 | sed 's/\([0-9][0-9]\?\).*/\1/') 
#            echo $diffnum
            [[ $diffnum > 0 ]] && fl="$(tput bold)$(tput setaf 3)${fl:0:$diffnum-1}$(tput bold)$(tput setaf 1)${fl:$diffnum-1:1}$(tput bold)$(tput setaf 3)${fl:$diffnum}$(tput sgr0)" #highlight difference in one word
        else
            fl=$(echo "$grespond" | grep -o '[^"]*/i[^"]*' | sed 's/\\u003cb\\u003e\\u003ci\\u003e/'$(tput bold)$(tput setaf 3)'/g' | sed 's/\\u003c\/i\\u003e\\u003c\/b\\u003e/'$(tput sgr0)'/g' | sed 's/\\u0026//g' | sed "s/\#39;/'/g" ) #google fixed text
        fi
#        echo $grespond | grep -o '\[.*\]' 

        echo -n "var myJSONObject = " > "$PR_DIR"/tmpjsobj
        echo -n "$grespond" | grep -o '\[.*\]' >> "$PR_DIR"/tmpjsobj
        echo ";">> "$PR_DIR"/tmpjsobj
        echo -n "$jsout_dictionary" >> "$PR_DIR"/tmpjsobj
        [[ ! $fl ]] &&  echo -n "dictionary(googlearr);" >> "$PR_DIR"/tmpjsobj
    
        #for english only. but it may be altered
        if [[ $r_words_count -eq 1 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') !=  $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then #dictionary

                js "$PR_DIR"/tmpjsobj > "$cachefile" #google translated text and dictionary to cache

                raw_phras=$(curl -s --user-agent "" http://www.macmillandictionary.com/dictionary/american/"$(echo $request | tr ' ' - )" | grep -o '<li ID.*End of DIV SENSE--></li>' | sed 's/<.\?span[^>]*>//g' )
                phras=$(echo $raw_phras |  sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"h2">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2\./') #getting phrases
                phras2=$(echo $raw_phras |  sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"EXAMPLE">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2/') #getting examples
                if [[ $phras ]]; then
                    echo -e "\033[1;35m"PHRASES:$(tput sgr0) >> "$cachefile"
                    join --nocheck-order -a 1 -a 2 <(echo "$phras") <(echo "$phras2")  | sed '/^$/d' >> "$cachefile"
#                    echo "$phras" > tm1
#                    echo "$phras2" >tm2
#                    echo -e "\033[34m"http://www.macmillandictionary.com/dictionary/american/"$request"$(tput sgr0) >> "$cachefile"
                fi

                trans=$(curl -s --user-agent "" http://lingvopro.abbyyonline.com/en/Translate/en-ru/"$request" | grep -o '"[^"]*Handlers/TranscriptionHandler\.ashx[^"]*"' | sed 's/.*=\(.*\)"/\1/'| echo -n -e $(sed 's/+/ /g; s/%/\\x/g')) #getting transcription
                [[ $trans ]] && echo "[$trans]" >> "$cachefile"

                cat "$cachefile" 2>/dev/null #output
                echo -e "\033[34m"http://oxforddictionaries.com/definition/english/"$request"$(tput sgr0) # just another good english dictionary

                if [[ $trans ]] ; then

                    #saving words
                    if [[ ! $(grep "$request" "$PR_DIR"/translated_words 2>/dev/null) ]]; then
                        #echo -e "$request \t\t\t\t\t\t\t\t\t\t [$trans]" >> "$PR_DIR"/translated_words
                        echo -e "$request" >> "$PR_DIR"/translated_words
                    fi

                    #getting sound (always) from forvo.com
                    if [[ ! -e "$cachefile".mp3 ]]; then
                        curl -s -c "$PR_DIR"/tmpcookie --connect-timeout $timeout -m $timeout --user-agent "" -d "login=$flogin&password=$fpass" http://ru.forvo.com/login/ -o/dev/null
                        if [[ -e "$PR_DIR"/tmpcookie ]]; then
                            slink=$(curl -s -b $PR_DIR/tmpcookie --connect-timeout $timeout -m $timeout http://ru.forvo.com/word/"$request"/ | grep -o '[^"]*/download/mp3/'"$request"'/en/[^"]*' |head -n 1 )
                            [[ $slink ]] && curl -s -b "$PR_DIR"/tmpcookie --connect-timeout $timeout -m $timeout --user-agent "" -o "$cachefile".mp3 http://ru.forvo.com"$slink"
                            rm "$PR_DIR"/tmpcookie 2>/dev/null
                        else
                            echo fail to get sound from forvo.com
                        fi
                    fi
                fi
                [[ $(ls "$cachefile" | wc -l) -gt $words_buffer ]] && find "$PR_DIR"/cache -mtime +20 -delete #cache cleaning
        else
            js "$PR_DIR"/tmpjsobj #google dictionary and translated text output
            [[ $fl ]] && echo $fl #google fixed text output

            if [[ $r_words_count -gt 1 && $r_words_count -le 3 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') !=  $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then #phrases for 2 3 words of phrasal verbs
                raw_phras=$(curl -s --user-agent "" http://www.macmillandictionary.com/dictionary/american/"$(echo $request | tr ' ' - )" | grep -o '<li.*End of DIV SENSE--></li>' | sed 's/<.\?span[^>]*>//g' )
                phras=$(echo $raw_phras |  sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"h2">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2\./') #getting phrases
                phras2=$(echo $raw_phras |  sed 's/End of DIV SENSE--><\/li>/End of DIV SENSE--><\/li>\n/g' | grep -n -o '"EXAMPLE">[^>]*<' | sed 's/^\([^:]*\):.*>\([^<]*\).*/\1 \2/') #getting examples
                if [[ $phras2 ]]; then
                    echo -e "\033[1;35m"PHRASES:$(tput sgr0)
                    join --nocheck-order -a 1 -a 2 <(echo "$phras") <(echo "$phras2") | sed '/^$/d'
                fi
            fi
        fi
    else #cache output
        cat "$cachefile" #output
        echo -e "\033[34m"http://oxforddictionaries.com/definition/english/"$request"$(tput sgr0)
    fi
    #sound output
    if [[ $sound == 1 && -e "$cachefile".mp3 ]]; then 
        stat=$(playsound --loop 1 "$cachefile".mp3 2>&1 | grep -o 'Couldn')
        [[ $stat ]] && rm "$cachefile".mp3
    fi
fi

exit


Last edited by 654321; 12-14-2012 at 02:03 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

User gets "The operation could not be completed because you do not have enough access privileges."

Does anyone know why this could be happening? User tries to save into a directory, but gets the error message. I checked permissions on directory and see the following: drwxrwxr-x 10 root pm 1024 Nov 22 14:46 Folder 1 drwxrwxr-x 10 root pm 1024 Oct 19 ... (1 Reply)
Discussion started by: scrivic
1 Replies

8. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

9. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question