<commit_before>#!/usr/bin/env perl

package RER::DataSource::TransilienGTFS;

use strict;
use warnings;
use utf8;
use 5.010;

use RER::Train;

use DateTime::Format::Strptime;
use DBI;


sub dsn      { $_[0]->{dsn} = $_[1] || $_[0]->{dsn}; }
sub username { $_[0]->{username} = $_[1] || $_[0]->{username}; }
sub password { $_[0]->{password} = $_[1] || $_[0]->{password}; }
sub dbh      { $_[0]->{dbh} = $_[1] || $_[0]->{dbh}; }


sub new {
    my ($self, %args) = @_;
    $self = {};

    $self->{dsn}      = $args{dsn};
    $self->{username} = $args{username};
    $self->{password} = $args{password};

    $self->{dbh} = DBI->connect(
        $self->{dsn},
        $self->{username},
        $self->{password}
    ) or die $DBI::errstr;

    $self->{sth_ttfd} = $self->{dbh}->prepare(
        'CALL train_times_for_date(?, ?, ?)');
    $self->{sth_tsl}  = $self->{dbh}->prepare(
        'CALL train_station_list(?, ?, ?)');

    return bless $self, __PACKAGE__;
}



sub check_ligne {
	my ($ligne) = @_;
	return '' if not defined $ligne;

	my $value = $ligne;

	# $value should usually contain a one-letter value, or
	# "TER".  However SNCF somehow manages to fuck this up
	# big time.
	given ($value) {
		$value = 'C' when /Gare d'Aus/i;
		$value = 'C' when /Invalides /i; # note the space
		$value = 'D' when /Evry Courc/i;
		$value = 'D' when /Grigny Cen/i;
		$value = 'D' when /Le Bras de/i;
		$value = 'D' when /Orangis Bo/i;
		$value = 'D' when /Juvisy => /i; # note the space
		$value = 'E' when /Haussmann /i; # note the space
		$value = 'H' when /LUZARCHES /i; # note the space
		$value = 'J' when /Gisors => /i; # note the space
		$value = 'J' when /Mantes la /i; # note the space
		$value = 'L' when /St Nom la /i; # note the space
		$value = 'R' when /Montargis /i; # note the space
		$value = 'TER' when 'Train';
	}
	return $value;
}




sub get_info_for_train {
    my ($self, $date, $station_code, $train_number) = @_;

    $self->{sth_ttfd}->execute($date, $station_code, $train_number);
    my $data = $self->{sth_ttfd}->fetchall_arrayref;

    # Hack spécifique aux gares ayant deux numéros UIC identiques
    if ($station_code eq 'ERT' && scalar @$data == 0) {
        $self->{sth_ttfd}->execute($date, 'ERU', $train_number);
        $data = $self->{sth_ttfd}->fetchall_arrayref;
    }

    # Hack spécifique aux numéros de train impairs : il arrive parfois que les
    # numéros de train changent de parité en cours de route.  Dans ce cas, il
    # faut prendre le numéro de train moins 1 et réessayer.
    # Bien entendu, ne pas faire ça sur les "numéros" de train RATP.
    if ($train_number !~ /[A-Z]{4}[0-9]{2}/ 
            && $train_number % 2 == 1 
            && scalar @$data == 0) {<commit_msg>Bugfix: improve non-numeric train number detection<commit_after>#!/usr/bin/env perl

package RER::DataSource::TransilienGTFS;

use strict;
use warnings;
use utf8;
use 5.010;

use RER::Train;

use DateTime::Format::Strptime;
use DBI;


sub dsn      { $_[0]->{dsn} = $_[1] || $_[0]->{dsn}; }
sub username { $_[0]->{username} = $_[1] || $_[0]->{username}; }
sub password { $_[0]->{password} = $_[1] || $_[0]->{password}; }
sub dbh      { $_[0]->{dbh} = $_[1] || $_[0]->{dbh}; }


sub new {
    my ($self, %args) = @_;
    $self = {};

    $self->{dsn}      = $args{dsn};
    $self->{username} = $args{username};
    $self->{password} = $args{password};

    $self->{dbh} = DBI->connect(
        $self->{dsn},
        $self->{username},
        $self->{password}
    ) or die $DBI::errstr;

    $self->{sth_ttfd} = $self->{dbh}->prepare(
        'CALL train_times_for_date(?, ?, ?)');
    $self->{sth_tsl}  = $self->{dbh}->prepare(
        'CALL train_station_list(?, ?, ?)');

    return bless $self, __PACKAGE__;
}



sub check_ligne {
	my ($ligne) = @_;
	return '' if not defined $ligne;

	my $value = $ligne;

	# $value should usually contain a one-letter value, or
	# "TER".  However SNCF somehow manages to fuck this up
	# big time.
	given ($value) {
		$value = 'C' when /Gare d'Aus/i;
		$value = 'C' when /Invalides /i; # note the space
		$value = 'D' when /Evry Courc/i;
		$value = 'D' when /Grigny Cen/i;
		$value = 'D' when /Le Bras de/i;
		$value = 'D' when /Orangis Bo/i;
		$value = 'D' when /Juvisy => /i; # note the space
		$value = 'E' when /Haussmann /i; # note the space
		$value = 'H' when /LUZARCHES /i; # note the space
		$value = 'J' when /Gisors => /i; # note the space
		$value = 'J' when /Mantes la /i; # note the space
		$value = 'L' when /St Nom la /i; # note the space
		$value = 'R' when /Montargis /i; # note the space
		$value = 'TER' when 'Train';
	}
	return $value;
}




sub get_info_for_train {
    my ($self, $date, $station_code, $train_number) = @_;

    $self->{sth_ttfd}->execute($date, $station_code, $train_number);
    my $data = $self->{sth_ttfd}->fetchall_arrayref;

    # Hack spécifique aux gares ayant deux numéros UIC identiques
    if ($station_code eq 'ERT' && scalar @$data == 0) {
        $self->{sth_ttfd}->execute($date, 'ERU', $train_number);
        $data = $self->{sth_ttfd}->fetchall_arrayref;
    }

    # Hack spécifique aux numéros de train impairs : il arrive parfois que les
    # numéros de train changent de parité en cours de route.  Dans ce cas, il
    # faut prendre le numéro de train moins 1 et réessayer.
    # Bien entendu, ne pas faire ça sur les "numéros" de train RATP.
    if ($train_number =~ /[0-9]{6}/ 
            && $train_number % 2 == 1 
            && scalar @$data == 0) {
{'fraction_non_alphanumeric': 0.18641592112470332, 'fraction_numerical': 0.00931166697096951, 'mean_word_length': 2.912857142857143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4537038', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2141, 'n_words': 591}