| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%9/9
$format ne "single" and $format ne "all" and $format ne "all_p" and $format ne "tabbed"
$format ne "single" and $format ne "all" and $format ne "all_p"
$format ne "single" and $format ne "all"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $input_file or defined $file_list
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $input_file and defined $file_list
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 53 | my $class_dir = dirname($classes_file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 53 | ▶ | while ( <$fh_classes> ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 159 | chomp; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # skip empty lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 159 | ▶ | if ( ! $_ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 53 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # check file format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 106 | my @p = split(/\t/, $_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 106 | ▶ | if ( scalar @p != 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 46 | ▶ | if ( $input_file eq "-" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $fh_list = \*STDIN; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | ▶ | open($fh_list, "<", $file_list) or croak("Not found: $file_list\n" . $!); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 6 | $list_dir = dirname($file_list) . "/"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 6 | ▶ | while ( my $file = <$fh_list> ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 12 | chomp $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 12 | ▶ | if ( ! -f $file ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||