apoc.nlp.aws.entities.stream
过程 Apoc 扩展
返回提供的文本的实体流
签名
apoc.nlp.aws.entities.stream(source :: ANY?, config = {} :: MAP?) :: (node :: NODE?, value :: MAP?, error :: MAP?)
安装依赖
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."
});
我们可以使用此过程从 Article 节点中提取实体。我们想要分析的文本存储在该节点的 body 属性中,因此我们需要通过 nodeProperty 配置参数来指定它。
以下流式输出 Pokemon 文章的实体
MATCH (a:Article {uri: "/blog/pokegraph-gotta-graph-em-all/"})
CALL apoc.nlp.aws.entities.stream(a, {
key: $apiKey,
secret: $apiSecret,
nodeProperty: "body"
})
YIELD value
UNWIND value.entities AS entity
RETURN entity;
| 实体 (entity) |
|---|
{score: 0.780032217502594, endOffset: 71, text: "Nintendo Switch", type: "COMMERCIAL_ITEM", beginOffset: 56} |
{score: 0.8155304193496704, endOffset: 151, text: "at least", type: "QUANTITY", beginOffset: 143} |
{score: 0.7507548332214355, endOffset: 156, text: "once", type: "QUANTITY", beginOffset: 152} |
{score: 0.8760746717453003, endOffset: 172, text: "twice a week", type: "QUANTITY", beginOffset: 160} |
{score: 0.9944096803665161, endOffset: 217, text: "Mario Kart 8", type: "TITLE", beginOffset: 205} |
{score: 0.9946564435958862, endOffset: 247, text: "Neo4j", type: "ORGANIZATION", beginOffset: 242} |
{score: 0.6274040937423706, endOffset: 256, text: "European", type: "LOCATION", beginOffset: 248} |
我们返回了 7 个不同的实体。然后,我们可以应用一个 Cypher 语句,为每个实体创建一个节点,并从这些节点中的每一个建立一个指向 Article(文章)节点的 ENTITY 关系。
MATCH (a:Article {uri: "/blog/pokegraph-gotta-graph-em-all/"})
CALL apoc.nlp.aws.entities.stream(a, {
key: $apiKey,
secret: $apiSecret,
nodeProperty: "body"
})
YIELD value
UNWIND value.entities AS entity
MERGE (e:Entity {name: entity.text})
SET e.type = entity.type
MERGE (a)-[:ENTITY]->(e)
如果想要自动创建一个关键短语图,请参阅 apoc.nlp.aws.entities.graph。