-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_events_parsing.py
226 lines (199 loc) · 8.01 KB
/
xml_events_parsing.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import xml.etree.ElementTree as ET
def get_goal_events(xml_goal_data, home_team_id, away_team_id):
root = ET.fromstring(xml_goal_data)
home_goals_before_half = 0
home_goals_after_half = 0
away_goals_before_half = 0
away_goals_after_half = 0
total_goals_before_half = 0
total_goals_after_half = 0
for value in root.findall('value'):
stats = value.find('stats')
if stats is not None and stats.find('goals') is not None:
elapsed = int(value.find('elapsed').text)
team_id = int(value.find('team').text)
if team_id == home_team_id:
if elapsed <= 45:
home_goals_before_half += 1
total_goals_before_half += 1
else:
home_goals_after_half += 1
total_goals_after_half += 1
elif team_id == away_team_id:
if elapsed <= 45:
away_goals_before_half += 1
total_goals_before_half += 1
else:
away_goals_after_half += 1
total_goals_after_half += 1
return home_goals_before_half, \
home_goals_after_half, \
away_goals_before_half, \
away_goals_after_half, \
total_goals_before_half, \
total_goals_after_half
def get_shoton_events(xml_shoton_data, home_team_id, away_team_id):
root = ET.fromstring(xml_shoton_data)
home_shots_before_half = 0
home_shots_after_half = 0
away_shots_before_half = 0
away_shots_after_half = 0
total_shots_before_half = 0
total_shots_after_half = 0
for value in root.findall('value'):
stats = value.find('stats')
if stats is not None and stats.find('shoton') is not None:
elapsed = int(value.find('elapsed').text)
if value.find('team') is None:
team_id = home_team_id
else:
team_id = int(value.find('team').text)
if team_id == home_team_id:
if elapsed <= 45:
home_shots_before_half += 1
total_shots_before_half += 1
else:
home_shots_after_half += 1
total_shots_after_half += 1
elif team_id == away_team_id:
if elapsed <= 45:
away_shots_before_half += 1
total_shots_before_half += 1
else:
away_shots_after_half += 1
total_shots_after_half += 1
return home_shots_before_half, \
home_shots_after_half, \
away_shots_before_half, \
away_shots_after_half, \
total_shots_before_half, \
total_shots_after_half
def get_shotoff_events(xml_shotoff_data, home_team_id, away_team_id):
root = ET.fromstring(xml_shotoff_data)
home_shots_off_before_half = 0
home_shots_off_after_half = 0
away_shots_off_before_half = 0
away_shots_off_after_half = 0
total_shots_off_before_half = 0
total_shots_off_after_half = 0
for value in root.findall('value'):
stats = value.find('stats')
if stats is not None and stats.find('shotoff') is not None:
elapsed = int(value.find('elapsed').text)
if value.find('team') is None:
team_id = home_team_id
else:
team_id = int(value.find('team').text)
if team_id == home_team_id:
if elapsed <= 45:
home_shots_off_before_half += 1
total_shots_off_before_half += 1
else:
home_shots_off_after_half += 1
total_shots_off_after_half += 1
elif team_id == away_team_id:
if elapsed <= 45:
away_shots_off_before_half += 1
total_shots_off_before_half += 1
else:
away_shots_off_after_half += 1
total_shots_off_after_half += 1
return home_shots_off_before_half, \
home_shots_off_after_half, \
away_shots_off_before_half, \
away_shots_off_after_half, \
total_shots_off_before_half, \
total_shots_off_after_half
def get_foulcommit_events(xml_foulcommit_data, home_team_id, away_team_id):
root = ET.fromstring(xml_foulcommit_data)
home_fouls_before_half = 0
home_fouls_after_half = 0
away_fouls_before_half = 0
away_fouls_after_half = 0
total_fouls_before_half = 0
total_fouls_after_half = 0
for value in root.findall('value'):
stats = value.find('stats')
if stats is not None and stats.find('foulscommitted') is not None:
elapsed = int(value.find('elapsed').text)
team_id = int(value.find('team').text)
if team_id == home_team_id:
if elapsed <= 45:
home_fouls_before_half += 1
total_fouls_before_half += 1
else:
home_fouls_after_half += 1
total_fouls_after_half += 1
elif team_id == away_team_id:
if elapsed <= 45:
away_fouls_before_half += 1
total_fouls_before_half += 1
else:
away_fouls_after_half += 1
total_fouls_after_half += 1
return home_fouls_before_half, \
home_fouls_after_half, \
away_fouls_before_half, \
away_fouls_after_half, \
total_fouls_before_half, \
total_fouls_after_half
def get_cards(xml_card_data, home_team_id, away_team_id):
root = ET.fromstring(xml_card_data)
home_yellow_before_half = 0
home_yellow_after_half = 0
away_yellow_before_half = 0
away_yellow_after_half = 0
total_yellow_before_half = 0
total_yellow_after_half = 0
home_red_before_half = 0
home_red_after_half = 0
away_red_before_half = 0
away_red_after_half = 0
total_red_before_half = 0
total_red_after_half = 0
home_card_score = 0
away_card_score = 0
card_score = 0
for value in root.findall('value'):
if value.find('team') is None:
team_id = home_team_id
else:
team_id = int(value.find('team').text)
if value.find('card_type') is None:
card_type = value.find('comment').text
else:
card_type = value.find('card_type').text
elapsed = int(value.find('elapsed').text)
if card_type == 'y':
card_score = 10
if elapsed <= 45:
if team_id == home_team_id:
home_yellow_before_half += 1
elif team_id == away_team_id:
away_yellow_before_half += 1
total_yellow_before_half += 1
else:
if team_id == home_team_id:
home_yellow_after_half += 1
elif team_id == away_team_id:
away_yellow_after_half += 1
total_yellow_after_half += 1
elif card_type == 'r':
card_score = 25
if elapsed <= 45:
if team_id == home_team_id:
home_red_before_half += 1
elif team_id == away_team_id:
away_red_before_half += 1
total_red_before_half += 1
else:
if team_id == home_team_id:
home_red_after_half += 1
elif team_id == away_team_id:
away_red_after_half += 1
total_red_after_half += 1
if team_id == home_team_id:
home_card_score += card_score
elif team_id == away_team_id:
away_card_score += card_score
return home_yellow_before_half, home_yellow_after_half, away_yellow_before_half, away_yellow_after_half, total_yellow_before_half, total_yellow_after_half, home_red_before_half, home_red_after_half, away_red_before_half, away_red_after_half, total_red_before_half, total_red_after_half, home_card_score, away_card_score, card_score