Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(RNeo4j)
- graph = startGraph("http://localhost:7474/db/data/", username="xxxx", password="xxxx")
- clear(graph)
- addConstraint(graph, "Tweet", "id")
- addConstraint(graph, "User", "username")
- addConstraint(graph, "Hashtag", "hashtag")
- addConstraint(graph, "Tags", "ent_tag")
- query = "
- CREATE (tweet:Tweet {id: {tweetID}})
- SET tweet.text = {text}
- CREATE (user:User {name: {Username}})
- CREATE (user)-[:TWEETED]->(tweet)
- FOREACH(reply_to_sn IN CASE {reply_to_sn} WHEN NULL then [] else [{reply_to_sn}] END |
- MERGE (replytouser:User {username:{reply_to_sn}})
- CREATE (tweet)-[:IN_REPLY_TO]->(replytouser)
- )
- FOREACH(retweet_sn IN CASE {retweet_sn} WHEN NULL THEN [] ELSE [{retweet_sn}] END |
- MERGE(retweet_user:User {username: {retweet_sn}})
- CREATE (tweet)-[:RETWEET_OF]->(retweet_user)
- )
- FOREACH(hastag_nodes IN CASE {hashtag_nodes} WHEN NULL then [] else [{hashtag_nodes}] END |
- MERGE (h:Hashtag {hashtag :{hashtag_nodes}})
- CREATE (tweet)-[:HASHTAG]->(h)
- )
- FOREACH(mentioned_users IN CASE {mentioned_users} WHEN NULL then [] else [{mentioned_users}] END |
- MERGE (m:User {username :{mentioned_users}})
- CREATE (tweet)-[:MENTIONED]->(m)
- )
- "
- tx = newTransaction(graph)
- for(i in 1:nrow(kdf)){
- row = kdf[i, ]
- appendCypher(tx, query,
- tweetID=row$id,
- text=row$text,
- Username=row$screenName,
- reply_to_sn=row$replyToSN,
- retweet_sn=getRetweetSN(row$text),
- hashtag_nodes=getHashtags(row$text),
- mentioned_users=getMentions(row$text))
- }
- commit(tx)
- query="
- MATCH(t:ent_tag {id : $twid, type :$etype, text :$etext})
- MATCH(tw:tweet {tweetID : $twid })
- CREATE (tw)-[:HAS_ENT]->(t)
- "
- tx=newTransaction(graph)
- for (i in 1:nrow(ent_tbl)){
- row = ent_tbl[i,]
- appendCypher(tx2, query,
- twid=row2$tweetid,
- etype=row2$etype,
- etext=row2$etext)
- }
- commit(tx)
- > summary(graph)
- This To That
- 1 User TWEETED Tweet
- 2 Tweet RETWEET_OF User
- 3 Tweet HASHTAG Hashtag
- 4 Tweet MENTIONED User
- 5 Tweet IN_REPLY_TO User
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement