Uno script di bash per tradurre file di testo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#!/bin/bash # uncomment and fix with appropriate values if you are behind a proxy #export https_proxy='http://localhost:3128' sl=$1 tl=$2 fileSorg=$3 fileDest=$4 base_url="https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sl}&tl=${tl}&dt=t&q=" ua='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' #if [[ "${tl}" != "es" ]]; then # sl=es #fi if [[ $1 == "help" ]]; then clear echo "HELP di traduci" echo "avviare il programma con ./traduci.sh [opzione1] [opzione2] filedilettura filediscrittura" echo "[opzione1] la sigla della nazione che si vuole tradurre" echo "[opzione2] la sigla della nazione della lingua tradotta" echo "-- le opzioni sono le sigle delle lingue supportate da google translator --" echo "[filedilettura] è il file di origine che si vuole tradurre" echo "[filediscrittura] è il file di destinazione della lingua tradotta" echo "" echo "******************************************************************************************" echo "" echo "esempio:" echo "se voglio tradurre il file inglese.txt in italiano e lo voglio salvare in un file italiano.txt" echo "./traduci.sh en it inglese.txt italiano.txt" echo "" echo "se voglio tradurre il file italiano.txt in inglese e lo voglio salvare in un file inglese.txt " echo "./traduci.sh it en italiano.txt inglese.txt" echo "" echo "******************************************************************************************" echo "by Luca Musty" exit 1 fi if [[ -f "$fileDest" ]]; then echo "file $fileDest esiste vuoi sostituirlo?" read; if [[ "$REPLY" == "s" ]]; then rm -f "$fileDest" else exit 1 fi fi echo "inizio traduzione da '$sl' a 'tsl', leggo dal file '$fileSorg' e scrivo su file '$fileDest'" let n=0 while read LREAD do let n++ done < "${fileSorg}" echo "inizio scrittura di $n righe" let n2=0 let tmp=25 while read LREAD do let n2++ #echo "lread = ${LREAD}" testo=$( echo "${LREAD}" ) ritorno=$( echo -e " \n" ) #echo "testo= $testo" let "perc=100*$n2/$n" if [[ $perc -eq $tmp ]];then echo "$perc%" let "tmp=$perc+25" fi if [[ "${#testo}" -gt 2 && "${testo}" != "" ]]; then OIFS=$IFS IFS='-->' read -a split <<< "$testo" if [[ ${#split[@]} -gt 1 ]]; then echo "${testo}" >> "${fileDest}" # echo "split $testo" else qry=$( echo ${testo} | sed -E 's/\s{1,}/\+/g' ) full_url=${base_url}${qry} response=$(curl -sA "${ua}" "${full_url}") #print only first translation from JSON testo=$( echo ${response} | sed 's/","/\n/g' | sed -E 's/\[|\]|"//g' | head -1 ) echo "${testo}" >> "${fileDest}" fi # echo $testo IFS=$OIFS unset OIFS else echo "" >> "${fileDest}" # echo "" fi done < "${fileSorg}" |
esempio per come utilizzarlo se lo script viene chiamato traduci.sh e impostato come file di avvio chmod +x traduci.sh
1 |
./traduci.sh it en fileOrigine.txt fileDestinazione.txt |