Passed
Push — master ( 7b5315...2b04ef )
by Ehsan
02:46
created

CommandExtractor::countKeywordOccurrence()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0164

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 21
cts 23
cp 0.913
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 5
nop 1
crap 5.0164
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
        $stemmedTokenizedMessage = $stemmer->stemAll($tokenizedMessage);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $stemmedTokenizedMessage exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
52 1
        $stemmedMessage = implode(' ', $stemmedTokenizedMessage);
53
54 1
        $keywordCount = [];
55 1
        foreach ($commands as $commandKey => $commandObject) {
56 1
            $keywords = $commandObject->getKeywords();
57
58 1
            if (empty($keywords)) {
59 1
                continue;
60
            }
61
62 1
            $stemmedKeywords = $stemmer->stemAll($keywords);
63
64 1
            $keywordsPositions = $this->getMessageUtility()->keywordPos($stemmedKeywords, $stemmedMessage);
65
66 1
            $total = 0;
67 1
            if (empty($keywordsPositions)) {
68
                $keywordCount[$commandKey] = $total;
69
                continue;
70
            }
71
72 1
            foreach ($keywordsPositions as $keywordPositions) {
73 1
                $total += count($keywordPositions);
74
            }
75
76 1
            $keywordCount[$commandKey] = $total;
77
        }
78
79 1
        return $keywordCount;
80
    }
81
82
    /**
83
     * @param $message
84
     *
85
     * @return Command|void
86
     */
87 6
    private function getCommandObjectByMessage($message)
88
    {
89 6
        $command = $this->getMessageUtility()->extractCommandName($message);
90
91
        // check command name
92 6
        if (empty($command)) {
93
            // get the default command if no command is find in the message
94 2
            $command = $this->getConfig()->get('defaultCommand');
95
96 2
            if (empty($command)) {
97 2
                $this->setError($this->getDictionary()->get('generic-messages')['noCommandMessage']);
98
99 2
                return;
100
            }
101
        }
102
103 4
        return $this->getCommandObjectByCommand($command);
104
    }
105
106
    /**
107
     * @param $command
108
     *
109
     * @return Command|void
110
     */
111 4
    private function getCommandObjectByCommand($command)
112
    {
113 4
        $commandObject = $this->getCommandContainer()->getAsObject($command);
114
115 4
        if ($this->validateCommandObject($commandObject) !== true) {
116 1
            return;
117
        }
118
119 4
        return $commandObject;
120
    }
121
122
    /**
123
     * Validate the command object.
124
     *
125
     * @param Command|null $commandObject
126
     *
127
     * @return bool
128
     */
129 4
    private function validateCommandObject($commandObject)
130
    {
131
        // check command details
132 4
        if (empty($commandObject)) {
133 1
            $this->setError(
134 1
                $this->getDictionary()->getValueByKey('generic-messages', 'unknownCommandMessage')
135
            );
136
137 1
            return false;
138
        }
139
140 4
        return true;
141
    }
142
143
    /**
144
     * @return string
145
     */
146 4
    public function getError()
147
    {
148 4
        return $this->error;
149
    }
150
151
    /**
152
     * @param string $error
153
     */
154 4
    public function setError($error)
155
    {
156 4
        $this->error = $error;
157 4
    }
158
159
    /**
160
     * @return Config
161
     */
162 3
    public function getConfig()
163
    {
164 3
        if ($this->config === null) {
165 2
            $this->config = (new Config());
166
        }
167
168 3
        return $this->config;
169
    }
170
171
    /**
172
     * @param Config $config
173
     */
174 1
    public function setConfig(Config $config)
175
    {
176 1
        $this->config = $config;
177 1
    }
178
179
    /**
180
     * @return MessageUtility
181
     */
182 7
    public function getMessageUtility()
183
    {
184 7
        if (!isset($this->messageUtility)) {
185 7
            $this->setMessageUtility(new MessageUtility());
186
        }
187
188 7
        return $this->messageUtility;
189
    }
190
191
    /**
192
     * @param MessageUtility $messageUtility
193
     */
194 7
    public function setMessageUtility(MessageUtility $messageUtility)
195
    {
196 7
        $this->messageUtility = $messageUtility;
197 7
    }
198
199
    /**
200
     * @return Dictionary
201
     */
202 3
    public function getDictionary()
203
    {
204 3
        if (!isset($this->dictionary)) {
205 3
            $this->setDictionary(new Dictionary());
206
        }
207
208 3
        return $this->dictionary;
209
    }
210
211
    /**
212
     * @param Dictionary $dictionary
213
     */
214 3
    public function setDictionary(Dictionary $dictionary)
215
    {
216 3
        $this->dictionary = $dictionary;
217 3
    }
218
219
    /**
220
     * @return CommandContainer
221
     */
222 5
    public function getCommandContainer()
223
    {
224 5
        if (!isset($this->commandContainer)) {
225 4
            $this->setCommandContainer(new CommandContainer());
226
        }
227
228 5
        return $this->commandContainer;
229
    }
230
231
    /**
232
     * @param CommandContainer $commandContainer
233
     */
234 5
    public function setCommandContainer(CommandContainer $commandContainer)
235
    {
236 5
        $this->commandContainer = $commandContainer;
237 5
    }
238
}
239