X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=train.pl;h=97c33d32534c0697bf451d629e7a4ded8e06e8a9;hb=2d2a3a01f1d6828f37de15bd52a7bae6d64da117;hp=95dcd06208b61f87bfadcafb3093d68407ae9144;hpb=9891e2aeab5f6e2f5893a4182a1166e38ab81fb5;p=wloh diff --git a/train.pl b/train.pl index 95dcd06..97c33d3 100755 --- a/train.pl +++ b/train.pl @@ -4,65 +4,79 @@ use strict; use warnings; no warnings qw(once); use POSIX; -require './config.pm'; - -my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password) - or die "connect: " . $DBI::errstr; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +use lib qw( include ); +require 'config.pm'; +require 'common.pm'; # Find last completely done season -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 $last_season = $ref->{'sesong'}; - -my $tmpnam = POSIX::tmpnam(); -open DATA, ">", $tmpnam - or die "$tmpnam: $!"; - -# Fetch name (ID) list -my $q = $dbh->prepare('SELECT DISTINCT id FROM fotballdeltagere'); -$q->execute(); -my @ids = (); -while (my $ref = $q->fetchrow_hashref) { - my $id = $ref->{'id'}; - push @ids, $id; -} - -print DATA scalar @ids, "\n"; -for my $id (@ids) { - print DATA $id, "\n"; +sub find_last_season { + 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'}; } -# Fetch games -$q = $dbh->prepare(' +sub fetch_games { + 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 -'); -$q->execute($last_season); + 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, $locale); + + while (my $ref = $q->fetchrow_hashref) { + next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0); + next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 150); + next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 150); + next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 0); + push @$games, { %$ref }; + $ids->{$ref->{'p1'}} = 1; + $ids->{$ref->{'p2'}} = 1; + } +} + +sub output_to_file { + my ($locale, $games, $ids) = @_; + + my $tmpnam = POSIX::tmpnam(); + open DATA, ">", $tmpnam + or die "$tmpnam: $!"; + + printf DATA "%s\n", $locale; + printf DATA "%d\n", scalar keys %$ids; + for my $id (keys %$ids) { + printf DATA "%d\n", $id; + } + for my $ref (@$games) { + printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'}; + } + close DATA; -while (my $ref = $q->fetchrow_hashref) { - next if ($ref->{'maalfor'} == 150 && $ref->{'maalmot'} == 0); - next if ($ref->{'maalfor'} == 0 && $ref->{'maalmot'} == 150); - printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'}; + return $tmpnam; } -close DATA; +my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password) + or die "connect: " . $DBI::errstr; +$dbh->{AutoCommit} = 1; -$dbh->do('DELETE FROM ratings'); -my $iq = $dbh->prepare('INSERT INTO ratings ( id, rating ) VALUES (?, ?)'); +my @locales = wloh_common::find_all_locales($dbh); +my @filenames = (); -open RATINGS, "$config::base_dir/bayeswf < $tmpnam |" - or die "bayeswf: $!"; -while () { - /(.*) (.*)/ or next; - $iq->execute($2, $1); +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($locale, \@games, \%ids); + push @filenames, $tmpnam; } -$dbh->commit; -unlink($tmpnam); +$dbh->disconnect; + +system("$config::base_dir/bayeswf", @filenames);