]> git.sesse.net Git - wloh/commitdiff
Train one model (with its own aux parms) per locale.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 May 2012 19:48:10 +0000 (21:48 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 May 2012 19:48:10 +0000 (21:48 +0200)
common.pm
train.pl
www/index.pl
www/rating.pl

index a5784003f39f2a13a9b73a128b2b44c96624f912..a5c730b806f3d0bda20e511bda9e77be7e8d2cf8 100644 (file)
--- a/common.pm
+++ b/common.pm
@@ -30,10 +30,10 @@ sub get_locale {
 }
 
 sub get_auxillary_parameters {
 }
 
 sub get_auxillary_parameters {
-       my ($dbh) = @_;
+       my ($dbh, $locale) = @_;
 
 
-       my $q = $dbh->prepare('SELECT * FROM aux_params');
-       $q->execute;
+       my $q = $dbh->prepare('SELECT * FROM aux_params WHERE kultur=?');
+       $q->execute($locale);
 
        my $aux_parms = {};
        while (my $ref = $q->fetchrow_hashref) {
 
        my $aux_parms = {};
        while (my $ref = $q->fetchrow_hashref) {
index 084f84b91c14f60029fe98f0b3e9dfcdb845f92f..9b1e0dcb923ef580014df114878e4a666a7c8199 100755 (executable)
--- a/train.pl
+++ b/train.pl
@@ -8,13 +8,13 @@ require './config.pm';
 
 # Find last completely done season 
 sub find_last_season {
 
 # Find last completely done season 
 sub find_last_season {
-       my $dbh = shift;
-       my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier GROUP BY sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) ORDER BY sesong DESC LIMIT 1');
+       my ($dbh, $locale) = @_;
+       my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier se JOIN fotballspraak sp ON se.spraak=sp.id GROUP BY kultur,sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) AND kultur=? ORDER BY kultur,sesong DESC LIMIT 1', undef, $locale);
        return $ref->{'sesong'};
 }
 
 sub fetch_games {
        return $ref->{'sesong'};
 }
 
 sub fetch_games {
-       my ($dbh, $last_season, $games, $ids) = @_;
+       my ($dbh, $locale, $last_season, $games, $ids) = @_;
        my $q = $dbh->prepare('
 SELECT
   deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
        my $q = $dbh->prepare('
 SELECT
   deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt
@@ -22,10 +22,11 @@ FROM
   Fotballresultater resultater
   JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie
   JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie
   Fotballresultater resultater
   JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie
   JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie
-  JOIN Fotballserier serier on resultater.Serie=serier.Nr
-WHERE deltager1.Nr > deltager2.nr
+  JOIN Fotballserier serier ON resultater.Serie=serier.Nr
+  JOIN Fotballspraak spraak ON serier.Spraak=spraak.Id
+WHERE deltager1.Nr > deltager2.nr AND kultur=?
        ');
        ');
-       $q->execute($last_season);
+       $q->execute($last_season, $locale);
 
        while (my $ref = $q->fetchrow_hashref) {
                next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0);
 
        while (my $ref = $q->fetchrow_hashref) {
                next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0);
@@ -58,7 +59,7 @@ sub output_to_file {
 }
 
 sub train_model {
 }
 
 sub train_model {
-       my ($filename, $ratings, $covariances, $aux_params) = @_;
+       my ($filename, $locale, $ratings, $covariances, $aux_params) = @_;
 
        open RATINGS, "$config::base_dir/bayeswf < $filename |"
                or die "bayeswf: $!";
 
        open RATINGS, "$config::base_dir/bayeswf < $filename |"
                or die "bayeswf: $!";
@@ -68,7 +69,7 @@ sub train_model {
                if ($x[0] eq 'covariance') {
                        push @$covariances, (join("\t", @x[1..3]));
                } elsif ($x[0] eq 'aux_param') {
                if ($x[0] eq 'covariance') {
                        push @$covariances, (join("\t", @x[1..3]));
                } elsif ($x[0] eq 'aux_param') {
-                       push @$aux_params, ("nb-NO" .  "\t" . $x[1] . "\t" . $x[2]);
+                       push @$aux_params, ($locale .  "\t" . $x[1] . "\t" . $x[2]);
                } else {
                        push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
                }
                } else {
                        push @$ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
                }
@@ -77,6 +78,19 @@ sub train_model {
        close RATINGS;
 }
 
        close RATINGS;
 }
 
+sub find_all_locales {
+       my $dbh = shift;
+       my $q = $dbh->prepare('SELECT kultur FROM fotballspraak');
+       $q->execute;
+
+       my @locales = ();
+       while (my $ref = $q->fetchrow_hashref) {
+               push @locales, $ref->{'kultur'};
+       }
+
+       return @locales;
+}
+
 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
        or die "connect: " . $DBI::errstr;
 $dbh->{AutoCommit} = 0;
 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
        or die "connect: " . $DBI::errstr;
 $dbh->{AutoCommit} = 0;
@@ -84,17 +98,22 @@ $dbh->{RaiseError} = 1;
 
 $dbh->do('SET client_min_messages TO WARNING');
 
 
 $dbh->do('SET client_min_messages TO WARNING');
 
-my $last_season = find_last_season($dbh);
-my @games = ();
-my %ids = ();
-fetch_games($dbh, $last_season, \@games, \%ids);
-my $tmpnam = output_to_file(\@games, \%ids);
+my @locales = find_all_locales($dbh);
 
 my @ratings = ();
 my @covariances = ();
 my @aux_params = ();
 
 my @ratings = ();
 my @covariances = ();
 my @aux_params = ();
-train_model($tmpnam, \@ratings, \@covariances, \@aux_params);
-unlink($tmpnam);
+
+for my $locale (@locales) {
+       my $last_season = find_last_season($dbh, $locale);
+       my @games = ();
+       my %ids = ();
+       fetch_games($dbh, $locale, $last_season, \@games, \%ids);
+       my $tmpnam = output_to_file(\@games, \%ids);
+
+       train_model($tmpnam, $locale, \@ratings, \@covariances, \@aux_params);
+       unlink($tmpnam);
+}
 
 $dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
 $dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
 
 $dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
 $dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
index c2efb97a2a43bd2fd059d2a55664cefcd26979f2..7546d658ac8a4b5bf14bf8a30b8e46d408c4092b 100755 (executable)
@@ -337,7 +337,8 @@ sub print_footer {
 EOF
 }
 
 EOF
 }
 
-my $aux_parms = wloh_common::get_auxillary_parameters($dbh);
+my $locale = wloh_common::get_locale($cgi);
+my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
 
 my $division = $cgi->param('divisjon') // -1;
 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
 
 my $division = $cgi->param('divisjon') // -1;
@@ -345,7 +346,6 @@ my $subdivision = $cgi->param('avdeling') // -1;
 my $match_player = $cgi->param('spiller');
 my $match_position = $cgi->param('posisjon');
 
 my $match_player = $cgi->param('spiller');
 my $match_position = $cgi->param('posisjon');
 
-my $locale = wloh_common::get_locale($cgi);
 my $season = wloh_common::get_max_season($dbh, $locale);
 die "Nonexistent locale!" if (!defined($season));
 
 my $season = wloh_common::get_max_season($dbh, $locale);
 die "Nonexistent locale!" if (!defined($season));
 
index f69b19ac2307a3f9392dadc625f14bfe7b190537..5eb822807d1859b7b013c6f17960f83de2c2aacc 100755 (executable)
@@ -20,7 +20,10 @@ $dbh->{RaiseError} = 1;
 
 binmode STDOUT, ':utf8';
 
 
 binmode STDOUT, ':utf8';
 
-my $aux_parms = wloh_common::get_auxillary_parameters($dbh);
+my $cgi = CGI->new;
+my $locale = wloh_common::get_locale($cgi);
+
+my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
 
 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
 
 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
@@ -78,8 +81,6 @@ printf <<"EOF", $aux_parms->{'rating_prior_stddev'}, $match_stddev;
       </tr>
 EOF
 
       </tr>
 EOF
 
-my $cgi = CGI->new;
-my $locale = wloh_common::get_locale($cgi);
 my $season = wloh_common::get_max_season($dbh, $locale);
 
 # Pick up all the subdivisions' ratings.
 my $season = wloh_common::get_max_season($dbh, $locale);
 
 # Pick up all the subdivisions' ratings.