Datasets:
ashishu007
commited on
Commit
•
1d8b647
1
Parent(s):
bba3b53
added linear input option
Browse files- sportsett_basketball.py +112 -2
sportsett_basketball.py
CHANGED
@@ -18,6 +18,7 @@
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
|
|
21 |
|
22 |
import datasets
|
23 |
|
@@ -61,9 +62,8 @@ _URLs = {
|
|
61 |
"test": "test.jsonl"
|
62 |
}
|
63 |
|
64 |
-
|
65 |
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
66 |
-
class
|
67 |
"""SportSett:Basketball datatset for Data-to-Text Generation."""
|
68 |
|
69 |
VERSION = datasets.Version("1.1.0")
|
@@ -519,6 +519,7 @@ class SportSettBasketball(datasets.GeneratorBasedBuilder):
|
|
519 |
}
|
520 |
},
|
521 |
"summaries": datasets.Sequence(datasets.Value("string")),
|
|
|
522 |
}
|
523 |
)
|
524 |
return datasets.DatasetInfo(
|
@@ -566,6 +567,114 @@ class SportSettBasketball(datasets.GeneratorBasedBuilder):
|
|
566 |
),
|
567 |
]
|
568 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
569 |
def _generate_examples(
|
570 |
self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
571 |
):
|
@@ -583,4 +692,5 @@ class SportSettBasketball(datasets.GeneratorBasedBuilder):
|
|
583 |
"game": data["game"],
|
584 |
"teams": data["teams"],
|
585 |
"summaries": data["summaries"],
|
|
|
586 |
}
|
|
|
18 |
import csv
|
19 |
import json
|
20 |
import os
|
21 |
+
from readline import parse_and_bind
|
22 |
|
23 |
import datasets
|
24 |
|
|
|
62 |
"test": "test.jsonl"
|
63 |
}
|
64 |
|
|
|
65 |
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
66 |
+
class SportsettBasketball(datasets.GeneratorBasedBuilder):
|
67 |
"""SportSett:Basketball datatset for Data-to-Text Generation."""
|
68 |
|
69 |
VERSION = datasets.Version("1.1.0")
|
|
|
519 |
}
|
520 |
},
|
521 |
"summaries": datasets.Sequence(datasets.Value("string")),
|
522 |
+
"linearized_input": datasets.Value("string")
|
523 |
}
|
524 |
)
|
525 |
return datasets.DatasetInfo(
|
|
|
567 |
),
|
568 |
]
|
569 |
|
570 |
+
def sort_players_by_pts(self, entry, type='HOME'):
|
571 |
+
"""
|
572 |
+
Sort players by points and return the indices sorted by points
|
573 |
+
bs --> [{'pts': 10}, {'pts': 30}, {'pts': 35}, {'pts': 5}]
|
574 |
+
return --> [2, 1, 0, 3]
|
575 |
+
"""
|
576 |
+
all_pts = [int(item['PTS']) for item in entry['teams'][type.lower()]['box_score']]
|
577 |
+
all_pts1 = [[item, idx] for idx, item in enumerate(all_pts)]
|
578 |
+
all_pts1.sort()
|
579 |
+
all_pts1.reverse()
|
580 |
+
return [item[1] for item in all_pts1]
|
581 |
+
|
582 |
+
def get_one_player_data(self, player_stats, team_name, rank):
|
583 |
+
"""
|
584 |
+
player_line = "<PLAYER> %s <TEAM> %s <POS> %s <RANK> %s <MIN> %d <PTS> %d <FG> %d %d %d <FG3> %d %d %d \
|
585 |
+
<FT> %d %d %d <REB> %d <AST> %d <STL> %s <BLK> %d <DREB> %d <OREB> %d <TO> %d"
|
586 |
+
"""
|
587 |
+
pos = f'STARTER YES' if player_stats['starter'] == True else f'STARTER NO'
|
588 |
+
player_line = f"<PLAYER> {player_stats['name']} <TEAM> {team_name} <POS> {pos} <RANK> {rank}"
|
589 |
+
player_line = f"{player_line} <MIN> {player_stats['MIN']} <PTS> {player_stats['PTS']} <FG> {player_stats['FGM']} {player_stats['FGA']} {player_stats['FG_PCT']}"
|
590 |
+
player_line = f"{player_line} <FG3> {player_stats['FG3M']} {player_stats['FG3A']} {player_stats['FG3_PCT']}"
|
591 |
+
player_line = f"{player_line} <FT> {player_stats['FTM']} {player_stats['FTA']} {player_stats['FT_PCT']}"
|
592 |
+
player_line = f"{player_line} <REB> {player_stats['TREB']} <AST> {player_stats['AST']} <STL> {player_stats['STL']}"
|
593 |
+
player_line = f"{player_line} <BLK> {player_stats['BLK']} <DREB> {player_stats['DREB']} <OREB> {player_stats['OREB']} <TO> {player_stats['TOV']}"
|
594 |
+
return player_line
|
595 |
+
|
596 |
+
def get_box_score(self, entry, type='HOME', winner='HOME'):
|
597 |
+
bs = entry['teams'][type.lower()]['box_score']
|
598 |
+
team_name = f"{entry['teams'][type.lower()]['place']} {entry['teams'][type.lower()]['name']}"
|
599 |
+
sorted_idx = self.sort_players_by_pts(entry, type)
|
600 |
+
won_or_lost = 'WON' if winner == type else 'LOST'
|
601 |
+
player_lines = [self.get_one_player_data(bs[idx], team_name, f'{won_or_lost}-{rank}') for rank, idx in enumerate(sorted_idx)]
|
602 |
+
return ' '.join(player_lines)
|
603 |
+
|
604 |
+
def get_team_line(self, entry, type='HOME', winner='HOME'):
|
605 |
+
"""
|
606 |
+
team_line = "%s <TEAM> %s <CITY> %s <TEAM-RESULT> %s <TEAM-PTS> %d <WINS-LOSSES> %d %d <QTRS> %d %d %d %d \
|
607 |
+
<TEAM-AST> %d <3PT> %d <TEAM-FG> %d <TEAM-FT> %d <TEAM-REB> %d <TEAM-TO> %d"
|
608 |
+
"""
|
609 |
+
line_score = entry['teams'][type.lower()]['line_score']['game']
|
610 |
+
team_line = f"<TEAM> {entry['teams'][type.lower()]['name']} <CITY> {entry['teams'][type.lower()]['place']}"
|
611 |
+
if winner == type:
|
612 |
+
team_line = f"{team_line} <TEAM-RESULT> won"
|
613 |
+
else:
|
614 |
+
team_line = f"{team_line} <TEAM-RESULT> lost"
|
615 |
+
team_line = f"{team_line} <TEAM-PTS> {line_score['PTS']} <WINS-LOSSES> {entry['teams'][type.lower()]['wins']} {entry['teams'][type.lower()]['losses']}"
|
616 |
+
team_line = f"{team_line} <QTRS> {entry['teams'][type.lower()]['line_score']['Q1']['PTS']} {entry['teams'][type.lower()]['line_score']['Q2']['PTS']}"
|
617 |
+
team_line = f"{team_line} {entry['teams'][type.lower()]['line_score']['Q3']['PTS']} {entry['teams'][type.lower()]['line_score']['Q4']['PTS']}"
|
618 |
+
team_line = f"{team_line} <TEAM-AST> {line_score['AST']} <3PT> {line_score['FG3M']} <TEAM-FG> {line_score['FGM']} <TEAM-FT> {line_score['FTM']}"
|
619 |
+
team_line = f"{team_line} <TEAM-REB> {line_score['TREB']} <TEAM-TO> {line_score['TOV']}"
|
620 |
+
return team_line
|
621 |
+
|
622 |
+
def get_box_and_line_scores(self, entry):
|
623 |
+
"""Get line- & box- scores data for a single game"""
|
624 |
+
|
625 |
+
home_team_pts = entry['teams']['home']['line_score']['game']['PTS']
|
626 |
+
vis_team_pts = entry['teams']['home']['line_score']['game']['PTS']
|
627 |
+
winner = 'HOME' if int(home_team_pts) > int(vis_team_pts) else 'VIS'
|
628 |
+
|
629 |
+
home_team_line = self.get_team_line(entry, type='HOME', winner=winner)
|
630 |
+
vis_team_line = self.get_team_line(entry, type='VIS', winner=winner)
|
631 |
+
line_score = f"<HOME> {home_team_line} <VIS> {vis_team_line}"
|
632 |
+
|
633 |
+
home_box_score = self.get_box_score(entry, type='HOME', winner=winner)
|
634 |
+
vis_box_score = self.get_box_score(entry, type='VIS', winner=winner)
|
635 |
+
box_score = f"{home_box_score} {vis_box_score}"
|
636 |
+
|
637 |
+
return f"{line_score} {box_score}"
|
638 |
+
|
639 |
+
def get_game_data(self, entry):
|
640 |
+
"""Get game data for a single game"""
|
641 |
+
game_date = f"{entry['day']} {entry['month']} {entry['year']}"
|
642 |
+
game_day = entry['dayname']
|
643 |
+
game_stadium = entry['stadium']
|
644 |
+
game_city = entry['city']
|
645 |
+
return f"<GAME-DATE> {game_date} <GAME-DAY> {game_day} <STADIUM> {game_stadium} <CITY> {game_city}"
|
646 |
+
|
647 |
+
def get_next_game_data_of_a_team(self, entry):
|
648 |
+
"""
|
649 |
+
next_game_line = "<NEXT-GAME-DATE> %s <NEXT-GAME-DAY> %s <NEXT-GAME-STADIUM> %s <NEXT-GAME-CITY> %s"
|
650 |
+
"""
|
651 |
+
next_game_date = f"{entry['day']} {entry['month']} {entry['year']}"
|
652 |
+
next_game_is_home = 'yes' if entry['is_home'] == 'True' else 'no'
|
653 |
+
next_game_line = f"<NEXT-DATE> {next_game_date} <NEXT-DAY> {entry['dayname']}"
|
654 |
+
next_game_line = f"{next_game_line} <NEXT-STADIUM> {entry['stadium']} <NEXT-CITY> {entry['city']}"
|
655 |
+
next_game_line = f"{next_game_line} <NEXT-OPPONENT-PLACE> {entry['opponent_place']} <NEXT-OPPONENT-NAME> {entry['opponent_name']}"
|
656 |
+
next_game_line = f"{next_game_line} <NEXT-IS-HOME> {next_game_is_home}"
|
657 |
+
return next_game_line
|
658 |
+
|
659 |
+
def get_next_game_info(self, entry):
|
660 |
+
"""
|
661 |
+
Get next game data for both teams in a game.
|
662 |
+
In case of no next game, all values will be ''.
|
663 |
+
"""
|
664 |
+
home_next_game = self.get_next_game_data_of_a_team(entry['teams']['home']['next_game'])
|
665 |
+
vis_next_game = self.get_next_game_data_of_a_team(entry['teams']['vis']['next_game'])
|
666 |
+
return f"<NEXT-HOME> {home_next_game} <NEXT-VIS> {vis_next_game}"
|
667 |
+
|
668 |
+
def linearize_input(self, entry):
|
669 |
+
"""
|
670 |
+
Linearizes the input to the model.
|
671 |
+
"""
|
672 |
+
game_data = self.get_game_data(entry['game'])
|
673 |
+
box_and_line_scores = self.get_box_and_line_scores(entry)
|
674 |
+
next_game_data = self.get_next_game_info(entry)
|
675 |
+
linearized_input = f"{game_data} {box_and_line_scores} {next_game_data}"
|
676 |
+
return linearized_input.replace(" ", "")
|
677 |
+
|
678 |
def _generate_examples(
|
679 |
self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
680 |
):
|
|
|
692 |
"game": data["game"],
|
693 |
"teams": data["teams"],
|
694 |
"summaries": data["summaries"],
|
695 |
+
"linearized_input": self.linearize_input(data)
|
696 |
}
|