cvs: pear /Services_Yahoo/Yahoo/ContentAnalysis AbstractContentAnalysis.php Response.php spellingSuggestion.php termExtraction.php
| From: | Martin Jansen | Date: | Mon, 18 Apr 2005 18:10:02 +0000 |
| Subject: | cvs: pear /Services_Yahoo/Yahoo/ContentAnalysis AbstractContentAnalysis.php Response.php spellingSuggestion.php termExtraction.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-31289@lists.php.net to get a copy of this message | ||
mj Mon Apr 18 14:10:02 2005 EDT
Added files:
/pear/Services_Yahoo/Yahoo/ContentAnalysis
AbstractContentAnalysis.php
Response.php
spellingSuggestion.php
termExtraction.php
Log:
* Added new files for the content analysis module.
http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Abstract search class * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: AbstractContentAnalysis.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "Services/Yahoo/ContentAnalysis/Response.php"; require_once "HTTP/Request.php"; /** * Abstract search class * * This abstract class serves as the base class for all different * types of searches that available through Services_Yahoo. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: AbstractContentAnalysis.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ abstract class Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $parameters = array("appid" => "PEAR_Services_Yahoo"); /** * Submits the search * * This method submits the search and handles the response. It * returns an instance of Services_Yahoo_Result which may be used * to further make use of the result. * * @access public * @return object Services_Yahoo_Response Search result * @throws Services_Yahoo_Exception */ public function submit() { $request = new HTTP_Request($this->requestURL); foreach ($this->parameters as $key => $value) { $request->addQueryString($key, $value); } $result = $request->sendRequest(); if (PEAR::isError($result)) { throw new Services_Yahoo_Exception($result->getMessage()); } return new Services_Yahoo_ContentAnalysis_Response($request); } /** * Set Application ID for the content analysis * * An Application ID is a string that uniquely identifies your * application. Think of it as like a User-Agent string. If you * have multiple applications, you should use a different ID for * each one. You can register your ID and make sure nobody is * already using your ID on Yahoo's Application ID registration * page. * * The ID defaults to "PEAR_Services_Yahoo", but you are free to * change it to whatever you want. Please note that the access * to the Yahoo API is not limited via the Application ID but via * the IP address of the host where the package is used. * * @link http://api.search.yahoo.com/webservices/register_application * @link http://developer.yahoo.net/documentation/rate.html * @access public * @param string Application ID * @return void */ public function setAppID($id) { $this->parameters['appid'] = $id; } /** * Set the query for the content analysis * * @access public * @param string Query for the content analysis */ public function setQuery($query) { $this->parameters['query'] = $query; } } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Services_Yahoo Content Analysis Response * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Response.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ /** * Services_Yahoo Content Analysis Response class * * This class provides methods for accessing the response of a content * analysis request. * * @category Services * @package Services_Yahoo * @extends Exception * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Response.php,v 1.1 2005/04/18 18:10:01 mj Exp $ */ class Services_Yahoo_ContentAnalysis_Response implements Iterator { private $isValidIterator = true; private $iteratorCounter = 0; private $request; private $results = array(); /** * Constructor * * @param object HTTP_Request Instance of HTTP_Request that was used for the request * @throws Services_Yahoo_Exception */ public function __construct(HTTP_Request $request) { $this->request = $request; $this->parseRequest(); if ($this->isError() == true) { $exception = new Services_Yahoo_Exception("Content analysis failed"); $exception->addErrors($this->getMessages()); throw $exception; } } // {{{ response handling /** * Get number of result sets returned by the content analysis * * @access public * @return integer Number of result sets returned */ public function getTotalResultsReturned() { return count((array)$this->xml->Result); } /** * Get the HTTP_Request instance that was used for the query * * Access to the HTTP_Request instance is useful for introspecting * into the request details. (E.g. for getting the HTTP response * code.) * * @access public * @return object HTTP_Request Instance of HTTP_Request */ public function getRequest() { return $this->request; } // }}} // {{{ Iterator implementation public function current() { return (string)$this->xml->Result[$this->iteratorCounter]; } public function next() { $this->iteratorCounter++; if (!isset($this->xml->Result[$this->iteratorCounter])) { $this->isValidIterator = false; } } public function key() { return $this->iteratorCounter; } public function rewind() { $this->iteratorCounter = 0; } public function valid() { return $this->isValidIterator; } // }}} // {{{ private methods /** * Parse XML from the response * * @access private * @throws Services_Yahoo_Exception */ private function parseRequest() { $this->xml = simplexml_load_string($this->request->getResponseBody()); if ($this->xml === false) { throw new Services_Yahoo_Exception("The response contained no valid XML"); } } /** * Determine if an error was returned by the Yahoo API * * This method evaluates the HTTP response code. If it indicates * an error, the method returns true. * * @access private * @return boolean True on error, otherwise false. */ private function isError() { return in_array($this->request->getResponseCode(), array(400, 403, 404, 503)); } /** * Get all error messages if the response contained an error * * Returns all errors in an numerically indexed array that were * part of the response. * * @access private * @see isError() * @return array */ private function getMessages() { $returnValue = array(); foreach ($this->xml->Message as $message) { $returnValue[] = $message; } return $returnValue; } // }}} } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Spelling Suggestion service * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: spellingSuggestion.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "AbstractContentAnalysis.php"; /** * Spelling Suggestion service class * * This class implements an interface to Yahoo's Spelling Suggestion * service by using the Yahoo API. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: spellingSuggestion.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo * @link http://developer.yahoo.net/content/V1/termExtraction.html */ class Services_Yahoo_ContentAnalysis_spellingSuggestion extends Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $requestURL = "http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion"; } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Term Extraction service class * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: termExtraction.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "AbstractContentAnalysis.php"; /** * Term Extraction service class * * This class implements an interface to Yahoo's Term Extraction service * by using the Yahoo API. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: termExtraction.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo * @link http://developer.yahoo.net/content/V1/termExtraction.html */ class Services_Yahoo_ContentAnalysis_termExtraction extends Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $requestURL = "http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction"; /** * Set the context to extract terms from. * * Note: One must not encode the context to UTF-8 before passing it * to this method. The encoding will be done automatically. * * @access public * @param string Context to extract terms from */ public function setContext($context) { $this->parameters['context'] = utf8_encode($context); } }
http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/AbstractContentAnalysis.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Abstract search class * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: AbstractContentAnalysis.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "Services/Yahoo/ContentAnalysis/Response.php"; require_once "HTTP/Request.php"; /** * Abstract search class * * This abstract class serves as the base class for all different * types of searches that available through Services_Yahoo. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: AbstractContentAnalysis.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ abstract class Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $parameters = array("appid" => "PEAR_Services_Yahoo"); /** * Submits the search * * This method submits the search and handles the response. It * returns an instance of Services_Yahoo_Result which may be used * to further make use of the result. * * @access public * @return object Services_Yahoo_Response Search result * @throws Services_Yahoo_Exception */ public function submit() { $request = new HTTP_Request($this->requestURL); foreach ($this->parameters as $key => $value) { $request->addQueryString($key, $value); } $result = $request->sendRequest(); if (PEAR::isError($result)) { throw new Services_Yahoo_Exception($result->getMessage()); } return new Services_Yahoo_ContentAnalysis_Response($request); } /** * Set Application ID for the content analysis * * An Application ID is a string that uniquely identifies your * application. Think of it as like a User-Agent string. If you * have multiple applications, you should use a different ID for * each one. You can register your ID and make sure nobody is * already using your ID on Yahoo's Application ID registration * page. * * The ID defaults to "PEAR_Services_Yahoo", but you are free to * change it to whatever you want. Please note that the access * to the Yahoo API is not limited via the Application ID but via * the IP address of the host where the package is used. * * @link http://api.search.yahoo.com/webservices/register_application * @link http://developer.yahoo.net/documentation/rate.html * @access public * @param string Application ID * @return void */ public function setAppID($id) { $this->parameters['appid'] = $id; } /** * Set the query for the content analysis * * @access public * @param string Query for the content analysis */ public function setQuery($query) { $this->parameters['query'] = $query; } } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/Response.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Services_Yahoo Content Analysis Response * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Response.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ /** * Services_Yahoo Content Analysis Response class * * This class provides methods for accessing the response of a content * analysis request. * * @category Services * @package Services_Yahoo * @extends Exception * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Response.php,v 1.1 2005/04/18 18:10:01 mj Exp $ */ class Services_Yahoo_ContentAnalysis_Response implements Iterator { private $isValidIterator = true; private $iteratorCounter = 0; private $request; private $results = array(); /** * Constructor * * @param object HTTP_Request Instance of HTTP_Request that was used for the request * @throws Services_Yahoo_Exception */ public function __construct(HTTP_Request $request) { $this->request = $request; $this->parseRequest(); if ($this->isError() == true) { $exception = new Services_Yahoo_Exception("Content analysis failed"); $exception->addErrors($this->getMessages()); throw $exception; } } // {{{ response handling /** * Get number of result sets returned by the content analysis * * @access public * @return integer Number of result sets returned */ public function getTotalResultsReturned() { return count((array)$this->xml->Result); } /** * Get the HTTP_Request instance that was used for the query * * Access to the HTTP_Request instance is useful for introspecting * into the request details. (E.g. for getting the HTTP response * code.) * * @access public * @return object HTTP_Request Instance of HTTP_Request */ public function getRequest() { return $this->request; } // }}} // {{{ Iterator implementation public function current() { return (string)$this->xml->Result[$this->iteratorCounter]; } public function next() { $this->iteratorCounter++; if (!isset($this->xml->Result[$this->iteratorCounter])) { $this->isValidIterator = false; } } public function key() { return $this->iteratorCounter; } public function rewind() { $this->iteratorCounter = 0; } public function valid() { return $this->isValidIterator; } // }}} // {{{ private methods /** * Parse XML from the response * * @access private * @throws Services_Yahoo_Exception */ private function parseRequest() { $this->xml = simplexml_load_string($this->request->getResponseBody()); if ($this->xml === false) { throw new Services_Yahoo_Exception("The response contained no valid XML"); } } /** * Determine if an error was returned by the Yahoo API * * This method evaluates the HTTP response code. If it indicates * an error, the method returns true. * * @access private * @return boolean True on error, otherwise false. */ private function isError() { return in_array($this->request->getResponseCode(), array(400, 403, 404, 503)); } /** * Get all error messages if the response contained an error * * Returns all errors in an numerically indexed array that were * part of the response. * * @access private * @see isError() * @return array */ private function getMessages() { $returnValue = array(); foreach ($this->xml->Message as $message) { $returnValue[] = $message; } return $returnValue; } // }}} } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/spellingSuggestion.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Spelling Suggestion service * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: spellingSuggestion.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "AbstractContentAnalysis.php"; /** * Spelling Suggestion service class * * This class implements an interface to Yahoo's Spelling Suggestion * service by using the Yahoo API. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: spellingSuggestion.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo * @link http://developer.yahoo.net/content/V1/termExtraction.html */ class Services_Yahoo_ContentAnalysis_spellingSuggestion extends Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $requestURL = "http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion"; } http://cvs.php.net/co.php/pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php?r=1.1&p=1 Index: pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php +++ pear/Services_Yahoo/Yahoo/ContentAnalysis/termExtraction.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /** * Term Extraction service class * * PHP version 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: termExtraction.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo */ require_once "AbstractContentAnalysis.php"; /** * Term Extraction service class * * This class implements an interface to Yahoo's Term Extraction service * by using the Yahoo API. * * @category Services * @package Services_Yahoo * @author Martin Jansen <mj@php.net> * @copyright 2005 Martin Jansen * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: termExtraction.php,v 1.1 2005/04/18 18:10:01 mj Exp $ * @link http://pear.php.net/package/Services_Yahoo * @link http://developer.yahoo.net/content/V1/termExtraction.html */ class Services_Yahoo_ContentAnalysis_termExtraction extends Services_Yahoo_ContentAnalysis_AbstractContentAnalysis { protected $requestURL = "http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction"; /** * Set the context to extract terms from. * * Note: One must not encode the context to UTF-8 before passing it * to this method. The encoding will be done automatically. * * @access public * @param string Context to extract terms from */ public function setContext($context) { $this->parameters['context'] = utf8_encode($context); } }