gpt4 book ai didi

neo4j - 使用 Cypher 以编程方式在 Neo4j 中创建单词之间的关系

转载 作者:行者123 更新时间:2023-12-01 23:49:37 29 4
gpt4 key购买 nike

我有一个 Neo4j 数据库,其中填充了约 35K 英语单词。我想在单词中的给定位置创建一个字母不同的单词之间的关系。即 (a)--(i) or (food)--(fool) or (cat)--(hat)

对于单字母单词,密码查询非常简单:

START n=node(), m=node() where n.name =~ '.' and m.name =~ '.' and NOT (n.name = m.name) create (n)-[:single_letter_change]->(m)

不幸的是,为多字母单词建立关系并不是那么简单。我知道可以创建一个集合,如下所示:

WITH ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']· 9 AS letters

我知道可以通过以下方式迭代一个范围:

FOREACH (i IN range(0,25))

但是我放在一起的任何东西似乎都很丑陋、困惑,而且在语法上是无效的。我相信在 Cypher 中有一种优雅的方法可以使用集合函数来完成此操作,但我花了几天时间试图弄明白,是时候寻求帮助了。

关于如何实现这一点的想法?我很乐意发布一些我尝试过的(无效的)密码查询,但我担心它们可能会混淆问题。

编辑:这是我尝试为两个字母单词的首字母设置关系的示例,但我认为它可能不合时宜,而且我知道它不会运行:

WITH ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] AS alphabet FOREACH (first IN range(0,25) | START n=node(), m=node() where n.name =~ (alphabet[{first}] + '.') and m.name =~ (alphabet[{first}] + '.') and NOT (n.name = m.name) create (n)-[:single_letter_change]->(m)

最佳答案

这可能需要一些改进,但我认为它符合要求

// match all the words, start with ones that are three characters long
match (w:Word)
where length(w.name) = 3
// create a collection the length of the matched word
with range(0,length(w.name)-1) as w_len, w
unwind w_len as idx
// iterate through the word and replace one letter at a time
// making a regex pattern
with "(?i)" + left(w.name,idx) + '.' + right(w.name,length(w.name)-idx-1) as pattern, w, w_len
// match the pattern against 3 letter words
// that are not the word
// not already like the word
// and match the pattern
match (new_word:Word)
where not (new_word = w)
and not((new_word)-[:LIKE]-(w))
and length(new_word.name) = length(w_len)
and new_word.name =~ pattern
// create the relationship
create (new_word)-[:LIKE]->(w)
return new_word, w

关于neo4j - 使用 Cypher 以编程方式在 Neo4j 中创建单词之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27207952/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com