Passed
Push — master ( 67508d...a08ac8 )
by Ehsan
03:55 queued 01:10
created

CommandExtractor::getCommandContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\utility\MessageUtility;
6
use NlpTools\Stemmers\PorterStemmer;
7
use NlpTools\Tokenizers\WhitespaceTokenizer;
8
9
/**
10
 * Class CommandExtractor.
11
 */
12
class CommandExtractor
13
{
14
    private $config;
15
    private $error;
16
    private $messageUtility;
17
    private $dictionary;
18
    private $commandContainer;
19
20
    /**
21
     * @param null $message
22
     *
23
     * @return Command|void
24
     */
25 7
    public function getCommandByMessage($message)
26
    {
27 7
        if (empty($message)) {
28 1
            $this->setError('Message is empty');
29
30 1
            return;
31
        }
32
33
        /*
34
         * Process the message and find explicitly specified command
35
         */
36 6
        $foundCommand = $this->getCommandObjectByMessage($message);
37
38 6
        return $foundCommand;
39
    }
40
41 1
    public function countKeywordOccurrence($message)
42
    {
43 1
        $commandContainer = $this->getCommandContainer();
44 1
        $commands = $commandContainer->getAllAsObject();
45
46 1
        $stemmer = new PorterStemmer();
47
48
        // tokenize $message
49 1
        $tokenizer = new WhitespaceTokenizer();
50 1
        $tokenizedMessage = $tokenizer->tokenize($message);
51 1
        $stemmedMessage = implode(' ', $stemmer->stemAll($tokenizedMessage));
52
53 1
        $keywordCount = [];
54 1
        foreach ($commands as $commandKey => $commandObject) {
55 1
            $keywords = $commandObject->getKeywords();
56
57 1
            if (empty($keywords)) {
58 1
                continue;
59
            }
60
61 1
            $stemmedKeywords = $stemmer->stemAll($keywords);
62
63 1
            $keywordsPositions = $this->getMessageUtility()->keywordPos($stemmedKeywords, $stemmedMessage);
64
65 1
            $total = 0;
66 1
            if (empty($keywordsPositions)) {
67
                $keywordCount[$commandKey] = $total;
68
                continue;
69
            }
70
71 1
            foreach ($keywordsPositions as $keywordPositions) {
72 1
                $total += count($keywordPositions);
73
            }
74
75 1
            $keywordCount[$commandKey] = $total;
76
        }
77
78 1
        return $keywordCount;
79
    }
80
81
    /**
82
     * @param $message
83
     *
84
     * @return Command|void
85
     */
86 6
    private function getCommandObjectByMessage($message)
87
    {
88 6
        $command = $this->getMessageUtility()->extractCommandName($message);
89
90
        // check command name
91 6
        if (empty($command)) {
92
            // get the default command if no command is find in the message
93 2
            $command = $this->getConfig()->get('defaultCommand');
94
95 2
            if (empty($command)) {
96 2
                $this->setError($this->getDictionary()->get('generic-messages')['noCommandMessage']);
97
98 2
                return;
99
            }
100
        }
101
102 4
        return $this->getCommandObjectByCommand($command);
103
    }
104
105
    /**
106
     * @param $command
107
     *
108
     * @return Command|void
109
     */
110 4
    private function getCommandObjectByCommand($command)
111
    {
112 4
        $commandObject = $this->getCommandContainer()->getAsObject($command);
113
114 4
        if ($this->validateCommandObject($commandObject) !== true) {
115 1
            return;
116
        }
117
118 4
        return $commandObject;
119
    }
120
121
    /**
122
     * Validate the command object.
123
     *
124
     * @param Command|null $commandObject
125
     *
126
     * @return bool
127
     */
128 4
    private function validateCommandObject($commandObject)
129
    {
130
        // check command details
131 4
        if (empty($commandObject)) {
132 1
            $this->setError(
133 1
                $this->getDictionary()->getValueByKey('generic-messages', 'unknownCommandMessage')
134
            );
135
136 1
            return false;
137
        }
138
139 4
        return true;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 4
    public function getError()
146
    {
147 4
        return $this->error;
148
    }
149
150
    /**
151
     * @param string $error
152
     */
153 4
    public function setError($error)
154
    {
155 4
        $this->error = $error;
156 4
    }
157
158
    /**
159
     * @return Config
160
     */
161 3
    public function getConfig()
162
    {
163 3
        if ($this->config === null) {
164 2
            $this->config = (new Config());
165
        }
166
167 3
        return $this->config;
168
    }
169
170
    /**
171
     * @param Config $config
172
     */
173 1
    public function setConfig(Config $config)
174
    {
175 1
        $this->config = $config;
176 1
    }
177
178
    /**
179
     * @return MessageUtility
180
     */
181 7
    public function getMessageUtility()
182
    {
183 7
        if (!isset($this->messageUtility)) {
184 7
            $this->setMessageUtility(new MessageUtility());
185
        }
186
187 7
        return $this->messageUtility;
188
    }
189
190
    /**
191
     * @param MessageUtility $messageUtility
192
     */
193 7
    public function setMessageUtility(MessageUtility $messageUtility)
194
    {
195 7
        $this->messageUtility = $messageUtility;
196 7
    }
197
198
    /**
199
     * @return Dictionary
200
     */
201 3
    public function getDictionary()
202
    {
203 3
        if (!isset($this->dictionary)) {
204 3
            $this->setDictionary(new Dictionary());
205
        }
206
207 3
        return $this->dictionary;
208
    }
209
210
    /**
211
     * @param Dictionary $dictionary
212
     */
213 3
    public function setDictionary(Dictionary $dictionary)
214
    {
215 3
        $this->dictionary = $dictionary;
216 3
    }
217
218
    /**
219
     * @return CommandContainer
220
     */
221 5
    public function getCommandContainer()
222
    {
223 5
        if (!isset($this->commandContainer)) {
224 4
            $this->setCommandContainer(new CommandContainer());
225
        }
226
227 5
        return $this->commandContainer;
228
    }
229
230
    /**
231
     * @param CommandContainer $commandContainer
232
     */
233 5
    public function setCommandContainer(CommandContainer $commandContainer)
234
    {
235 5
        $this->commandContainer = $commandContainer;
236 5
    }
237
}
238