Skip to content

Instantly share code, notes, and snippets.

@Taresin
Last active February 3, 2025 09:41
Show Gist options
  • Save Taresin/70b67c0886779a25ccfad2d874bbc19d to your computer and use it in GitHub Desktop.
Save Taresin/70b67c0886779a25ccfad2d874bbc19d to your computer and use it in GitHub Desktop.
Adding all the train stations on the Sydney trains network into a knowledge graph
import os
# Iterate through all .txt files in "data" folder
if not os.path.exists("output"):
os.makedirs("output")
station_list = set()
for file in os.listdir("data"):
if file.endswith(".txt"):
text = open("data/{}".format(file), "r").read().split("\n")
# Store all unseen stations
station_list.update(text)
# Store all relationships
relationships = zip(text[:-1], text[1:])
output_file_name = file.replace(".txt", ".csv")
output = open("output/{}".format(output_file_name), "w")
output.write("stop_id_from,stop_id_to")
output.write("\n")
output.write("\n".join("{},{}".format(parent, child) for parent, child in relationships))
output.close()
# Write all stations to a file
station_list = sorted(list(station_list))
output = open("output/all_stations.csv", "w")
output.write("\n".join(station_list))
output.close()
# Print all outputted files
print(", ".join(["'{}'".format(i) for i in os.listdir("output")]))
LOAD CSV FROM 'file:///all_stations.csv' AS row
MERGE (s:Stop {name: row[0]});
UNWIND ['metro.csv', 't1.csv', 't1_western.csv', 't1_western_1.csv', 't2.csv', 't4.csv', 't4_1.csv', 't5.csv', 't6.csv', 't7.csv', 't8.csv', 't8_airport.csv', 't9.csv'] AS file
LOAD CSV WITH HEADERS FROM 'file:///' + file AS row
CALL(row) {
MATCH (a:Stop {name: row.stop_id_from})
MATCH (b:Stop {name: row.stop_id_to})
OPTIONAL MATCH (a)-[r:NEXT_STOP]-(b) // note the undirected pattern to check both directions
WITH a, b, count(r) AS relCount
WHERE relCount = 0
MERGE (a)-[:NEXT_STOP]->(b)
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Allawah
Arncliffe
Artarmon
Ashfield
Asquith
Auburn
Banksia
Bankstown
Barangaroo
Bardwell Park
Beecroft
Bella Vista
Berala
Berowra
Beverly Hills
Bexley North
Birrong
Blacktown
Bondi Junction
Burwood
Cabramatta
Campbelltown
Canley Vale
Caringbah
Carlton
Carramar
Castle Hill
Casula
Central
Chatswood
Cheltenham
Cherrybrook
Chester Hill
Circular Quay
Clarendon
Clyde
Como
Concord West
Cronulla
Crows Nest
Croydon
Denistone
Domestic Airport
Doonside
East Hills
East Richmond
Eastwood
Edgecliff
Edmondson Park
Emu Plains
Engadine
Epping
Erskineville
Fairfield
Flemington
Gadigal
Glenfield
Gordon
Granville
Green Square
Guildford
Gymea
Harris Park
Heathcote
Helensburgh
Hills Showground
Holsworthy
Homebush
Hornsby
Hurstville
Ingleburn
International Airport
Jannali
Kellyville
Killara
Kings Cross
Kingsgrove
Kingswood
Kirrawee
Kogarah
Leightonfield
Leppington
Leumeah
Lewisham
Lidcombe
Lindfield
Liverpool
Loftus
Macarthur
Macdonaldtown
Macquarie Fields
Macquarie Park
Macquarie University
Marayong
Martin Place
Mascot
Meadowbank
Merrylands
Milsons Point
Minto
Miranda
Mortdale
Mount Colah
Mount Druitt
Mount Kuring-gai
Mulgrave
Museum
Narwee
Newtown
Normanhurst
North Ryde
North Strathfield
North Sydney
Norwest
Oatley
Olympic Park
Padstow
Panania
Parramatta
Pendle Hill
Pennant Hills
Penrith
Penshurst
Petersham
Pymble
Quakers Hill
Redfern
Regents Park
Revesby
Rhodes
Richmond
Riverstone
Riverwood
Rockdale
Rooty Hill
Roseville
Rouse Hill
Schofields
Sefton
Seven Hills
St James
St Leonards
St Marys
St Peters
Stanmore
Strathfield
Summer Hill
Sutherland
Sydenham
Tallawong
Tempe
Thornleigh
Toongabbie
Town Hall
Turramurra
Turrella
Victoria Cross
Villawood
Vineyard
Wahroonga
Waitara
Warrawee
Warwick Farm
Waterfall
Waterloo
Waverton
Wentworthville
Werrington
West Ryde
Westmead
Windsor
Wolli Creek
Wollstonecraft
Woolooware
Wynyard
Yagoona
Yennora
stop_id_from stop_id_to
Sydenham Waterloo
Waterloo Central
Central Gadigal
Gadigal Martin Place
Martin Place Barangaroo
Barangaroo Victoria Cross
Victoria Cross Crows Nest
Crows Nest Chatswood
Chatswood North Ryde
North Ryde Macquarie Park
Macquarie Park Macquarie University
Macquarie University Epping
Epping Cherrybrook
Cherrybrook Castle Hill
Castle Hill Hills Showground
Hills Showground Norwest
Norwest Bella Vista
Bella Vista Kellyville
Kellyville Rouse Hill
Rouse Hill Tallawong
stop_id_from stop_id_to
Berowra Mount Kuring-gai
Mount Kuring-gai Mount Colah
Mount Colah Asquith
Asquith Hornsby
Hornsby Waitara
Waitara Wahroonga
Wahroonga Warrawee
Warrawee Turramurra
Turramurra Pymble
Pymble Gordon
Gordon Killara
Killara Lindfield
Lindfield Roseville
Roseville Chatswood
Chatswood Artarmon
Artarmon St Leonards
St Leonards Wollstonecraft
Wollstonecraft Waverton
Waverton North Sydney
North Sydney Milsons Point
Milsons Point Wynyard
Wynyard Town Hall
Town Hall Central
Central Redfern
Redfern Burwood
Burwood Strathfield
Strathfield Lidcombe
Lidcombe Auburn
Auburn Clyde
Clyde Granville
Granville Harris Park
Harris Park Parramatta
stop_id_from stop_id_to
Richmond East Richmond
East Richmond Clarendon
Clarendon Windsor
Windsor Mulgrave
Mulgrave Vineyard
Vineyard Riverstone
Riverstone Schofields
Schofields Quakers Hill
Quakers Hill Marayong
Marayong Blacktown
Blacktown Seven Hills
Seven Hills Toongabbie
Toongabbie Pendle Hill
Pendle Hill Wentworthville
Wentworthville Westmead
Westmead Parramatta
Parramatta Harris Park
Harris Park Granville
Granville Clyde
Clyde Auburn
Auburn Lidcombe
Lidcombe Strathfield
Strathfield Burwood
Burwood Redfern
Redfern Central
Central Town Hall
Town Hall Wynyard
Wynyard Milsons Point
Milsons Point North Sydney
North Sydney Waverton
Waverton Wollstonecraft
Wollstonecraft St Leonards
St Leonards Artarmon
Artarmon Chatswood
stop_id_from stop_id_to
Emu Plains Penrith
Penrith Kingswood
Kingswood Werrington
Werrington St Marys
St Marys Mount Druitt
Mount Druitt Rooty Hill
Rooty Hill Doonside
Doonside Blacktown
stop_id_from stop_id_to
Leppington Edmondson Park
Edmondson Park Glenfield
Glenfield Casula
Casula Liverpool
Liverpool Warwick Farm
Warwick Farm Cabramatta
Cabramatta Carramar
Carramar Villawood
Villawood Leightonfield
Leightonfield Chester Hill
Chester Hill Sefton
Sefton Regents Park
Regents Park Berala
Berala Canley Vale
Canley Vale Fairfield
Fairfield Yennora
Yennora Guildford
Guildford Merrylands
Merrylands Parramatta
Parramatta Harris Park
Harris Park Granville
Granville Clyde
Clyde Auburn
Auburn Lidcombe
Lidcombe Flemington
Flemington Homebush
Homebush Strathfield
Strathfield Burwood
Burwood Croydon
Croydon Ashfield
Ashfield Summer Hill
Summer Hill Lewisham
Lewisham Petersham
Petersham Stanmore
Stanmore Newtown
Newtown Macdonaldtown
Macdonaldtown Redfern
Redfern Central
Central Town Hall
Town Hall Wynyard
Wynyard Circular Quay
Circular Quay St James
St James Museum
stop_id_from stop_id_to
Helensburgh Waterfall
Waterfall Heathcote
Heathcote Engadine
Engadine Loftus
Loftus Sutherland
Sutherland Jannali
Jannali Como
Como Oatley
Oatley Mortdale
Mortdale Penshurst
Penshurst Hurstville
Hurstville Allawah
Allawah Carlton
Carlton Kogarah
Kogarah Rockdale
Rockdale Banksia
Banksia Arncliffe
Arncliffe Wolli Creek
Wolli Creek Tempe
Tempe Sydenham
Sydenham Redfern
Redfern Central
Central Town Hall
Town Hall Martin Place
Martin Place Kings Cross
Kings Cross Edgecliff
Edgecliff Bondi Junction
stop_id_from stop_id_to
Cronulla Woolooware
Woolooware Caringbah
Caringbah Miranda
Miranda Gymea
Gymea Kirrawee
Kirrawee Sutherland
stop_id_from stop_id_to
Leppington Edmondson Park
Edmondson Park Glenfield
Glenfield Casula
Casula Liverpool
Liverpool Warwick Farm
Warwick Farm Cabramatta
Cabramatta Canley Vale
Canley Vale Fairfield
Fairfield Yennora
Yennora Guildford
Guildford Merrylands
Merrylands Harris Park
Harris Park Parramatta
Parramatta Westmead
Westmead Wentworthville
Wentworthville Pendle Hill
Pendle Hill Toongabbie
Toongabbie Seven Hills
Seven Hills Blacktown
Blacktown Marayong
Marayong Quakers Hill
Quakers Hill Schofields
Schofields Riverstone
Riverstone Vineyard
Vineyard Mulgrave
Mulgrave Windsor
Windsor Clarendon
Clarendon East Richmond
East Richmond Richmond
stop_id_from stop_id_to
Bankstown Yagoona
Yagoona Birrong
Birrong Regents Park
Regents Park Berala
Berala Lidcombe
stop_id_from stop_id_to
Lidcombe Olympic Park
stop_id_from stop_id_to
Macarthur Campbelltown
Campbelltown Leumeah
Leumeah Minto
Minto Ingleburn
Ingleburn Macquarie Fields
Macquarie Fields Glenfield
Glenfield Holsworthy
Holsworthy East Hills
East Hills Panania
Panania Revesby
Revesby Padstow
Padstow Riverwood
Riverwood Narwee
Narwee Beverly Hills
Beverly Hills Kingsgrove
Kingsgrove Bexley North
Bexley North Bardwell Park
Bardwell Park Turrella
Turrella Sydenham
Sydenham St Peters
St Peters Erskineville
Erskineville Redfern
Redfern Central
Central Museum
Museum St James
St James Circular Quay
Circular Quay Wynyard
Wynyard Town Hall
Town Hall Wynyard
Wynyard Circular Quay
Circular Quay St James
St James Museum
stop_id_from stop_id_to
Turrella Wolli Creek
Wolli Creek International Airport
International Airport Domestic Airport
Domestic Airport Mascot
Mascot Green Square
Green Square Central
stop_id_from stop_id_to
Hornsby Normanhurst
Normanhurst Thornleigh
Thornleigh Pennant Hills
Pennant Hills Beecroft
Beecroft Cheltenham
Cheltenham Epping
Epping Eastwood
Eastwood Denistone
Denistone West Ryde
West Ryde Meadowbank
Meadowbank Rhodes
Rhodes Concord West
Concord West North Strathfield
North Strathfield Strathfield
Strathfield Burwood
Burwood Redfern
Redfern Central
Central Town Hall
Town Hall Wynyard
Wynyard Milsons Point
Milsons Point North Sydney
North Sydney Waverton
Waverton Wollstonecraft
Wollstonecraft St Leonards
St Leonards Artarmon
Artarmon Chatswood
Chatswood Roseville
Roseville Lindfield
Lindfield Killara
Killara Gordon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment