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