in reply to
No such file or directory error
You have a lot of duplication in your code so I cleaned it up a bit and removed some redundant code.
#!/usr/bin/perl
use warnings;
use strict;
use List::Util qw/ max sum /;
my %file_types = (
ENG => 'ENGLISH',
AFR => 'AFRIKAANS',
NDB => 'NDEBELE',
SEP => 'SEPEDI',
SIS => 'SISWATI',
SOT => 'SOTHO',
TSO => 'TSONGA',
TSW => 'TSWANA',
VEN => 'VENDA',
XHO => 'XHOSA',
ZUL => 'ZULU',
);
my %words;
for my $file ( <korpus*.txt> ) {
$file =~ /\Akorpus([A-Z]{3})\.txt\z/ or next;
my $type = $1;
exists $file_types{ $type } or next;
local $/;
open my $FH, '<', $file or die "Cannot open '$file' because: $!";
@{ $words{ $type } }{ grep length, split /\n/, <$FH> } = ();
}
#############################################################
my $input;
do {
print "To classify a single file, press 1\nTo work within a direct
+ory, press 2\n";
chomp( $input = <STDIN> );
} until $input =~ /\A[12]\z/;
if ( $input == 1 ) {
print "Please type the name of the file you wish to classify\n";
chomp( my $filename = <STDIN> );
folder( $filename );
}
else {
my $dir = 'C:\Users\ZB\Desktop\Text Files';
opendir my $DIR, $dir or die "cannot opendir $dir";
for my $file ( map "$dir/$_", readdir $DIR ) {
next unless -f $file;
folder( $file );
}
}
######################################################################
+############################
sub folder {
my ( $fname ) = @_;
open my $FIN, '<', $fname or die "Cannot open '$fname' because: $!
+";
my %hash;
while ( <$FIN> ) {
while ( /(\w+)/g ) {
$hash{ lc $1 }++;
}
}
######################################################
my $total = sum values %hash;
my @highest = ( '', 0 );
for my $type ( keys %file_types ) {
my $percentile = sum( @hash{ keys %{ $words{ $type } } } ) / $
+total;
@highest = ( $type, $percentile ) if $highest[ 1 ] < $percenti
+le;
}
########################################################## D
+etermine lingo through percentiles
print "The file '$fname' is $file_types{ $highest[ 0 ] }\n";
}