bin/yali-identifier

filter: Statementstmt 97.3% 72 / 74 Branchbran 85.2% 29 / 34 Conditioncond 94.4% 17 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 90.9% 131 / 144 SCAR 0.0
CC
1
Cov
94%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« bin/yali-builder ↑ Summary bin/yali-language-identifier »
1 #!/usr/bin/env perl
2  
3 # ABSTRACT: Script for language identification with custom models
4 # PODNAME: yali-identifier
5  
6 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 70 ▶use Lingua::YALI::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 70 ▶use Lingua::YALI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 70 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 70 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 70 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 70 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 70 our $VERSION = '0.015'; # VERSION
18  
19 70 my $file_list = undef;
20 70 my $input_file = undef;
21 70 my $format = "single";
22 70 my $classes_file = undef;
23 70 my $help = 0;
24 70 my $each_line = undef;
25  
26 70 ▶my $result = GetOptions("filelist=s" => \$file_list,
Condition66%2/3
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'classes|c=s', \$classes_file, 'format|f=s', \$format, 'each|e', \$each_line, 'help|h', \$help) || pod2usage('-exitval', 105)
l!l&&r!l&&!r
6307
MC/DC50%1/2
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'classes|c=s', \$classes_file, 'format|f=s', \$format, 'each|e', \$each_line, 'help|h', \$help) || pod2usage('-exitval', 105)
&GetOptions('filelist=s', \$file_list, 'input|i=s', \$input_file, 'classes|c=s', \$classes_file, 'format|f=s', \$format, '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, 'classes|c=s', \$classes_file, 'format|f=s', \$format, 'each|e', \$each_line, 'help|h', \$help)B= pod2usage('-exitval', 105)
ABresult
1X1
011
000
27 "input|i=s" => \$input_file, # string
28 "classes|c=s" => \$classes_file,
29 "format|f=s" => \$format,
30 "each|e" => \$each_line,
31 "help|h" => \$help
32 ) || pod2usage(-exitval => 105);
33  
34 63 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 4 59 63
35 4 pod2usage(-exitval => 0);
36 }
37  
38 # check output format
39 59 ▶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 57 59
Condition100%9/9
$format ne "single" and $format ne "all" and $format ne "all_p" and $format ne "tabbed"
!ll&&!rl&&r
5342
$format ne "single" and $format ne "all" and $format ne "all_p"
!ll&&!rl&&r
5036
$format ne "single" and $format ne "all"
!ll&&!rl&&r
4739
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
40 2 pod2usage(-exitval => 101, -msg => "Unsupported format $format.");
41 }
42  
43 # reading input from STDIN is default
44 57 ▶if ( ! defined($input_file) && ! defined($file_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_file or defined $file_list) 15 42 57
Condition100%3/3
defined $input_file or defined $file_list
l!l&&r!l&&!r
35715
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
45 15 $input_file = "-";
46 }
47  
48 # it is incorrect when parameter filelist and input are both specified
49 57 ▶if ( defined($input_file) && defined($file_list) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_file and defined $file_list) 2 55 57
Condition100%3/3
defined $input_file and defined $file_list
!ll&&!rl&&r
7482
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
50 2 pod2usage(-exitval => 101, -msg => "Options --filelist and --input can not be specified in the same time.");
51 }
52  
53 # file list has to be specified
54 55 ▶if ( ! defined($classes_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $classes_file) 1 54 55
55 1 pod2usage(-exitval => 101, -msg => "File with classes has to be specified.");
56 }
57  
58 54 my $identifier = Lingua::YALI::Identifier->new();
59  
60 # register classes
61 54 my @classes = ();
62 54 ▶open(my $fh_classes, "<", $classes_file) or croak("Not found: $classes_file; " . $!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh_classes, "<", $classes_file or croak("Not found: $classes_file; " . $!) 1 53 54
63 53 my $class_dir = dirname($classes_file);
64 53 ▶while ( <$fh_classes> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh_classes)) 159 53 212
65 159 chomp;
66  
67 # skip empty lines
68 159 ▶ if ( ! $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 53 106 159
69 53 next;
70 }
71  
72 # check file format
73 106 my @p = split(/\t/, $_);
74 106 ▶ if ( scalar @p != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @p != 2) 0 106 106
75 0 croak("File with classes has to have 2 columns. Line $. contains " . (scalar @p) . " columns.");
76 }
77  
78 # try to fix file name
79 106 ▶ if ( ! -f $p[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $p[1]) 106 0 106
80 106 my $prev = $p[1];
81 106 $p[1] = $class_dir . "/" . $p[1];
82 106 warn("File $prev does not exist, using $p[1] instead.");
83 }
84  
85 # register class
86 106 push(@classes, $p[0]);
87 106 $identifier->add_class($p[0], $p[1]);
88 }
89  
90 # identify classes
91 53 ▶if ( defined($input_file) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $input_file) { } 46 7 53
elsif (defined $file_list) { } 7 0 7
92 46 ▶ if ( $input_file eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file eq '-') { } 37 9 46
93 37 Lingua::YALI::_identify_handle($identifier, \*STDIN, $format, \@classes, $each_line);
94 } else {
95 9 Lingua::YALI::_identify($identifier, $input_file, $format, \@classes, $each_line);
96 }
97 } elsif ( defined($file_list) ) {
98 7 my $fh_list = undef;
99 7 my $list_dir = "";
100  
101 7 ▶ if ( $file_list eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_list eq '-') { } 0 7 7
102 0 $fh_list = \*STDIN;
103 } else {
104 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
105 6 $list_dir = dirname($file_list) . "/";
106 }
107 6 ▶ while ( my $file = <$fh_list> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readline $fh_list)) 12 6 18
108 12 chomp $file;
109 12 ▶ if ( ! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 12 0 12
110 12 $file = $list_dir . $file;
111 }
112 12 Lingua::YALI::_identify($identifier, $file, $format, \@classes, $each_line);
113 }
114 }
115  
116 __END__
117  
118 =pod
119  
120 =encoding UTF-8
121  
122 =head1 NAME
123  
124 yali-identifier - Script for language identification with custom models
125  
126 =head1 VERSION
127  
128 version 0.015
129  
130 =head1 SYNOPSIS
131  
132 yali-identifier [options]
133  
134 Identify languages with models trained by yali-builder.
135  
136 Options:
137  
138 -i, --input=F input file. When F is -, read standard input.
139 --filelist=F input files are read from F. When F is -, read them from standard input.
140 -f, --format=FORMAT output FORMAT
141 -c, --classes=F file with paths to model. Containing two columns - the first with class, the second one with path to the model.
142 -h, --help prints documentation
143  
144 FORMAT:
145  
146 single - prints out only the most probable class
147 all - prints out all classes sorted according to their probability descendetly
148 all_p - prints out all classes with probability sorted according to their probability descendetly
149 tabbed - prints out probabilities of classes separated by tab in order used in --classes
150  
151 More examples are available at L<http://search.cpan.org/perldoc?Lingua%3A%3AYALI%3A%3AExamples>
152  
153 Version: 0.015 (2014-05-04)
154  
155 =head1 AUTHOR
156  
157 Martin Majlis <martin@majlis.cz>
158  
159 =head1 COPYRIGHT AND LICENSE
160  
161 This software is Copyright (c) 2012 by Martin Majlis.
162  
163 This is free software, licensed under:
164  
165 The (three-clause) BSD License
166  
167 =cut
« bin/yali-builder ↑ Summary bin/yali-language-identifier »