Location: PHPKode > scripts > PHP Grammar > php-grammar/grammar.php
<?php

/*
************************************************************

GRAMMAR SUPER CLASS

Use this class to create W3C Grammars in XML format.
Complete reference at 
http://www.w3.org/TR/2004/REC-speech-grammar-20040316/

Written by Mark Headd, http://www.voiceingov.org
This work is licensed under a Creative Commons License
http://creativecommons.org/licenses/by-nc-sa/2.0/


************************************************************

************************************************************
ECHO XML PROLGOUE INFORMATION AND DTD
************************************************************
*/
echo '<?xml version="1.0" ?>';
echo "\n";
echo "<!DOCTYPE grammar PUBLIC \"-//W3C//DTD GRAMMAR 1.0//EN\"";
echo "\n";
echo "\"http://www.w3.org/TR/speech-grammar/grammar.dtd\">";
echo "\n";

class grammar 
{

/*
************************************************************

PROPERTIES

In most cases, these may be changed by passing a value as 
a parameter with the appropriate function call, or by 
calling specifc attribute setting functions from the  
createGRAMMAR sub class.

************************************************************
*/

var $language="en-US";
var $rootRule="mainRule";
var $rule_id="mainRule";
var $slot;

/*
************************************************************
FUNCTION TO GENERATE ROOT ELEMENT AND ATTRIBUTES
************************************************************
*/
function gramStart($lang=NULL, $root=NULL, $mode=NULL)
{
 if($root) {
 $this->rootRule=$root;
 }
 if($lang) {
 $this->language=$lang;
 }
 echo "<grammar ";
 if($mode) {
 echo "mode =\"".$mode."\" ";
 } 
 echo "version=\"1.0\" xml:lang=\"".$this->language."\"";
 echo " xmlns=\"http://www.w3.org/2001/06/grammar\"";
 echo " root=\"".$this->rootRule."\"";
 echo ">";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE CLOSING TAG FOR ROOT ELEMENT 
************************************************************
*/
function gramEnd()
{
 echo "</grammar>";
}

/*
************************************************************
FUNCTION TO GENERATE RULE ELEMENT AND ATTRIBUTES
************************************************************
*/
function ruleStart($scope="public", $id=NULL)
{
 if($id) {
 $this->rule_id=$id;
 }
 echo "<rule id=\"".$this->rule_id."\" scope=\"".$scope."\"";
 echo ">";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE CLOSING TAG FOR RULE ELEMENT 
************************************************************
*/
function ruleEnd()
{
 echo "</rule>";
 echo "\n";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE ONE-OF TAG AND ATTRIBUTE 
************************************************************
*/
function oneOfstart($lang=NULL)
{
 echo "<one-of";
 if ($lang) {
 $this->language=$lang;
 echo " xml:lang=\"".$this->language."\"";
 }
 echo ">";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE CLOSING TAG FOR ONE-OF ELEMENT  
************************************************************
*/
function oneOfend()
{
 echo "</one-of>";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE ITEM TAG AND ATTRIBUTES
************************************************************
*/
function itemStart($repeat=NULL, $repeat_prob=NULL, $weight=NULL, $lang=NULL)
{
 echo "<item";
 // If repeat attribute is given, echo repeat frequency
 if ($repeat) {
 echo " repeat=\"".$repeat."\"";
 } 
 // If repeat-prob attribute is given, echo repeat probability
 if ($repeat-prob) {
 echo " repeat-prob=\"".$repeat_prob."\"";
 }
 // If weight attribute is given, echo weight
 if ($weight) {
 echo " weight=\"".$weight."\"";
 }
 if ($lang) {
 $this->language=$lang;
 echo " xml:lang=\"".$this->language."\"";
 }
 echo ">";
}

/*
************************************************************
FUNCTION TO GENERATE CLOSING TAG FOR ITEM ELEMENT  
************************************************************
*/
function itemEnd()
{
 echo "</item>";
 echo "\n";
}

/*
************************************************************
FUNCTION TO GENERATE TOKEN ELEMENT AND ATTRIBUTES
************************************************************
*/
function token($lang=NULL, $text)
{
 if (!$text) {
 echo '<!-- ERROR: You must pass text to this function e.g., token("", "red"), or token ("en-US", "fish")-->';
 }
 echo "<token";
 if ($lang) {
 $this->language=$lang;
 echo " xml:lang=\"".$this->language."\"";
 }
 echo ">";
 echo $text;
 echo "</token>";
}

/*
************************************************************
FUNCTION TO GENERATE TAG ELEMENT AND ATTRIBUTES 
************************************************************
*/
function tag($value, $slotval=NULL)
{
 if(!isset($this->slot) && (!$slotval)) {
 echo '<!-- ERROR: You must use pass a slot value, or use the setSlot function to designate a slot name -->';
 }
 if($slotval) {
 $this->slot=$slotval;
 }
 if(!$value) {
 echo '<!-- ERROR: You must pass a value to this function -->';
 }
 echo " <tag>". $this->slot."=\"".$value."\"";
 echo "</tag> ";
}

/*
************************************************************
FUNCTION TO GENERATE RULE REFERENCE AND ATTRIBUTES 
************************************************************
*/
function ruleRef($uri=NULL, $special=NULL)
{
 if (!$uri && !$special) {
 echo '<!-- ERROR: You must pass a URI or a SPECIAL designation to this function -->';
 }
 if ($uri && $special) {
 echo '<!-- ERROR: You must pass a URI or a SPECIAL designation to this function, not both -->';
 }
 if ($uri) {
 echo "<ruleref uri=\"".$uri."\"/>";
 }
 if ($special) {
 echo "<ruleref special=\"".$special."/>";
 }
} 

/*
************************************************************
FUNCTION TO GENERATE EXAMPLE ELEMENT AND ATTRIBUTES 
************************************************************
*/
function example($text)
{
echo "<example>".$text."</example>";
}

/*
************************************************************
FUNCTION TO GENERATE LEXICON ELEMENT AND ATTRIBUTES
************************************************************
*/
function lexicon($uri, $type=NULL)
{
  if(!$uri) {
	echo '<!-- ERROR: You must pass a URI to this function -->';
	}
	echo "<lexicon uri=\"".$uri."\"";
	if($type) {
	echo " type=\"".$type."\"";
	}
	echo " />";
}

/*
************************************************************
FUNCTION TO GENERATE META ELEMENT AND ATTRIBUTES
************************************************************
*/
function meta($name=NULL, $http_equiv=NULL, $content) 
{
 if (!$name && !$http_equiv) {
 echo '<!-- ERROR: You must pass a name or a http-equiv value to this function -->';
 }
 if ($name && $http_equiv) {
 echo '<!-- ERROR: You must pass a name or a http-equiv value to this function, not both -->';
 }
 if ($name) {
 echo "<meta name=\"".$name." content=\"".$content."/>";
 }
 if ($http_equiv) {
 echo "<meta http-equiv=\"".$http_equiv." content=\"".$content."/>";
 }
}

}
?> 
Return current item: PHP Grammar