<commit_before><?php
/**
 * This script converts the iRail stations to the GTFS stops.txt file
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$dist = "dist/stops.txt";

function getStops(){
	$client = new Client();
	$url = "https://irail.be/stations/NMBS";
	$response = $client->get($url, [
	    'headers' => [
	        'Accept'     => 'application/json',
	    ]
	]);

	$json = $response->getBody();

	return json_decode($json);
}

function appendCSV($dist, $csv) {
	file_put_contents($dist, trim($csv).PHP_EOL, FILE_APPEND);
}

// header CSV
$header = "stop_id,stop_name,stop_lat,stop_lon,location_type";
appendCSV($dist, $header);

// content
$stops = getStops()->{"@graph"};

$csv = "";
for($i=0; $i<count($stops); $i++){
	$stop = $stops[$i];

	if (preg_match("/NMBS\/(\d+)/i", $stop->{"@id"}, $matches)) {
        $stop_id = 'stops:' . $matches[1];
    }

	$csv .= $stop_id . ",";
	$csv .= $stop->{"name"} . ",";
	$csv .= $stop->{"latitude"} . ",";
	$csv .= $stop->{"longitude"} . ",";
	$csv .= "0"; // what we describe are all parent_stations, which have "platforms"

	appendCSV($dist,$csv);
	$csv = "";
}<commit_msg>Fix #15. Platforms added as stops<commit_after><?php
/**
 * This script converts the iRail stations to the GTFS stops.txt file
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ServerException;

$configs = include('config.php');

$dist = "dist/stops.txt";

function getStops(){
	$client = new Client();
	$url = "https://irail.be/stations/NMBS";
	$response = $client->get($url, [
	    'headers' => [
	        'Accept'     => 'application/json',
	    ]
	]);

	$json = $response->getBody();

	return json_decode($json);
}

function getNumberAndLetterPairOfPlatforms($url) {
	$client = new Client();

	try {
		$response = $client->get($url, [
		    'headers' => [
		        'Accept'     => 'application/json',
		    ]
		]);
	} catch (RequestException $e) {
	    return NULL;
	} catch (ServerException $e) {
		return NULL;
	}

	$arrivals = json_decode($response->getBody())->{"@graph"};

	// Loop through train-arrivals and keep biggest platformnumber
	$maxNr = NULL;
	$maxLetter = NULL;
	for ($i=0; $i < count($arrivals); $i++) {
		$platform = $arrivals[$i]->{"platform"};
		if ($platform != "") {
			if (is_numeric($platform)) {
				// Regular platform number
				if ($platform > $maxNr) {
					$maxNr = $platform;
				}
			} else {
				// Platform is a letter
				if ($platform > $maxLetter) {
					$maxLetter = $platform;
				}
			}
		}		
	}

	return [$maxNr, $maxLetter];
}

function getStopName($parent_name, $platformNr) {
	global $configs;

	if ($configs["language"] == 'en') {
		$stop_name = $parent_name . ' platform ' . $platformNr;
	} else if ($configs["language"] == 'fr') {
		$stop_name = $parent_name . ' quai ' . $platformNr;
	} else if ($configs["language"] == 'de') {
		$stop_name = $parent_name . ' bahnsteig ' . $platformNr;
	} else {
		$stop_name = $parent_name . ' perron ' . $platformNr;
	}

	return $stop_name;
}

function addStop($stop_id, $stop_name, $stop_lat, $stop_lon, $stop_station_type) {
	global $dist; 

	$csv = "";
	$csv .= $stop_id . ",";
	$csv .= $stop_name . ",";
	$csv .= $stop_lat . ",";
	$csv .= $stop_lon . ",";
	$csv .= $stop_station_type;

	appendCSV($dist,$csv);
}

function appendCSV($dist, $csv) {
	file_put_contents($dist, trim($csv).PHP_EOL, FILE_APPEND);
}

// header CSV
$header = "stop_id,stop_name,stop_lat,stop_lon,location_type";
appendCSV($dist, $header);

// content
$stops = getStops()->{"@graph"};

for ($i=0; $i<count($stops); $i++) {
	$stop = $stops[$i];

	$parent_id = $stop->{"@id"};
	if (preg_match("/NMBS\/(\d+)/i", $parent_id, $matches)) {
        $parent_stop_id = 'stops:' . $matches[1];
    }
    $parent_name = $stop->{"name"};
    $parent_lat = $stop->{"latitude"};
    $parent_lon = $stop->{"longitude"};

    // what we describe are all parent_stations, which have "platforms"
    // Because we don't always the platforms of a trip, we need to add these parent_stations as normal stops
    $parent_station_type = 0;

    // Add parent station as stop
    addStop($parent_stop_id, $parent_name, $parent_lat, $parent_lon, $parent_station_type);

	// Search highest platformnumber of station
	// Add different platforms: stop_id of station + # + number of platform 
	$nrAndLetter = getNumberAndLetterPairOfPlatforms($parent_id);

	$nr = $nrAndLetter[0];
	$letter = $nrAndLetter[1];

	// Add stop for every platform with a number
	if ($nr != null) {
		$j = 1;
		while ($j <= $nr) {
			$stop_id = $parent_stop_id . ':' . $j;

			$stop_name = getStopName($parent_name, $j);
			$stop_type = 0;

			// Todo: get latitude and longitude of every platform
			addStop($stop_id, $stop_name, $parent_lat, $parent_lon, $stop_type);

			$j++;
		}
	}
	// Add stop for every platform with a letter
	if ($letter != null) {
		$j = 'A';
		while ($j <= $letter) {
			$stop_id = $parent_stop_id . ':' . $j;

			$stop_name = getStopName($parent_name, $j);
			$stop_type = 0;

			// Todo: get latitude and longitude of every platform
			addStop($stop_id, $stop_name, $parent_lat, $parent_lon, $stop_type);

			$j++;
		}
	}
}
{'fraction_non_alphanumeric': 0.19443922600037572, 'fraction_numerical': 0.0026300958106331017, 'mean_word_length': 3.6215277777777777, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5289466', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1958, 'n_words': 436}