From 804fa2a59fe6aabb2291e2785ab8dec63d686f12 Mon Sep 17 00:00:00 2001 From: Nimay Gupta Date: Fri, 26 Mar 2021 15:32:40 +0530 Subject: [PATCH] lab6 updated --- Lab6-Neo4j/loaddata.py | 21 ++++++++++++++------- Lab6-Neo4j/queries.txt | 6 +++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Lab6-Neo4j/loaddata.py b/Lab6-Neo4j/loaddata.py index a14797b..e0b6545 100644 --- a/Lab6-Neo4j/loaddata.py +++ b/Lab6-Neo4j/loaddata.py @@ -2,44 +2,51 @@ file = open("loaddata.cypher", 'w') +user = {} +tweet = {} +hashtag = {} + with open("users.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('CREATE (:User {id: ' + row[0] + ', name: "' + row[1] + '"});\n') + user[row[0]] = row[1] + file.write('CREATE (:User {name: "' + row[1] + '"});\n') with open("tweets.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('CREATE (:Tweet {id: ' + row[0] + ', text: "' + row[1] + '"});\n') + tweet[row[0]] = row[1] + file.write('CREATE (:Tweet {text: "' + row[1] + '"});\n') with open("hashtags.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('CREATE (:Hashtag {id: ' + row[0] + ', tag: "' + row[1] + '"});\n') + hashtag[row[0]] = row[1] + file.write('CREATE (:Hashtag {tag: "' + row[1] + '"});\n') with open("follows.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('MATCH (a:User {id: ' + row[0] + '}), (b:User {id: ' + row[1] + '}) CREATE (a)-[:Follows]->(b);\n') + file.write('MATCH (a:User {name: "' + user[row[0]] + '"}), (b:User {name: "' + user[row[1]] + '"}) CREATE (a)-[:Follows]->(b);\n') with open("sent.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('MATCH (a:User {id: ' + row[0] + '}), (b:Tweet {id: ' + row[1] + '}) CREATE (a)-[:Sent]->(b);\n') + file.write('MATCH (a:User {name: "' + user[row[0]] + '"}), (b:Tweet {text: "' + tweet[row[1]] + '"}) CREATE (a)-[:Sent]->(b);\n') with open("mentions.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('MATCH (a:Tweet {id: ' + row[0] + '}), (b:User {id: ' + row[1] + '}) CREATE (a)-[:Mentions]->(b);\n') + file.write('MATCH (a:Tweet {text: "' + tweet[row[0]] + '"}), (b:User {name: "' + user[row[1]] + '"}) CREATE (a)-[:Mentions]->(b);\n') with open("contains.csv", 'r') as csvfile: csvreader = csv.reader(csvfile) fields = next(csvreader) for row in csvreader: - file.write('MATCH (a:Tweet {id: ' + row[0] + '}), (b:Hashtag {id: ' + row[1] + '}) CREATE (a)-[:Contains]->(b);\n') \ No newline at end of file + file.write('MATCH (a:Tweet {text: "' + tweet[row[0]] + '"}), (b:Hashtag {tag: "' + hashtag[row[1]] + '"}) CREATE (a)-[:Contains]->(b);\n') \ No newline at end of file diff --git a/Lab6-Neo4j/queries.txt b/Lab6-Neo4j/queries.txt index a3da163..0476a0d 100644 --- a/Lab6-Neo4j/queries.txt +++ b/Lab6-Neo4j/queries.txt @@ -4,11 +4,11 @@ b. MATCH (t:Tweet)-[:Contains]->(h:Hashtag) WHERE h.tag = 'proud' AND t.text =~ c. MATCH (n:User)-[:Sent]->(t:Tweet)<-[:Sent]-(:User {name: "Jessica"}) WHERE NOT (t)-[:Contains]->(:Hashtag) RETURN n.name, t.text; -d. MATCH (n:User)-[:Sent]->(t:Tweet)<-[:Sent]-(j:User {name: "Jessica"}) WHERE NOT (t)-[:Contains]->(:Hashtag) AND NOT (n)-[:Follows]->(j) RETURN n.name; +d. MATCH (n:User)-[:Sent]->(t:Tweet)<-[:Sent]-(j:User {name: "Jessica"}) WHERE NOT (t)-[:Contains]->(:Hashtag) AND NOT (n)-[:Follows]->(j) RETURN n.name, t.text; -e. MATCH (:User)-[:Follows]->(n:User) RETURN n.name, count(*) as followers ORDER BY followers DESC LIMIT 5; +e. MATCH (:User)-[:Follows]->(n:User) RETURN n.name, count(*) as cnt ORDER BY cnt DESC, n.name LIMIT 5; -f. MATCH p = (:User {name: "Ashley"})-[:Follows*]->(n:User) WHERE n.name<>"Ashley" RETURN n.name, max(length(p)) +f. MATCH p = (:User {name: "Ashley"})-[:Follows*]->(n:User) WHERE n.name<>"Ashley" with n.name as x, min(length(p)) as d return max(d); g. MATCH (:User {name: "Thomas"})-[:Follows]->(:User)<-[:Follows]-(n:User) RETURN n.name, count(*) as cnt ORDER BY cnt DESC, n.name LIMIT 5;