| 1 |
#!/usr/bin/perl -s |
| 2 |
use strict; |
| 3 |
use POSIX; |
| 4 |
|
| 5 |
use Text::Ngram qw(ngram_counts); |
| 6 |
use Text::ExtractWords qw(words_count words_list); |
| 7 |
use Text::Affixes; |
| 8 |
|
| 9 |
my $version = '0.02'; |
| 10 |
|
| 11 |
our ($v,$h,$d,$u,$verbose,$locale); |
| 12 |
|
| 13 |
if ($locale) { |
| 14 |
if ($u && $locale !~ /utf/i) { |
| 15 |
verbose("setting locale: $locale.UTF-8"); |
| 16 |
POSIX::setlocale( &POSIX::LC_CTYPE, "$locale.UTF-8" ); |
| 17 |
} else { |
| 18 |
verbose("setting locale: $locale"); |
| 19 |
POSIX::setlocale( &POSIX::LC_CTYPE, $locale ); |
| 20 |
} |
| 21 |
use locale; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
our $utf8 = undef; |
| 26 |
$utf8 = 1 if $u; |
| 27 |
|
| 28 |
show_help() if $h; |
| 29 |
show_version() if $v; |
| 30 |
|
| 31 |
unless (-d || @ARGV) { |
| 32 |
$d = 1; |
| 33 |
} |
| 34 |
|
| 35 |
if ($d) { |
| 36 |
my @languages = @ARGV || <*-*>; |
| 37 |
for (@languages) { |
| 38 |
/(.+)-(.+)/ || |
| 39 |
die "Can't figure out the language tag and name out of '$_'\n"; |
| 40 |
make_module($1, $2, <$_/*>); |
| 41 |
} |
| 42 |
} |
| 43 |
else { |
| 44 |
my $tag = shift || die "You must provide a language tag.\n"; |
| 45 |
my $language = shift || die "You must provide a language name.\n"; |
| 46 |
@ARGV || die "You must provide at least one file.\n"; |
| 47 |
verbose("Creating module for $tag for $language language"); |
| 48 |
make_module(lc($tag), lc($language), @ARGV); |
| 49 |
} |
| 50 |
|
| 51 |
############### |
| 52 |
# subroutines # |
| 53 |
############### |
| 54 |
|
| 55 |
sub verbose { |
| 56 |
print STDERR $_[0], "\n" if $verbose; |
| 57 |
} |
| 58 |
|
| 59 |
### |
| 60 |
|
| 61 |
sub show_help { |
| 62 |
die "Usage: make-lingua-identify-language tag language file1 file2 |
| 63 |
or: make-lingua-identify-language -d directory1 directory2 |
| 64 |
make-lingua-identify-language: creates Lingua::Identify language modules |
| 65 |
|
| 66 |
Examples: |
| 67 |
make-lingua-identify-language en english file1 |
| 68 |
make-lingua-identify-language -d en-english/ pt-portuguese/ |
| 69 |
make-lingua-identify-language |
| 70 |
|
| 71 |
Options: |
| 72 |
-d directory mode |
| 73 |
-h displays this messages and exit |
| 74 |
-v show version and exit |
| 75 |
-verbose verbose mode |
| 76 |
-u unicode |
| 77 |
" |
| 78 |
} |
| 79 |
|
| 80 |
### |
| 81 |
|
| 82 |
sub show_version { |
| 83 |
die "make-lingua-identify-language version $version\n"; |
| 84 |
} |
| 85 |
|
| 86 |
### |
| 87 |
|
| 88 |
sub make_module { |
| 89 |
my ($tag, $name, @files) = @_; |
| 90 |
|
| 91 |
verbose("Studying $name ($tag)"); |
| 92 |
|
| 93 |
# read all files in its directory |
| 94 |
my $text; |
| 95 |
|
| 96 |
my $meta = { |
| 97 |
'language_name' => $name, |
| 98 |
'sets' => [], |
| 99 |
}; |
| 100 |
$tag =~ /^..$/ and $meta->{'two_letter_code'} = $tag; |
| 101 |
$tag =~ /^...$/ and $meta->{'three_letter_code'} = $tag; |
| 102 |
|
| 103 |
for (@files) { |
| 104 |
if ($utf8) { |
| 105 |
open( STDINO, '<:utf8', $_ ) || die $!; |
| 106 |
} else { |
| 107 |
open( STDINO, $_ ) || die $!; |
| 108 |
} |
| 109 |
if ($_ eq 'META.yml') { |
| 110 |
verbose("\tfound META.yml"); |
| 111 |
while (<STDINO>) { # META.yml is processed here |
| 112 |
if (/^(\w+):\s*(\w+)$/) { |
| 113 |
$meta->{$1} = $2; |
| 114 |
verbose("\tassigned $1 to $2"); |
| 115 |
} |
| 116 |
elsif (/^(\w+):\s*$/) { |
| 117 |
my $id = $1; |
| 118 |
while (<STDINO>) { |
| 119 |
last if /^\s*$/; |
| 120 |
if (/^\s*(\w*)\s*$/) { |
| 121 |
push @{$meta->{$id}}, $1; |
| 122 |
verbose("\tpushed $1 into $id"); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
else { |
| 129 |
if ($locale) { |
| 130 |
$text .= join "\n", map { lc } <STDINO>; |
| 131 |
} else { |
| 132 |
$text .= join "\n", <STDINO>; |
| 133 |
} |
| 134 |
} |
| 135 |
close STDINO; |
| 136 |
} |
| 137 |
|
| 138 |
# write some headers |
| 139 |
if ($utf8) { |
| 140 |
open( STDOUTO, ">:utf8" , ( uc $tag ) . ".pm" ) || die; |
| 141 |
} else { |
| 142 |
open( STDOUTO, ">" . ( uc $tag ) . ".pm" ) || die; |
| 143 |
} |
| 144 |
my $sets = join ", ", map { "'$_'" } @{$meta->{'sets'}}; |
| 145 |
verbose("\t$sets"); |
| 146 |
print STDOUTO "use utf8;\n" if $utf8; |
| 147 |
print STDOUTO "use strict;\n", |
| 148 |
"\n\${Lingua::Identify::languages{'_versions'}{'$tag'}} = '$version';\n", |
| 149 |
"\n\${Lingua::Identify::languages{'_names'}{'$tag'}} = '$name';\n", |
| 150 |
"\n\${Lingua::Identify::languages{'_sets'}{'$tag'}} = '$sets';\n"; |
| 151 |
|
| 152 |
# write POD |
| 153 |
|
| 154 |
my $module_name = uc $tag; |
| 155 |
my $podname = ucfirst $name; |
| 156 |
|
| 157 |
print STDOUTO pod_unindent(" |
| 158 |
|
| 159 |
=head1 NAME |
| 160 |
|
| 161 |
Lingua::Identify::$module_name - Meta-information on $podname |
| 162 |
|
| 163 |
=head1 SYNOPSIS |
| 164 |
|
| 165 |
Nothing here is meant for public consumption. This module is to be |
| 166 |
loaded by Lingua::Identify. |
| 167 |
|
| 168 |
=head1 DESCRIPTION |
| 169 |
|
| 170 |
Automatically generated. Do not change this module yourself unless |
| 171 |
you know what you're doing. |
| 172 |
|
| 173 |
=head1 SEE ALSO |
| 174 |
|
| 175 |
Lingua::Identify(3). |
| 176 |
|
| 177 |
=head1 AUTHOR |
| 178 |
|
| 179 |
Jose Castro, C<< <cog\@cpan.org> >> |
| 180 |
|
| 181 |
=head1 COPYRIGHT AND LICENSE |
| 182 |
|
| 183 |
Copyright (C) 2010 by Alberto Simoes |
| 184 |
|
| 185 |
This library is free software; you can redistribute it and/or modify |
| 186 |
it under the same terms as Perl itself, either Perl version 5.8.4 or, |
| 187 |
at your option, any later version of Perl 5 you may have available. |
| 188 |
|
| 189 |
=cut |
| 190 |
"); |
| 191 |
|
| 192 |
# write prefixes information |
| 193 |
verbose("\tstudying prefixes"); |
| 194 |
|
| 195 |
my $prefixes = get_prefixes( { min => 1, max => 4 }, $text ); |
| 196 |
|
| 197 |
for my $i ( 1 .. 4 ) { |
| 198 |
select STDOUTO; |
| 199 |
print "\n\${Lingua::Identify::languages{'prefixes$i'}{'$tag'}} = {\n"; |
| 200 |
my $total; |
| 201 |
for ( values %{ $$prefixes{$i} } ) { $total += $_; } |
| 202 |
for ( |
| 203 |
( |
| 204 |
sort { $$prefixes{$i}{$b} <=> $$prefixes{$i}{$a} } |
| 205 |
keys %{ $$prefixes{$i} } |
| 206 |
)[ 0 .. 19 ] |
| 207 |
) |
| 208 |
{ |
| 209 |
print " '$_'\t=> ", $$prefixes{$i}{$_} / $total, ",\n"; |
| 210 |
} |
| 211 |
print "};\n"; |
| 212 |
} |
| 213 |
|
| 214 |
# write suffixes information |
| 215 |
verbose("\tstudying suffixes"); |
| 216 |
|
| 217 |
my $suffixes = get_suffixes( { min => 1, max => 4 }, $text ); |
| 218 |
for my $i ( 1 .. 4 ) { |
| 219 |
select STDOUTO; |
| 220 |
print "\n\${Lingua::Identify::languages{'suffixes$i'}{'$tag'}} = {\n"; |
| 221 |
my $total; |
| 222 |
for ( values %{ $$suffixes{$i} } ) { $total += $_; } |
| 223 |
for ( |
| 224 |
( |
| 225 |
sort { $$suffixes{$i}{$b} <=> $$suffixes{$i}{$a} } |
| 226 |
keys %{ $$suffixes{$i} } |
| 227 |
)[ 0 .. 19 ] |
| 228 |
) |
| 229 |
{ |
| 230 |
print " '$_'\t=> ", $$suffixes{$i}{$_} / $total, ",\n"; |
| 231 |
} |
| 232 |
print "};\n"; |
| 233 |
} |
| 234 |
|
| 235 |
# write words information |
| 236 |
verbose("\tstudying words"); |
| 237 |
|
| 238 |
my $hash_w; |
| 239 |
if ($utf8) { |
| 240 |
$hash_w = my_words_count($text); |
| 241 |
} else { |
| 242 |
my %hash; |
| 243 |
words_count(\%hash, $text); |
| 244 |
$hash_w = \%hash; |
| 245 |
} |
| 246 |
my $total; |
| 247 |
for ( values %$hash_w ) { $total += $_; } |
| 248 |
print "\n\${Lingua::Identify::languages{'smallwords'}{'$tag'}} = {\n"; |
| 249 |
for (( sort { $hash_w->{$b} <=> $hash_w->{$a} } |
| 250 |
grep { !/(?:[_'",;:.«»0-9\(\)\[\]\{\}\/\\\!\?%]|^-|-$)/ } keys %$hash_w ) |
| 251 |
[ 0 .. 19 ] ) |
| 252 |
{ |
| 253 |
print " '$_'\t=> ", $hash_w->{$_} / $total, ",\n"; |
| 254 |
} |
| 255 |
print "};\n"; |
| 256 |
|
| 257 |
# write ngrams information |
| 258 |
my $f = sub { ngram_counts( {spaces => 0}, $_[0], $_[1]) }; |
| 259 |
get_ngrams($tag, 'ngrams', 1, 4, $f, $text ); |
| 260 |
|
| 261 |
# close the file |
| 262 |
close(STDOUT); |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
############################### |
| 267 |
|
| 268 |
sub get_ngrams { |
| 269 |
my ($tag, $what, $min, $max, $function, $text) = @_; |
| 270 |
|
| 271 |
for my $gram ( $min .. $max ) { |
| 272 |
|
| 273 |
verbose("\tstudying $what $gram"); |
| 274 |
|
| 275 |
#my $hash_r = ngram_counts( $text, $gram ); |
| 276 |
my $hash = &{$function}($text, $gram); |
| 277 |
|
| 278 |
my $total; |
| 279 |
for ( values %$hash ) { |
| 280 |
$total += $_; |
| 281 |
} |
| 282 |
|
| 283 |
print "\n\${Lingua::Identify::languages{'$what$gram'}{'$tag'}} = {\n"; |
| 284 |
|
| 285 |
for ((sort { $$hash{$b} <=> $$hash{$a} } keys %$hash )[ 0 .. 19 ]) { |
| 286 |
print " '$_' => ", $$hash{$_} / $total, ",\n"; |
| 287 |
} |
| 288 |
|
| 289 |
print "};\n"; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
### |
| 296 |
|
| 297 |
sub pod_unindent { ( local $_ = shift ) =~ s/^ +//mg; $_ } |
| 298 |
|
| 299 |
sub my_words_count { |
| 300 |
my $text = shift; |
| 301 |
my $count; |
| 302 |
for my $word (split /[\n\s]+/, $text) { |
| 303 |
$count->{$word}++; |
| 304 |
} |
| 305 |
return $count; |
| 306 |
} |
| 307 |
|
| 308 |
__END__ |
| 309 |
|
| 310 |
=head1 NAME |
| 311 |
|
| 312 |
make-lingua-identify-language - creates language modules for Lingua::Identify |
| 313 |
|
| 314 |
=head1 SYNOPSIS |
| 315 |
|
| 316 |
make-lingua-identify-language Language-Tag Language-Name file1 [file2 ...] |
| 317 |
|
| 318 |
or |
| 319 |
|
| 320 |
make-lingua-identify-language -d TAG1-LANGUAGE1/ [TAG2-LANGUAGE2/ ...] |
| 321 |
|
| 322 |
or |
| 323 |
|
| 324 |
make-lingua-identify |
| 325 |
|
| 326 |
=head1 DESCRIPTION |
| 327 |
|
| 328 |
Creates language modules to be used by Lingua::Identify. |
| 329 |
|
| 330 |
After creating the modules, you still have to install them. |
| 331 |
|
| 332 |
Please note that this script is still at an early stage. Please do not even |
| 333 |
look at the code... |
| 334 |
|
| 335 |
Without parameters, make-lingua-identify-language assumes mode -d and goes |
| 336 |
through all the directories in the current one. This is useful to be used in a |
| 337 |
directory where you something like this: |
| 338 |
|
| 339 |
. |
| 340 |
|-- en-english |
| 341 |
| `-- english.txt |
| 342 |
|-- fr-french |
| 343 |
| `-- french1.txt |
| 344 |
| `-- french2.txt |
| 345 |
`-- pt-portuguese |
| 346 |
`-- portuguese.txt |
| 347 |
|
| 348 |
=head2 OPTIONS |
| 349 |
|
| 350 |
=head2 -d |
| 351 |
|
| 352 |
Directory mode. Each parameter passed should be a directory whose name |
| 353 |
must be of the form tag-name (e.g., en-english/ ). Each of the |
| 354 |
directories passed should contain text files that can be used to train |
| 355 |
Lingua::Identify. |
| 356 |
|
| 357 |
=head2 -D |
| 358 |
|
| 359 |
Debug mode. Only for development. |
| 360 |
|
| 361 |
=head2 -h |
| 362 |
|
| 363 |
Display help and exit. |
| 364 |
|
| 365 |
=head2 -v |
| 366 |
|
| 367 |
Show version and exit. |
| 368 |
|
| 369 |
=head2 -verbose |
| 370 |
|
| 371 |
Verbose mode. |
| 372 |
|
| 373 |
=head2 -locale=C<< <locale> >> |
| 374 |
|
| 375 |
Set a specific locale. This way your text will be all lowercased before analysed. |
| 376 |
|
| 377 |
=head1 META.yml |
| 378 |
|
| 379 |
C<META.yml> files are not parsed as other files, they are ignored. |
| 380 |
|
| 381 |
In directory mode (C<-d> switch), C<META.yml> files are checked for |
| 382 |
info on languages codes and sets. |
| 383 |
|
| 384 |
Here's a simple C<META.yml> for you to put in your directories: |
| 385 |
|
| 386 |
two_letter_code: pt |
| 387 |
three_letter_code: por |
| 388 |
sets: |
| 389 |
spoken_in_portugal |
| 390 |
|
| 391 |
With that, the language will be identified with the two letter code |
| 392 |
"pt" or the three letter code "por"; it will also be in the set |
| 393 |
":spoken_in_portugal". |
| 394 |
|
| 395 |
=head1 CONTRIBUTING WITH NEW LANGUAGES |
| 396 |
|
| 397 |
Please do not contribute with modules you made yourself. It's easier |
| 398 |
to contribute with unprocessed text, because that allows for new |
| 399 |
versions of Lingua::Identify not having to drop languages down in case |
| 400 |
I can't contact you by that time. |
| 401 |
|
| 402 |
Use I<make-lingua-identify-language> to create a new module for your |
| 403 |
own personal use, if you must, but try to contribute with unprocessed |
| 404 |
text rather than those modules. |
| 405 |
|
| 406 |
=head1 SEE ALSO |
| 407 |
|
| 408 |
Lingua::Identify(3), langident(1) |
| 409 |
|
| 410 |
A linguist and/or a shrink. |
| 411 |
|
| 412 |
The latest CVS version of C<Lingua::Identify> (which includes |
| 413 |
I<make-lingua-identify>) can be attained at |
| 414 |
http://natura.di.uminho.pt/natura/viewcvs.cgi/Lingua/Identify/ |
| 415 |
|
| 416 |
ISO 639 Language Codes, at http://www.w3.org/WAI/ER/IG/ert/iso639.htm |
| 417 |
|
| 418 |
=head1 AUTHOR |
| 419 |
|
| 420 |
Jose Alves de Castro, E<lt>[email protected]<gt> |
| 421 |
|
| 422 |
=head1 COPYRIGHT AND LICENSE |
| 423 |
|
| 424 |
Copyright 2004-2005 by Jose Alves de Castro |
| 425 |
|
| 426 |
This library is free software; you can redistribute it and/or modify |
| 427 |
it under the same terms as Perl itself. |
| 428 |
|
| 429 |
=cut |