1 | <?php |
||
13 | class CommandExtractor |
||
14 | { |
||
15 | private $config; |
||
16 | private $error; |
||
17 | private $messageUtility; |
||
18 | private $dictionary; |
||
19 | private $commandContainer; |
||
20 | |||
21 | /** |
||
22 | * @param null $message |
||
23 | * |
||
24 | * @return Command|void |
||
25 | */ |
||
26 | 7 | public function getCommandByMessage($message) |
|
39 | |||
40 | /** |
||
41 | * @param $message |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 2 | public function countKeywordOccurrence($message) |
|
46 | { |
||
47 | 2 | $stemmer = new PorterStemmer(); |
|
48 | |||
49 | // tokenize $message |
||
50 | 2 | $stemmed = implode(' ', $stemmer->stemAll((new WhitespaceAndPunctuationTokenizer())->tokenize($message))); |
|
51 | |||
52 | 2 | $count = []; |
|
53 | 2 | foreach ($this->getCommandContainer()->getAllAsObject() as $commandKey => $commandObject) { |
|
54 | 2 | $keywordsCount = $this->commandKeywordOccurrence($commandObject, $stemmed); |
|
55 | |||
56 | 2 | $total = 0; |
|
57 | 2 | if (empty($keywordsCount)) { |
|
58 | 2 | $count[$commandKey] = $total; |
|
59 | 2 | continue; |
|
60 | } |
||
61 | |||
62 | $count[$commandKey] = array_sum($keywordsCount); |
||
63 | } |
||
64 | |||
65 | 2 | return $count; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param Command $command |
||
70 | * @param string $message |
||
71 | * |
||
72 | * @return array|void |
||
73 | */ |
||
74 | 2 | private function commandKeywordOccurrence(Command $command, $message) |
|
87 | |||
88 | /** |
||
89 | * @param $message |
||
90 | * |
||
91 | * @return Command|null |
||
92 | */ |
||
93 | 6 | private function getCommandObjectByMessage($message) |
|
94 | { |
||
95 | 6 | $command = $this->getMessageUtility()->extractCommandName($message); |
|
96 | 6 | if (empty($command)) { |
|
97 | 2 | $command = (new ArrayUtility())->maxPositiveValueKey($this->countKeywordOccurrence($message)); |
|
98 | } |
||
99 | |||
100 | // check command name |
||
101 | 6 | if (empty($command)) { |
|
102 | // get the default command if no command is find in the message |
||
103 | 2 | $command = $this->checkDefaultCommand(); |
|
104 | } |
||
105 | |||
106 | 6 | return $this->getCommandObjectByCommand($command); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return mixed|void |
||
111 | */ |
||
112 | 2 | private function checkDefaultCommand() |
|
113 | { |
||
114 | 2 | $command = $this->getConfig()->get('defaultCommand'); |
|
115 | |||
116 | 2 | if (empty($command)) { |
|
117 | 2 | $this->setError($this->getDictionary()->get('generic-messages')['noCommandMessage']); |
|
118 | |||
119 | 2 | return; |
|
120 | } |
||
121 | |||
122 | return $command; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param $command |
||
127 | * |
||
128 | * @return Command|void |
||
129 | */ |
||
130 | 6 | private function getCommandObjectByCommand($command) |
|
144 | |||
145 | /** |
||
146 | * Validate the command object. |
||
147 | * |
||
148 | * @param Command|null $commandObject |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 4 | private function validateCommandObject($commandObject) |
|
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | 4 | public function getError() |
|
173 | |||
174 | /** |
||
175 | * @param string $error |
||
176 | */ |
||
177 | 4 | public function setError($error) |
|
181 | |||
182 | /** |
||
183 | * @return Config |
||
184 | */ |
||
185 | 3 | public function getConfig() |
|
193 | |||
194 | /** |
||
195 | * @param Config $config |
||
196 | */ |
||
197 | 1 | public function setConfig(Config $config) |
|
201 | |||
202 | /** |
||
203 | * @return MessageUtility |
||
204 | */ |
||
205 | 6 | public function getMessageUtility() |
|
213 | |||
214 | /** |
||
215 | * @param MessageUtility $messageUtility |
||
216 | */ |
||
217 | 6 | public function setMessageUtility(MessageUtility $messageUtility) |
|
221 | |||
222 | /** |
||
223 | * @return Dictionary |
||
224 | */ |
||
225 | 3 | public function getDictionary() |
|
233 | |||
234 | /** |
||
235 | * @param Dictionary $dictionary |
||
236 | */ |
||
237 | 3 | public function setDictionary(Dictionary $dictionary) |
|
241 | |||
242 | /** |
||
243 | * @return CommandContainer |
||
244 | */ |
||
245 | 6 | public function getCommandContainer() |
|
253 | |||
254 | /** |
||
255 | * @param CommandContainer $commandContainer |
||
256 | */ |
||
257 | 6 | public function setCommandContainer(CommandContainer $commandContainer) |
|
261 | } |
||
262 |