apoc.nlp.aws.keyPhrases.graph
过程 Apoc 扩展
为提供的文本创建(虚拟)关键短语图
安装依赖
NLP 过程依赖于 Kotlin 和客户端库,这些库未包含在 APOC Extended 库中。
这些依赖项包含在 apoc-nlp-dependencies-2025.10.0-all.jar 中,可从 发布页面 下载。下载该文件后,应将其放入 plugins 目录并重启 Neo4j 服务器。
设置 API 密钥
我们可以按照 docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html 上的说明生成访问密钥和密码。完成后,我们可以填充并执行以下命令来创建包含这些详细信息的参数。
apiKey 和 apiSecret 参数
:param apiKey => ("<api-key-here>");
:param apiSecret => ("<api-secret-here>");
或者,我们可以将这些凭据添加到 apoc.conf 中,并使用静态值存储函数来检索它们。
apoc.static.aws.apiKey=<api-key-here>
apoc.static.aws.apiSecret=<api-secret-here>
apoc.conf 中检索 AWS 凭据
RETURN apoc.static.getAll("aws") AS aws;
| aws |
|---|
{apiKey: "<api-key-here>", apiSecret: "<api-secret-here>"} |
使用示例
本节中的示例基于以下示例图
CREATE (:Article {
uri: "/blog/pokegraph-gotta-graph-em-all/",
body: "These days I’m rarely more than a few feet away from my Nintendo Switch and I play board games, card games and role playing games with friends at least once or twice a week. I’ve even organised lunch-time Mario Kart 8 tournaments between the Neo4j European offices!"
});
CREATE (:Article {
uri: "https://en.wikipedia.org/wiki/Nintendo_Switch",
body: "The Nintendo Switch is a video game console developed by Nintendo, released worldwide in most regions on March 3, 2017. It is a hybrid console that can be used as a home console and portable device. The Nintendo Switch was unveiled on October 20, 2016. Nintendo offers a Joy-Con Wheel, a small steering wheel-like unit that a Joy-Con can slot into, allowing it to be used for racing games such as Mario Kart 8."
});
我们可以使用此过程自动创建关键短语图。提取出的每个关键短语都将创建一个带有 KeyPhrase 标签的节点。
默认情况下,返回一个虚拟图,但可以通过指定 write: true 配置来持久化该图。
MATCH (a:Article {uri: "/blog/pokegraph-gotta-graph-em-all/"})
CALL apoc.nlp.aws.keyPhrases.graph(a, {
key: $apiKey,
secret: $apiSecret,
nodeProperty: "body",
writeRelationshipType: "KEY_PHRASE",
write: true
})
YIELD graph AS g
RETURN g;
我们可以在 Pokemon 关键短语图 中看到虚拟图的 Neo4j 浏览器可视化效果。
然后,我们可以编写一个查询来返回已创建的关键短语。
MATCH (a:Article {uri: "/blog/pokegraph-gotta-graph-em-all/"})
RETURN a.uri AS article,
[(a)-[:KEY_PHRASE]->(k:KeyPhrase) | k.text] AS keyPhrases;
| article | keyPhrases |
|---|---|
"/blog/pokegraph-gotta-graph-em-all/" |
["Neo4j 欧洲办事处", "一周", "朋友", "8 场锦标赛", "午餐时间的马里奥赛车", "纸牌游戏", "桌游", "角色扮演游戏", "我的任天堂 Switch", "几英尺以上", "这些天"] |
如果我们想流式传输返回的关键短语并对结果应用自定义逻辑,请参阅 apoc.nlp.aws.keyPhrases.stream。