-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapis.py
More file actions
176 lines (157 loc) · 5.62 KB
/
apis.py
File metadata and controls
176 lines (157 loc) · 5.62 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import pandas as pd
from pandas import DataFrame
def time2float(time_str):
h, m = time_str.split(":")
return int(h) + int(m) / 60
class IntercityTransport:
def __init__(self, path: str = "../../database/intercity_transport/", en_version=False):
file_suffix = "_en" if en_version else ""
curdir = os.path.dirname(os.path.realpath(__file__))
self.base_path = os.path.join(curdir, path)
self.airplane_path = self.base_path + f"airplane{file_suffix}.jsonl"
self.airplane_df = pd.read_json(
self.airplane_path, lines=True, keep_default_dates=False
)
city_list = [
"上海",
"北京",
"深圳",
"广州",
"重庆",
"苏州",
"成都",
"杭州",
"武汉",
"南京",
]
self.city_list = city_list
self.city_en_list = [
"shanghai", "beijing", "shenzhen", "guangzhou", "chongqing",
"suzhou", "chengdu", "hangzhou", "wuhan", "nanjing",
]
self.city_cn_to_en = dict(zip(self.city_list, self.city_en_list))
self.city_en_to_cn = dict(zip(self.city_en_list, self.city_list))
self.train_df_dict = {}
for start_city in city_list:
for end_city in city_list:
if start_city == end_city:
continue
train_path = (
self.base_path
+ "train/"
+ f"from_{start_city}_to_{end_city}{file_suffix}.json"
)
train_df = pd.read_json(train_path)
self.train_df_dict[(start_city, end_city)] = train_df
def select(
self, start_city, end_city, intercity_type, earliest_leave_time="00:00"
) -> DataFrame:
if start_city in self.city_en_to_cn:
start_city = self.city_en_to_cn[start_city]
if end_city in self.city_en_to_cn:
end_city = self.city_en_to_cn[end_city]
if intercity_type not in ["train", "airplane"]:
return "only support intercity_type in ['train','airplane']"
res = self._select(start_city, end_city, intercity_type)
bool_list = [False] * len(res)
for i in range(len(res)):
if time2float(res.loc[i, "BeginTime"]) >= time2float(earliest_leave_time):
bool_list[i] = True
return res[bool_list]
def _select(self, start_city, end_city, intercity_type) -> DataFrame:
# intercity_type=='train' | 'airplane'
if intercity_type == "airplane":
if len(self.airplane_df) == 0:
return None
filtered_flights = self.airplane_df[
(self.airplane_df["From"].str.contains(start_city))
& (self.airplane_df["To"].str.contains(end_city))
]
sorted_flights = filtered_flights.sort_values(by="BeginTime").reset_index(
drop=True
)
return sorted_flights
if intercity_type == "train":
if len(self.train_df_dict[(start_city, end_city)]) == 0:
return None
filtered_trains = self.train_df_dict[(start_city, end_city)]
sorted_trains = filtered_trains.sort_values(by="BeginTime").reset_index(
drop=True
)
return sorted_trains
if __name__ == "__main__":
a = IntercityTransport()
city_list = [
"上海",
"北京",
"深圳",
"广州",
"重庆",
"苏州",
"成都",
"杭州",
"武汉",
"南京",
]
city_en_list = [
"shanghai",
"beijing",
"shenzhen",
"guangzhou",
"chongqing",
"suzhou",
"chengdu",
"hangzhou",
"wuhan",
"nanjing",
]
str_list = []
for i in range(len(city_list)):
for j in range(i + 1, len(city_list)):
tmp_len = 0
tmp = a.select(city_list[i], city_list[j], "train")
if not isinstance(tmp, DataFrame):
tmp_len = 0
else:
tmp_len = len(tmp)
if tmp_len > 0:
str_list.append(
"('{}','{}','{}',{})".format(
city_en_list[i], city_en_list[j], "train", tmp_len
)
)
tmp = a.select(city_list[j], city_list[i], "train")
if not isinstance(tmp, DataFrame):
tmp_len = 0
else:
tmp_len = len(tmp)
if tmp_len > 0:
str_list.append(
"('{}','{}','{}',{})".format(
city_en_list[j], city_en_list[i], "train", tmp_len
)
)
tmp = a.select(city_list[i], city_list[j], "flight")
if not isinstance(tmp, DataFrame):
tmp_len = 0
else:
tmp_len = len(tmp)
if tmp_len > 0:
str_list.append(
"('{}','{}','{}',{})".format(
city_en_list[i], city_en_list[j], "airplane", tmp_len
)
)
tmp = a.select(city_list[j], city_list[i], "flight")
if not isinstance(tmp, DataFrame):
tmp_len = 0
else:
tmp_len = len(tmp)
if tmp_len > 0:
str_list.append(
"('{}','{}','{}',{})".format(
city_en_list[j], city_en_list[i], "airplane", tmp_len
)
)
print(",\n".join(str_list))