-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquarterfinals.py
280 lines (215 loc) · 8.59 KB
/
quarterfinals.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Module: quarterfinlas.py
# module to build lexer and parser for quarter finals
import ply.yacc as yacc
import ply.lex as lex
# to store match fixtures
matchlist = []
# to store match scorers
scorerlist = []
# to store match details (stadium, attendence, referee)
matchdetails = []
# to store penalty scorers
rightpenscorerslist = []
tokens = [
"OPENQUARTER",
"CLOSEQUARTER",
"OPENMATCHDIV",
"OPENPOINTTABLE",
"CLOSEPOINTTABLE",
"OPENTABLE",
"OPENLIST",
"CLOSELIST",
"OPENROW",
"CLOSEROW",
"OPENDATA",
"CLOSEDATA",
"OPENHEADER",
"CLOSEHEADER",
"OPENHREF",
"CLOSEHREF",
"SEPARATOR",
"CONTENT",
"GARBAGE",
"OPENLINK",
"OPENABBR",
"CLOSEABBR",
"FRIGHTDIV",
]
def t_OPENQUARTER(t):
"""<span\sclass="mw-headline"\sid="Quarter-finals">Quarter\-finals</span>"""
return t
def t_CLOSEQUARTER(t):
"""<span\sclass="mw-headline"\sid="Semi-finals">Semi\-finals</span>"""
return t
# def t_OPENPOINTTABLE(t):
# '''<table\sclass="wikitable"\sstyle="text-align:center;">'''
# return t
def t_OPENTABLE(t):
"""<table\sclass="fevent">"""
return t
def t_CLOSEPOINTTABLE(t):
"""</table>"""
return t
def t_OPENMATCHDIV(t):
"""<div\sitemscope=""\sitemtype="http&\#58;//schema.org/SportsEvent"\sclass="footballbox">"""
return t
def t_FRIGHTDIV(t):
"""<div\sclass="fright">"""
return t
def t_SEPARATOR(t):
"""<link\srel="mw-deduplicated-inline-style"\shref="mw-data:TemplateStyles:r997937747"/>"""
return t
def t_OPENROW(t):
r"<tr.*?>"
return t
def t_CLOSEROW(t):
r"</tr>"
return t
def t_OPENDATA(t):
r"<td.*?>"
return t
def t_CLOSEDATA(t):
r"</td>"
return t
def t_OPENLINK(t):
r"<link.*?>"
def t_OPENABBR(t):
r"<abbr.*?>"
def t_CLOSEABBR(t):
r"</abbr>"
def t_OPENLIST(t):
r"<li .*?>"
return t
def t_CLOSELIST(t):
r"</li>"
return t
def t_OPENHREF(t):
r"<a .*?>"
return t
def t_CLOSEHREF(t):
r"</a>"
return t
def t_OPENHEADER(t):
r"<th.*?>"
return t
def t_CLOSEHEADER(t):
r"</th>"
return t
def t_CONTENT(t):
r'[A-Za-z0-9ñáãćéíøäæóōúńÁÉÜÚÑğÓÍïüšëčýÿû,$#§&.+\-:;@ \'")\(–]+'
return t
def t_GARBAGE(t):
r"<.*?>"
def t_error(t):
t.lexer.skip(1)
# grammar to find quarterfinals stage in the html file
def p_start(p):
"""start : before OPENQUARTER skip SEPARATOR handlematches CLOSEQUARTER"""
# grammar to handle each match row and extract match details (score)
def p_handlematches(p):
"""handlematches : skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails SEPARATOR handlematches
| OPENMATCHDIV skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails SEPARATOR handlematches
| OPENMATCHDIV skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails
| skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF skip CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENHEADER CONTENT CLOSEHEADER OPENDATA handlepenscorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails SEPARATOR handlematches
| OPENMATCHDIV skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF skip CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENHEADER CONTENT CLOSEHEADER OPENDATA handlepenscorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails SEPARATOR handlematches
| OPENMATCHDIV skip OPENTABLE OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CONTENT CLOSEHEADER OPENHEADER OPENHREF CONTENT CLOSEHREF skip CLOSEHEADER OPENHEADER CONTENT OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENDATA skip CLOSEDATA OPENDATA handlescorer CLOSEDATA CLOSEROW OPENROW OPENHEADER OPENHREF CONTENT CLOSEHREF CLOSEHEADER CLOSEROW OPENROW OPENDATA handlescorer CLOSEDATA OPENHEADER CONTENT CLOSEHEADER OPENDATA handlepenscorer CLOSEDATA CLOSEROW CLOSEPOINTTABLE FRIGHTDIV handledetails
|"""
if len(p) > 1 and len(p) < 45:
matchlist.append(p[7] + " " + p[13] + " " + p[19])
elif len(p) > 45:
matchlist.append(p[7] + " " + p[13] + " " + p[20])
# grammar to handle scorer details and store it
def p_handlescorer(p):
"""handlescorer : skip OPENLIST OPENHREF CONTENT CLOSEHREF skip CLOSELIST handlescorer
|"""
if len(p) > 1:
scorerlist.append(p[4])
# grammar to handle penalty scorer details and store it
def p_handlepenscorer(p):
"""handlepenscorer : skip OPENLIST CONTENT OPENHREF CONTENT CLOSEHREF skip CLOSELIST handlepenscorer
|"""
if len(p) > 1:
rightpenscorerslist.append(p[5])
# grammar to handle stadium details and store it
def p_handledetails(p):
"""handledetails : OPENHREF CONTENT CLOSEHREF CONTENT OPENHREF CONTENT CLOSEHREF CONTENT CONTENT OPENHREF CONTENT CLOSEHREF skip"""
matchdetails.append(
p[2] + " " + p[4] + " " + p[6] + " " + p[8] + " " + p[9] + " " + p[11]
)
def p_before(p):
"""before : CONTENT before
| OPENHREF before
| CLOSEHREF before
| OPENHEADER before
| CLOSEHEADER before
| OPENDATA before
| CLOSEDATA before
| OPENROW before
| CLOSEROW before
| OPENLIST before
| CLOSELIST before
| OPENTABLE before
| OPENLINK before
| CLOSEPOINTTABLE before
| OPENMATCHDIV before
| FRIGHTDIV before
| SEPARATOR before
|"""
def p_skip(p):
"""skip : CONTENT skip
| OPENHREF skip
| CLOSEHREF skip
|"""
def p_error(p):
# print("Error ", p)
pass
def quarterfinals():
global matchlist
global scorerlist
global matchdetails
global rightpenscorerslist
matchlist = []
scorerlist = []
matchdetails = []
rightpenscorerslist = []
lexer = lex.lex()
parser = yacc.yacc()
f = open("fifa.html", "r", encoding="utf-8")
data = f.read()
lexer.input(data)
res = parser.parse(data)
matchlist.reverse()
# formatting the data for better and generalized access in the future
quarterFinalData = {
"matchlist": matchlist,
"1": {
"score": matchlist[0],
"details": matchdetails[0],
"scorerlist": [scorerlist[0], scorerlist[1]],
"isPen": True,
"penscorers": [scorerlist[2], scorerlist[3],scorerlist[4],scorerlist[5], rightpenscorerslist[0], rightpenscorerslist[1], rightpenscorerslist[2], rightpenscorerslist[3]],
},
"2": {
"score": matchlist[1],
"details": matchdetails[1],
"scorerlist": [scorerlist[6], scorerlist[7],scorerlist[8]],
"isPen": True,
"penscorers": [scorerlist[9], scorerlist[10],scorerlist[11],scorerlist[12],scorerlist[13], rightpenscorerslist[4], rightpenscorerslist[5], rightpenscorerslist[6], rightpenscorerslist[7], rightpenscorerslist[8]],
},
"3": {
"score": matchlist[2],
"details": matchdetails[2],
"scorerlist": [scorerlist[14]],
"isPen": False,
"penscorers": []
},
"4": {
"score": matchlist[3],
"details": matchdetails[3],
"scorerlist": [scorerlist[15],scorerlist[16],scorerlist[17]],
"isPen": False,
"penscorers": []
},
}
f.close()
return quarterFinalData