Skip to content

Instantly share code, notes, and snippets.

@beaulebens
Last active May 17, 2016 15:08
Show Gist options
  • Save beaulebens/c37abd37a72488c2c69b0a8db136e9e6 to your computer and use it in GitHub Desktop.
Save beaulebens/c37abd37a72488c2c69b0a8db136e9e6 to your computer and use it in GitHub Desktop.
<?php
// Quick utterance generator for Alexa Skills Kit.
// Define utterance templates below, then set up token variants as arrays below that.
// Tokenized utterances
$utterances = array(
'Nearest for the {closest} {car}',
'Nearest where the {closest} {car} is',
'Nearest where is the {closest} {car}',
"Nearest where's the {closest} {car}",
'ReserveNearest to {reserve} me the {closest} {car}',
'ReserveNearest to {reserve} the {closest} {car}',
);
// Token variants, variables named after the tokens used above
$closest = array( 'nearest', 'closest' );
$car = array( 'car', 'vehicle' );
$reserve = array( 'reserve', 'book', 'get' );
// Take an array of strings, and replace a token with its variants in it
function replace_token_variants( $strings, $token, $variants ) {
$out = array();
foreach ( $strings as $string ) {
foreach ( $variants as $variant ) {
$out[] = str_replace( '{' . $token . '}', $variant, $string );
}
}
return $out;
}
$out = array();
foreach ( $utterances as $utterance ) {
preg_match_all( '/({([^}]+)})/', $utterance, $tokens );
$utter_variations = array( $utterance );
for ( $n = 0; $n < count( $tokens[2] ); $n++ ) {
$utter_variations = replace_token_variants( $utter_variations, $tokens[2][$n], ${$tokens[2][$n]} );
}
$out = array_merge( $out, $utter_variations );
}
echo implode( $out, "\n" ) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment