Advertisement
Guest User

PHP translate TXT to PGN

a guest
Dec 13th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2.   /**
  3.   *
  4.   *** IBM chave de API <sua chave:>
  5.   *
  6.   ***IBM credencial Tradutor <sua chave:>
  7.   *
  8.   ***IBM URL credencial <seu_link:>
  9.   *
  10.   * @author Pedro Serer
  11.   */
  12.  
  13.   error_reporting(0);
  14.  
  15.   /**
  16.   *  *Função para tradução de arquivos PGN
  17.   *  * @param string (A entrada é a string a ser traduzida).
  18.   *
  19.   *  * @return string (O retorno é a string de entrada
  20.   *   em formato Json e traduzida).
  21.   */
  22.  
  23.   function tradutor ($string) {
  24.  
  25.       $postfields = [
  26.           'text'   => "['$string']",
  27.           'source' => 'en',
  28.           'target' => 'pt'
  29.       ];
  30.  
  31.       $data_string = json_encode($postfields);
  32.  
  33.       $header = [
  34.           'Authorization: Basic '. base64_encode('apikey:<suachave>'),
  35.           'Content-type: application/json',
  36.           'Content-length: '.strlen($data_string)
  37.       ];
  38.  
  39.       //Habilitar isso quando for debugar
  40.       # ob_start();
  41.      # $out = fopen('php://output', 'w');
  42.  
  43.       $url = 'https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01';
  44.       $cl  = curl_init();
  45.  
  46.       curl_setopt($cl, CURLOPT_URL, $url);
  47.       curl_setopt($cl, CURLOPT_POST, true);
  48.       curl_setopt($cl, CURLOPT_POSTFIELDS, $data_string);
  49.       curl_setopt($cl, CURLOPT_HTTPHEADER, $header);
  50.       curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
  51.       curl_setopt($cl, CURLOPT_VERBOSE, true);
  52.       curl_setopt($cl, CURLOPT_STDERR, $out);
  53.  
  54.       # fclose($out);
  55.      # $debug = ob_get_clean();
  56.      # echo $debug;
  57.  
  58.       $ret = curl_exec($cl); //ret == retorno
  59.       curl_close($cl);
  60.  
  61.       return $ret;
  62.   }
  63.  
  64.  
  65.   /**
  66.   * Trecho que lê o aquivo escolhido pelo usuário
  67.   */
  68.  
  69.   $filename = $argv[1];
  70.   $file = fopen($filename, 'a+');
  71.   $nPgn = fopen('_Nova'.$filename, 'w');
  72.  
  73.   echo "\n\n";
  74.   echo "----------------------------------------------------------\n";
  75.   echo "------------------ By Pedro Serer ------------------------\n";
  76.   echo "----------------------------------------------------------";
  77.   echo "\n\nPGN TRANSLATOR \n\t-- Digitar no terminal: traduzirPgn.php pgn.txt\n";
  78.   echo "\t-- (ctrl + C) para cancelar...\n";
  79.  
  80.   echo "\nTraduzindo....";
  81.  
  82.   while (!feof($file))
  83.   {
  84.     echo ".";
  85.     $line  = fgets($file, filesize($filename)) . '{kkk}';
  86.     $parse = json_decode(tradutor($line), true);
  87.     $write = fwrite($nPgn, $parse['translations'][0]['translation']);
  88.  
  89.     if (!$write) {
  90.       echo "\nErro ao traduzir! Tente novamente...\n";
  91.       break;
  92.     }
  93.   }
  94.  
  95.   fclose($file);
  96.   fclose($nPgn);
  97.  
  98.   echo "\n\nFinalizado! Pressione qualquer botao para sair...\n";
  99.   system('pause');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement