Skip to content

Commit

Permalink
General enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jul 17, 2017
1 parent 531e59d commit fe10539
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
13 changes: 3 additions & 10 deletions src/Botonomous/CommandExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,20 @@ public function getCommandByMessage($message)

public function countKeywordOccurrence($message)
{
$commandContainer = $this->getCommandContainer();
$commands = $commandContainer->getAllAsObject();

$stemmer = new PorterStemmer();

// tokenize $message
$tokenizer = new WhitespaceTokenizer();
$tokenizedMessage = $tokenizer->tokenize($message);
$stemmedMessage = implode(' ', $stemmer->stemAll($tokenizedMessage));
$stemmedMessage = implode(' ', $stemmer->stemAll((new WhitespaceTokenizer())->tokenize($message)));

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

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

$stemmedKeywords = $stemmer->stemAll($keywords);

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

$total = 0;
if (empty($keywordsPositions)) {
Expand Down
25 changes: 20 additions & 5 deletions src/Botonomous/utility/MessageUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ private function getUserLink()
return $this->linkToUser($this->getConfig()->get('botUserId'));
}

/**
* @param array $keywords
* @param $message
*
* @return array
*/
public function keywordPos(array $keywords, $message)
{
$found = [];
Expand Down Expand Up @@ -153,6 +159,12 @@ public function keywordPos(array $keywords, $message)
return $found;
}

/**
* @param array $tokensPositions
* @param $newPosition
*
* @return bool
*/
private function isPositionTaken(array $tokensPositions, $newPosition)
{
if (empty($tokensPositions)) {
Expand All @@ -171,12 +183,15 @@ private function isPositionTaken(array $tokensPositions, $newPosition)
return false;
}

/**
* @param $newPosition
* @param $position
* @param $tokenLength
*
* @return bool
*/
private function isPositionIn($newPosition, $position, $tokenLength)
{
if ($newPosition >= $position && $newPosition < $position + $tokenLength) {
return true;
}

return false;
return $newPosition >= $position && $newPosition < $position + $tokenLength;
}
}

0 comments on commit fe10539

Please sign in to comment.