Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- *** IBM chave de API <sua chave:>
- *
- ***IBM credencial Tradutor <sua chave:>
- *
- ***IBM URL credencial <seu_link:>
- *
- * @author Pedro Serer
- */
- error_reporting(0);
- /**
- * *Função para tradução de arquivos PGN
- * * @param string (A entrada é a string a ser traduzida).
- *
- * * @return string (O retorno é a string de entrada
- * em formato Json e traduzida).
- */
- function tradutor ($string) {
- $postfields = [
- 'text' => "['$string']",
- 'source' => 'en',
- 'target' => 'pt'
- ];
- $data_string = json_encode($postfields);
- $header = [
- 'Authorization: Basic '. base64_encode('apikey:<suachave>'),
- 'Content-type: application/json',
- 'Content-length: '.strlen($data_string)
- ];
- //Habilitar isso quando for debugar
- # ob_start();
- # $out = fopen('php://output', 'w');
- $url = 'https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01';
- $cl = curl_init();
- curl_setopt($cl, CURLOPT_URL, $url);
- curl_setopt($cl, CURLOPT_POST, true);
- curl_setopt($cl, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($cl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($cl, CURLOPT_VERBOSE, true);
- curl_setopt($cl, CURLOPT_STDERR, $out);
- # fclose($out);
- # $debug = ob_get_clean();
- # echo $debug;
- $ret = curl_exec($cl); //ret == retorno
- curl_close($cl);
- return $ret;
- }
- /**
- * Trecho que lê o aquivo escolhido pelo usuário
- */
- $filename = $argv[1];
- $file = fopen($filename, 'a+');
- $nPgn = fopen('_Nova'.$filename, 'w');
- echo "\n\n";
- echo "----------------------------------------------------------\n";
- echo "------------------ By Pedro Serer ------------------------\n";
- echo "----------------------------------------------------------";
- echo "\n\nPGN TRANSLATOR \n\t-- Digitar no terminal: traduzirPgn.php pgn.txt\n";
- echo "\t-- (ctrl + C) para cancelar...\n";
- echo "\nTraduzindo....";
- while (!feof($file))
- {
- echo ".";
- $line = fgets($file, filesize($filename)) . '{kkk}';
- $parse = json_decode(tradutor($line), true);
- $write = fwrite($nPgn, $parse['translations'][0]['translation']);
- if (!$write) {
- echo "\nErro ao traduzir! Tente novamente...\n";
- break;
- }
- }
- fclose($file);
- fclose($nPgn);
- echo "\n\nFinalizado! Pressione qualquer botao para sair...\n";
- system('pause');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement