1
|
|
|
<?php |
2
|
|
|
namespace Posibrain; |
3
|
|
|
|
4
|
|
|
use Monolog\Logger; |
5
|
|
|
include_once (__DIR__ . '/../tools.php'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @author Fylhan (http://fylhan.la-bnbox.fr) |
10
|
|
|
* @license LGPL-2.1+ |
11
|
|
|
*/ |
12
|
|
|
class TchatBot implements ITchatBot |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
private static $logger = NULL; |
16
|
|
|
|
17
|
|
|
private $identity; |
18
|
|
|
|
19
|
|
|
private $positrons; |
20
|
|
|
|
21
|
|
|
public function __construct($id = '', $lang = '', $params = array()) |
22
|
|
|
{ |
23
|
|
|
// Logger |
24
|
|
View Code Duplication |
if (NULL == self::$logger) { |
|
|
|
|
25
|
|
|
self::$logger = new Logger(__CLASS__); |
26
|
|
|
if (! empty($params) && isset($params['loggerHandler'])) { |
27
|
|
|
self::$logger->pushHandler($params['loggerHandler']); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Config |
32
|
|
|
$this->identity = new TchatBotIdentity($id, $lang, $params); |
33
|
|
|
|
34
|
|
|
// Brain Manager |
35
|
|
|
$this->positrons = new Positroner($this->identity, $params); |
|
|
|
|
36
|
|
|
$this->positrons->loadPositrons(@$params['positrons'], $this->identity, $params); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function isTriggered($userMessage, $userName = '', $dateTime = 0) |
40
|
|
|
{ |
41
|
|
|
$request = new TchatMessage($userMessage, $userName, $dateTime); |
42
|
|
|
return $this->positrons->isBotTriggered($request); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function generateAnswer($userMessage, $userName = '', $dateTime = 0) |
46
|
|
|
{ |
47
|
|
|
$request = new TchatMessage($userMessage, $userName, $dateTime); |
48
|
|
|
if (!$this->positrons->isBotTriggered($request)) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
$request = $this->positrons->analyseRequest($request); |
52
|
|
|
$memory = $this->positrons->loadMemory($request); |
53
|
|
|
$answer = $this->positrons->generateSymbolicAnswer($request, $memory); |
54
|
|
|
$answer = $this->positrons->provideMeaning($request, $memory, $answer); |
55
|
|
|
$answer = $this->positrons->beautifyAnswer($request, $memory, $answer); |
56
|
|
|
if (null == $answer || ('' == $answer->getMessage() && '' == $answer->getName())) { |
57
|
|
|
$answer = new TchatMessage('Ssqdijoezf ? Jkfd.', 'QTzbn'); |
58
|
|
|
} |
59
|
|
|
return $answer->toArray(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getIdentity() |
63
|
|
|
{ |
64
|
|
|
return $this->identity; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setConfig($config) |
68
|
|
|
{ |
69
|
|
|
$this->identity = $config; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setBrain($brain) |
73
|
|
|
{ |
74
|
|
|
$this->positrons = $brain; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.