forked from yesboyhongtai/knight_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
145 lines (128 loc) · 4.31 KB
/
preprocess.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# import csv
# with open("./training_data/cluster_map/cluster_map") as f:
# reader = csv.reader(f)
# for row in reader:
# print row
# f.close()
import time
#process the cluster_map data.
cluster_dict = {}
cluster_dict_reverse = {}
for line in open("./training_data/cluster_map/cluster_map"):
district_hash, district_id = line.split("\t")
district_id.strip('\n')
cluster_dict[district_hash] = district_id
cluster_dict_reverse[district_id] = district_hash
#print district_hash, district_id
#process the poi data.
poi_dict = {}
poi_class = []
for line in open("./training_data/poi_data/poi_data"):
pos = line.find("\t", 0, 35)
district_hash = line[0:pos]
poi_class_str = line[pos+1 : len(line)]
poi_class_str = poi_class_str[:-1] #delete last char '\n'
poi_class = poi_class_str.split("\t")
poi_dict[district_hash] = poi_class
#process the traffic data.
traffic_dict = {}
tj_level =[] #tj_level[4] = tj_time
for line in open("./training_data/traffic_data/traffic_data"):
first_sep_pos = line.find("\t", 0, 35)
last_sep_pos = line.rfind("\t")
district_hash = line[0 : first_sep_pos]
tj_level_str = line[first_sep_pos : len(line)]
tj_level_str = tj_level_str[:-1] #delete last char '\n'
tj_level = tj_level_str.split("\t")
tj_time = tj_level[4]
tj_level.pop()
traffic_dict[district_hash + "_" + tj_time] = tj_level
#process the weather data.
weather_dict = {}
weather_info = []
for line in open("./training_data/weather_data/weather_data"):
first_sep_pos = line.find("\t")
date = line[0:first_sep_pos]
weather_info_str = line[first_sep_pos : len(line)]
weather_info_str = weather_info_str[:-1] #delete last char '\n'
weather_info = weather_info_str.split("\t")
weather_dict[date] = weather_info
#get the spice number for time
#2016-01-15 00:35:11
#2016-01-15 4
def getTimeSpice(time):
struct_time = time.strptime(time, '%Y-%m-%d %X')
hour = struct_time[3]
minute = struct_time[4]
second = struct_time[5]
time_spice = hour*6 + minute/10 + 1
return time_spice
#process the order data and generate the complete data.
#schema structure:
#order_id, driver_id, passenger_id, start_district_id, start_tj_level, start_poi_class,
#dest_district_id, dest_tj_level, dest_poi_class, price, time, weather, temperature, pm2.5
output = open("complete_data.csv", "wb")
for line in open("./training_data/order_data/order_data"):
order_id, driver_id, passenger_id, start_district_hash, dest_district_hash, price, time \
= line.split("\t")
time = time.strip('\n')
time_spice = getTimeSpice(time)
if "NULL" == driver_id:
flag = 0
else:
flag = 1
if start_district_hash in cluster_dict.keys():
start_district_id = cluster_dict[start_district_hash]
else:
start_district_id = ""
if dest_district_hash in cluster_dict.keys():
dest_district_id = cluster_dict[dest_district_hash]
else:
dest_district_id = ""
start_district_time = start_district_hash + "_" + time
dest_district_time = dest_district_hash + "_" + time
if start_district_time in traffic_dict.keys():
start_tj_level = traffic_dict[start_district_time]
start_tj_level_str = "_".join(start_tj_level)
else:
start_tj_level_str = ""
if dest_district_time in traffic_dict.keys():
dest_tj_level = traffic_dict[dest_district_time]
dest_tj_level_str = "_".join(dest_tj_level)
else:
dest_tj_level_str = ""
if start_district_hash in poi_dict.keys():
start_poi_class = poi_dict[start_district_hash]
start_poi_class_str = "_".join(start_poi_class)
else:
start_poi_class_str = ""
if dest_district_hash in poi_dict.keys():
dest_poi_class = poi_dict[dest_district_hash]
dest_poi_class_str = "_".join(dest_poi_class)
else:
dest_poi_class_str = ""
if time in weather_dict.keys():
weather_info = weather_dict[time]
weather = weather_info[0]
temperature = weather_info[1]
pm25 = weather_info[2]
else:
weather = ""
temperature = ""
pm25 = ""
outputline = []
outputline.append(start_district_id)
outputline.append(start_tj_level_str)
outputline.append(start_poi_class_str)
outputline.append(dest_district_id)
outputline.append(dest_tj_level_str)
outputline.append(dest_poi_class_str)
outputline.append(weather)
outputline.append(temperature)
outputline.append(pm25)
outputline.append(time_spice)
outputline.append(flag)
output_str = "\t".join(outputline)
output.write(output_str + "\n")
print("Write following string to file: %s" %(output_str))
output.close()