apoc.nlp.gcp.classify.graph
Procedure APOC Full
Classifies a document into categories.
Install Dependencies
The NLP procedures have dependencies on Kotlin and client libraries that are not included in the APOC Library.
These dependencies are included in apoc-nlp-dependencies-4.4.0.34.jar, which can be downloaded from the releases page.
Once that file is downloaded, it should be placed in the plugins
directory and the Neo4j Server restarted.
Setting up API Key
We can generate an API Key that has access to the Cloud Natural Language API by going to console.cloud.google.com/apis/credentials. Once we’ve created a key, we can populate and execute the following command to create a parameter that contains these details.
apiKey
parameter:param apiKey => ("<api-key-here>")
Alternatively we can add these credentials to apoc.conf
and load them using the static value storage functions.
See Static Value Storage.
apoc.static.gcp.apiKey=<api-key-here>
apoc.conf
RETURN apoc.static.getAll("gcp") AS gcp;
gcp |
---|
{apiKey: "<api-key-here>"} |
Usage Examples
The examples in this section are based on the following sample graph:
CREATE (:Article {
uri: "https://neo4j.com/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."
});
We can use this procedure to automatically create the category graph.
As well as having the Category
label, each category node will have another label based on the value of the type
property.
By default, a virtual graph is returned.
The following returns a virtual graph of categories for the Pokemon article:
MATCH (a:Article {uri: "https://neo4j.com/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;
We can see a Neo4j Browser visualization of the virtual graph in Pokemon categories graph.
The following creates a HAS_CATEGORY
relationship from the article to each entity:
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;
We can then write a query to return the entities that have been created.
MATCH (article:Article)
RETURN article.uri AS article,
[(article)-[r:HAS_CATEGORY]->(c) | {category: c.text, score: r.gcpCategoryScore}] AS categories;
article | categories |
---|---|
"https://neo4j.com/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}] |
If we want to stream back categories and apply custom logic to the results, see apoc.nlp.gcp.classify.stream.