#!/usr/bin/perl

use strict;

my @folders = grep { /^[A-Z_]+$/ } grep { -d $_ } < * >;

for my $folder (@folders) {
    print STDERR "Training [$folder]";
    if (-f "$folder/$folder.txt.bz2") {
        print STDERR "\n";
        my ($name) = grep { /^[a-z]/ } map { s!$folder/!!; $_ } <$folder/*>;
        print STDERR " ! language name: $name\n";
        print STDERR " ! language code: $folder\n";

        bunzip_data($folder);

        print STDERR " - training model...";
        `cd $folder; perl ../../make-lingua-identify-language -u $folder $name $folder.txt`;
        print STDERR "done\n";


        bzip_data($folder);
    } else {
        print STDERR " - skipped (no training data)\n";
    }
}


sub bunzip_data {
    my $lc = shift;
    print STDERR " - uncompressing training data...";
    `bunzip2 $lc/$lc.txt.bz2`;
    print STDERR "done\n";
}

sub bzip_data {
    my $lc = shift;
    print STDERR " - compressing training data...";
    `bzip2 $lc/$lc.txt`;
    print STDERR "done\n";
}
