Location: PHPKode > scripts > Define Word > define-word/DefineWord.class.php
<?php


/**
  * PHP-Class DefineWord Version 1.0 , released 17-DEC-2006
  * Author: Dror Golan, hide@address.com
  *
  * License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html)
  *
  * If you find it useful, you might rate it on http://www.phpclasses.org
  * If you use this class in a productional environment, you may drop me a note, so I can add a link to the page.
  *
  ** TODO : 
  *
  * License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html)
  *
  * This program is free software;
  *
  * you can redistribute it and/or modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2 of the License,
  * or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along with this program;
  * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  *
  **/

  
  
    
/**
  *  This class uses the CURL packge and the DICT protocol (RFC 2229) in order
  *  to fetch definitions of a word from a remote dictionary host server.The result definitions are stored inside an array
  *  and can be filtered according to size (short or normal) , definition category. the default hosted server this class uses
  *  is at http://dict.org , while it can use with any dict protocol server list (http://luetzschena-stahmeln.de/dictd/index.php. The usage of the this resources)
  *  the copyright information of the dict resources is located here : http://www.dict.org/bin/Dict?Form=Dict1&Query=00-database-info&Strategy=*&Database=*
  *   
  **/
 
  

class DefineWord {

    ////////////////////////////////
	//
	//	PUBLIC PARAMS
	//

		 /**
		  * @shortdesc word to define
          * @type String
		  * @public
          **/
		 var $word;
		 
		 /**
		  * @shortdesc total number of Definitions
          * @type Integer
		  * @public
          **/		 
		 var $totalDefinitions;
		 
		 /**
		  * @shortdesc Array which stores word definitions
          * @type String Array
		  * @public
          **/		 
		  var $definitionsArray;
		  
		  /**
		  * @shortdesc Url Src of dict repository server
          * @type String 
		  * @default Value : "dict.org"
		  * @browse the following URL for other resources:http://luetzschena-stahmeln.de/dictd/index.php
		  * @public
          **/	
		  	 
		  var $srcReference;
		  
		  /**
		  * @shortdesc Paramteres for Curl's query request
          * @type String 
		  * @Value : "short" : retrieve shortest definition
		  *        : Any other String : filter definitions from a designated Category
		  *                             e.g. (Math) , (Naut) , refer to dict protocol definition for Category syntax
          **/	
		  
		  var $params;
		  
		  /**
		  * @shortdesc maximum allowed definition 
          * @type Integer 
          **/	
		  var $limit;

		  
		  
		  
//Constructor		  
function DefineWord ($theWord,$src="dict.org",$limit=NULL,$params=NULL) {
$this->srcReference=$src;
if ($limit==NULL) $limit=100;
$this->limit=$limit;
$this->params=$params;
$this->word=$theWord;
$this->definitionsArray = $this->getDefinitionsFromSrc($theWord,$limit,$params);
$this->totalDefinitions = count($this->definitionsArray);
}


/**
		  *
		  * @shortdesc create associative array of difinitions of a word
		  * Parameters : 
		  * theWord : the word to be translate
		  * limit   : limit the maximum amount of definitions
		  * params  : "short" - retrieve the short type of definition (exlude the part
		  *                     after the ';'
		  *           (Category Name) -  Filter the result to a specific Category
		  *                              Name in Bracets [e.g. "(Math)" ,"(Astr)"] 
		  * @public
		  *
		  **/
function getDefinitionsFromSrc($theWord,$limit,$params) {

     // Maximum definition per word.
     if ($limit==Null)
	    $limit = 100;
	
    // commit Request and fetch response
    $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "dict://$this->srcReference/d:($this->word)");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $definition = curl_exec($ch);
    curl_close($ch);
	
	 // Parse Resonse
	 $line = split("\n", $definition);
	 foreach ($line as $key=>$value){
	 $tmp = $line[$key];
	 $tmp = trim($tmp);
	 $tmp = str_replace('.)',')',$tmp);
	 $line[$key] = $tmp;
	 }
   
      $iindex = 0;
	  $definitionIndex = NULL;
	  foreach ($line as $key=>$value) {
	          if  ((strpos($value,'.') == 1) && (is_numeric(substr($value,0,1))))  $definitionIndex[$iindex++]=$key; }
	
      // accumulate definitions 
	  $myndex = 0;
	  if ((count($definitionIndex)>0) AND ( $definitionIndex != NULL)) {
	  foreach ($definitionIndex as $key=>$value) {

      $thisLine = $line[$value];
	  $thisLinePlus1 = $line[$value+1];
	  $thisLinePlus2 = $line[$value+2];
	  $thisLinePlus3 = $line[$value+3];
		  
	  
	  if (strpos ($thisLine,'.',2) > 0)  $myDefinitions[$myndex++] =  $thisLine;
	  elseif
	       (strpos ($thisLinePlus1,'.',1) > 2) $myDefinitions[$myndex++] = $thisLine.' '.$thisLinePlus1;
	      elseif
		        (strpos ($thisLinePlus2,'.',1) > 2) $myDefinitions[$myndex++] = $thisLine.' '.$thisLinePlus1.' '.$thisLinePlus2;
	  		   elseif
			         (strpos ($thisLinePlus3,'.',1) > 2) $myDefinitions[$myndex++] = $thisLine.' '.$thisLinePlus1.' '.$thisLinePlus2.' '.$thisLinePlus3;
					else 
					    $myDefinitions[$myndex++] = 'error';
	  
	  
	 } //  $definitionIndex
     $finalDefinitions = NULL;
	 $iindex = 1;
	 foreach ($myDefinitions as $key=>$value) {
	   if ($value != 'error'){
	    $define = explode(".",$value);
		if ($params==NULL)
	    	$finalDefinitions[$iindex++] = $define[1]; 
		elseif ($params=="short") {
		  $cutExtra = explode(";",$define[1]);
		  $finalDefinitions[$iindex++] = $cutExtra[0]; }
		elseif (strpos("@@@".$define[1],$params)>0) {
		   $finalDefinitions[$iindex++] = $define[1]; } 
		}}
		
		
		return array_slice($finalDefinitions, 0, $this->limit);  ;
	 
	 } // not an empty query.
	 
	 
} // getDifinitions

/**
		  *
		  * @shortdesc print a list of definitions
		  * Parameters : 
		  * mode : "game" - in "game" mode , the qwery parameters are not printed
		  *                 please refer to the guessWhat.php example file for more detailed
		  * @public
		  *
		  **/

function print_def_array($mode=NULL){
if ($mode!="game") {
echo "Definition(s) of <b><font color=\"red\">$this->word</font></b>, $this->totalDefinitions Definitions founded:<br>";
echo "Query Params:<br>";
echo "DB SRC : $this->srcReference<br>";
if ($this->params!=NULL) {
if ($this->params=="short") echo 'cut off additional phrase (text after ";") from text<br>';
else echo "filter results to Category:$this->params<br>";}
if ($this->limit!=NULL) echo "Limit results for maximum of $this->limit results<br>";
} // mode=game;
if (($this->totalDefinitions)>0) {
$ResultArray = $this->definitionsArray;
foreach ($ResultArray as $key=>$value) { 
  $actualIndex = $key + 1;
  echo "<b>[".$actualIndex."] </b>".$value."<br>";} }

} // print_def_array

} // class
Return current item: Define Word