apoc.nlp.gcp.classify.graph

过程 Apoc 扩展

将文档分类为类别。

签名

apoc.nlp.gcp.classify.graph(source :: ANY?, config = {} :: MAP?) :: (graph :: MAP?)

输入参数

名称 类型 默认

source

ANY?

null

config

MAP?

{}

输出参数

名称 类型

graph(图)

MAP?

安装依赖

NLP 过程依赖于 Kotlin 和客户端库,这些库未包含在 APOC Extended 库中。

这些依赖项包含在 apoc-nlp-dependencies-2025.10.0-all.jar 中,可从 发布页面 下载。下载该文件后,应将其放入 plugins 目录并重启 Neo4j 服务器。

设置 API 密钥

您可以前往 console.cloud.google.com/apis/credentials 生成有权访问 Cloud Natural Language API 的 API 密钥。创建密钥后,我们可以填充并执行以下命令来创建一个包含这些详细信息的参数。

以下定义了 apiKey 参数
:param apiKey => ("<api-key-here>")

或者,我们可以将这些凭据添加到 apoc.conf 中,并使用静态值存储函数加载它们。

apoc.conf
apoc.static.gcp.apiKey=<api-key-here>
以下从 apoc.conf 中检索 GCP 凭据
RETURN apoc.static.getAll("gcp") AS gcp;
表 1. 结果
gcp

{apiKey: "<api-key-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."
});

我们可以使用此过程自动创建类别图。除了拥有 Category 标签外,每个类别节点还将根据 type 属性的值拥有另一个标签。默认情况下,返回的是虚拟图。

以下代码返回关于 Pokemon 文章的类别虚拟图

MATCH (a:Article {uri: "/blog/pokegraph-gotta-graph-em-all/"})
CALL apoc.nlp.gcp.classify.graph(a, {
  key: $apiKey,
  nodeProperty: "body",
  writeRelationshipType: "CATEGORY"
})
YIELD graph AS g
RETURN g;

我们可以在 Pokemon 类别图中查看该虚拟图的 Neo4j 浏览器可视化效果。

apoc.nlp.gcp.classify.graph
图 1. Pokemon 类别图

以下代码创建了从文章到每个实体的 HAS_CATEGORY 关系

MATCH (a:Article)
WITH collect(a) AS articles
CALL apoc.nlp.gcp.classify.graph(articles, {
  key: $apiKey,
  nodeProperty: "body",
  writeRelationshipType: "HAS_CATEGORY",
  writeRelationshipProperty: "gcpCategoryScore",
  write: true
})
YIELD graph AS g
RETURN g;

然后,我们可以编写一个查询来返回已创建的实体。

以下返回文章及其对应的实体
MATCH (article:Article)
RETURN article.uri AS article,
       [(article)-[r:HAS_CATEGORY]->(c) | {category: c.text, score: r.gcpCategoryScore}] AS categories;
表 2. 结果
article categories

"/blog/pokegraph-gotta-graph-em-all/"

[{category: "/Games", score: 0.91}]

"https://en.wikipedia.org/wiki/Nintendo_Switch"

[{category: "/Computers & Electronics/Consumer Electronics/Game Systems & Consoles", score: 0.99}, {category: "/Games/Computer & Video Games", score: 0.99}]

如果我们想要流式传输回类别并对结果应用自定义逻辑,请参阅 apoc.nlp.gcp.classify.stream

© . This site is unofficial and not affiliated with Neo4j, Inc.