Skip to content

Commit

Permalink
Add keywordCount function
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jul 17, 2017
1 parent 3af3212 commit 67fa719
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Botonomous/CommandContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CommandContainer extends AbstractCommandContainer
'description' => 'List all the available commands',
'keywords' => [
'help',
'ask'
],
],
'qa' => [
Expand Down
20 changes: 9 additions & 11 deletions src/Botonomous/CommandExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,28 @@ public function countKeywordOccurrence($message)
// tokenize $message
$stemmedMessage = implode(' ', $stemmer->stemAll((new WhitespaceTokenizer())->tokenize($message)));

$keywordCount = [];
$count = [];
foreach ($this->getCommandContainer()->getAllAsObject() as $commandKey => $commandObject) {
$keywords = $commandObject->getKeywords();

if (empty($keywords)) {
continue;
}

$keywordsPositions = $this->getMessageUtility()->keywordPos($stemmer->stemAll($keywords), $stemmedMessage);
$keywordsCount = $this->getMessageUtility()->keywordCount(
$stemmer->stemAll($keywords),
$stemmedMessage
);

$total = 0;
if (empty($keywordsPositions)) {
$keywordCount[$commandKey] = $total;
if (empty($keywordsCount)) {
$count[$commandKey] = $total;
continue;
}

foreach ($keywordsPositions as $keywordPositions) {
$total += count($keywordPositions);
}

$keywordCount[$commandKey] = $total;
$count[$commandKey] = array_sum($keywordsCount);
}

return $keywordCount;
return $count;
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Botonomous/utility/MessageUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ private function getUserLink()
public function keywordPos(array $keywords, $message)
{
$found = [];

if (empty($keywords)) {
return $found;
}
Expand All @@ -159,6 +158,21 @@ public function keywordPos(array $keywords, $message)
return $found;
}

public function keywordCount(array $keywords, $message)
{
$keysPositions = $this->keywordPos($keywords, $message);

if (empty($keysPositions)) {
return;
}

foreach ($keysPositions as $key => $positions) {
$keysPositions[$key] = count($positions);
}

return $keysPositions;
}

/**
* @param array $tokensPositions
* @param $newPosition
Expand Down
38 changes: 38 additions & 0 deletions tests/Botonomous/utility/MessageUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,42 @@ public function testKeywordPos()

$this->assertEquals($expected, $result);
}

public function testKeywordCount()
{
$utility = new MessageUtility();

$result = $utility->keywordCount([
'two words',
'word',
'two',
'words',
' plus',
], 'This is a two words plus one word and two words');

$expected = [
'two words' => 2,
'word' => 1,
' plus' => 1,
];

$this->assertEquals($expected, $result);

$result = $utility->keywordCount([
"What's",
'the',
'the weather',
' like ',
'tomorrow',
], "What's the weather like tomorrow?");

$expected = [
"What's" => 1,
'the weather' => 1,
'tomorrow' => 1,
' like ' => 1,
];

$this->assertEquals($expected, $result);
}
}

0 comments on commit 67fa719

Please sign in to comment.