Neo4j:拆分字符串并获取位置 [英] Neo4j: Split string and get position

查看:408
本文介绍了Neo4j:拆分字符串并获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字段拆分为不同的值,并将每个值存储在不同的节点中.对于每个创建的节点,我要存储位置. 示例:

I need to split a field in different values and store each value in a different node. For each created node I want to store the position. Example:

Sentence             Words
My car is red        My;car;is;red

使用:

FOREACH (w IN SPLIT(line.TWords, ";") |
 MERGE (wd:Word {word: w})

我可以拆分字段并存储不同的单词,但是我想存储关系中的位置.

I can split the field and store the different words, but I'd like to store the position on the relationship.

My car is red  -[HAS_WORD {position:1}]-> My
My car is red  -[HAS_WORD {position:2}]-> car
My car is red  -[HAS_WORD {position:3}]-> is
My car is red  -[HAS_WORD {position:4}]-> red

我怎么能得到这个?

解决方案

USING PERIODIC COMMIT LOAD CSV WITH HEADERS  FROM 'file:///output_2016-05-06_0203_Neo4jImport.csv' AS line FIELDTERMINATOR "\t"

MERGE (s:Segment{text: line.Source})
MERGE (ta:Segment{text: line.Target})

WITH SPLIT(line.SWords, ";") AS SWords, line, s, ta
UNWIND RANGE(0, SIZE(SWords)-1) as i
    MERGE (s)-[r:HAS_WORD {position:i+1}]->(w:Word {word: SWords[i]})

WITH SPLIT(line.TWords, ";") AS TWords, line, ta
UNWIND RANGE(0, SIZE(TWords)-1) as i
    MERGE (ta)-[r:HAS_WORD {position:i+1}]->(w:Word {word: TWords[i]})

请确保拳头WITH在第二个WITH中具有必要的变量引用:WITH SPLIT(line.SWords, ";") AS SWords, line, s, ta

Be sure that the fist WITH has the variable references necessary in second WITH: WITH SPLIT(line.SWords, ";") AS SWords, line, s, ta

推荐答案

您可以基于拆分的大小使用范围,假设包含该句子的节点由sentence标识:

You can use a range based on the size of the split, assuming the node containing the sentence is identified with sentence :

WITH split(line.TWords, ';') as splitted
UNWIND range(0, size(splitted) -1) as i
MERGE (w:Word {word: splitted[i]})
MERGE (sentence)-[:HAS_WORD {position: i}]->(w)

更新

USING PERIODIC COMMIT LOAD CSV WITH HEADERS 
FROM 'file:///output_2016-05-06_0203_Neo4jImport.csv' 
AS line FIELDTERMINATOR "\t" 
MERGE (s:Segment{text: line.Source}) 
WITH SPLIT(line.SWords, ";") AS SWords, line 
UNWIND RANGE(0, SIZE(SWords)-1) as i 
MERGE (s)-[r:HAS_WORD {position:i+1}]->(w:Word {word: SWords[i]}) 

这篇关于Neo4j:拆分字符串并获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