Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created December 28, 2010 18:41
Show Gist options
  • Select an option

  • Save mheadd/757528 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/757528 to your computer and use it in GitHub Desktop.
A sample Tropo application that captures a street address through speech recognition
<?php
// Get the zip code from the caller.
$zip = ask("Say your 5 digit zip code", array("choices" => "[5 DIGITS]"));
// Get the address, using a dynamically generated grammar for the user zip code.
$address = ask("What is your street address, beginning with your street number?",
array("choices" => "http://path/to/web/host/streets.php?zip={$zip->value}"));
// Write the address to the logger.
_log("*** ".$address->value." ***");
// Tell the caller the address they entered.
say("You said, ".$address->value.", ".implode(" ", str_split($zip->value)).". Goodbye.");
hangup();
?>
DROP DATABASE IF EXISTS streets;
CREATE DATABASE streets;
USE streets;
DROP TABLE IF EXISTS names;
CREATE TABLE names(
street_name VARCHAR(150),
street_type VARCHAR(50),
street_zip CHAR(5)
);
INSERT INTO names (street_name, street_type, street_zip) VALUES ('First', 'Avenue', '18752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Grant', 'Avenue', '18752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Josiah', 'Parkway', '187252');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Milberry', 'Extension', '19752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Jones', 'Street', '19752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Martin Luther King', 'Boulevard', '19752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Halsey', 'Place', '19752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Murphy', 'Lane', '18752');
INSERT INTO names (street_name, street_type, street_zip) VALUES ('Chery Blossom', 'Circle', '18752');
<?php
error_reporting(0);
// Inlcude the PHPGrammar class file.
require_once 'classes/PHPGrammar.php';
$zip = (int) $_REQUEST['zip'];
$sql = "SELECT street_name, street_type FROM names WHERE street_zip = $zip";
$host = "";
$user = "";
$password = "";
$database = "streets";
// Create a new grammar instance.
$grammar = new Grammar(true, "en-US", "voice", "main", true);
// Main grammar rule.
$grammar->startRule("main", GrammarScope::$public);
$grammar->startItem(null, "1-");
$grammar->ruleRef("#number");
$grammar->endElement();
$grammar->startItem();
$grammar->ruleRef("#name");
$grammar->endElement();
$grammar->startItem(null, "0-1");
$grammar->ruleRef("#direction");
$grammar->endElement();
$grammar->endElement();
// The street number rule.
$grammar->startRule("number", GrammarScope::$private);
$grammar->startOneOf();
for($i=0; $i<10; $i++) {
$grammar->item($i);
}
$grammar->endElement();
$grammar->endElement();
// The street name rule.
$grammar->startRule("name", GrammarScope::$private);
$conn = mysql_connect($host, $user, $password);
mysql_select_db($database, $conn);
$result = mysql_query($sql);
// Iterate over results of MySQL query and create an item for each.
$grammar->startOneOf();
while ($row = mysql_fetch_assoc($result)) {
$grammar->item($row["street_name"]." ".$row["street_type"]);
}
$grammar->endElement();
$grammar->endElement();
// The street number rule.
$grammar->startRule("direction", GrammarScope::$private);
$grammar->startOneOf();
$grammar->item("north west");
$grammar->item("south west");
$grammar->item("north east");
$grammar->item("south east");
$grammar->endElement();
$grammar->endElement();
// Write the grammar out for applications to consume.
$grammar->writeGrammar();
?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<grammar xmlns="http://www.w3.org/2001/06/grammar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/06/grammar http://www.w3.org/TR/speech-grammar/grammar.xsd" version="1.0" xml:lang="en-US" mode="voice" root="main">
<rule id="main" scope="public">
<item repeat="1-">
<ruleref uri="#number"/>
</item>
<item>
<ruleref uri="#name"/>
</item>
<item repeat="0-1">
<ruleref uri="#direction"/>
</item>
</rule>
<rule id="number" scope="private">
<one-of>
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
</one-of>
</rule>
<rule id="name" scope="private">
<one-of>
<!-- Items in this rule are generated dynamically. -->
</one-of>
</rule>
<rule id="direction" scope="private">
<one-of>
<item>north west</item>
<item>south west</item>
<item>north east</item>
<item>south east</item>
</one-of>
</rule>
</grammar>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment