-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_triplets.py
More file actions
71 lines (64 loc) · 3.16 KB
/
export_triplets.py
File metadata and controls
71 lines (64 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from neo4j import GraphDatabase
import pandas as pd
import os
from dotenv import load_dotenv
load_dotenv()
def get_neo4j_driver():
uri = os.getenv("NEO4J_URI")
user = os.getenv("NEO4J_USER")
password = os.getenv("NEO4J_PASSWORD")
try:
driver = GraphDatabase.driver(uri, auth=(user, password))
driver.verify_connectivity()
return driver
except Exception as e:
print(f"Errore nella connessione a Neo4j: {e}")
return None
def run_cypher_query(_driver, query):
with _driver.session(database=os.getenv("NEO4J_DATABASE")) as session:
results = session.run(query)
return [record.data() for record in results]
if __name__ == "__main__":
driver = get_neo4j_driver()
if driver is None:
exit(1)
# Estraiamo tutte le triplette (head, relation, tail)
query = """
MATCH (p:Player)-[r:playsForTeam]->(t:Team) RETURN p.playerId AS head, type(r) AS relation, t.teamId AS tail
UNION
MATCH (m:Manager)-[r:coachesTeam]->(t:Team) RETURN m.managerId AS head, type(r) AS relation, t.teamId AS tail
UNION
MATCH (p:Player)-[r:participatedInMatch]->(m:Match) RETURN p.playerId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (t:Team)-[r:wasHomeTeamIn]->(m:Match) RETURN t.teamId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (t:Team)-[r:wasAwayTeamIn]->(m:Match) RETURN t.teamId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (m:Match)-[r:playedAt]->(s:Stadium) RETURN m.matchId AS head, type(r) AS relation, s.stadiumId AS tail
UNION
MATCH (g:Goal)-[r:scoredIn]->(m:Match) RETURN g.goalId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (g:Goal)-[r:scoredBy]->(p:Player) RETURN g.goalId AS head, type(r) AS relation, p.playerId AS tail
UNION
MATCH (b:Booking)-[r:givenIn]->(m:Match) RETURN b.bookingId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (b:Booking)-[r:receivedBy]->(p:Player) RETURN b.bookingId AS head, type(r) AS relation, p.playerId AS tail
UNION
MATCH (p:Player)-[r:wonAward]->(a:Award) RETURN p.playerId AS head, type(r) AS relation, a.awardId AS tail
UNION
MATCH (s:Substitution)-[r:occurredInMatch]->(m:Match) RETURN s.substitutionId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (s:Substitution)-[r:hasPlayerComingOn]->(p:Player) RETURN s.substitutionId AS head, type(r) AS relation, p.playerId AS tail
UNION
MATCH (s:Substitution)-[r:hasPlayerGoingOff]->(p:Player) RETURN s.substitutionId AS head, type(r) AS relation, p.playerId AS tail
UNION
MATCH (t:Tournament)-[r:hasParticipant]->(te:Team) RETURN t.tournamentId AS head, type(r) AS relation, te.teamId AS tail
UNION
MATCH (t:Tournament)-[r:includesMatch]->(m:Match) RETURN t.tournamentId AS head, type(r) AS relation, m.matchId AS tail
UNION
MATCH (t:Tournament)-[r:hasWinner]->(te:Team) RETURN t.tournamentId AS head, type(r) AS relation, te.teamId AS tail
"""
results = run_cypher_query(driver, query)
df = pd.DataFrame(results)
df.to_csv('fifa_triplets.tsv', sep='\t', header=False, index=False)
print("Triplets exported to fifa_triplets.tsv")