Spaces:
Runtime error
Runtime error
| import re | |
| from fpl_client import FPLClient | |
| import logging | |
| # Initialize the FPL client | |
| fpl_client = FPLClient() | |
| # Get the list of teams | |
| teams = fpl_client.get_teams() | |
| def extract_player_name(query): | |
| # Updated regex to include accented characters and names with periods | |
| match = re.search(r"(\b[A-Z][a-zA-Z]*[a-zA-Z.]*\b(?:\s[A-Z][a-zA-Z]*[a-zA-Z.]*\b)?)", query, re.UNICODE) | |
| logging.info(f"Extracted player name: {match}") | |
| return match.group(1) if match else None | |
| def extract_from_quotes(query): | |
| match = re.search(r'"([^"]*)"', query) | |
| logging.info(f"Extracted text from quotes: {match}") | |
| return match.group(1) if match else None | |
| def extract_team_name(query): | |
| logging.info(f"Extracting team name from query: {query}") | |
| logging.debug(f"Teams: {teams}") | |
| # Add your logic to extract team names, could use a list of teams | |
| for team in teams: | |
| if team.lower() in query.lower(): | |
| return team | |
| return None | |
| def process_query(query, fpl_client): | |
| if "stats" in query.lower(): | |
| player_name = extract_player_name(query) | |
| if player_name: | |
| stats = fpl_client.get_player_stats(player_name) | |
| if stats == "Player not found": | |
| logging.info("Player name not found in the query. \nTrying to extract from quotes.") | |
| player_name = extract_from_quotes(query) | |
| if player_name == None: | |
| return 'Player name not found in the query. \nTry "Player-Name" in double-quotes' | |
| stats = fpl_client.get_player_stats(player_name) | |
| return stats | |
| return 'Player name not found in the query. \nTry "Player-Name" in double-quotes' | |
| if "team" in query.lower() or "players" in query.lower(): | |
| # Extract the team name from the query | |
| team_name = extract_team_name(query) | |
| # Check if a valid team name was found | |
| if team_name: | |
| # Get the list of players for the specified team | |
| players = fpl_client.get_team_players(team_name) | |
| # Return the formatted list of players from the specified team | |
| return f"Players from {team_name} are:\n" + "\n".join(players) | |
| # If no valid team name was found, enumerate the teams and format them as a numbered list | |
| enumerated_teams = [f"{index + 1}. {team}" for index, team in enumerate(teams)] | |
| return "It sound like you need the latest EPL team list: " + "\n" + "\n".join(enumerated_teams) | |
| if "injuries" in query.lower(): | |
| injuries = fpl_client.list_injuries() | |
| formatted_injuries = "\n\n".join([f"Player: {name} Injury: {news}" for name, news in injuries]) | |
| injury_count = len(injuries) | |
| return f"There are {injury_count} players with injuries:\n\n{formatted_injuries}" | |
| if "dream" in query.lower(): | |
| dt = fpl_client.dream_team() | |
| if dt == []: | |
| return "Dream Team not available" | |
| return "Dream Team is:\n" + "\n".join(dt) | |
| return "I'm not sure how to help with that." | |