| 1 |
#!/usr/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use 5.006; |
| 5 |
|
| 6 |
use Getopt::Std qw(getopts); |
| 7 |
use Lingua::Identify qw(:all); |
| 8 |
|
| 9 |
# get the user options |
| 10 |
our %opts = get_options(); |
| 11 |
|
| 12 |
# special requests |
| 13 |
show_help() if $opts{h}; |
| 14 |
show_version() if $opts{v}; |
| 15 |
show_languages() if $opts{l}; |
| 16 |
choose_languages() if $opts{o}; |
| 17 |
|
| 18 |
# check options for sanity |
| 19 |
if ($opts{m}) { |
| 20 |
$opts{m} > 0 || die "langident: please provide a positive value for -m\n"; |
| 21 |
$opts{a} = $opts{m}; |
| 22 |
} |
| 23 |
|
| 24 |
$opts{s} ||= "\n"; |
| 25 |
|
| 26 |
# identify the language... |
| 27 |
|
| 28 |
if (@ARGV) { # ...of files |
| 29 |
my @files = @ARGV; |
| 30 |
for (sort @files) { |
| 31 |
-f && -r && -s || next; |
| 32 |
if ($opts{E}) { |
| 33 |
open (FILE,"<:encoding($opts{E})" ,$_) || |
| 34 |
do {print STDERR "Could not open $_ ($!)\n" ; next}; |
| 35 |
} else { |
| 36 |
open (FILE,"<:utf8" ,$_) || do {print STDERR "Could not open $_ ($!)\n" ; next}; |
| 37 |
} |
| 38 |
local $/ = $opts{s}; |
| 39 |
print $_, ":", (join $", ident(<FILE>)), "\n"; |
| 40 |
close (FILE); |
| 41 |
} |
| 42 |
} |
| 43 |
else { # ...of STDIN |
| 44 |
binmode(STDIN, ":utf8"); |
| 45 |
print ((join $", ident(<>)), "\n"); |
| 46 |
} |
| 47 |
|
| 48 |
# |
| 49 |
# subroutines |
| 50 |
# |
| 51 |
|
| 52 |
### |
| 53 |
|
| 54 |
sub get_options { |
| 55 |
my %opts; |
| 56 |
getopts('acde:hlo:ps:vm:E:', \%opts ); |
| 57 |
|
| 58 |
foreach my $key ( keys %opts ) |
| 59 |
{ |
| 60 |
$opts{$key} = 1 unless defined $opts{$key} |
| 61 |
} |
| 62 |
|
| 63 |
debug_options( %opts ) if $opts{d}; |
| 64 |
|
| 65 |
return %opts; |
| 66 |
} |
| 67 |
|
| 68 |
### |
| 69 |
|
| 70 |
sub debug_options { |
| 71 |
my %opts = @_; |
| 72 |
|
| 73 |
#local $^W = 0; |
| 74 |
no warnings; |
| 75 |
{ |
| 76 |
print STDERR <<"HERE"; |
| 77 |
------------------------------------------------------------------------- |
| 78 |
Command line options |
| 79 |
------------------------------------------------------------------------- |
| 80 |
a $opts{a} |
| 81 |
c $opts{c} |
| 82 |
d $opts{d} |
| 83 |
e $opts{e} |
| 84 |
h $opts{h} |
| 85 |
l $opts{l} |
| 86 |
m $opts{m} |
| 87 |
o $opts{o} |
| 88 |
p $opts{p} |
| 89 |
s $opts{s} |
| 90 |
v $opts{v} |
| 91 |
------------------------------------------------------------------------- |
| 92 |
HERE |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
### |
| 97 |
|
| 98 |
sub show_help { |
| 99 |
die "Usage: langident file1 file2 |
| 100 |
or: langident -a -p file |
| 101 |
or: cat file | langident |
| 102 |
langident: identifies the languages files are written in |
| 103 |
|
| 104 |
Options: |
| 105 |
-a show all results |
| 106 |
-c show confidence level |
| 107 |
-d debug |
| 108 |
-e METHODS select the method(s) to use |
| 109 |
-h displays this messages and exit |
| 110 |
-l list available languages and exit |
| 111 |
-m NUMBER sets maximum number of results (languages) to display |
| 112 |
-o LANGS work only with specified languages |
| 113 |
-p also show percentages |
| 114 |
-s SIZE maximum size to examine |
| 115 |
-v show version and exit |
| 116 |
" |
| 117 |
} |
| 118 |
|
| 119 |
### |
| 120 |
|
| 121 |
sub show_version { |
| 122 |
die "langident version 0.08 (Lingua::Identify ", Lingua::Identify->VERSION, ")\n"; |
| 123 |
} |
| 124 |
|
| 125 |
### |
| 126 |
|
| 127 |
# identify the language |
| 128 |
sub ident { |
| 129 |
if ($opts{a} || $opts{p} || $opts{c}) { |
| 130 |
my @results = langof(get_config(),@_); |
| 131 |
@results || return (); |
| 132 |
if ($opts{m}) { |
| 133 |
my $m = $opts{m} > @results / 2 ? @results - 1 : (($opts{m} * 2) - 1); |
| 134 |
@results = @results[0 .. $m]; |
| 135 |
} |
| 136 |
my @confidence = $opts{c} ? scalar confidence(@results) * 100 : (); |
| 137 |
@results = @results[0,1] unless $opts{a}; |
| 138 |
@results = grep /[a-z]{2}/, @results unless $opts{p}; |
| 139 |
for (@results) {$_*=100 if /\d/} |
| 140 |
return (shift @results, @confidence, @results); |
| 141 |
} |
| 142 |
else { |
| 143 |
return (langof(get_config(),@_))[0]; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
### |
| 148 |
|
| 149 |
# HELP!!! I'm an innocent comment trapped in here by an evil programmer :-( |
| 150 |
# Please get me out of here :-( My family is probably looking for me :-( |
| 151 |
|
| 152 |
### |
| 153 |
|
| 154 |
sub get_config { |
| 155 |
my %config; |
| 156 |
|
| 157 |
# get the methods to use |
| 158 |
if (defined $opts{e}) { |
| 159 |
my %methods; |
| 160 |
for (get_all_methods()) { $methods{$_} = 0 } |
| 161 |
for (split /,/, $opts{e}) { |
| 162 |
my ($m, $v) = split /=/; |
| 163 |
defined $methods{$_} || next; |
| 164 |
$methods{$_} = $v || 1; |
| 165 |
} |
| 166 |
$config{'method'} = \%methods; |
| 167 |
} |
| 168 |
|
| 169 |
return \%config; |
| 170 |
} |
| 171 |
|
| 172 |
### |
| 173 |
|
| 174 |
sub choose_languages { |
| 175 |
my $t = set_active_languages(split /,/, $opts{o}); |
| 176 |
$opts{m} = $t if $opts{m} > $t; |
| 177 |
} |
| 178 |
|
| 179 |
# show all available languages |
| 180 |
sub show_languages { |
| 181 |
for (sort (get_all_languages())) { |
| 182 |
print uc $_, " - ", ucfirst name_of($_) ,"\n"; |
| 183 |
} |
| 184 |
exit; |
| 185 |
} |
| 186 |
|
| 187 |
__END__ |
| 188 |
|
| 189 |
=head1 NAME |
| 190 |
|
| 191 |
langident - identifies the language files are written in |
| 192 |
|
| 193 |
=head1 SYNOPSIS |
| 194 |
|
| 195 |
langident [OPTIONS] file1 [file2 ...] |
| 196 |
|
| 197 |
=head1 DESCRIPTION |
| 198 |
|
| 199 |
Identifies the language files are written in using Perl module |
| 200 |
Lingua::Identify. |
| 201 |
|
| 202 |
=head2 OPTIONS |
| 203 |
|
| 204 |
=head2 -a |
| 205 |
|
| 206 |
Show all results (not just the most probable language). |
| 207 |
|
| 208 |
=head2 -c |
| 209 |
|
| 210 |
Show confidence level for most probable language (it will be the first value |
| 211 |
right after the most probable language). |
| 212 |
|
| 213 |
=head2 -d |
| 214 |
|
| 215 |
Debug (development only). |
| 216 |
|
| 217 |
=head2 -E ENCODING |
| 218 |
|
| 219 |
Select an input encoding. Defaults to UTF-8. |
| 220 |
|
| 221 |
# use ISO-8859-1 (latin1) |
| 222 |
langident -E ISO-8859-1 file |
| 223 |
|
| 224 |
=head2 -e METHODS |
| 225 |
|
| 226 |
Select the method(s) to use. There are three ways of doing this: |
| 227 |
|
| 228 |
# simply using a method |
| 229 |
langident -e ngrams3 file |
| 230 |
|
| 231 |
# using several methods (separate them with a comma) |
| 232 |
langident -e prefixes3,suffixes3 |
| 233 |
|
| 234 |
# using several methods and assign different weights to each of them |
| 235 |
langident -e smallwords=2,prefixes=1,ngrams3=1.3 |
| 236 |
|
| 237 |
The available methods are the following: B<smallwords>, B<prefixes1>, |
| 238 |
B<prefixes2>, B<prefixes3>, B<prefixes4>, B<suffixes1>, B<suffixes2>, |
| 239 |
B<suffixes3>, B<suffixes4>, B<ngrams1>, B<ngrams2>, B<ngrams3> and B<ngrams4>. |
| 240 |
|
| 241 |
=head2 -h |
| 242 |
|
| 243 |
Display help message and exit. |
| 244 |
|
| 245 |
=head2 -l |
| 246 |
|
| 247 |
List all available languages and exit. |
| 248 |
|
| 249 |
=head2 -m NUMBER |
| 250 |
|
| 251 |
Set maximum number of results (languages) to display (shows the N most probable |
| 252 |
languages, by descending order of probability). |
| 253 |
|
| 254 |
Overrides the -a switch. |
| 255 |
|
| 256 |
=head2 -o LANGUAGES |
| 257 |
|
| 258 |
Only work with specified languages. |
| 259 |
|
| 260 |
# identify between Portuguese and English only |
| 261 |
langident -o pt,en * |
| 262 |
|
| 263 |
=head2 -p |
| 264 |
|
| 265 |
Also show percentages. |
| 266 |
|
| 267 |
=head2 -s SIZE |
| 268 |
|
| 269 |
Maximum size to examine. |
| 270 |
|
| 271 |
=head2 -v |
| 272 |
|
| 273 |
Show version and exit. |
| 274 |
|
| 275 |
=head1 EXAMPLES |
| 276 |
|
| 277 |
Use methods ngrams2 and ngrams1, assigning the double of importance to ngrams2 |
| 278 |
(-e switch); output will include the three most probable languages (-m switch) |
| 279 |
with its percentages (-p switch) and also the confidence level (-c switch) of |
| 280 |
the first result. |
| 281 |
|
| 282 |
$ langident -e ngrams2=2,ngrams1 -c -p -m 3 README |
| 283 |
README:en 65.7209505939491 7.8971987481393 ga 4.11905889385895 tr 4.08487011400505 |
| 284 |
$ |
| 285 |
|
| 286 |
=head1 TO DO |
| 287 |
|
| 288 |
=over 6 |
| 289 |
|
| 290 |
=item * Add a switch to ignore HTML tags (and maybe other formats too) |
| 291 |
|
| 292 |
=back |
| 293 |
|
| 294 |
=head1 SEE ALSO |
| 295 |
|
| 296 |
Lingua::Identify(3), Text::ExtractWords(3), Text::Ngram(3), Text::Affixes(3). |
| 297 |
|
| 298 |
A linguist and/or a shrink. |
| 299 |
|
| 300 |
The latest CVS version of C<Lingua::Identify> (which includes I<langident>) can |
| 301 |
be attained at http://natura.di.uminho.pt/natura/viewcvs.cgi/Lingua/Identify/ |
| 302 |
|
| 303 |
ISO 639 Language Codes, at http://www.w3.org/WAI/ER/IG/ert/iso639.htm |
| 304 |
|
| 305 |
=head1 AUTHOR |
| 306 |
|
| 307 |
Jose Alves de Castro, E<lt>[email protected]<gt> |
| 308 |
|
| 309 |
=head1 COPYRIGHT AND LICENSE |
| 310 |
|
| 311 |
Copyright 2004 by Jose Alves de Castro |
| 312 |
|
| 313 |
This library is free software; you can redistribute it and/or modify |
| 314 |
it under the same terms as Perl itself. |
| 315 |
|
| 316 |
=cut |