line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::YALI; |
2
|
|
|
|
|
|
|
# ABSTRACT: YALI - Yet Another Language Identifier. |
3
|
|
|
|
|
|
|
|
4
|
28
|
|
|
28
|
|
106
|
use strict; |
|
28
|
|
|
|
|
36
|
|
|
28
|
|
|
|
|
670
|
|
5
|
28
|
|
|
28
|
|
90
|
use warnings; |
|
28
|
|
|
|
|
28
|
|
|
28
|
|
|
|
|
525
|
|
6
|
28
|
|
|
28
|
|
84
|
use Carp; |
|
28
|
|
|
|
|
29
|
|
|
28
|
|
|
|
|
16299
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.014_01'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# TODO: refactor - remove bzcat |
13
|
|
|
|
|
|
|
sub _open |
14
|
|
|
|
|
|
|
{ |
15
|
14
|
|
|
14
|
|
25
|
my ($f) = @_; |
16
|
|
|
|
|
|
|
|
17
|
14
|
100
|
|
|
|
371
|
croak("Not found: $f") if !-e $f; |
18
|
|
|
|
|
|
|
|
19
|
13
|
|
|
|
|
24
|
my $opn; |
20
|
|
|
|
|
|
|
my $hdl; |
21
|
13
|
|
|
|
|
32342
|
my $ft = qx(file '$f'); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# file might not recognize some files! |
24
|
13
|
50
|
33
|
|
|
396
|
if ( $f =~ /\.gz$/ || $ft =~ /gzip compressed data/ ) { |
|
|
50
|
33
|
|
|
|
|
25
|
0
|
|
|
|
|
0
|
$opn = "zcat $f |"; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
elsif ( $f =~ /\.bz2$/ || $ft =~ /bzip2 compressed data/ ) { |
28
|
0
|
|
|
|
|
0
|
$opn = "bzcat $f |"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
else { |
31
|
13
|
|
|
|
|
44
|
$opn = "$f"; |
32
|
|
|
|
|
|
|
} |
33
|
13
|
50
|
|
|
|
712
|
open($hdl,"<:bytes", $opn) or croak ("Can't open '$opn': $!"); |
34
|
13
|
|
|
|
|
76
|
binmode $hdl, ":bytes"; |
35
|
13
|
|
|
|
|
134
|
return $hdl; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _identify_handle |
39
|
|
|
|
|
|
|
{ |
40
|
0
|
|
|
0
|
|
|
my ($identifier, $fh, $format, $languages, $each_line) = @_; |
41
|
0
|
0
|
|
|
|
|
if ( $each_line ) { |
42
|
0
|
|
|
|
|
|
while (<$fh>) { |
43
|
0
|
|
|
|
|
|
chomp; |
44
|
0
|
|
|
|
|
|
_identify_string($identifier, $_, $format, $languages); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} else { |
47
|
0
|
|
|
|
|
|
my $result = $identifier->identify_handle($fh); |
48
|
0
|
|
|
|
|
|
_print_result($result, $format, $languages); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _identify |
53
|
|
|
|
|
|
|
{ |
54
|
0
|
|
|
0
|
|
|
my ($identifier, $file, $format, $languages, $each_line) = @_; |
55
|
0
|
|
|
|
|
|
my $fh = Lingua::YALI::_open($file); |
56
|
0
|
|
|
|
|
|
_identify_handle($identifier, $fh, $format, $languages, $each_line); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _identify_string |
60
|
|
|
|
|
|
|
{ |
61
|
0
|
|
|
0
|
|
|
my ($identifier, $string, $format, $languages) = @_; |
62
|
0
|
|
|
|
|
|
my $result = $identifier->identify_string($string); |
63
|
0
|
|
|
|
|
|
_print_result($result, $format, $languages); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _print_result |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
0
|
|
|
my ($result, $format, $languages) = @_; |
69
|
0
|
|
|
|
|
|
my $line = ""; |
70
|
0
|
0
|
|
|
|
|
if ( $format eq "single" ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
if ( scalar @$result > 0 ) { |
72
|
0
|
|
|
|
|
|
$line = $result->[0]->[0]; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} elsif ( $format eq "all" ) { |
75
|
0
|
|
|
|
|
|
$line = join("\t", map { $_->[0] } @{$result}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} elsif ( $format eq "all_p" ) { |
77
|
0
|
|
|
|
|
|
$line = join("\t", map { $_->[0].":".$_->[1] } @{$result}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
} elsif ( $format eq "tabbed" ) { |
79
|
0
|
|
|
|
|
|
my %res = (); |
80
|
0
|
|
|
|
|
|
map { $res{$_->[0]} = $_->[1] } @{$result}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
$line = join("\t", map { $res{$_} } @$languages); |
|
0
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
} else { |
83
|
0
|
|
|
|
|
|
croak("Unsupported format $format"); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
print $line . "\n"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__END__ |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=pod |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=encoding UTF-8 |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 NAME |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Lingua::YALI - YALI - Yet Another Language Identifier. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 VERSION |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
version 0.014_01 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
The YALI package is a collection of modules and tools for language identification. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
It was developed at the L<Institute of Formal and Applied Linguistics|http://ufal.mff.cuni.cz/> at Charles University in Prague. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
More information can be found at the L<YALI homepage|http://ufal.mff.cuni.cz/~majlis/yali/>. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 Modules |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=over |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * L<Lingua::YALI::Examples|Lingua::YALI::Examples> - contains examples. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item * L<Lingua::YALI::LanguageIdentifier|Lingua::YALI::LanguageIdentifier> - is a language identification module capable of identifying 122 languages. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item * L<Lingua::YALI::Builder|Lingua::YALI::Builder> - is a module used to train custom language models. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item * L<Lingua::YALI::Identifier|Lingua::YALI::Identifier> - allows to use your own models for identification. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=back |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 Tools |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * L<yali-language-identifier|bin/yali-language-identifier> - tool for a language identification with pretrained models |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item * L<yali-builder|bin/yali-builder> - tool for a building custom language models. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * L<yali-identifier|bin/yali-identifier> - tool for a language identification with custom language models. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=back |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 WHY TO USE YALI |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=over |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item * Contains pretrained models for identifying 122 languages. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * Allows to create own models, trained on texts from specific domain, which outperforms the pretrained ones. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item * It is based on published paper L<http://ufal.mff.cuni.cz/~majlis/yali/>. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=back |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 COMPARISON WITH OTHERS |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item * L<Lingua::Lid|Lingua::Lid> can recognize 45 languages and returns only the most probable result without any weight. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item * L<Lingua::Ident|Lingua::Ident> requires training files, so it is similar to L<Lingua::YALI::LanguageIdentifier|Lingua::YALI::LanguageIdentifier>, |
158
|
|
|
|
|
|
|
but it does not provide any options for constructing models. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * L<Lingua::Identify|Lingua::Identify> can recognize 33 languages but it does not allows you to use different models. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 AUTHOR |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Martin Majlis <martin@majlis.cz> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This software is Copyright (c) 2012 by Martin Majlis. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This is free software, licensed under: |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
The (three-clause) BSD License |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Martin Majlis <martin@majlis.cz> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This software is Copyright (c) 2012 by Martin Majlis. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This is free software, licensed under: |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
The (three-clause) BSD License |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=cut |