1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Bootstrap the Rivescript client. |
5
|
|
|
* |
6
|
|
|
* @package Rivescript-php |
7
|
|
|
* @subpackage Core |
8
|
|
|
* @category Client |
9
|
|
|
* @author Shea Lewis <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Axiom\Rivescript; |
13
|
|
|
|
14
|
|
|
use Axiom\Rivescript\Cortex\ContentLoader\ContentLoader; |
15
|
|
|
use Axiom\Rivescript\Cortex\Input; |
16
|
|
|
use Axiom\Rivescript\Cortex\Output; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The main Rivescript client. |
20
|
|
|
*/ |
21
|
|
|
class Rivescript extends ContentLoader |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Create a new Rivescript instance. |
25
|
|
|
* |
26
|
|
|
* @throws \Axiom\Rivescript\Exceptions\ContentLoadingException |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
|
32
|
|
|
include __DIR__ . '/bootstrap.php'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load Rivescript interpretable content. |
37
|
|
|
* Into the Interpreter. |
38
|
|
|
* |
39
|
|
|
* Please note: This supports |
40
|
|
|
* |
41
|
|
|
* - Directory path to Rivescript interpretable files. |
42
|
|
|
* - Array of absolute paths to Rivescript interpretable files |
43
|
|
|
* - Absolute string containing path to Rivescript interpretable file. |
44
|
|
|
* - A stream of text with Rivescript interpretable script. |
45
|
|
|
* |
46
|
|
|
* Please note 2: |
47
|
|
|
* |
48
|
|
|
* If you profile a directory with rivescript documents make sure they are |
49
|
|
|
* all interpretable rivescript will throw syntax errors while trying to |
50
|
|
|
* parse those files. |
51
|
|
|
* |
52
|
|
|
* @param array<string>|string $info The files to read |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function load($info) |
57
|
|
|
{ |
58
|
|
|
parent::load($info); |
59
|
|
|
$this->processInformation(); |
60
|
|
|
//synapse()->brain->teach($file); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Stream new information into the brain. |
65
|
|
|
* |
66
|
|
|
* @param string $string The string of information to feed the brain. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function stream(string $string) |
71
|
|
|
{ |
72
|
|
|
$this->writeToMemory($string); |
73
|
|
|
$this->processInformation(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Process new information in the |
78
|
|
|
* stream. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
private function processInformation() |
83
|
|
|
{ |
84
|
|
|
synapse()->brain->teach($this->getStream()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Make the client respond to a message. |
89
|
|
|
* |
90
|
|
|
* @param string $message The message the client has to process and respond to. |
91
|
|
|
* @param string $user The user id. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function reply(string $message, string $user = 'local-user'): string |
96
|
|
|
{ |
97
|
|
|
$input = new Input($message, $user); |
98
|
|
|
$output = new Output($input); |
99
|
|
|
|
100
|
|
|
synapse()->input = $input; |
101
|
|
|
|
102
|
|
|
$output = $output->process(); |
103
|
|
|
|
104
|
|
|
synapse()->memory->inputs()->push($message); |
105
|
|
|
synapse()->memory->replies()->push($output); |
106
|
|
|
|
107
|
|
|
return $output; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|