Advertisement
Guest User

new api

a guest
Jun 12th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.97 KB | None | 0 0
  1. <?php
  2.     /*
  3.      ChatterBotAPI
  4.      Copyright (C) 2011 pierredavidbelanger@gmail.com
  5.      
  6.      This program is free software: you can redistribute it and/or modify
  7.      it under the terms of the GNU Lesser General Public License as published by
  8.      the Free Software Foundation, either version 3 of the License, or
  9.      (at your option) any later version.
  10.      
  11.      This program is distributed in the hope that it will be useful,
  12.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.      GNU Lesser General Public License for more details.
  15.      
  16.      You should have received a copy of the GNU Lesser General Public License
  17.      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.     */
  19.    
  20.     #################################################
  21.    # API
  22.    #################################################
  23.    
  24.     class ChatterBotType
  25.     {
  26.         const CLEVERBOT = 1;
  27.         const JABBERWACKY = 2;
  28.         const PANDORABOTS = 3;
  29.     }
  30.    
  31.     class ChatterBotFactory
  32.     {
  33.         public function create($type, $arg = null)
  34.         {
  35.             switch ($type)
  36.             {
  37.                 case ChatterBotType::CLEVERBOT:
  38.                 {
  39.                     return new _Cleverbot('http://www.cleverbot.com/webservicemin', 26);
  40.                 }
  41.                 case ChatterBotType::JABBERWACKY:
  42.                 {
  43.                     return new _Cleverbot('http://jabberwacky.com/webservicemin', 20);
  44.                 }
  45.                 case ChatterBotType::PANDORABOTS:
  46.                 {
  47.                     if ($arg == null) {
  48.                         throw new Exception('PANDORABOTS needs a botid arg');
  49.                     }
  50.                     return new _Pandorabots($arg);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.    
  56.     abstract class ChatterBot
  57.     {
  58.         public function createSession()
  59.         {
  60.             return null;
  61.         }
  62.     }
  63.    
  64.     abstract class ChatterBotSession
  65.     {
  66.         public function thinkThought($thought)
  67.         {
  68.             return $thought;
  69.         }
  70.        
  71.         public function think($text)
  72.         {
  73.             $thought = new ChatterBotThought();
  74.             $thought->setText($text);
  75.             return $this->thinkThought($thought)->getText();
  76.         }
  77.     }
  78.    
  79.     class ChatterBotThought
  80.     {
  81.         private $text;
  82.        
  83.         public function getText()
  84.         {
  85.             return $this->text;
  86.         }
  87.        
  88.         public function setText($text)
  89.         {
  90.             $this->text = $text;
  91.         }
  92.     }
  93.  
  94.     #################################################
  95.    # Cleverbot impl
  96.    #################################################
  97.    
  98.     class _Cleverbot extends ChatterBot
  99.     {
  100.         private $url;
  101.         private $endIndex;
  102.        
  103.         public function __construct($url, $endIndex)
  104.         {
  105.             $this->url = $url;
  106.             $this->endIndex = $endIndex;
  107.         }
  108.        
  109.         public function getUrl()
  110.         {
  111.             return $this->url;
  112.         }
  113.        
  114.         public function setUrl($url)
  115.         {
  116.             $this->url = $url;
  117.         }        
  118.  
  119.         public function getEndIndex()
  120.         {
  121.             return $this->endIndex;
  122.         }
  123.        
  124.         public function setEndIndex($endIndex)
  125.         {
  126.             $this->endIndex = $endIndex;
  127.         }        
  128.  
  129.         public function createSession()
  130.         {
  131.             return new _CleverbotSession($this);
  132.         }
  133.     }
  134.    
  135.     class _CleverbotSession extends ChatterBotSession
  136.     {
  137.         private $bot;
  138.         private $vars;
  139.  
  140.         public function __construct($bot)
  141.         {
  142.             $this->bot = $bot;
  143.             $this->vars = array();
  144.             $this->vars['start'] = 'y';
  145.             $this->vars['icognoid'] = 'wsf';
  146.             $this->vars['fno'] = '0';
  147.             $this->vars['sub'] = 'Say';
  148.             $this->vars['islearning'] = '1';
  149.             $this->vars['cleanslate'] = 'false';
  150.         }
  151.  
  152.         public function thinkThought($thought)
  153.         {
  154.             $this->vars['stimulus'] = $thought->getText();
  155.             $data = http_build_query($this->vars);
  156.             $dataToDigest = substr($data, 9, $this->bot->getEndIndex());
  157.             $dataDigest = md5($dataToDigest);
  158.             $this->vars['icognocheck'] = $dataDigest;
  159.             $response = _utils_post($this->bot->getUrl(), $this->vars);
  160.             $responseValues = explode("\r", $response);
  161.             //self.vars['??'] = _utils_string_at_index($responseValues, 0);
  162.             $this->vars['sessionid'] = _utils_string_at_index($responseValues, 1);
  163.             $this->vars['logurl'] = _utils_string_at_index($responseValues, 2);
  164.             $this->vars['vText8'] = _utils_string_at_index($responseValues, 3);
  165.             $this->vars['vText7'] = _utils_string_at_index($responseValues, 4);
  166.             $this->vars['vText6'] = _utils_string_at_index($responseValues, 5);
  167.             $this->vars['vText5'] = _utils_string_at_index($responseValues, 6);
  168.             $this->vars['vText4'] = _utils_string_at_index($responseValues, 7);
  169.             $this->vars['vText3'] = _utils_string_at_index($responseValues, 8);
  170.             $this->vars['vText2'] = _utils_string_at_index($responseValues, 9);
  171.             $this->vars['prevref'] = _utils_string_at_index($responseValues, 10);
  172.             //$this->vars['??'] = _utils_string_at_index($responseValues, 11);
  173.             $this->vars['emotionalhistory'] = _utils_string_at_index($responseValues, 12);
  174.             $this->vars['ttsLocMP3'] = _utils_string_at_index($responseValues, 13);
  175.             $this->vars['ttsLocTXT'] = _utils_string_at_index($responseValues, 14);
  176.             $this->vars['ttsLocTXT3'] = _utils_string_at_index($responseValues, 15);
  177.             $this->vars['ttsText'] = _utils_string_at_index($responseValues, 16);
  178.             $this->vars['lineRef'] = _utils_string_at_index($responseValues, 17);
  179.             $this->vars['lineURL'] = _utils_string_at_index($responseValues, 18);
  180.             $this->vars['linePOST'] = _utils_string_at_index($responseValues, 19);
  181.             $this->vars['lineChoices'] = _utils_string_at_index($responseValues, 20);
  182.             $this->vars['lineChoicesAbbrev'] = _utils_string_at_index($responseValues, 21);
  183.             $this->vars['typingData'] = _utils_string_at_index($responseValues, 22);
  184.             $this->vars['divert'] = _utils_string_at_index($responseValues, 23);
  185.             $responseThought = new ChatterBotThought();
  186.             $responseThought->setText(_utils_string_at_index($responseValues, 16));
  187.             return $responseThought;
  188.         }
  189.     }
  190.    
  191.     #################################################
  192.    # Pandorabots impl
  193.    #################################################
  194.    
  195.     class _Pandorabots extends ChatterBot
  196.     {
  197.         private $botid;
  198.        
  199.         public function __construct($botid)
  200.         {
  201.             $this->botid = $botid;
  202.         }
  203.        
  204.         public function getBotid()
  205.         {
  206.             return $this->botid;
  207.         }
  208.        
  209.         public function setBotid($botid)
  210.         {
  211.             $this->botid = $botid;
  212.         }        
  213.        
  214.         public function createSession()
  215.         {
  216.             return new _PandorabotsSession($this);
  217.         }
  218.     }
  219.    
  220.     class _PandorabotsSession extends ChatterBotSession
  221.     {
  222.         private $vars;
  223.        
  224.         public function __construct($bot)
  225.         {
  226.             $this->vars = array();
  227.             $this->vars['botid'] = $bot->getBotid();
  228.             $this->vars['custid'] = uniqid();
  229.         }
  230.        
  231.         public function thinkThought($thought)
  232.         {
  233.             $this->vars['input'] = $thought->getText();
  234.             $response = _utils_post('http://www.pandorabots.com/pandora/talk-xml', $this->vars);
  235.             $element = new SimpleXMLElement($response);
  236.             $result = $element->xpath('//result/that/text()');
  237.             $responseThought = new ChatterBotThought();
  238.             $responseThought->setText(trim($result[0][0]));
  239.             return $responseThought;
  240.         }
  241.     }
  242.    
  243.     #################################################
  244.    # Utils
  245.    #################################################
  246.  
  247.     function _utils_post($url, $params)
  248.     {
  249.         $contextParams = array();
  250.         $contextParams['http'] = array();
  251.         $contextParams['http']['method'] = 'POST';
  252.         $contextParams['http']['content'] = http_build_query($params);
  253.         $context = stream_context_create($contextParams);
  254.         $fp = fopen($url, 'rb', false, $context);
  255.         $response = stream_get_contents($fp);
  256.         fclose($fp);
  257.         return $response;
  258.     }
  259.    
  260.     function _utils_string_at_index($strings, $index)
  261.     {
  262.         if (count($strings) > $index)
  263.         {
  264.             return $strings[$index];
  265.         }
  266.         else
  267.         {
  268.             return '';
  269.         }
  270.     }
  271. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement