Skip to content

Commit

Permalink
Find command by keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jul 18, 2017
1 parent 5c3a063 commit ea4baa6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Botonomous/CommandExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Botonomous;

use Botonomous\utility\ArrayUtility;
use Botonomous\utility\MessageUtility;
use NlpTools\Stemmers\PorterStemmer;
use NlpTools\Tokenizers\WhitespaceTokenizer;
Expand Down Expand Up @@ -35,6 +36,10 @@ public function getCommandByMessage($message)
*/
$foundCommand = $this->getCommandObjectByMessage($message);

if (empty($foundCommand)) {
(new ArrayUtility())->maxPositiveValueKey($this->countKeywordOccurrence($message));
}

return $foundCommand;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Botonomous/utility/ArrayUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,21 @@ public function sortArrayByLength($array)

return $array;
}

/**
* Find the key for the max positive value
*
* @param $array
*
* @return mixed
*/
public function maxPositiveValueKey($array)
{
$maxValue = max($array);

if ($maxValue > 0) {
return array_search($maxValue, $array);
}
}

}
35 changes: 35 additions & 0 deletions tests/Botonomous/utility/ArrayUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,39 @@ public function testGetArrayPath()
$this->assertEquals($inputOutput['expected'], $result);
}
}

public function testMaxPositiveValueKey()
{
$utility = new ArrayUtility();

$inputsOutputs = [
[
'input' => [
'test' => 1,
'test2' => 2,
],
'output' => 'test2'
],
[
'input' => [
'test' => 1,
'test2' => 0,
],
'output' => 'test'
],
[
'input' => [
'test' => 0,
'test2' => -2,
],
'output' => null
],
];

foreach ($inputsOutputs as $inputOutput) {
$result = $utility->maxPositiveValueKey($inputOutput['input']);

$this->assertEquals($inputOutput['output'], $result);
}
}
}

0 comments on commit ea4baa6

Please sign in to comment.