diff options
author | Luke Miller <lmiller@i-neda.com> | 2013-08-04 20:32:06 +0100 |
---|---|---|
committer | Luke Miller <lmiller@i-neda.com> | 2013-08-04 20:32:06 +0100 |
commit | 0bdb6e09e5ae9449b9560dc44a47a8fdd219b82d (patch) | |
tree | d6582457436bfff245d5e94d0cd6bc97d0c023fd /tube_data_parser.rb |
Initial commit of v1.0.0
Diffstat (limited to 'tube_data_parser.rb')
-rw-r--r-- | tube_data_parser.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tube_data_parser.rb b/tube_data_parser.rb new file mode 100644 index 0000000..7863df3 --- /dev/null +++ b/tube_data_parser.rb @@ -0,0 +1,31 @@ +require 'yaml' +require 'json' +csv_data = [] +File.open('tube_data_2006-07-26.csv','r').readlines[1..-1].each {|line| csv_data << line.split(',')} +tube_data_hash = Hash.new {|key,value| key[value] = {}} +csv_data.each do |line,direction,from_station,to_station,distance,unimpeded_time,peak_time,off_peak_time| + tube_data_hash["#{from_station.gsub(/ \(.*\)/,'')}_#{line}"]["#{to_station.gsub(/ \(.*\)/,'')}_#{line}"] = {'line' => line, 'direction' => direction, 'distance' => distance.to_f, 'unimpeded_time' => unimpeded_time.to_f, 'peak_time' => peak_time.to_f, 'off_peak_time' => off_peak_time.to_f} +end +tube_data_hash.keys.each do |from_station| + stations = tube_data_hash.find_all {|f_stat,t_stats| f_stat.split('_')[0] == from_station.split('_')[0] } + if stations.size == 1 + tube_data_hash[from_station.gsub(/_.*/,'')] = tube_data_hash.delete(from_station) + to_stations = tube_data_hash[from_station.gsub(/_.*/,'')] + to_stations.keys.each {|station| to_stations[station.gsub(/_.*/,'')] = to_stations.delete(station)} + else + stations.each do |f_stat,t_stats| + tube_data_hash[from_station].merge! t_stats + end + end +end +change_stations = tube_data_hash.find_all {|f_stat,t_stats| f_stat.match /_/ }.map {|f_stat,t_stats| f_stat} +change_stations.each do |from_station| + to_stations = tube_data_hash[from_station] + to_stations.keys.each {|station| to_stations[station.gsub(/_.*/,'')] = to_stations.delete(station) unless change_stations.include?(station)} + tube_data_hash[from_station][from_station.gsub(/_.*/,'')] = {'line' => from_station.split('_')[1], 'direction' => nil, 'distance' => 0.01, 'unimpeded_time' => 0.01, 'peak_time' => 0.01, 'off_peak_time' => 0.01} + tube_data_hash[from_station].each do |to_station,data| + tube_data_hash[from_station.gsub(/_.*/,'')][from_station] = {'line' => from_station.split('_')[1], 'direction' => nil, 'distance' => 0.01, 'unimpeded_time' => 0.01, 'peak_time' => 0.01, 'off_peak_time' => 0.01} + end +end +File.new('graph.yaml', 'w') << tube_data_hash.to_yaml +File.new('graph.json', 'w') << tube_data_hash.to_json |