首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Neo4j - Graphaware reco4php -名称空间问题

Neo4j - Graphaware reco4php -名称空间问题
EN

Stack Overflow用户
提问于 2018-09-20 16:51:01
回答 1查看 170关注 0票数 0

我试图在Neo4j - reco4php页面上运行这个链接演示,我非常小心地执行了每一个步骤,但是最后我得到了一个奇怪的错误。

让我更详细地解释:

这是我试图运行的代码:

代码语言:javascript
复制
require 'vendor/autoload.php';

use GraphAware\Common\Cypher\Statement;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Context\Context;
use GraphAware\Reco4PHP\Engine\SingleDiscoveryEngine;

class RatedByOthers extends SingleDiscoveryEngine
{
public function discoveryQuery(Node $input, Context $context)
{
    $query = 'MATCH (input:User) WHERE id(input) = {id}
    MATCH (input)-[:RATED]->(m)<-[:RATED]-(o)
    WITH distinct o
    MATCH (o)-[:RATED]->(reco)
    RETURN distinct reco LIMIT 500';

    return Statement::create($query, ['id' => $input->identity()]);
}

public function name()
{
    return "rated_by_others";
}
}

上面的代码扩展了类"SingleDiscoveryEngine“:

代码语言:javascript
复制
declare (strict_types = 1);

namespace GraphAware\Reco4PHP\Engine;

use GraphAware\Common\Result\Record;
use GraphAware\Common\Result\ResultCollection;
use GraphAware\Common\Type\Node;
use GraphAware\Reco4PHP\Context\Context;
use GraphAware\Reco4PHP\Result\Recommendations;
use GraphAware\Reco4PHP\Result\SingleScore;

abstract class SingleDiscoveryEngine implements DiscoveryEngine
{
private static $DEFAULT_RECO_NAME = 'reco';
private static $DEFAULT_SCORE_NAME = 'score';
private static $DEFAULT_REASON_NAME = 'reason';


public function buildScore(Node $input, Node $item, Record $record, Context 
$context) : SingleScore
{
    $score = $record->hasValue($this->scoreResultName()) ? $record- >value($this->scoreResultName()) : $this->defaultScore();
    $reason = $record->hasValue($this->reasonResultName()) ? $record- >value($this->reasonResultName()) : null;

    return new SingleScore($score, $reason);
}


final public function produceRecommendations(Node $input, ResultCollection 
$resultCollection, Context $context) : Recommendations
{
    $result = $resultCollection->get($this->name());
    $recommendations = new Recommendations($context);

    foreach ($result->records() as $record) {
        if ($record->hasValue($this->recoResultName())) {
            $recommendations->add($record->get($this->recoResultName()), $this->name(), $this->buildScore($input, $record->get($this->recoResultName()), 
$record, $context));
        }
    }

    return $recommendations;
}

/**
 * {@inheritdoc}
 */
public function recoResultName() : string
{
    return self::$DEFAULT_RECO_NAME;
}

/**
 * {@inheritdoc}
 */
public function scoreResultName() : string
{
    return self::$DEFAULT_SCORE_NAME;
}

/**
 * {@inheritdoc}
 */
public function reasonResultName() : string
{
    return self::$DEFAULT_REASON_NAME;
}

/**
 * {@inheritdoc}
 */
public function defaultScore() : float
{
    return 1.0;
}
}

上面的类还实现了以下类:

代码语言:javascript
复制
namespace GraphAware\Reco4PHP\Engine;

use GraphAware\Common\Cypher\StatementInterface;
use GraphAware\Common\Result\Record;
use GraphAware\Common\Type\Node;
use GraphAware\Common\Result\ResultCollection;
use GraphAware\Reco4PHP\Context\Context;
use GraphAware\Reco4PHP\Result\Recommendations;
use GraphAware\Reco4PHP\Result\SingleScore;

interface DiscoveryEngine
{
/**
 * @return string The name of the discovery engine
 */
public function name() : string;

/**
 * The statement to be executed for finding items to be recommended.
 *
 * @param Node    $input
 * @param Context $context
 *
 * @return \GraphAware\Common\Cypher\Statement
 */
public function discoveryQuery(Node $input, Context $context) : StatementInterface;

/**
 * Returns the score produced by the recommended item.
 *
 * @param Node    $input
 * @param Node    $item
 * @param Record  $record
 * @param Context $context
 *
 * @return \GraphAware\Reco4PHP\Result\SingleScore A single score produced for the recommended item
 */
public function buildScore(Node $input, Node $item, Record $record, Context $context) : SingleScore;

/**
 * Returns a collection of Recommendation object produced by this discovery engine.
 *
 * @param Node             $input
 * @param ResultCollection $resultCollection
 * @param Context          $context
 *
 * @return Recommendations
 */
public function produceRecommendations(Node $input, ResultCollection $resultCollection, Context $context) : Recommendations;

/**
 * @return string The column identifier of the row result representing the recommended item (node)
 */
public function recoResultName() : string;

/**
 * @return string The column identifier of the row result representing the score to be used, note that this
 *                is not mandatory to have a score in the result. If empty, the score will be the float value returned by
 *                <code>defaultScore()</code> or the score logic if the concrete class override the <code>buildScore</code>
 *                method.
 */
public function scoreResultName() : string;

/**
 * @return float The default score to be given to the discovered recommended item
 */
public function defaultScore() : float;
}

当我运行代码时,我会得到以下错误:

致命错误:声明RatedByOther::name()必须与第9行的GraphAware\Reco4PHP\Engine\DiscoveryEngine::name():字符串兼容

我检查了几个小时的代码,我认为它应该可以正常工作,我不知道问题是从哪里来的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-20 18:05:17

我自己找到了答案。

通过在函数启动后声明返回类型来解决问题:

代码语言:javascript
复制
public function discoveryQuery(Node $input, Context $context) : GraphAware\Common\Cypher\StatementInterface   {
$query = 'MATCH (input:User) WHERE id(input) = {id}
MATCH (input)-[:RATED]->(m)<-[:RATED]-(o)
WITH distinct o
MATCH (o)-[:RATED]->(reco)
RETURN distinct reco LIMIT 500';

return Statement::create($query, ['id' => $input->identity()]);
}

public function name() : string 
{
return "rated_by_others";
}

希望它能在未来帮助到其他人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52429823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档