|
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\Cortex; |
|
12
|
|
|
|
|
13
|
|
|
use Axiom\Rivescript\Rivescript; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Brain class |
|
17
|
|
|
* |
|
18
|
|
|
* The Brain teaches the interpreter how to respond from |
|
19
|
|
|
* the user's input. |
|
20
|
|
|
* |
|
21
|
|
|
* PHP version 7.4 and higher. |
|
22
|
|
|
* |
|
23
|
|
|
* @category Core |
|
24
|
|
|
* @package Cortext |
|
25
|
|
|
* @author Shea Lewis <[email protected]> |
|
26
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
27
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
|
28
|
|
|
* @since 0.3.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
class Brain |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* A collection of topics. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array<Topic> |
|
36
|
|
|
*/ |
|
37
|
|
|
protected array $topics; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Axiom\Rivescript\Rivescript |
|
41
|
|
|
*/ |
|
42
|
|
|
protected Rivescript $master; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new instance of Brain. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->createTopic('random'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set the interpreter for future reference. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Axiom\Rivescript\Rivescript $master |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setMaster(Rivescript $master): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->master = $master; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Teach the Brain with new information. |
|
66
|
|
|
* |
|
67
|
|
|
* @param resource $stream the stream to read from. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function teach($stream): void |
|
72
|
|
|
{ |
|
73
|
|
|
$commands = synapse()->commands; |
|
74
|
|
|
|
|
75
|
|
|
if (is_resource($stream)) { |
|
76
|
|
|
$lineNumber = 0; |
|
77
|
|
|
|
|
78
|
|
|
rewind($stream); |
|
79
|
|
|
|
|
80
|
|
|
while (!feof($stream)) { |
|
81
|
|
|
$line = fgets($stream); |
|
82
|
|
|
$node = new Node($line, $lineNumber++); |
|
83
|
|
|
|
|
84
|
|
|
$error = $node->checkSyntax(); |
|
85
|
|
|
|
|
86
|
|
|
if ($error) { |
|
87
|
|
|
$this->master->say("Error: {$error}"); |
|
88
|
|
|
} |
|
89
|
|
|
if ($node->isInterrupted() === true || $node->isComment() === true || $node->isEmpty()) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$commands->each(function ($command) use ($node) { |
|
94
|
|
|
$class = "\\Axiom\\Rivescript\\Cortex\\Commands\\$command"; |
|
95
|
|
|
$commandClass = new $class(); |
|
96
|
|
|
$commandClass->parse($node); |
|
97
|
|
|
}); |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
|
|
echo "CANNOT TEACH INVALID RESOURCE STREAM\n"; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return a topic. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string|null $name The name of the topic to return. |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed|null |
|
110
|
|
|
*/ |
|
111
|
|
|
public function topic(string $name = null) |
|
112
|
|
|
{ |
|
113
|
|
|
if (is_null($name)) { |
|
114
|
|
|
$name = synapse()->memory->shortTerm()->get('topic') ?: 'random'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (!isset($this->topics[$name])) { |
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->topics[$name]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Create a new Topic. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name The name of the topic to create. |
|
128
|
|
|
* |
|
129
|
|
|
* @return Topic |
|
130
|
|
|
*/ |
|
131
|
|
|
public function createTopic(string $name): Topic |
|
132
|
|
|
{ |
|
133
|
|
|
$this->topics[$name] = new Topic($name); |
|
134
|
|
|
|
|
135
|
|
|
return $this->topics[$name]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|