Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. library(RNeo4j)
  2. graph = startGraph("http://localhost:7474/db/data/", username="xxxx", password="xxxx")
  3. clear(graph)
  4.  
  5. addConstraint(graph, "Tweet", "id")
  6. addConstraint(graph, "User", "username")
  7. addConstraint(graph, "Hashtag", "hashtag")
  8. addConstraint(graph, "Tags", "ent_tag")
  9.  
  10.  
  11. query = "
  12. CREATE (tweet:Tweet {id: {tweetID}})
  13. SET tweet.text = {text}
  14.  
  15. CREATE (user:User {name: {Username}})
  16.  
  17. CREATE (user)-[:TWEETED]->(tweet)
  18. FOREACH(reply_to_sn IN CASE {reply_to_sn} WHEN NULL then [] else [{reply_to_sn}] END |
  19. MERGE (replytouser:User {username:{reply_to_sn}})
  20. CREATE (tweet)-[:IN_REPLY_TO]->(replytouser)
  21. )
  22.  
  23. FOREACH(retweet_sn IN CASE {retweet_sn} WHEN NULL THEN [] ELSE [{retweet_sn}] END |
  24. MERGE(retweet_user:User {username: {retweet_sn}})
  25. CREATE (tweet)-[:RETWEET_OF]->(retweet_user)
  26. )
  27.  
  28. FOREACH(hastag_nodes IN CASE {hashtag_nodes} WHEN NULL then [] else [{hashtag_nodes}] END |
  29. MERGE (h:Hashtag {hashtag :{hashtag_nodes}})
  30. CREATE (tweet)-[:HASHTAG]->(h)
  31. )
  32.  
  33. FOREACH(mentioned_users IN CASE {mentioned_users} WHEN NULL then [] else [{mentioned_users}] END |
  34. MERGE (m:User {username :{mentioned_users}})
  35. CREATE (tweet)-[:MENTIONED]->(m)
  36. )
  37.  
  38. "
  39. tx = newTransaction(graph)
  40.  
  41. for(i in 1:nrow(kdf)){
  42. row = kdf[i, ]
  43. appendCypher(tx, query,
  44. tweetID=row$id,
  45. text=row$text,
  46. Username=row$screenName,
  47. reply_to_sn=row$replyToSN,
  48. retweet_sn=getRetweetSN(row$text),
  49. hashtag_nodes=getHashtags(row$text),
  50. mentioned_users=getMentions(row$text))
  51.  
  52. }
  53. commit(tx)
  54.  
  55. query="
  56. MATCH(t:ent_tag {id : $twid, type :$etype, text :$etext})
  57. MATCH(tw:tweet {tweetID : $twid })
  58. CREATE (tw)-[:HAS_ENT]->(t)
  59. "
  60. tx=newTransaction(graph)
  61. for (i in 1:nrow(ent_tbl)){
  62. row = ent_tbl[i,]
  63. appendCypher(tx2, query,
  64. twid=row2$tweetid,
  65. etype=row2$etype,
  66. etext=row2$etext)
  67. }
  68.  
  69.  
  70.  
  71. commit(tx)
  72.  
  73. > summary(graph)
  74. This To That
  75. 1 User TWEETED Tweet
  76. 2 Tweet RETWEET_OF User
  77. 3 Tweet HASHTAG Hashtag
  78. 4 Tweet MENTIONED User
  79. 5 Tweet IN_REPLY_TO User
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement