# 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 {
- 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
FROM
- Fotballresultater resultater
+ ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) 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);
}
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: $!";
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]);
}
close RATINGS;
}
+sub find_all_locales {
+ my $dbh = shift;
+ my $q = $dbh->prepare('SELECT kultur FROM fotballspraak WHERE nyestesesong<>-1');
+ $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;
$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 = ();
-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');