File size: 3,098 Bytes
86e230c
 
 
 
 
 
 
 
 
 
 
58612e0
 
 
86e230c
 
23152a0
 
 
 
 
86e230c
 
58612e0
86e230c
 
 
 
 
 
 
 
 
 
 
23152a0
 
 
 
 
 
86e230c
23152a0
86e230c
58612e0
 
86e230c
58612e0
 
86e230c
58612e0
86e230c
58612e0
 
 
 
 
 
 
 
86e230c
796b25f
 
 
 
 
8b6f519
 
 
 
 
 
 
86e230c
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
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."