Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <pre>
  2. <?
  3.  
  4. class Slon {
  5.    
  6.     // regexp patterns
  7.     public $triggers = array (
  8.         "футбол"=>"ногомяч говно!",
  9.         "за500"=>"Ежедневный запиццот рулит :power:",
  10.         "(за550|за600)"=>".i.",
  11.         "(ссср|коммунизм|ленин|сталин|капитализм)"=>"Коммунизм - это хорошо! Ленин и Сталин чёткие мужики! Даёшь старые границы СССР! Капитализм в топку! :power:",
  12.         "религ.*"=>"Религию фтопку!",
  13.         "ВРС"=>"ПФФ! Бабушкины сказки, разжижение мозга!",
  14.         "хобот"=>"У меня самый большой хобот :power:",
  15.         //add custom triggers here...
  16.         //"" => "",
  17.     );          
  18.    
  19.     // returns random opinion
  20.     public function getRandomOpinion()
  21.     {
  22.         $opinions = array(".i.", ":power:", ":mellow:");
  23.        
  24.         return $opinions[rand(0, count($opinions)-1)];
  25.     }
  26.    
  27.     // scan sentence for triggers, stopping on first one
  28.     public function scanSentence($sentence)
  29.     {
  30.         $retVal = "";
  31.        
  32.         foreach ($this->triggers as $pattern=>$response)
  33.         {
  34.             if (preg_match("#$pattern#i", $sentence))
  35.             {
  36.                 $retVal = $response;
  37.                 break;
  38.             }
  39.         }
  40.        
  41.         $retVal = ($retVal) ? $retVal : $this->getRandomOpinion();
  42.         return $retVal;
  43.     }
  44. };
  45.  
  46. // --- ENTRY POINT
  47.  
  48. // all strings are cp1251, so we need to set locale for correct preg_match processing
  49. setlocale(LC_ALL, 'ru_RU.CP1251', 'rus_RUS.CP1251', 'Russian_Russia.1251');
  50.  
  51. // create Slon!
  52. $S = new Slon();
  53.  
  54. // Let's ask him some questions
  55. $sentences = array (
  56.     "Слон, как ты относишься к религии?",
  57.     "Что думаешь по поводу ВРС?",
  58.     "Когда начнёшь делать за550?",
  59.     "Любишь футбол?",
  60.     "Ленин чмо?",
  61.     // you are free to add your own questions
  62.     // "",
  63. );
  64.  
  65. foreach ($sentences as $sentence)
  66. {
  67.     echo ">> $sentence\n";
  68.     echo "<< " . $S->scanSentence($sentence) . "\n\n";
  69. }
  70.  
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement