Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:
A question was asked a few days ago about online translations, language translate, where OP seemed more concerned about asking the question than getting results and writing them up legibly. I've been working on the same problem, having grown weary of all the GUI events involved in pasting text in and out of online translators. I wanted to post what what I have to address a warning and to share my experience with a wonderful tool.
Being unable to figure out what all is involved in getting a google API key, I found something else that works as a translate shell that I can manipulate using perl, and without cost. soimort translate shell was easy to install and has given me functionality that I lacked using google alone. Just today, I wrote code to get translations from 3 other translation sites. I'm gonna put abridged output, the driver .pl, and the subroutine for creating russian captions between readmore tags, then pull out lines to discuss thereafter.
$ ./1.keyboard1.pl title is 1.keyboard path1 is /home/bob/2.scripts/pages/1.keyboard abs is /home/bob/2.scripts/pages/1.keyboard/1.keyboard1.pl variable is переменн&# +1072;я переменная + is variable ini path is ...internals... object created, back with caller word is 1.keyboard dir2 is perlmonks 1.keyboard1.html 1.keyboard2.html 1.keyboard3.html files are 1.keyboard1.html 1.keyboard2.html 1.keyboard3.html old num is 3 Make rus captions(y/n)?: y matching are a.txt b.txt c.txt rus_munge is /home/bob/2.scripts/pages/1.keyboard/template_stuff/trans +lations/trans.02-11-2018-16-14-09.txt rus_path is /home/bob/2.scripts/pages/1.keyboard/template_stuff/ruscap +tions/a.txt rus_path is /home/bob/2.scripts/pages/1.keyboard/template_stuff/ruscap +tions/b.txt rus_path is /home/bob/2.scripts/pages/1.keyboard/template_stuff/ruscap +tions/c.txt Get other translations(y/n)?: y getting translation from yandex getting translation from bing getting translation from apertium remote_dir is 1.keyboard4 result is <!DOCTYPE html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="/css/1.keyboard1.css"/> <title>1.keyboard</title> </head> <body> <div class="wrapper"> <div class = origin> <h3>Vancouver, Fri Nov 2 16:14:53 2018</h3> </div> <h1>qwerty: йцукен</h1> dir is /home/bob/2.scripts/pages/1.keyboard/template_stuff/captions dir is /home/bob/2.scripts/pages/1.keyboard/template_stuff/ruscaptions tmpl is /home/bob/2.scripts/pages/1.keyboard/template_stuff/code2.tmpl Put file to server(y/n)?: y server dir is perlmonks mkdir1 failed path3 is /home/bob/2.scripts/pages/1.keyboard/template_stuff/1.keyboar +d1.css mkdir2 failed /pmimage/1.keyboard4 ... return is 1.keyboard4.html $ $ cat 1.keyboard1.pl #!/usr/bin/perl -w use 5.011; use lib "template_stuff"; use html7; use trans1; use Path::Tiny; use utf8; use Encode; use open OUT => ':encoding(UTF-8)', ':std'; # initializations that must precede main data structure my $ts = "template_stuff"; my $images = "aimages"; my $captions = "captions"; my $ruscaptions = "ruscaptions"; ## turning things to Path::Tiny # decode paths my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my $title = $path1->basename; $abs = decode('UTF-8', $abs); $path1 = decode('UTF-8', $path1); $title = decode('UTF-8', $title); say "title is $title"; say "path1 is $path1"; say "abs is $abs"; my $path2 = path( $path1, $ts ); # page params my %vars = ( title => $title, headline => undef, place => 'Vancouver', base_url => 'http://www.merrillpjensen.com', css_file => "${title}1.css", header => path( $path2, "hc_input2.txt" ), footer => path( $path2, "footer_center3.txt" ), body => path( $path2, "rebus5.tmpl" ), print_script => "1", code_tmpl => path( $path2, "code2.tmpl" ), oitop => path( $path2, "oitop.txt" ), oibottom => path( $path2, "oibottom.txt" ), to_images => path( $path2, $images ), eng_captions => path( $path2, $captions ), rus_captions => path( $path2, $ruscaptions ), translations => path( $path2, 'translations' ), bottom => path( $path2, "bottom1.txt" ), book => 'qwerty: ', chapter => 'йцукен', print_module => 0, script_file => $abs, module_tmpl => path( $path2, "code3.tmpl" ), server_dir => 'perlmonks', image_dir => 'pmimage', ts => 'template_system', css_path => $path2, ); my $variable = "переменl +5;ая"; say "variable is $variable"; my $переменна +103; = "variable"; say "переменна&# +1103; is $переменн +072;я"; my $rvars = \%vars; my $return = create_page( $rvars ); say "return is $return"; __END__ $ cd template_stuff/ $ cat trans1.pm package trans1; require Exporter; use utils1; our @ISA = qw(Exporter); our @EXPORT = qw( get_tiny make_russian_captions ); sub make_russian_captions { use 5.011; use warnings; use POSIX qw(strftime); use Path::Tiny; use Encode; use open OUT => ':encoding(UTF-8)', ':std'; my $rvars = shift; my %vars = %$rvars; my $munge = strftime( "%d-%m-%Y-%H-%M-%S\.txt", localtime ); my $in_path = path( $vars{translations}, $munge )->touchpath; my $lang = 'ru'; #system("pwd >$in_path"); works my @matching2; opendir( my $hh, $vars{eng_captions} ) or die "death $!\n"; while ( defined( $_ = readdir($hh) ) ) { if (m/txt$/) { push( @matching2, $_ ); } } #important to sort @matching2 = sort @matching2; say "matching are @matching2"; my $rus_munge = path( $vars{translations}, "trans." . $munge ); say "rus_munge is $rus_munge"; # open file for writing my $fh = path($in_path)->openw_utf8; foreach (@matching2) { my $eng_path = path( $vars{eng_captions}, $_ ); say $fh "##$_##"; my $rus_path = path( $vars{rus_captions}, $_ )->touchpath; say "rus_path is $rus_path"; my $content = path($eng_path)->slurp_utf8; $content =~ s/^\s+|\s+$//g; say $fh "$content"; system("trans :$lang file://$eng_path >$rus_path"); } print "Get other translations(y/n)?: "; my $prompt = <STDIN>; chomp $prompt; if ( $prompt eq ( "y" | "Y" ) ) { my @translators = qw /yandex bing apertium /; for my $remote (@translators) { my $trans_munge = path( $vars{translations}, "$remote." . $munge + ); ## use trans shell say "getting translation from $remote"; system("trans :$lang -e $remote file://$in_path >$trans_munge"); } } return "nothing yet"; } sub get_tiny { ...elided... } 1; $
To start with, I think I figured out what use utf8; entails with this code:
my $variable = "переменная"; say "variable is $variable"; my $переменная = "variable"; say "переменная is $переменная";
This gives
variable is переменная переменная is variable
, but with use utf8; commented out, I get:
$ ./1.keyboard1.pl Can't use global $Ð in "my" at ./1.keyboard1.pl line 65, near "my $Ð" Unrecognized character \xBF; marked by <-- HERE after my $Ð<-- HERE near column 6 at ./1.keyboard1.pl line 65. $
Line 65 is the second declaration, so you can have exotic utf8 values without use utf8;, but not variable names. (More an observation than a question.)
$sftp->mkdir("/$server_dir") or warn "mkdir1 failed $!\n"; $sftp->mkdir("/$image_dir") or warn "mkdir2 failed $!\n";
Now for the questions. I'm looking for a way to make the failure of mkdir more informative for the case that the directory already exists. I keep the mkdir line so that it will work the first time it is used. Every subsequent time, it fails, and $! is stone silent. This module is on github at html7.pm.
This is supposedly the list of engines for use, but I have found that spell, aspell, and apertium simply fail, and deepl gives me instead what looks to be a good french translation. I have google make my default captions, and then consult what bing and yandex have done. For what I have seen, if you're making russian translations, yandex might be the best.
$ trans -S aspell * google deepl bing spell apertium yandex $
The functionality I would like to add are conditions such that if a translate call hangs, I can get to the next one, so what I'm fishing for is code that would go the next in the for loop if it lasts for, say, a minute.
print "Get other translations(y/n)?: "; my $prompt = <STDIN>; chomp $prompt; if ( $prompt eq ( "y" | "Y" ) ) { my @translators = qw /yandex bing apertium /; for my $remote (@translators) { my $trans_munge = path( $vars{translations}, "$remote." . $munge + ); ## use trans shell say "getting translation from $remote"; system("trans :$lang -e $remote file://$in_path >$trans_munge"); }
Finally, I leave some examples of usage for those interested enough to delve into readmore tags. It shows how different engines can be used, and how simple it is to re-translate phrases and see what I call "the synonym cloud," which I find critical in getting the "flavor" of words. What translators will do with strings like "Windows", "plug and play," Gedit, Georgia is idiomatic, and you can't hold it against a machine that it *isn't* supposed to translate certain words. Example of output here figuring out how to work my new cyrillic keyboard.
$ trans -e yandex :ru dog dog собака (sobaka) English -> Русский $ trans -e Bing :ru dog dog Собака English -> Русский $ trans снимок снимок (snimok) snapshot Definitions of снимок Русский -> English noun snapshot снимок, фотография, фотокарточка, выстрел, выстрел навскидку снимок snapshot $ trans :ru "voter suppression" voter suppression голосовать за удаление (golosovat' za udaleniye) Translations of voter suppression Français -> Русский voter suppression голосовать за удаление, подавление голосов $ trans голосовать за удаление голосовать (golosovat') vote Definitions of голосовать Русский -> English verb vote голосовать, вносить предложение, баллотировать, ассигновать, признавать, выделять poll голосовать, проводить голосование, подрезать верхушку, подсчитывать голоса, получать голоса, стричь волосы ballot голосовать, баллотировать, тянуть жребий vote by ballot голосовать take a ballot голосовать elect by ballot голосовать divide делить, разделять, делиться, разбивать, разделяться, голосовать poll one's vote голосовать phrase cast a vote голосовать, подавать голос голосовать vote, to vote за (za) for Definitions of за български -> English preposition for за, на, по, в продължение на, от, с ... for one's sake за prefix pro- про-, в полза на, за за for удаление (udaleniye) deletion Definitions of удаление Русский -> English noun remove удаление, шаг, колено, ступень, степень отдаления, стадия removal удаление, устранение, отстранение, перемещение, снос, смещение disposal удаление, распоряжение, размещение, избавление, продажа, устранение deletion удаление, стирание, вычеркивание, вымарывание, вымарка, то, что вычеркнуто excision удаление, иссечение, вырезание, вырезка, отрезание, вырезывание ablation удаление, устранение, таяние ледников, вырезывание, размывание пород withdrawal изъятие, отход, удаление, вывод войск, увод, отозвание resection резекция, удаление, обратная засечка, вырезание, устранение extirpation экстирпация, удаление, искоренение, истребление, вылущение, выведение deletion, removal $
Thank you for your comments,
|
|---|