Skip to content

Commit

Permalink
lab6 updated
Browse files Browse the repository at this point in the history
  • Loading branch information
nimay-gupta committed Mar 26, 2021
1 parent 2cdf2e5 commit 804fa2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 14 additions & 7 deletions Lab6-Neo4j/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
file.write('MATCH (a:Tweet {text: "' + tweet[row[0]] + '"}), (b:Hashtag {tag: "' + hashtag[row[1]] + '"}) CREATE (a)-[:Contains]->(b);\n')
6 changes: 3 additions & 3 deletions Lab6-Neo4j/queries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 804fa2a

Please sign in to comment.