forked from TallChris91/PASS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ruleset_module.py
1162 lines (1106 loc) · 51.7 KB
/
Ruleset_module.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
sys.path.append(r'C:\\Users\\u1269857\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages')
sys.path.append('C:\\Program Files\\Anaconda3\\Lib\\site-packages')
#reload(sys)
#sys.setdefaultencoding('utf-8')
import os.path
from bs4 import BeautifulSoup
import xlrd
import re
import random
from operator import itemgetter
def winninggoalwithassist(soup, gamecourselist, idx):
if 'assist' in gamecourselist[idx]:
homegoals = soup.find('highlights').find('home').find('goalscorerslist').findChildren()
awaygoals = soup.find('highlights').find('away').find('goalscorerslist').findChildren()
# If both teams have scored and the final goal difference was one
if (len(homegoals) > 0) and (len(awaygoals) > 0) and (abs(len(homegoals) - len(awaygoals)) == 1):
# If the goal was the last goal by the winning team
# Check which team has made the most goals
if len(homegoals) > len(awaygoals):
winningteam = 'home'
losingteam = 'away'
else:
winningteam = 'away'
losingteam = 'home'
# If this goal is the final goal by the winning team, it is the winning goal as well (since this goal meant they won with the 1 goal difference)
if idx == len(gamecourselist) - 1:
return True
# If not, check if there were any other goals made by this team
else:
# First make a list after the event
afterevents = gamecourselist[idx + 1:]
# Then search the list for goals for the winning team (penalty goal, regular goal, or own goal by other team)
for event in afterevents:
if ((event['event'] == 'regular goal') and (event['team'] == winningteam)) or ((event['event'] == 'penalty goal') and (event['team'] == winningteam)) or ((event['event'] == 'own goal') and (event['team'] == losingteam)):
# If a goal for the winning team is found, the goal is not the winning goal
return False
# If there were no other goals for the winning team found, the goal is the winning goal
return True
else:
return False
def winninggoal(soup, gamecourselist, idx):
homegoals = soup.find('highlights').find('home').find('goalscorerslist').findChildren()
awaygoals = soup.find('highlights').find('away').find('goalscorerslist').findChildren()
# If both teams have scored and the final goal difference was one
if (len(homegoals) > 0) and (len(awaygoals) > 0) and (abs(len(homegoals) - len(awaygoals)) == 1):
# If the goal was the last goal by the winning team
# Check which team has made the most goals
if len(homegoals) > len(awaygoals):
winningteam = 'home'
losingteam = 'away'
else:
winningteam = 'away'
losingteam = 'home'
# If this goal is the final goal by the winning team, it is the winning goal as well (since this goal meant they won with the 1 goal difference)
if idx == len(gamecourselist) - 1:
return True
# If not, check if there were any other goals made by this team
else:
# First make a list after the event
afterevents = gamecourselist[idx + 1:]
# Then search the list for goals for the winning team (penalty goal, regular goal, or own goal by other team)
for event in afterevents:
if ((event['event'] == 'regular goal') and (event['team'] == winningteam)) or ((event['event'] == 'penalty goal') and (event['team'] == winningteam)) or ((event['event'] == 'own goal') and (event['team'] == losingteam)):
# If a goal for the winning team is found, the goal is not the winning goal
return False
# If there were no other goals for the winning team found, the goal is the winning goal
return True
def onlygoal(soup, gamecourselist, idx):
homegoals = soup.find('highlights').find('home').find('goalscorerslist').findChildren()
awaygoals = soup.find('highlights').find('away').find('goalscorerslist').findChildren()
# If the total amount of goals is one, it is the only goal made
if len(homegoals) + len(awaygoals) == 1:
return True
else:
return False
def finalgoal(gamecourselist, idx):
#There should be more than one goal scored in the match for this condition to fire
goalnumber = 0
for event in gamecourselist:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
if 'player' in event:
goalnumber += 1
else:
goalnumber += 2
if goalnumber > 1:
# If the goal is the last entry in the gamecourselist, it is the final goal
if idx == len(gamecourselist) - 1:
return True
# Else, check if there were goals scored after the event
else:
# First make a list after the event
afterevents = gamecourselist[idx + 1:]
# Then search the list for goals
for event in afterevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
return True
else:
return False
def secondgoal(gamecourselist, idx):
# Check the name of the goalscorer
player = gamecourselist[idx]['player']
# First event of the game can never be the second+ goal of a player
if idx == 0:
return False
# Check the events before the current events
beforeevents = gamecourselist[:idx]
numbergoals = 0
for event in beforeevents:
if (type(event) == dict) and ((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')):
# Get all the values for keys containing 'player'
players = [value for key, value in event.items() if 'player' in key]
for player in players:
if player == gamecourselist[idx]['player']:
numbergoals += 1
if numbergoals > 1:
return 'goal ' + str(numbergoals)
else:
return False
def earlygoal(gamecourselist, idx, homeaway):
#For now, I only use the first goal of the match
if idx == 0:
# Check the events before the current events
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
# Check the minute the goal is scored
try:
minute = gamecourselist[idx]['minute']
except KeyError:
print(gamecourselist[idx])
print(homeaway)
sys.exit(1)
# For now, the goals that are scored in the first 10 minutes of the first or second half are marked as early goals
if (minute <= 10) or ((minute >= 45) and (minute <= 55)):
return True
else:
return False
def leadgoal(gamecourselist, idx):
# For now, I only mark the first goal in the game as the lead goal (I could revise this: every goal that changes the equal score to a lead
if idx == 0:
return True
# Check the events before the current events
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
return True
def anschlusstreffer(homeaway, gamecourselist, idx):
# First two goals can never be an anschlusstreffer
if idx <= 1:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if ((event['event'] == 'regular goal') and (event['team'] == focus)) or ((event['event'] == 'penalty goal') and (event['team'] == focus)) or ((event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif ((event['event'] == 'regular goal') and (event['team'] == other)) or ((event['event'] == 'penalty goal') and (event['team'] == other)) or ((event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
focusgoals += 2
# There should have been a lead of two goals for one team for this goal to be the anschlusstreffer
if abs(focusgoals - othergoals) == 2:
# And this goal should be a goal that brings the difference back to one
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 1:
return True
else:
return False
else:
return False
def lateequalizer(homeaway, gamecourselist, idx):
# First goal can never be an equalizer
if idx == 0:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or ((event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or ((event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of 1 goal for the focus team for this goal to be the equalizer
if abs(focusgoals - othergoals) == 1:
# And this goal should be a goal that brings the difference to zero
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 0:
#And the minute should be at least the 80th to be a late equalizer
if gamecourselist[idx]['minute'] >= 80:
return True
else:
return False
else:
return False
else:
return False
def equalizer(homeaway, gamecourselist, idx):
# First goal can never be an equalizer
if idx == 0:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of 1 goal for the focus team for this goal to be the equalizer
if abs(focusgoals - othergoals) == 1:
# And this goal should be a goal that brings the difference to zero
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 0:
return True
else:
return False
else:
return False
def twoplusdifference(homeaway, gamecourselist, idx):
# First two goals can never result in a 2+ goal difference
if idx <= 1:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
focusgoals += 2
# There should have been a lead of at least 2 goals already for the other team for this goal to be the two+ goal difference
difference = abs(focusgoals - othergoals)
if difference >= 2:
# And this goal should make it more
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) > difference:
return True
else:
return False
else:
return False
def twodifference(homeaway, gamecourselist, idx):
# First goal can never result in a 2 goal difference
if idx == 0:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or ((event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of 1 goal already for one team for this goal to be the two goal difference
if abs(focusgoals - othergoals) == 1:
# And this goal should be a goal that brings the difference to two
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 2:
return True
else:
return False
else:
return False
def withassist(gamecourselist, idx):
if 'assist' in gamecourselist[idx]:
return True
else:
return False
def twosuccessive(gamecourselist, idx):
if idx == len(gamecourselist) - 1:
return False
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx + 1]['event'] == 'regular goal')) and (gamecourselist[idx]['team'] == gamecourselist[idx + 1]['team']):
return True
else:
return False
def ergebniskosmetik(homeaway, gamecourselist, idx):
# First two goals can never result in an ergebniskosmetik
if idx <= 1:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of at least 2 goals for one team for this goal to be the ergebniskosmetik
difference = abs(focusgoals - othergoals)
if difference >= 2:
#And this goal should make the difference one less
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) < difference:
# Besides that, this goal should be the last goal of the game
# Which is the case if this is the last event of the game
if idx == len(gamecourselist) - 1:
return True
# If the event is not the last event, look at the afterevents
afterevents = gamecourselist[idx + 1:]
# And see if one of those events is a goal
for event in afterevents:
if ((event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (
event['event'] == 'own goal')):
return False
return True
else:
return False
else:
return False
def multitwiceyellow(gamestatisticslist, idx):
#There should be more than one gamestatistics event to have multiple yellow/reds
yellowredcount = 0
for event in gamestatisticslist:
if type(event) == dict:
if event['event'] == 'twice yellow':
if 'player 1' in event:
return True
else:
yellowredcount += 1
if yellowredcount < 2:
return False
else:
newdict = {'event': 'twice yellow'}
num = 0
for idx2, event in enumerate(gamestatisticslist):
if (type(gamestatisticslist[idx2]) == dict) and (gamestatisticslist[idx2]['event'] == 'twice yellow'):
num += 1
for key in gamestatisticslist[idx2]:
if key != 'event':
newdict.update({key + ' ' + str(num): gamestatisticslist[idx2][key]})
gamestatisticslist[idx2] = ''
gamestatisticslist[idx] = newdict
return True
def twicefocus(homeaway, gamestatisticslist, idx):
if (type(gamestatisticslist) == dict) and (gamestatisticslist[idx]['team'] == homeaway):
return True
else:
return False
def multiyellowcards(gamestatisticslist, idx):
yellowcount = 0
for event in gamestatisticslist:
if type(event) == dict:
if event['event'] == 'yellow card':
if 'player 1' in event:
return True
else:
yellowcount += 1
if yellowcount <= 1:
return False
else:
newdict = {'event': 'yellow card'}
num = 0
for idx2, event in enumerate(gamestatisticslist):
if gamestatisticslist[idx2]['event'] == 'yellow card':
num += 1
for key in gamestatisticslist[idx2]:
if key != 'event':
newdict.update({key + ' ' + str(num): gamestatisticslist[idx2][key]})
gamestatisticslist[idx2] = ''
gamestatisticslist[idx] = newdict
return True
def oneyellowcards(gamestatisticslist):
#For now, every yellow card from 1 until infinity is using this rule, in the future this should possibly apply to multiple yellow cards only
yellowcount = 0
for event in gamestatisticslist:
if type(event) == dict:
if event['event'] == 'yellow card':
if 'player' in event:
yellowcount += 1
if yellowcount == 1:
return True
else:
return False
def earlyredcard(gamestatisticslist, idx):
#For now, I only use the first red card of the match
if idx == 0:
# Check the events before the current events
beforeevents = gamestatisticslist[:idx]
for event in beforeevents:
if (event['event'] == 'red card'):
return False
# Check the minute the goal is scored
minute = gamestatisticslist[idx]['minute']
# For now, the goals that are scored in the first 10 minutes of the first half are marked as early reds
if minute <= 10:
return True
else:
return False
def redfocus(homeaway, gamestatisticslist, idx):
if gamestatisticslist[idx]['team'] == homeaway:
return True
else:
return False
def finaltwoplusdifference(soup):
homegoals = int(soup.find('highlights').find('home').find('finalgoals').text)
awaygoals = int(soup.find('highlights').find('away').find('finalgoals').text)
finaldifference = abs(homegoals-awaygoals)
if finaldifference > 2:
return True
else:
return False
def manygoals(soup):
homegoals = int(soup.find('highlights').find('home').find('finalgoals').text)
awaygoals = int(soup.find('highlights').find('away').find('finalgoals').text)
if homegoals + awaygoals > 5:
return True
else:
return False
def nogoals(soup):
homegoals = int(soup.find('highlights').find('home').find('finalgoals').text)
awaygoals = int(soup.find('highlights').find('away').find('finalgoals').text)
if homegoals + awaygoals == 0:
return True
else:
return False
def focusredcards(gamestatisticslist, homeaway):
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
for idx, event in enumerate(gamestatisticslist):
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == focus):
return True
return False
def otherredcards(gamestatisticslist, homeaway):
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
for idx, event in enumerate(gamestatisticslist):
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == other):
return True
return False
def winnerredcards(gamecourselist, gamestatisticslist, homeaway):
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
focusgoals = 0
othergoals = 0
# Get the goals for the focus team and other team
for eventidx, event in enumerate(gamecourselist):
if ((event['event'] == 'regular goal') and (event['team'] == homeaway)) or (
(event['event'] == 'penalty goal') and (event['team'] == homeaway)) or ((event['event'] == 'own goal') and (event['team'] != homeaway)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
if ((event['event'] == 'regular goal') and (event['team'] != homeaway)) or (
(event['event'] == 'penalty goal') and (event['team'] != homeaway)) or ((event['event'] == 'own goal') and (event['team'] == homeaway)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
#The winner has scored more goals
if othergoals > focusgoals:
winner = other
if focusgoals > othergoals:
winner = focus
else:
return False
for idx, event in enumerate(gamestatisticslist):
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == focus) and (winner == focus):
return True
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == other) and (winner == other):
return True
return False
def loserredcards(gamecourselist, gamestatisticslist, homeaway):
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
focusgoals = 0
othergoals = 0
# Get the goals for the focus team and other team
for eventidx, event in enumerate(gamecourselist):
if ((event['event'] == 'regular goal') and (event['team'] == homeaway)) or (
(event['event'] == 'penalty goal') and (event['team'] == homeaway)) or (
(event['event'] == 'own goal') and (event['team'] != homeaway)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
if ((event['event'] == 'regular goal') and (event['team'] != homeaway)) or (
(event['event'] == 'penalty goal') and (event['team'] != homeaway)) or (
(event['event'] == 'own goal') and (event['team'] == homeaway)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# The winner has scored more goals
if othergoals > focusgoals:
winner = other
if focusgoals > othergoals:
winner = focus
for idx, event in enumerate(gamestatisticslist):
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == focus) and (winner != focus):
return True
if ((event['event'] == 'red card') or (event['event'] == 'twice yellow')) and (event['team'] == other) and (winner != other):
return True
return False
def finalgoalfocusteam(gamecourselist, homeaway):
#There should be more than one goal scored in the match for this condition to fire
goalnumber = 0
goaleventteam = []
for event in gamecourselist:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal'):
goalnumber += 1
goaleventteam.append(event['team'])
if goalnumber > 1:
# If the goal is the last entry in the goaleventlist, it is the final goal
if goaleventteam[-1] == homeaway:
return True
else:
return False
else:
return False
'''
num = len(gamecourselist)-1
while num >= 0:
#Don't work with own goals
if gamecourselist[num]['event'] == 'own goal':
return False
if (gamecourselist[num]['event'] == 'regular goal') or (gamecourselist[num]['event'] == 'penalty goal'):
if gamecourselist[num]['team'] == homeaway:
return True
else:
return False
num -= 1
return False
'''
def finalgoaltitle(gamecourselist, homeaway):
#There should be more than one goal scored in the match for this condition to fire
goalnumber = 0
goaleventteam = []
for event in gamecourselist:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal'):
goalnumber += 1
goaleventteam.append(event['team'])
if goalnumber > 1:
return True
else:
return False
def focusteamplayedaway(homeaway):
if homeaway == 'away':
return True
else:
return False
def lateequalizerfocusteam(homeaway, gamecourselist, idx):
# First goal can never be an equalizer
if idx == 0:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
#And no goals have been made after this goal
afterevents = gamecourselist[idx + 1:]
for event in afterevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of 1 goal for the focus team for this goal to be the equalizer
if abs(focusgoals - othergoals) == 1:
# And this goal should be a goal that brings the difference to zero
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 0:
#And the minute should be at least the 80th to be a late equalizer
if gamecourselist[idx]['minute'] >= 80:
#And the goal should be made by the focus team
if gamecourselist[idx]['team'] == homeaway:
return True
else:
return False
else:
return False
else:
return False
else:
return False
def lateequalizerotherteam(homeaway, gamecourselist, idx):
# First goal can never be an equalizer
if idx == 0:
return False
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
#And no goals have been made after this goal
afterevents = gamecourselist[idx + 1:]
for event in afterevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# There should have been a lead of 1 goal for the focus team for this goal to be the equalizer
if abs(focusgoals - othergoals) == 1:
# And this goal should be a goal that brings the difference to zero
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 0:
#And the minute should be at least the 80th to be a late equalizer
if gamecourselist[idx]['minute'] >= 80:
#And the goal should be made by the focus team
if gamecourselist[idx]['team'] != homeaway:
return True
else:
return False
else:
return False
else:
return False
else:
return False
def latelossfocusteam(homeaway, gamecourselist, idx):
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
#And no goals have been made after this goal
afterevents = gamecourselist[idx + 1:]
for event in afterevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# The standings should be equal for this goal to be a late defeat, and goals should be scored
if (abs(focusgoals - othergoals) == 0) and (focusgoals != 0) and (othergoals != 0):
# And this goal should be a goal that gives the other team a one goal lead
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 1:
#And the minute should be at least the 80th to be a late equalizer
if gamecourselist[idx]['minute'] >= 80:
#And the goal should be made by the other team
if gamecourselist[idx]['team'] != homeaway:
return True
else:
return False
else:
return False
else:
return False
else:
return False
def latewinfocusteam(homeaway, gamecourselist, idx):
# Assign who is the focus team and who the other
if homeaway == 'home':
focus = homeaway
other = 'away'
else:
focus = homeaway
other = 'home'
#And no goals have been made after this goal
afterevents = gamecourselist[idx + 1:]
for event in afterevents:
if (event['event'] == 'regular goal') or (event['event'] == 'penalty goal') or (event['event'] == 'own goal'):
return False
# Get the score before the current goal
focusgoals = 0
othergoals = 0
beforeevents = gamecourselist[:idx]
for event in beforeevents:
if (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == focus)) or (
(event['event'] == 'own goal') and (event['team'] == other)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
elif (((event['event'] == 'regular goal') or (event['event'] == 'penalty goal')) and (event['team'] == other)) or (
(event['event'] == 'own goal') and (event['team'] == focus)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
# The standings should be equal for this goal to be a late win
if (abs(focusgoals - othergoals) == 0) and (focusgoals != 0) and (othergoals != 0):
# And this goal should be a goal that gives the focus team a one goal lead
if ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == focus)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == other)):
focusgoals += 1
elif ((gamecourselist[idx]['event'] == 'regular goal') and (gamecourselist[idx]['team'] == other)) or ((event['event'] == 'penalty goal') and (gamecourselist[idx]['team'] == other)) or ((gamecourselist[idx]['event'] == 'own goal') and (gamecourselist[idx]['team'] == focus)):
othergoals += 1
if abs(focusgoals - othergoals) == 1:
#And the minute should be at least the 80th to be a late equalizer
if gamecourselist[idx]['minute'] >= 80:
#And the goal should be made by the other team
if gamecourselist[idx]['team'] == homeaway:
return True
else:
return False
else:
return False
else:
return False
else:
return False
def focusteamredcard(gamestatisticslist, homeaway):
focusteamreds = 0
otherteamreds = 0
for idx, event in enumerate(gamestatisticslist):
if (event['event'] == 'red card') or (event['event'] == 'twice yellow'):
if event['team'] == homeaway:
focusteamreds += 1
else:
otherteamreds += 1
if (focusteamreds > 0) and (otherteamreds == 0):
return True
else:
return False
def otherteamredcard(gamestatisticslist, homeaway):
focusteamreds = 0
otherteamreds = 0
for idx, event in enumerate(gamestatisticslist):
if (event['event'] == 'red card') or (event['event'] == 'twice yellow'):
if event['team'] == homeaway:
focusteamreds += 1
else:
otherteamreds += 1
if (focusteamreds == 0) and (otherteamreds > 0):
return True
else:
return False
def comebacklossfocus(gamecourselist, homeaway):
differencelist = []
focusgoals = 0
othergoals = 0
#Get the goals for the focus team and other team
for eventidx, event in enumerate(gamecourselist):
if ((event['event'] == 'regular goal') and (event['team'] == homeaway)) or ((event['event'] == 'penalty goal') and (event['team'] == homeaway)) or ((event['event'] == 'own goal') and (event['team'] != homeaway)):
if 'player' in event:
focusgoals += 1
else:
focusgoals += 2
if ((event['event'] == 'regular goal') and (event['team'] != homeaway)) or ((event['event'] == 'penalty goal') and (event['team'] != homeaway)) or ((event['event'] == 'own goal') and (event['team'] == homeaway)):
if 'player' in event:
othergoals += 1
else:
othergoals += 2
#And see how much more (or less) goals the other team has made
differencelist.append(othergoals - focusgoals)
try:
maxdifference = max(differencelist)
except ValueError:
return False
#If the difference at one point was 3 goals or more in favor of the other team, it opens up the possibility for a comeback
if maxdifference >= 3:
#Get the differences after the maximum difference
maxidx = differencelist.index(max(differencelist))
afterdifference = differencelist[maxidx + 1:]
#If there were changes to the score after the maximum difference
if len(afterdifference) > 0:
#And the difference was at one point one goal or less, there was a comeback
if min(afterdifference) <= 1:
return True
else:
return False
else:
return False
else:
return False
def comebackother(gamecourselist, homeaway):
differencelist = []
focusgoals = 0
othergoals = 0
#Get the goals for the focus team and other team
for eventidx, event in enumerate(gamecourselist):
if ((event['event'] == 'regular goal') and (event['team'] == homeaway)) or ((event['event'] == 'penalty goal') and (event['team'] == homeaway)) or ((event['event'] == 'own goal') and (event['team'] != homeaway)):
if 'player' in event: