Last active
April 3, 2020 23:05
-
-
Save kberov/05d77ca6e89a969dfaf31e7633ae6589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojo::Base -strict; | |
use Mojo::Util qw(dumper getopt encode decode); | |
use Mojo::File 'path'; | |
getopt | |
'i|infile=s' => \my $file_in, | |
'o|outfile=s' => \my $file_out, | |
'c|cz2bg' => \my $_cz2bg_, | |
'b|bg2cz' => \my $_bg2cz_; | |
die qq|Usage | |
# Transliterate a file from CZ to BG writing and save the output to --outfile. | |
$0 --infile FILE --outfile FILE --cz2bg | |
# Transliterate a file from BG to CZ writing and print to the terminal. | |
$0 --infile FILE --bg2cz | |
| unless ($file_in && ($_cz2bg_ || $_bg2cz_)); | |
my $cz = [ | |
qw(a á b c č d ď e é ě f g h ch i í j k l m n ň o ó p q r ř s š št t ť u ú ů v w x y ý z ž ju ja) | |
]; | |
my $bg = [ | |
qw(а а́ б ц ч д дь е е́ ѣ ф г ғ х и и́ й к л м н нь о о́ п кю р р̌ с ш щ т ть у у́ у̊ в вв ѯ ы ы́ з ж ю я) | |
]; | |
my $cz2bg = {}; | |
my $bg2cz = {}; | |
for my $i (0 .. @$cz - 1) { | |
$cz2bg->{$cz->[$i]} = $bg->[$i]; | |
$cz2bg->{uc $cz->[$i]} = uc $bg->[$i]; | |
$bg2cz->{$bg->[$i]} = $cz->[$i]; | |
$bg2cz->{uc $bg->[$i]} = uc $cz->[$i]; | |
} | |
my $to = $_bg2cz_ ? $bg2cz : $cz2bg; | |
my $from = join '|', | |
sort { length $b <=> length $a } map({ucfirst} keys %$to), keys %$to; | |
#say $from; | |
my $txt = decode 'UTF-8' => path($file_in)->slurp; | |
$txt =~ s/($from)/$to->{$1} ? $to->{$1} : $1/egsm; | |
$file_out &&= IO::File->new($file_out, 'w') || die 'File could not be created:' . $!; | |
$file_out ||= \*STDOUT; | |
$file_out->say(encode 'UTF-8' => $txt); | |
$file_out->close; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment