| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2007-2008 ... | ||||||||||||||||||||||||||||
| 5 | # Alex Linke <alinke@lingua-systems.com> | ||||||||||||||||||||||||||||
| 6 | # Rona Linke <rlinke@lingua-systems.com> | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2009-2016 Lingua-Systems Software GmbH | ||||||||||||||||||||||||||||
| 8 | # Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH | ||||||||||||||||||||||||||||
| 9 | # Copyright (C) 2017 Netzum Sorglos Software GmbH | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | require 5.008; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Lingua::Translit; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Lingua::Translit::Tables qw/:all/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | my $VERSION = '0.5'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =pod | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | translit - transliterate text between various writing systems | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | translit B<-t> I<name> B<-r> B<-i> I<file> B<-o> I<file> -l -v -h | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | B<translit> transliterates B<UTF-8> encoded natural language text from one | ||||||||||||||||||||||||||||
| 35 | writing system to another. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | It utilizes the Perl module L<Lingua::Translit> to accomplish this task and | ||||||||||||||||||||||||||||
| 38 | follows the provided transliteration standards as strict as possible. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | B<translit> regards the following commandline switches: | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =over 4 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item B<--trans>, B<-t> I<name> | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Use the transliteration standard named I<name>. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item B<--reverse>, B<-r> | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Transliterate in reverse direction. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | B<NOTE>: Not every transliteration is capable of being reversed. | ||||||||||||||||||||||||||||
| 53 | If unsure, have a look at the list of supported transliterations using the | ||||||||||||||||||||||||||||
| 54 | B<--list> switch. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item B<--infile>, B<-i> I<FILE> | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Read text from I<FILE>. Defaults to STDIN. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item B<--outfile>, B<-o> I<FILE> | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Write the transliterated text to I<FILE>. Defaults to STDOUT. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =item B<--list>, B<-l> | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Print a list of supported transliterations showing their | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =over 4 | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item * | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | name | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item * | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | reversibility | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item * | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | description | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =back | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =item B<--verbose>, B<-v> | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Print verbose status messages to STDERR. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item B<--help>, B<-h> | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Show a short help text introducing command line switches only. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =back | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Set defaults where possible | ||||||||||||||||||||||||||||
| 97 | 4 | my %opt = ( | |||||||||||||||||||||||||||
| 98 | trans => "", | ||||||||||||||||||||||||||||
| 99 | infile => "", | ||||||||||||||||||||||||||||
| 100 | outfile => "", | ||||||||||||||||||||||||||||
| 101 | reverse => 0, | ||||||||||||||||||||||||||||
| 102 | list => 0, | ||||||||||||||||||||||||||||
| 103 | verbose => 0, # off | ||||||||||||||||||||||||||||
| 104 | help => 0 | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | show_help(1) | ||||||||||||||||||||||||||||
| 108 | unless GetOptions( | ||||||||||||||||||||||||||||
| 109 | "trans|t=s" => \$opt{trans}, | ||||||||||||||||||||||||||||
| 110 | "infile|i=s" => \$opt{infile}, | ||||||||||||||||||||||||||||
| 111 | "outfile|o=s" => \$opt{outfile}, | ||||||||||||||||||||||||||||
| 112 | "reverse|r" => \$opt{reverse}, | ||||||||||||||||||||||||||||
| 113 | "list|l" => \$opt{list}, | ||||||||||||||||||||||||||||
| 114 | "verbose|v" => \$opt{verbose}, | ||||||||||||||||||||||||||||
| 115 | "help|h" => \$opt{help} | ||||||||||||||||||||||||||||
| 116 | 4 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 4 | ▶ | show_help(0) if $opt{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 2 | ▶ | show_list() if $opt{list}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | show_help(1) unless $opt{trans}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Assure the requested transliteration is supported... | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | die "$opt{trans} is not supported.\n" unless translit_supported( $opt{trans} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # ...and reverse transliteration is supported, too - if requested | ||||||||||||||||||||||||||||
| 125 | die "$opt{trans} cannot be reversed.\n" | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( $opt{reverse} && !translit_reverse_supported( $opt{trans} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{"reverse"} and not translit_reverse_supported($opt{"trans"})
MC/DC0%0/2
$opt{"reverse"} and not translit_reverse_supported($opt{"trans"})
$opt{"reverse"}
not translit_reverse_supported($opt{"trans"})
Truth table0%0/3
A and not(B)
A= $opt{"reverse"}B= not translit_reverse_supported($opt{"trans"})
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # If no input file was specified, use STDIN as a fallback. This way, translit | ||||||||||||||||||||||||||||
| 129 | # may also be used in pipes! | ||||||||||||||||||||||||||||
| 130 | 0 | my $in; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( $opt{infile} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | print STDERR "Reading input from $opt{infile}...\n" if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | open IN, "$opt{infile}" or die "$opt{infile}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | $in = *IN; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | print STDERR "Reading input from STDIN...\n" if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $in = *STDIN; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # If no output file was specified, use STDOUT as a fallback. | ||||||||||||||||||||||||||||
| 146 | 0 | my $out; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ( $opt{outfile} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | print STDERR "Writing output to $opt{outfile}...\n" if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | open OUT, "> $opt{outfile}" or die "$opt{outfile}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | $out = *OUT; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | print STDERR "Writing output to STDOUT...\n" if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $out = *STDOUT; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Slurp in all the input and close filehandle | ||||||||||||||||||||||||||||
| 162 | 0 | local $/; | |||||||||||||||||||||||||||
| 163 | 0 | my $text = <$in>; | |||||||||||||||||||||||||||
| 164 | 0 | close($in); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Transliterate | ||||||||||||||||||||||||||||
| 167 | 0 | my $tr = new Lingua::Translit( $opt{trans} ); | |||||||||||||||||||||||||||
| 168 | 0 | my $text_tr; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | unless ( $opt{reverse} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | print STDERR "Transliterating according to ", $tr->name(), "...\n" | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | $text_tr = $tr->translit($text); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | else { | ||||||||||||||||||||||||||||
| 177 | print STDERR "Transliterating according to ", $tr->name(), " (reverse)...\n" | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if $opt{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | $text_tr = $tr->translit_reverse($text); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Write the transliterated text to the filehandle and close it | ||||||||||||||||||||||||||||
| 184 | 0 | print $out $text_tr; | |||||||||||||||||||||||||||
| 185 | 0 | close($out); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub show_help { | ||||||||||||||||||||||||||||
| 188 | 2 | ▶ | my $retval = shift(); | ||||||||||||||||||||||||||
|
Subroutine
show_help: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 2 | print "translit v$VERSION -- ", | |||||||||||||||||||||||||||
| 191 | "(c) 2009-2016 Lingua-Systems Software GmbH\n\n", | ||||||||||||||||||||||||||||
| 192 | "usage: $0 -i FILE -o FILE -t NAME -r -l -v -h\n\n", | ||||||||||||||||||||||||||||
| 193 | " --infile -i FILE read input from FILE\n", | ||||||||||||||||||||||||||||
| 194 | " --outfile -o FILE write output to FILE\n", | ||||||||||||||||||||||||||||
| 195 | " --trans -t NAME use transliteration NAME\n", | ||||||||||||||||||||||||||||
| 196 | " --reverse -r transliterate in reverse direction\n", | ||||||||||||||||||||||||||||
| 197 | " --list -l list all supported transliterations\n\n", | ||||||||||||||||||||||||||||
| 198 | " --verbose -v print verbose status messages\n", | ||||||||||||||||||||||||||||
| 199 | " --help -h show this help\n\n", | ||||||||||||||||||||||||||||
| 200 | "Read translit(1) for details.\n"; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | exit($retval); | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | sub show_list { | ||||||||||||||||||||||||||||
| 206 | 2 | ▶ | print "Transliterations supported by Lingua::Translit v" | ||||||||||||||||||||||||||
|
Subroutine
show_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | . $Lingua::Translit::VERSION . ":\n\n"; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | translit_list_supported(); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 2 | exit(0); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 RESTRICTIONS | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | The input has to be UTF-8 encoded. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 BUGS | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | None known. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Please report bugs using CPAN's request tracker at | ||||||||||||||||||||||||||||
| 223 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Lingua-Translit>. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | L<Lingua::Translit>, L<Lingua::Translit::Tables> | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Alex Linke <alinke@netzum-sorglos.de> | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Rona Linke <rlinke@netzum-sorglos.de> | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Copyright (C) 2007-2008 Alex Linke and Rona Linke | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Copyright (C) 2009-2016 Lingua-Systems Software GmbH | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Copyright (C) 2016-2017 Netzum Sorglos, Lingua-Systems Software GmbH | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Copyright (C) 2017 Netzum Sorglos Software GmbH | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 246 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =cut | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # vim: set ft=perl sts=4 sw=4 ts=4 ai et: | ||||||||||||||||||||||||||||