| 1 | package Lingua::YALI; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: YALI - Yet Another Language Identifier. | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 238 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 238 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 238 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 238 | our $VERSION = '0.015'; # VERSION | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | # TODO: refactor - remove bzcat | ||||||||||||||||||||||||||||||||
| 13 | sub _open | ||||||||||||||||||||||||||||||||
| 14 | { | ||||||||||||||||||||||||||||||||
| 15 | 65 | ▶ | my ($f) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_open: called CC=7 SCAR=22.3
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 65 | ▶ | croak("Not found: $f") if !-e $f; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 61 | my $opn; | |||||||||||||||||||||||||||||||
| 20 | my $hdl; | ||||||||||||||||||||||||||||||||
| 21 | 61 | my $ft = qx(file '$f'); | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # file might not recognize some files! | ||||||||||||||||||||||||||||||||
| 24 | 61 | ▶ | if ( $f =~ /\.gz$/ || $ft =~ /gzip compressed data/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$f =~ /\.gz$/ or $ft =~ /gzip compressed data/
MC/DC0%0/2
$f =~ /\.gz$/ or $ft =~ /gzip compressed data/
$f =~ /\.gz$/
$ft =~ /gzip compressed data/
Truth table33%1/3
A or B
A= $f =~ /\.gz$/B= $ft =~ /gzip compressed data/
| |||||||||||||||||||||||||||||||||
| 25 | 0 | $opn = "zcat $f |"; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | ▶ | elsif ( $f =~ /\.bz2$/ || $ft =~ /bzip2 compressed data/ ) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$f =~ /\.bz2$/ or $ft =~ /bzip2 compressed data/
MC/DC0%0/2
$f =~ /\.bz2$/ or $ft =~ /bzip2 compressed data/
$f =~ /\.bz2$/
$ft =~ /bzip2 compressed data/
Truth table33%1/3
A or B
A= $f =~ /\.bz2$/B= $ft =~ /bzip2 compressed data/
| |||||||||||||||||||||||||||||||||
| 28 | 0 | $opn = "bzcat $f |"; | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | else { | ||||||||||||||||||||||||||||||||
| 31 | 61 | $opn = "$f"; | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | 61 | ▶ | open($hdl,"<:bytes", $opn) or croak ("Can't open '$opn': $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 61 | binmode $hdl, ":bytes"; | |||||||||||||||||||||||||||||||
| 35 | 61 | return $hdl; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | sub _identify_handle | ||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||
| 40 | 114 | ▶ | my ($identifier, $fh, $format, $languages, $each_line) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_identify_handle: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 41 | 114 | ▶ | if ( $each_line ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 36 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 126 | chomp; | |||||||||||||||||||||||||||||||
| 44 | 126 | _identify_string($identifier, $_, $format, $languages); | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||||||
| 47 | 78 | my $result = $identifier->identify_handle($fh); | |||||||||||||||||||||||||||||||
| 48 | 78 | _print_result($result, $format, $languages); | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | sub _identify | ||||||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||||||
| 54 | 43 | ▶ | my ($identifier, $file, $format, $languages, $each_line) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_identify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 43 | my $fh = Lingua::YALI::_open($file); | |||||||||||||||||||||||||||||||
| 56 | 40 | _identify_handle($identifier, $fh, $format, $languages, $each_line); | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | sub _identify_string | ||||||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||||||
| 61 | 126 | ▶ | my ($identifier, $string, $format, $languages) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_identify_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 62 | 126 | my $result = $identifier->identify_string($string); | |||||||||||||||||||||||||||||||
| 63 | 126 | _print_result($result, $format, $languages); | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | sub _print_result | ||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||
| 68 | 204 | ▶ | my ($result, $format, $languages) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_print_result: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||
| 69 | 204 | my $line = ""; | |||||||||||||||||||||||||||||||
| 70 | 204 | ▶ | if ( $format eq "single" ) { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 71 | 185 | ▶ | if ( scalar @$result > 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 185 | $line = $result->[0]->[0]; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | } elsif ( $format eq "all" ) { | ||||||||||||||||||||||||||||||||
| 75 | 6 | $line = join("\t", map { $_->[0] } @{$result}); | |||||||||||||||||||||||||||||||
| 76 | } elsif ( $format eq "all_p" ) { | ||||||||||||||||||||||||||||||||
| 77 | 6 | $line = join("\t", map { $_->[0].":".$_->[1] } @{$result}); | |||||||||||||||||||||||||||||||
| 78 | } elsif ( $format eq "tabbed" ) { | ||||||||||||||||||||||||||||||||
| 79 | 7 | my %res = (); | |||||||||||||||||||||||||||||||
| 80 | 7 | map { $res{$_->[0]} = $_->[1] } @{$result}; | |||||||||||||||||||||||||||||||
| 81 | 7 | $line = join("\t", map { $res{$_} } @$languages); | |||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||
| 83 | 0 | croak("Unsupported format $format"); | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 204 | print $line . "\n"; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 238 | 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.015 | ||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||