public function removeIrrelevant(NodeInterface $input, RecommendationEngine $engine, Recommendations $recommendations)
 {
     foreach ($recommendations->getItems() as $recommendation) {
         foreach ($engine->filters() as $filter) {
             if (!$filter->doInclude($input, $recommendation->item())) {
                 $recommendations->remove($recommendation);
             }
         }
     }
 }
 public function buildQuery(NodeInterface $input, Recommendations $recommendations)
 {
     $ids = [];
     foreach ($recommendations->getItems() as $recommendation) {
         $ids[] = $recommendation->item()->identity();
     }
     $query = 'UNWIND {ids} as id
     MATCH (n) WHERE id(n) = id
     RETURN id, size((n)<-[:FOLLOWS]-()) as followersCount';
     return Statement::create($query, ['ids' => $ids]);
 }
 public final function produceRecommendations(NodeInterface $input, ResultCollection $resultCollection)
 {
     $result = $resultCollection->get($this->name());
     $recommendations = new Recommendations($this->name());
     foreach ($result->records() as $record) {
         if ($record->hasValue($this->recoResultName())) {
             $recommendations->add($record->value($this->recoResultName()), $this->name(), $this->buildScore($input, $record->value($this->recoResultName()), $record));
         }
     }
     return $recommendations;
 }
Example #4
0
 public function buildQuery(Node $input, Recommendations $recommendations)
 {
     $query = 'UNWIND {ids} as id
     MATCH (n) WHERE id(n) = id
     MATCH (n)<-[r:RATED]-(u)
     RETURN id(n) as id, sum(r.rating) as score';
     $ids = [];
     foreach ($recommendations->getItems() as $item) {
         $ids[] = $item->item()->identity();
     }
     return Statement::create($query, ['ids' => $ids]);
 }
 public final function handleResultSet(Node $input, Result $result, Recommendations $recommendations)
 {
     $recordsMap = [];
     foreach ($result->records() as $i => $record) {
         if (!$record->hasValue($this->idResultName())) {
             throw new \InvalidArgumentException(sprintf('The record does not contain a value with key "%s" in "%s"', $this->idResultName(), $this->name()));
         }
         $recordsMap[$record->get($this->idResultName())] = $record;
     }
     foreach ($recommendations->getItems() as $recommendation) {
         if (array_key_exists($recommendation->item()->identity(), $recordsMap)) {
             $this->postProcess($input, $recommendation, $recordsMap[$recommendation->item()->identity()]);
         }
     }
 }
 private function removeIrrelevant(Node $input, RecommendationEngine $engine, Recommendations $recommendations, array $blacklist)
 {
     foreach ($recommendations->getItems() as $recommendation) {
         if (array_key_exists($recommendation->item()->identity(), $blacklist)) {
             $recommendations->remove($recommendation);
         } else {
             foreach ($engine->filters() as $filter) {
                 if (!$filter->doInclude($input, $recommendation->item())) {
                     $recommendations->remove($recommendation);
                     break;
                 }
             }
         }
     }
 }
 public function execute(NodeInterface $input, Recommendations $recommendations, RecommendationEngine $recommendationEngine)
 {
     $this->stack = $this->databaseService->getDriver()->stack('post_process_' . $recommendationEngine->name());
     foreach ($recommendationEngine->postProcessors() as $postProcessor) {
         if ($postProcessor instanceof CypherAwarePostProcessor) {
             foreach ($recommendations->getItems() as $recommendation) {
                 $this->prepareQuery($input, $recommendation, $postProcessor);
             }
         }
     }
     try {
         $results = $this->databaseService->getDriver()->runStack($this->stack);
         $this->stack = null;
         return $results;
     } catch (\Exception $e) {
         throw new \RuntimeException('PostProcess Query Exception - ' . $e->getMessage());
     }
 }
 public function testResultGetTwoScoresIfDiscoveredTwice()
 {
     $node = FakeNode::createDummy();
     $list = new Recommendations();
     $list->add($node, 'e1', new SingleScore(1));
     $list->add($node, 'e2', new SingleScore(1));
     $this->assertEquals(1, $list->size());
     $this->assertEquals(2, $list->getItems()[0]->totalScore());
     $this->assertCount(2, $list->get(0)->getScores());
     $this->assertArrayHasKey('e1', $list->get(0)->getScores());
     $this->assertArrayHasKey('e2', $list->get(0)->getScores());
 }
 /**
  * @param \GraphAware\Common\Type\NodeInterface $input
  * @param \GraphAware\Reco4PHP\Result\Recommendations $recommendations
  * @param \GraphAware\Reco4PHP\Engine\RecommendationEngine $recommendationEngine
  *
  * @return \GraphAware\Common\Result\ResultCollection
  */
 public function execute(NodeInterface $input, Recommendations $recommendations, RecommendationEngine $recommendationEngine)
 {
     $stack = $this->databaseService->getDriver()->stack('post_process_' . $recommendationEngine->name());
     foreach ($recommendationEngine->postProcessors() as $postProcessor) {
         if ($postProcessor instanceof CypherAwarePostProcessor) {
             foreach ($recommendations->getItems() as $recommendation) {
                 $tag = sprintf('post_process_%s_%d', $postProcessor->name(), $recommendation->item()->identity());
                 $statement = $postProcessor->buildQuery($input, $recommendation);
                 $stack->push($statement->text(), $statement->parameters(), $tag);
             }
         }
     }
     try {
         $results = $this->databaseService->getDriver()->runStack($stack);
         return $results;
     } catch (\Exception $e) {
         throw new \RuntimeException('PostProcess Query Exception - ' . $e->getMessage());
     }
 }
 public function testReasons()
 {
     $node = FakeNode::createDummy();
     $list = new Recommendations(new SimpleContext());
     $list->add($node, 'disc1', new SingleScore(1, 'reason1'));
     $list->add($node, 'disc1', new SingleScore(1, 'reason2'));
     $list->add($node, 'disc2', new SingleScore(3, 'reason3'));
     for ($i = 0; $i < 10; ++$i) {
         $list->add($node, 'disc3', new SingleScore(1, 'reasond' . $i));
     }
     $reco = $list->get(0);
     $this->assertEquals(15, $reco->totalScore());
     $this->assertArrayHasKey('disc3', $reco->getScores());
     $this->assertCount(10, $reco->getScore('disc3')->getScores());
 }
 /**
  * @param \GraphAware\Reco4PHP\Result\Recommendations $recommendations
  */
 public function merge(Recommendations $recommendations)
 {
     foreach ($recommendations->getItems() as $recommendation) {
         $this->getOrCreate($recommendation->item())->addScores($recommendation->getScores());
     }
 }