<commit_before><?php

// Base URL for the Next to Arrive API.
define("NTA_BASE_URL", "http://www3.septa.org/hackathon/NextToArrive/");

// URL to SEPTA stations grammar.
define("GRAMMAR_URL", "https://raw.github.com/mheadd/septalking/master/septa-stops.xml");

// Voice to use when rendering TTS.
define("TTS_VOICE_NAME", "Victor");

// Number of train departures to return to user.
define("NUM_TRAINS", 1);

// Confidence level to use for confirming input.
define("CONFIDENCE_LEVEL", .43);

/**
 * Function to format train information for direct routes.
 */
function sayDirect($template, $train, $from, $to, $voice) {

	$delay = ($train->orig_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say = str_replace(
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->orig_train)), $from, trim($train->orig_departure_time), $to, trim($train->orig_arrival_time), $delay), 
		$template);
	say($say, array("voice" => $voice));	
}

/**
 * Function to format train information for indirect (multi-leg) routes.
 */
function sayInDirect($template, $train, $from, $to, $voice) {

	// Say connecting station.
	say("This trip has a connection at " . $train->Connection. ".", array("voice" => $voice));
	
	// Say first leg of trip.
	$delay = ($train->orig_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say1 = str_replace( 
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->orig_train)), $from, trim($train->orig_departure_time), $train->Connection, trim($train->orig_arrival_time), $delay), 
		$template);
	say($say1, array("voice" => $voice));	
	
	// Say second leg of trip.
	$delay = ($train->term_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say2 = str_replace( 
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->term_train)), $train->Connection, trim($train->term_depart_time), $to, trim($train->term_arrival_time), $delay), 
		$template);
	say($say2, array("voice" => $voice));	
	
}

/**
 * Function to ask caller for station name.
 */
function getStationName($prompt, $options) {
	$station = ask($prompt, $options);
	
	if($station->value == 'NO_MATCH') {
		say("Sorry, I dont recognize that station.", array("voice" => $options["voice"]));
		getStationName($prompt, $options);
	}
	
	// Attempts over.
	if($station->value == '') {
		say("Sorry, I did not get your response. Please try again later. Goodbye", array("voice" => $options["voice"]));
		hangup();
	}
	else {
		_log("*** Recognized: ". $station->value ." ***");
	}
	
	if($station->choice->confidence < CONFIDENCE_LEVEL) {
		say("I think you said, " . $station->value . ".", array("voice" => $options["voice"]));
		if(confirmEntry($options["voice"])) {
			return $station->value;
		}
		else {
			_log("*** Caller rejected recognized input. ***");
			unset($station);
			getStationName($prompt, $options);
		}
	}
	else {
		return $station->value;
	}
}

/**
 * Helper function to confirm entry.
 */
function confirmEntry($voice) {
	$confirm = ask("Is that correct?", array("choices" => "yes,no", "voice" => $voice));
	return $confirm->value == "yes" ? true : false;
}

/**
 * Helper function to format station names for NTA API
 */

function formatStationName($name) {
	return str_replace(" ", "%20", ucwords($name)); 
}

// Settings based on channel used.
$timeout = ($currentCall->channel == "TEXT") ? 60.0 : 10.0;
$attempts = ($currentCall->channel == "TEXT") ? 1 : 3;
$choices = ($currentCall->channel == "TEXT") ? "[ANY]" : GRAMMAR_URL ;

// Message templates to use when rendering train info. on voice / text channels.
$voice_template = "Train %train_num%, Leaving from %from% at %departure_time%, arriving at %to% at %arrive_time%, currently running %delay%";
$text_template = "Train %train_num% from %from% (%departure_time%) to %to% (%arrive_time%): %delay%.";
$template = ($currentCall->channel == "TEXT") ? $text_template : $voice_template;

// Options to use when asking the caller for input.<commit_msg>Fixed null station value on retry<commit_after><?php

// Base URL for the Next to Arrive API.
define("NTA_BASE_URL", "http://www3.septa.org/hackathon/NextToArrive/");

// URL to SEPTA stations grammar.
define("GRAMMAR_URL", "https://raw.github.com/mheadd/septalking/master/septa-stops.xml");

