Skip to content

Commit 20cb235

Browse files
authored
[NOID] NLP documentation into APOC pages (#4076) (#4118)
1 parent 99ffe61 commit 20cb235

16 files changed

+1123
-1
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// tag::constraint[]
2+
CREATE CONSTRAINT n10s_unique_uri ON (r:Resource) ASSERT r.uri IS UNIQUE;
3+
// end::constraint[]
4+
5+
// tag::init[]
6+
CALL n10s.graphconfig.init({handleVocabUris: "MAP"});
7+
// end::init[]
8+
9+
// tag::mappings[]
10+
call n10s.nsprefixes.add('neo','neo4j://voc#');
11+
CALL n10s.mapping.add("neo4j://voc#subCatOf","SUB_CAT_OF");
12+
CALL n10s.mapping.add("neo4j://voc#about","ABOUT");
13+
// end::mappings[]
14+
15+
// tag::software-systems[]
16+
WITH "https://query.wikidata.org/sparql?query=prefix%20neo%3A%20%3Cneo4j%3A%2F%2Fvoc%23%3E%20%0A%23Cats%0A%23SELECT%20%3Fitem%20%3Flabel%20%0ACONSTRUCT%20%7B%0A%3Fitem%20a%20neo%3ACategory%20%3B%20neo%3AsubCatOf%20%3FparentItem%20.%20%20%0A%20%20%3Fitem%20neo%3Aname%20%3Flabel%20.%0A%20%20%3FparentItem%20a%20neo%3ACategory%3B%20neo%3Aname%20%3FparentLabel%20.%0A%20%20%3Farticle%20a%20neo%3AWikipediaPage%3B%20neo%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%7D%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20(wdt%3AP31%7Cwdt%3AP279)*%20wd%3AQ2429814%20.%0A%20%20%3Fitem%20wdt%3AP31%7Cwdt%3AP279%20%3FparentItem%20.%0A%20%20%3Fitem%20rdfs%3Alabel%20%3Flabel%20.%0A%20%20filter(lang(%3Flabel)%20%3D%20%22en%22)%0A%20%20%3FparentItem%20rdfs%3Alabel%20%3FparentLabel%20.%0A%20%20filter(lang(%3FparentLabel)%20%3D%20%22en%22)%0A%20%20%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%3Farticle%20schema%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AinLanguage%20%22en%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AisPartOf%20%3Chttps%3A%2F%2Fen.wikipedia.org%2F%3E%20.%0A%20%20%20%20%7D%0A%20%20%0A%7D" AS softwareSystemsUri
17+
CALL n10s.rdf.import.fetch(softwareSystemsUri, 'Turtle' , { headerParams: { Accept: "application/x-turtle" } })
18+
YIELD terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams
19+
RETURN terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams;
20+
// end::software-systems[]
21+
22+
// tag::programming-languages[]
23+
WITH "https://query.wikidata.org/sparql?query=prefix%20neo%3A%20%3Cneo4j%3A%2F%2Fvoc%23%3E%20%0A%23Cats%0A%23SELECT%20%3Fitem%20%3Flabel%20%0ACONSTRUCT%20%7B%0A%3Fitem%20a%20neo%3ACategory%20%3B%20neo%3AsubCatOf%20%3FparentItem%20.%20%20%0A%20%20%3Fitem%20neo%3Aname%20%3Flabel%20.%0A%20%20%3FparentItem%20a%20neo%3ACategory%3B%20neo%3Aname%20%3FparentLabel%20.%0A%20%20%3Farticle%20a%20neo%3AWikipediaPage%3B%20neo%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%7D%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20(wdt%3AP31%7Cwdt%3AP279)*%20wd%3AQ9143%20.%0A%20%20%3Fitem%20wdt%3AP31%7Cwdt%3AP279%20%3FparentItem%20.%0A%20%20%3Fitem%20rdfs%3Alabel%20%3Flabel%20.%0A%20%20filter(lang(%3Flabel)%20%3D%20%22en%22)%0A%20%20%3FparentItem%20rdfs%3Alabel%20%3FparentLabel%20.%0A%20%20filter(lang(%3FparentLabel)%20%3D%20%22en%22)%0A%20%20%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%3Farticle%20schema%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AinLanguage%20%22en%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AisPartOf%20%3Chttps%3A%2F%2Fen.wikipedia.org%2F%3E%20.%0A%20%20%20%20%7D%0A%20%20%0A%7D" AS programmingLanguagesUri
24+
CALL n10s.rdf.import.fetch(programmingLanguagesUri, 'Turtle' , { headerParams: { Accept: "application/x-turtle" } })
25+
YIELD terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams
26+
RETURN terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams;
27+
// end::programming-languages[]
28+
29+
// tag::data-formats[]
30+
WITH "https://query.wikidata.org/sparql?query=prefix%20neo%3A%20%3Cneo4j%3A%2F%2Fvoc%23%3E%20%0A%23Cats%0A%23SELECT%20%3Fitem%20%3Flabel%20%0ACONSTRUCT%20%7B%0A%3Fitem%20a%20neo%3ACategory%20%3B%20neo%3AsubCatOf%20%3FparentItem%20.%20%20%0A%20%20%3Fitem%20neo%3Aname%20%3Flabel%20.%0A%20%20%3FparentItem%20a%20neo%3ACategory%3B%20neo%3Aname%20%3FparentLabel%20.%0A%20%20%3Farticle%20a%20neo%3AWikipediaPage%3B%20neo%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%7D%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20(wdt%3AP31%7Cwdt%3AP279)*%20wd%3AQ24451526%20.%0A%20%20%3Fitem%20wdt%3AP31%7Cwdt%3AP279%20%3FparentItem%20.%0A%20%20%3Fitem%20rdfs%3Alabel%20%3Flabel%20.%0A%20%20filter(lang(%3Flabel)%20%3D%20%22en%22)%0A%20%20%3FparentItem%20rdfs%3Alabel%20%3FparentLabel%20.%0A%20%20filter(lang(%3FparentLabel)%20%3D%20%22en%22)%0A%20%20%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%3Farticle%20schema%3Aabout%20%3Fitem%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AinLanguage%20%22en%22%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20schema%3AisPartOf%20%3Chttps%3A%2F%2Fen.wikipedia.org%2F%3E%20.%0A%20%20%20%20%7D%0A%20%20%0A%7D" AS dataFormatsUri
31+
CALL n10s.rdf.import.fetch(dataFormatsUri, 'Turtle' , { headerParams: { Accept: "application/x-turtle" } })
32+
YIELD terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams
33+
RETURN terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams;
34+
// end::data-formats[]
35+
36+
// tag::version-control[]
37+
MATCH path = (c:Category {name: "version control system"})<-[:SUB_CAT_OF*]-(child)
38+
RETURN path
39+
LIMIT 25;
40+
// end::version-control[]
41+
42+
43+
// tag::dev-to-articles[]
44+
LOAD CSV WITH HEADERS FROM 'https://github.com/neo4j-examples/nlp-knowledge-graph/raw/master/import/articles.csv' AS row
45+
RETURN row
46+
LIMIT 10;
47+
// end::dev-to-articles[]
48+
49+
// tag::dev-to-import[]
50+
CALL apoc.periodic.iterate(
51+
"LOAD CSV WITH HEADERS FROM 'https://github.com/neo4j-examples/nlp-knowledge-graph/raw/master/import/articles.csv' AS row
52+
RETURN row",
53+
"MERGE (a:Article {uri: row.uri})
54+
WITH a
55+
CALL apoc.load.html(a.uri, {
56+
body: 'body div.spec__body p',
57+
title: 'h1',
58+
time: 'time'
59+
})
60+
YIELD value
61+
UNWIND value.body AS item
62+
WITH a,
63+
apoc.text.join(collect(item.text), '') AS body,
64+
value.title[0].text AS title,
65+
value.time[0].attributes.datetime AS date
66+
SET a.body = body , a.title = title, a.datetime = datetime(date)",
67+
{batchSize: 5, parallel: true}
68+
)
69+
YIELD batches, total, timeTaken, committedOperations
70+
RETURN batches, total, timeTaken, committedOperations;
71+
// end::dev-to-import[]
72+
73+
// tag::set-key[]
74+
:params key => ("<insert-key-here>")
75+
// end::set-key[]
76+
77+
// tag::nlp-import[]
78+
CALL apoc.periodic.iterate(
79+
"MATCH (a:Article)
80+
WHERE not(exists(a.processed))
81+
RETURN a",
82+
"CALL apoc.nlp.gcp.entities.stream([item in $_batch | item.a], {
83+
nodeProperty: 'body',
84+
key: $key
85+
})
86+
YIELD node, value
87+
SET node.processed = true
88+
WITH node, value
89+
UNWIND value.entities AS entity
90+
WITH entity, node
91+
WHERE not(entity.metadata.wikipedia_url is null)
92+
MERGE (page:Resource {uri: entity.metadata.wikipedia_url})
93+
SET page:WikipediaPage
94+
MERGE (node)-[:HAS_ENTITY]->(page)",
95+
{batchMode: "BATCH_SINGLE", batchSize: 10, params: {key: $key}})
96+
YIELD batches, total, timeTaken, committedOperations
97+
RETURN batches, total, timeTaken, committedOperations;
98+
// end::nlp-import[]
99+
100+
// tag::semantic-search[]
101+
MATCH (c:Category {name: "NoSQL database management system"})
102+
CALL n10s.inference.nodesInCategory(c, {
103+
inCatRel: "ABOUT",
104+
subCatRel: "SUB_CAT_OF"
105+
})
106+
YIELD node
107+
MATCH (node)<-[:HAS_ENTITY]-(article)
108+
RETURN article.uri AS uri, article.title AS title, article.datetime AS date,
109+
collect(n10s.rdf.getIRILocalName(node.uri)) as explicitTopics
110+
ORDER BY date DESC
111+
LIMIT 5;
112+
// end::semantic-search[]
113+
114+
// tag::similar-articles-1[]
115+
MATCH (a:Article {uri: "https://dev.to/qainsights/performance-testing-neo4j-database-using-bolt-protocol-in-apache-jmeter-1oa9"}),
116+
path = (a)-[:HAS_ENTITY]->(wiki)-[:ABOUT]->(cat),
117+
otherPath = (wiki)<-[:HAS_ENTITY]-(other)
118+
return path, otherPath;
119+
// end::similar-articles-1[]
120+
121+
// tag::similar-articles-2[]
122+
MATCH (a:Article {uri: "https://dev.to/qainsights/performance-testing-neo4j-database-using-bolt-protocol-in-apache-jmeter-1oa9"}),
123+
entityPath = (a)-[:HAS_ENTITY]->(wiki)-[:ABOUT]->(cat),
124+
path = (cat)-[:SUB_CAT_OF]->(parent)<-[:SUB_CAT_OF]-(otherCat),
125+
otherEntityPath = (otherCat)<-[:ABOUT]-(otherWiki)<-[:HAS_ENTITY]-(other)
126+
RETURN other.title, other.uri,
127+
[(other)-[:HAS_ENTITY]->()-[:ABOUT]->(entity) | entity.name] AS otherCategories,
128+
collect([node in nodes(path) | node.name]) AS pathToOther;
129+
// end::similar-articles-2[]
130+
131+
// tag::custom-ontology-init[]
132+
CALL n10s.nsprefixes.add('owl','http://www.w3.org/2002/07/owl#');
133+
CALL n10s.nsprefixes.add('rdfs','http://www.w3.org/2000/01/rdf-schema#');
134+
CALL n10s.mapping.add("http://www.w3.org/2000/01/rdf-schema#subClassOf","SUB_CAT_OF");
135+
CALL n10s.mapping.add("http://www.w3.org/2000/01/rdf-schema#label","name");
136+
CALL n10s.mapping.add("http://www.w3.org/2002/07/owl#Class","Category");
137+
// end::custom-ontology-init[]
138+
139+
// tag::custom-ontology-preview[]
140+
CALL n10s.rdf.preview.fetch("http://www.nsmntx.org/2020/08/swStacks","Turtle");
141+
// end::custom-ontology-preview[]
142+
143+
// tag::custom-ontology-import[]
144+
CALL n10s.rdf.import.fetch("http://www.nsmntx.org/2020/08/swStacks","Turtle")
145+
YIELD terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams
146+
RETURN terminationStatus, triplesLoaded, triplesParsed, namespaces, callParams;
147+
// end::custom-ontology-import[]
148+
149+
150+
// tag::zookeeper[]
151+
match path = (c:WikipediaPage)-[:ABOUT]->(category)-[:SUB_CAT_OF*]->(:Category {name: "NoSQL database management system"})
152+
where c.uri contains "Apache_ZooKeeper"
153+
RETURN path;
154+
// end::zookeeper[]
23.9 KB
Loading
125 KB
Loading
137 KB
Loading
285 KB
Loading
28 KB
Loading
Lines changed: 1 addition & 0 deletions
Loading
163 KB
Loading
37.2 KB
Loading

docs/asciidoc/modules/ROOT/images/stacks.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)