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\Collections\Collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Topic class |
17
|
|
|
* |
18
|
|
|
* Stores information about a topic. |
19
|
|
|
* |
20
|
|
|
* PHP version 7.4 and higher. |
21
|
|
|
* |
22
|
|
|
* @category Core |
23
|
|
|
* @package Cortext |
24
|
|
|
* @author Shea Lewis <[email protected]> |
25
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
26
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
27
|
|
|
* @since 0.3.0 |
28
|
|
|
*/ |
29
|
|
|
class Topic |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The name of this Topic. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected string $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The triggers for this Topic. |
40
|
|
|
* |
41
|
|
|
* @var Collection<string, mixed> |
42
|
|
|
*/ |
43
|
|
|
public Collection $triggers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The responses for this Topic. |
47
|
|
|
* |
48
|
|
|
* @var Collection<string, mixed> |
49
|
|
|
*/ |
50
|
|
|
public Collection $responses; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a new Topic instance. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
*/ |
57
|
|
|
public function __construct(string $name) |
58
|
|
|
{ |
59
|
|
|
$this->name = $name; |
60
|
|
|
$this->triggers = new Collection([]); |
61
|
|
|
$this->responses = new Collection([]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return triggers associated with this branch. |
66
|
|
|
* |
67
|
|
|
* @return Collection<string, mixed> |
68
|
|
|
*/ |
69
|
|
|
public function triggers(): Collection |
70
|
|
|
{ |
71
|
|
|
return $this->triggers; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the responses associated with this branch. |
76
|
|
|
* |
77
|
|
|
* @return Collection<string, mixed> |
78
|
|
|
*/ |
79
|
|
|
public function responses(): Collection |
80
|
|
|
{ |
81
|
|
|
return $this->responses; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sort triggers based on type and word count from |
86
|
|
|
* largest to smallest. |
87
|
|
|
* |
88
|
|
|
* @param \Axiom\Collections\Collection $triggers |
89
|
|
|
* |
90
|
|
|
* @return \Axiom\Collections\Collection |
91
|
|
|
*/ |
92
|
|
|
public function sortTriggers(Collection $triggers): Collection |
93
|
|
|
{ |
94
|
|
|
$triggers = $this->determineWordCount($triggers); |
95
|
|
|
$triggers = $this->determineTypeCount($triggers); |
96
|
|
|
|
97
|
|
|
return $triggers->sort(function ($current, $previous) { |
98
|
|
|
return ($current['order'] < $previous['order']) ? -1 : 1; |
99
|
|
|
})->reverse(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Determine the order in the triggers. |
104
|
|
|
* |
105
|
|
|
* @param Collection<array> $triggers A collection of triggers. |
106
|
|
|
* |
107
|
|
|
* @return Collection<array> |
108
|
|
|
*/ |
109
|
|
|
protected function determineTypeCount(Collection $triggers): Collection |
110
|
|
|
{ |
111
|
|
|
return $triggers->each(function ($data, $trigger) use ($triggers) { |
112
|
|
|
if (isset($data['type'])) { |
113
|
|
|
switch ($data['type']) { |
114
|
|
|
case 'atomic': |
115
|
|
|
$data['order'] += 4000000; |
116
|
|
|
break; |
117
|
|
|
case 'alphabetic': |
118
|
|
|
$data['order'] += 3000000; |
119
|
|
|
break; |
120
|
|
|
case 'numeric': |
121
|
|
|
$data['order'] += 2000000; |
122
|
|
|
break; |
123
|
|
|
case 'global': |
124
|
|
|
$data['order'] += 1000000; |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$triggers->put($trigger, $data); |
129
|
|
|
} |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sort triggers based on word count from |
135
|
|
|
* largest to smallest. |
136
|
|
|
* |
137
|
|
|
* @param Collection<array> $triggers A collection of triggers. |
138
|
|
|
* |
139
|
|
|
* @return Collection<array> |
140
|
|
|
*/ |
141
|
|
|
protected function determineWordCount(Collection $triggers): Collection |
142
|
|
|
{ |
143
|
|
|
return $triggers->each(function ($data, $trigger) use ($triggers) { |
144
|
|
|
$data['order'] = count(explode(' ', $trigger)); |
145
|
|
|
|
146
|
|
|
$triggers->put($trigger, $data); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|