1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Rivescript-php |
4
|
|
|
* |
5
|
|
|
* (c) Shea Lewis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Axiom\Rivescript; |
12
|
|
|
|
13
|
|
|
use Axiom\Rivescript\Cortex\ContentLoader\ContentLoader; |
14
|
|
|
use Axiom\Rivescript\Cortex\Input; |
15
|
|
|
use Axiom\Rivescript\Cortex\Output; |
16
|
|
|
use Axiom\Rivescript\Traits\Tags; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Rivescript class |
20
|
|
|
* |
21
|
|
|
* The entry point for using the interpreter. |
22
|
|
|
* |
23
|
|
|
* PHP version 7.4 and higher. |
24
|
|
|
* |
25
|
|
|
* @category Core |
26
|
|
|
* @package Cortext |
27
|
|
|
* @author Shea Lewis <[email protected]> |
28
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
29
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
30
|
|
|
* @since 0.3.0 |
31
|
|
|
*/ |
32
|
|
|
class Rivescript extends ContentLoader |
33
|
|
|
{ |
34
|
|
|
use Tags; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A recursion limit before an attempt to |
38
|
|
|
* fetch a reply will be abandoned. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
public int $depth = 50; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Error messages. |
46
|
|
|
* |
47
|
|
|
* @var array|string[] |
48
|
|
|
*/ |
49
|
|
|
public array $errors = [ |
50
|
|
|
"replyNotMatched" => "ERR: No Reply Matched", |
51
|
|
|
"replyNotFound" => "ERR: No Reply Found", |
52
|
|
|
"objectNotFound" => "[ERR: Object Not Found]", |
53
|
|
|
"deepRecursion" => "ERR: Deep Recursion Detected" |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Flag to indicating if utf8 |
58
|
|
|
* modes is enabled. |
59
|
|
|
* |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
protected bool $utf8 = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a new Rivescript instance. |
66
|
|
|
* |
67
|
|
|
* @throws \Axiom\Rivescript\Exceptions\ContentLoadingException |
68
|
|
|
*/ |
69
|
|
|
public function __construct() |
70
|
|
|
{ |
71
|
|
|
parent::__construct(); |
72
|
|
|
|
73
|
|
|
include __DIR__ . '/bootstrap.php'; |
74
|
|
|
|
75
|
|
|
synapse()->brain->setMaster($this); |
76
|
|
|
|
77
|
|
|
$this->registerTags(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize the tags |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
private function registerTags(): void |
86
|
|
|
{ |
87
|
|
|
synapse()->tags->each( |
88
|
|
|
function ($tag) { |
89
|
|
|
$class = "\\Axiom\\Rivescript\\Cortex\\Tags\\$tag"; |
90
|
|
|
$tagInstance = new $class(); |
91
|
|
|
|
92
|
|
|
$tagInfo = $tagInstance->getTagName(); |
93
|
|
|
if (is_array($tagInfo)) { |
94
|
|
|
foreach ($tagInfo as $tagName) { |
95
|
|
|
synapse()->memory->tags()->put($tagName, $tagInstance); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
synapse()->memory->tags()->put($tagInfo, $tagInstance); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Load Rivescript interpretable content. |
106
|
|
|
* Into the Interpreter. |
107
|
|
|
* |
108
|
|
|
* Please note: This supports |
109
|
|
|
* |
110
|
|
|
* - Directory path to Rivescript interpretable files. |
111
|
|
|
* - Array of absolute paths to Rivescript interpretable files |
112
|
|
|
* - Absolute string containing path to Rivescript interpretable file. |
113
|
|
|
* - A stream of text with Rivescript interpretable script. |
114
|
|
|
* |
115
|
|
|
* Please note 2: |
116
|
|
|
* |
117
|
|
|
* If you profile a directory with rivescript documents make sure they are |
118
|
|
|
* all interpretable rivescript will throw syntax errors while trying to |
119
|
|
|
* parse those files. |
120
|
|
|
* |
121
|
|
|
* @param array<string>|string $info The files to read |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function load($info): void |
126
|
|
|
{ |
127
|
|
|
parent::load($info); |
128
|
|
|
$this->processInformation(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Stream new information into the brain. |
133
|
|
|
* |
134
|
|
|
* @param string $string The string of information to feed the brain. |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function stream(string $string): void |
139
|
|
|
{ |
140
|
|
|
fseek($this->getStream(), 0, SEEK_SET); |
141
|
|
|
rewind($this->getStream()); |
142
|
|
|
|
143
|
|
|
$this->writeToMemory($string); |
144
|
|
|
$this->processInformation(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Process new information in the |
149
|
|
|
* stream. |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
private function processInformation(): void |
154
|
|
|
{ |
155
|
|
|
synapse()->memory->local()->put('concat', 'none'); |
156
|
|
|
synapse()->brain->teach($this->getStream()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set user variables. |
161
|
|
|
* |
162
|
|
|
* @param string $user The user for this variable. |
163
|
|
|
* @param string $name The name of the variable. |
164
|
|
|
* @param string $value The value of the variable. |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function setUservar(string $user, string $name, string $value): void |
169
|
|
|
{ |
170
|
|
|
synapse()->memory->user($user)->put($name, $value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get user variable. |
175
|
|
|
* |
176
|
|
|
* @param string $user The user for this variable. |
177
|
|
|
* @param string $name The name of the variable. |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public function getUservar(string $user, string $name) |
182
|
|
|
{ |
183
|
|
|
return synapse()->memory->user($user)->get($name); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $string |
188
|
|
|
* @param bool $utf8 |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
private function stripNasties(string $string, bool $utf8): string |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
return preg_replace("/[^A-Za-z0-9 ]/m", "", $string); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Make the client respond to a message. |
199
|
|
|
* |
200
|
|
|
* @param string $msg The message the client has to process and respond to. |
201
|
|
|
* @param string $user The user id. |
202
|
|
|
* @param string|null $scope Not used at this point. |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function reply(string $msg, string $user = 'local-user', string $scope = null): string |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
|
209
|
|
|
// FIXME: Must be $user, $message, Sscope |
210
|
|
|
$msg = $this->stripNasties($msg, ""); |
|
|
|
|
211
|
|
|
synapse()->brain->say("Asked to reply to [{$user}] {$msg}"); |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
$input = new Input($msg, $user); |
215
|
|
|
$output = new Output($input); |
216
|
|
|
|
217
|
|
|
synapse()->input = $input; |
218
|
|
|
|
219
|
|
|
$output = $output->process(); |
220
|
|
|
// $output = synapse()->brain->getReplies($user, $message, "normal", 0, $scope); |
221
|
|
|
|
222
|
|
|
synapse()->memory->inputs()->push($msg); |
223
|
|
|
synapse()->memory->replies()->push($output); |
224
|
|
|
|
225
|
|
|
return $output; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|