/**
 * IntentClassifier - Intent classifier
 * This example demonstrates how to classify messages into different intents
 * using regular expressions.
 *
 * Part III of the book "Introduction to Programming in Java"
 */

import java.util.*;
import java.util.regex.*;

public class IntentClassifier {

    // Enum representing the supported intents
    public enum Intent {
        GREETING,         // Greetings
        GOODBYE,          // Farewells
        THANKS,           // Expressions of gratitude
        WEATHER_QUESTION, // Weather questions
        FOOD_ORDER,       // Food orders
        FLIGHT_BOOKING,   // Flight bookings
        UNKNOWN           // Unknown intent
    }

    // Map of regex patterns for each intent
    private static final Map<Intent, List<Pattern>> INTENT_PATTERNS = new EnumMap<>(Intent.class);

    static {
        // Greetings
        INTENT_PATTERNS.put(Intent.GREETING, Arrays.asList(
            Pattern.compile("\\b(hi|hello|good morning|good afternoon|good evening|hey|what's up)\\b", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^hi!?$", Pattern.CASE_INSENSITIVE)
        ));

        // Farewells
        INTENT_PATTERNS.put(Intent.GOODBYE, Arrays.asList(
            Pattern.compile("\\b(bye|goodbye|see you|farewell|cya)\\b", Pattern.CASE_INSENSITIVE),
            Pattern.compile("^see ya( later)?!?$", Pattern.CASE_INSENSITIVE)
        ));

        // Expressions of gratitude
        INTENT_PATTERNS.put(Intent.THANKS, Arrays.asList(
            Pattern.compile("\\b(thanks|thank you|thx|appreciate it|cheers)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Weather questions
        INTENT_PATTERNS.put(Intent.WEATHER_QUESTION, Arrays.asList(
            Pattern.compile("\\b(weather|forecast|rain|sunny|cold|hot|temperature)\\b", Pattern.CASE_INSENSITIVE),
            Pattern.compile("how is the weather|what's the weather", Pattern.CASE_INSENSITIVE)
        ));

        // Food orders
        INTENT_PATTERNS.put(Intent.FOOD_ORDER, Arrays.asList(
            Pattern.compile("\\b(want|order|like|get)\\b.*\\b(food|eat|pizza|burger|hamburger|sushi|sandwich)\\b", Pattern.CASE_INSENSITIVE),
            Pattern.compile("\\b(hungry|eat|pizza|burger)\\b", Pattern.CASE_INSENSITIVE)
        ));

        // Flight bookings
        INTENT_PATTERNS.put(Intent.FLIGHT_BOOKING, Arrays.asList(
            Pattern.compile("\\b(book|buy|ticket|flight|airplane|airline|fly)\\b", Pattern.CASE_INSENSITIVE)
        ));
    }

    /**
     * Classifies a message into an intent.
     * @param message The user's message
     * @return The classified intent
     */
    public static Intent classify(String message) {
        String normalizedMessage = message.toLowerCase().trim();

        for (Map.Entry<Intent, List<Pattern>> entry : INTENT_PATTERNS.entrySet()) {
            Intent intent = entry.getKey();
            List<Pattern> patterns = entry.getValue();

            for (Pattern pattern : patterns) {
                if (pattern.matcher(normalizedMessage).find()) {
                    return intent;
                }
            }
        }

        return Intent.UNKNOWN;
    }

    public static void main(String[] args) {
        System.out.println("Classifying: 'Hello, how are you?'");
        System.out.println("  Intent: " + classify("Hello, how are you?"));

        System.out.println("\nClassifying: 'See you later!'");
        System.out.println("  Intent: " + classify("See you later!"));

        System.out.println("\nClassifying: 'Thanks for your help'");
        System.out.println("  Intent: " + classify("Thanks for your help"));

        System.out.println("\nClassifying: 'What's the weather like today?'");
        System.out.println("  Intent: " + classify("What's the weather like today?"));

        System.out.println("\nClassifying: 'I want a pizza'");
        System.out.println("  Intent: " + classify("I want a pizza"));

        System.out.println("\nClassifying: 'I need to book a flight'");
        System.out.println("  Intent: " + classify("I need to book a flight"));

        System.out.println("\nClassifying: 'What is the capital of France?'");
        System.out.println("  Intent: " + classify("What is the capital of France?"));
    }
}
