bin/yali-language-identifier

filter: Statementstmt 98.3% 61 / 62 Branchbran 88.4% 23 / 26 Conditioncond 94.4% 17 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 91.8% 113 / 123 SCAR 0.0
CC
1
Cov
95%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« bin/yali-identifier ↑ Summary
1 #!/usr/bin/env perl
2  
3 # ABSTRACT: Script for language identification
4 # PODNAME: yali-language-identifier
5  
6 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 82 ▶use Lingua::YALI::LanguageIdentifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use Lingua::YALI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 82 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 82 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 82 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 82 our $VERSION = '0.015'; # VERSION
17  
18 82 my $file_list = undef;
19 82 my $input_file = undef;
20 82 my $format = "single";
21 82 my $languages = undef;
22 82 my $help = 0;
23 82 my $supported = 0;
24 82 my $each_line = undef;
25  
26 82 ▶my $result = GetOptions("filelist=s" => \$file_list,
Condition66%2/3
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'languages|l=s', \$languages, 'format|f=s', \$format, 'supported|s', \$supported, 'each|e', \$each_line, 'help|h', \$help) || pod2usage('-exitval', 105)
l!l&&r!l&&!r
7309
MC/DC50%1/2
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'languages|l=s', \$languages, 'format|f=s', \$format, 'supported|s', \$supported, 'each|e', \$each_line, 'help|h', \$help) || pod2usage('-exitval', 105)
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'languages|l=s', \$languages, 'format|f=s', \$format, 'supported|s', \$supported, 'each|e', \$each_line, 'help|h', \$help) pod2usage('-exitval', 105)
Truth table66%2/3
A || B
A= &GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'languages|l=s', \$languages, 'format|f=s', \$format, 'supported|s', \$supported, 'each|e', \$each_line, 'help|h', \$help)B= pod2usage('-exitval', 105)
ABresult
1X1
011
000
27 "input|i=s" => \$input_file, # string
28 "languages|l=s" => \$languages,
29 "format|f=s" => \$format,
30 "supported|s" => \$supported,,
31 "each|e" => \$each_line,
32 "help|h" => \$help
33 ) || pod2usage(-exitval => 105);
34  
35 73 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 4 69 73
36 4 pod2usage(-exitval => 0);
37 }
38  
39 69 my $identifier = Lingua::YALI::LanguageIdentifier->new();
40  
41 # print out supported files and terminate
42 69 ▶if ( $supported ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supported) 8 61 69
43 8 print join("\n", @{$identifier->get_available_languages()}), "\n";
44 8 exit;
45 }
46  
47 # check output format
48 61 ▶if ( $format ne "single" && $format ne "all" && $format ne "all_p" && $format ne "tabbed" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format ne "single" and $format ne "all" and $format ne "all_p" and $format ne "tabbed") 2 59 61
Condition100%9/9
$format ne "single" and $format ne "all" and $format ne "all_p" and $format ne "tabbed"
!ll&&!rl&&r
5632
$format ne "single" and $format ne "all" and $format ne "all_p"
!ll&&!rl&&r
5335
$format ne "single" and $format ne "all"
!ll&&!rl&&r
5038
MC/DC0%0/4
$format ne "single" and $format ne "all" and $format ne "all_p" and $format ne "tabbed"
$format ne "single" $format ne "all" $format ne "all_p" $format ne "tabbed"
Truth table0%0/5
A and B and C and D
A= $format ne "single"B= $format ne "all"C= $format ne "all_p"D= $format ne "tabbed"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
49 2 pod2usage(-exitval => 101, -msg => "Unsupported format $format.");
50 }
51  
52 # reading input from STDIN is default
53 59 ▶if ( ! defined($input_file) && ! defined($file_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_file or defined $file_list) 16 43 59
Condition100%3/3
defined $input_file or defined $file_list
l!l&&r!l&&!r
36716
MC/DC100%2/2
defined $input_file or defined $file_list
defined $input_file defined $file_list
Truth table100%3/3
A or B
A= defined $input_fileB= defined $file_list
ABresult
1X1
011
000
54 16 $input_file = "-";
55 }
56  
57 # it is incorrect when parameter filelist and input are both specified
58 59 ▶if ( defined($input_file) && defined($file_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_file and defined $file_list) 3 56 59
Condition100%3/3
defined $input_file and defined $file_list
!ll&&!rl&&r
7493
MC/DC100%2/2
defined $input_file and defined $file_list
defined $input_file defined $file_list
Truth table100%3/3
A and B
A= defined $input_fileB= defined $file_list
ABresult
0X0
100
111
59 3 pod2usage(-exitval => 101, -msg => "Options --filelist and --input can not be specified in the same time.");
60 }
61  
62 56 ▶if ( ! defined($languages) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $languages) 1 55 56
63 1 pod2usage(-exitval => 101, -msg => "Options --languages has to be specified.");
64 }
65  
66 # register required languages
67 55 my @languages = split(/ /, $languages);
68 55 $identifier->add_language(@languages);
69  
70 # identify languages
71 54 ▶if ( defined($input_file) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $input_file) { } 47 7 54
elsif (defined $file_list) { } 7 0 7
72 47 ▶ if ( $input_file eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file eq '-') { } 37 10 47
73 37 Lingua::YALI::_identify_handle($identifier, \*STDIN, $format, \@languages, $each_line);
74 } else {
75 10 Lingua::YALI::_identify($identifier, $input_file, $format, \@languages, $each_line);
76 }
77 } elsif ( defined($file_list) ) {
78 7 my $fh_list = undef;
79 7 my $list_dir = "";
80  
81 7 ▶ if ( $file_list eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_list eq '-') { } 0 7 7
82 0 $fh_list = \*STDIN;
83 } else {
84 7 ▶ open($fh_list, "<", $file_list) or croak("Not found: $file_list\n" . $!);
Branch: 1 branch
BranchTrueFalseTotal
open $fh_list, "<", $file_list or croak("Not found: $file_list\n" . $!) 1 6 7
85 6 $list_dir = dirname($file_list) . "/";
86 }
87 6 ▶ while ( my $file = <$fh_list> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readline $fh_list)) 12 6 18
88 12 chomp $file;
89 12 ▶ if ( ! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 12 0 12
90 12 $file = $list_dir . $file;
91 }
92 12 Lingua::YALI::_identify($identifier, $file, $format, \@languages, $each_line);
93 }
94 }
95  
96 __END__
97  
98 =pod
99  
100 =encoding UTF-8
101  
102 =head1 NAME
103  
104 yali-language-identifier - Script for language identification
105  
106 =head1 VERSION
107  
108 version 0.015
109  
110 =head1 SYNOPSIS
111  
112 yali-language-identifier [options]
113  
114 Identify languages with pretrained models.
115  
116 Options:
117  
118 -i, --input=F input file. When F is -, read standard input.
119 --filelist=F input files are read from F. When F is -, read them from standard input.
120 -f, --format=FORMAT output FORMAT
121 -l, --languages=L ISO 639-3 codes of languages seperated by space
122 -s, --supported prints list of supported languages
123 -h, --help prints documentation
124  
125 FORMAT:
126  
127 single - prints out only the most probable language
128 all - prints out all languages sorted according to their probability descendetly
129 all_p - prints out all languages with probability sorted according to their probability descendetly
130 tabbed - prints out probabilities of languages separated by tab in order used for --languages
131  
132 EXAMPLES:
133  
134 Is file /etc/passwd written in english or czech?
135 cat /etc/passwd | yali-language-identifier -i=- -l="eng ces"
136  
137 More examples are available at L<http://search.cpan.org/perldoc?Lingua%3A%3AYALI%3A%3AExamples>
138  
139 Version: 0.015 (2014-05-04)
140  
141 =head1 AUTHOR
142  
143 Martin Majlis <martin@majlis.cz>
144  
145 =head1 COPYRIGHT AND LICENSE
146  
147 This software is Copyright (c) 2012 by Martin Majlis.
148  
149 This is free software, licensed under:
150  
151 The (three-clause) BSD License
152  
153 =cut
« bin/yali-identifier ↑ Summary