// Voice to use when rendering TTS.
define("TTS_VOICE_NAME", "Victor");

// Number of train departures to return to user.
define("NUM_TRAINS", 1);

// Confidence level to use for confirming input.
define("CONFIDENCE_LEVEL", .43);

/**
 * Function to format train information for direct routes.
 */
function sayDirect($template, $train, $from, $to, $voice) {

	$delay = ($train->orig_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say = str_replace(
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->orig_train)), $from, trim($train->orig_departure_time), $to, trim($train->orig_arrival_time), $delay), 
		$template);
	say($say, array("voice" => $voice));	
}

/**
 * Function to format train information for indirect (multi-leg) routes.
 */
function sayInDirect($template, $train, $from, $to, $voice) {

	// Say connecting station.
	say("This trip has a connection at " . $train->Connection. ".", array("voice" => $voice));
	
	// Say first leg of trip.
	$delay = ($train->orig_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say1 = str_replace( 
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->orig_train)), $from, trim($train->orig_departure_time), $train->Connection, trim($train->orig_arrival_time), $delay), 
		$template);
	say($say1, array("voice" => $voice));	
	
	// Say second leg of trip.
	$delay = ($train->term_delay == "On time") ? "  on schedule" : $train->orig_delay . " late.";
	$say2 = str_replace( 
		array('%train_num%', '%from%', '%departure_time%', '%to%', '%arrive_time%', '%delay%'), 
		array(implode(" ", str_split($train->term_train)), $train->Connection, trim($train->term_depart_time), $to, trim($train->term_arrival_time), $delay), 
		$template);
	say($say2, array("voice" => $voice));	
	
}

/**
 * Function to ask caller for station name.
 */
function getStationName($prompt, $options) {
	$station = ask($prompt, $options);
	
	if($station->value == 'NO_MATCH') {
		say("Sorry, I dont recognize that station.", array("voice" => $options["voice"]));
		getStationName($prompt, $options);
	}
	
	// Attempts over.
	if($station->value == '') {
		say("Sorry, I did not get your response. Please try again later. Goodbye", array("voice" => $options["voice"]));
		hangup();
	}
	
	if($station->choice->confidence < CONFIDENCE_LEVEL) {
		say("I think you said, " . $station->value . ".", array("voice" => $options["voice"]));
		if(confirmEntry($options["voice"])) {
			_log("*** Returning: ". $station->value ." ***");
			return $station->value;
		}
		else {
			_log("*** Caller rejected recognized input. ***");
			unset($station);
			getStationName($prompt, $options);
		}
	}
	else {
		_log("*** Returning: ". $station->value ." ***");
		return $station->value;
	}
}

/**
 * Helper function to confirm entry.
 */
function confirmEntry($voice) {
	$confirm = ask("Is that correct?", array("choices" => "yes,no", "voice" => $voice));
	return $confirm->value == "yes" ? true : false;
}

/**
 * Helper function to format station names for NTA API
 */

function formatStationName($name) {
	return str_replace(" ", "%20", ucwords($name)); 
}

// Settings based on channel used.
$timeout = ($currentCall->channel == "TEXT") ? 60.0 : 10.0;
$attempts = ($currentCall->channel == "TEXT") ? 1 : 3;
$choices = ($currentCall->channel == "TEXT") ? "[ANY]" : GRAMMAR_URL ;

// Message templates to use when rendering train info. on voice / text channels.
$voice_template = "Train %train_num%, Leaving from %from% at %departure_time%, arriving at %to% at %arrive_time%, currently running %delay%";
$text_template = "Train %train_num% from %from% (%departure_time%) to %to% (%arrive_time%): %delay%.";
$template = ($currentCall->channel == "TEXT") ? $text_template : $voice_template;

// Options to use when asking the caller for input.
{'fraction_non_alphanumeric': 0.20389502434390214, 'fraction_numerical': 0.004275026718916993, 'mean_word_length': 4.656145063801209, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 84, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '567676', 'n_tokens_mistral': 2968, 'n_tokens_neox': 2799, 'n_words': 716}