/**
 * PronounReplacer - Grammatical pronoun transformation
 * This example demonstrates how to swap pronouns to produce
 * more natural responses, just like ELIZA does.
 *
 * Part III of the book "Introduction to Programming in Java"
 */

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

public class PronounReplacer {

    private static final Map<String, String> PRONOUN_MAP = Map.of(
        "i", "you",
        "me", "you",
        "my", "your",
        "am", "are",
        "you", "I",
        "your", "my",
        "are", "am"
    );

    private static final Pattern PRONOUN_PATTERN =
        Pattern.compile("\\b(i|me|my|am|you|your|are)\\b");

    /**
     * Transforms first-person pronouns into second-person pronouns and vice versa
     * in a single pass so swapped words are not replaced back.
     * @param phrase The phrase to transform
     * @return The phrase with transformed pronouns
     */
    public static String replacePronouns(String phrase) {
        String lowerPhrase = phrase.toLowerCase();
        Matcher matcher = PRONOUN_PATTERN.matcher(lowerPhrase);
        return matcher.replaceAll(match -> PRONOUN_MAP.get(match.group(1)));
    }

    public static void main(String[] args) {
        System.out.println("Original:    'I want my coffee'");
        System.out.println("Transformed: '" + replacePronouns("I want my coffee") + "'");
        System.out.println();

        System.out.println("Original:    'You have your pen'");
        System.out.println("Transformed: '" + replacePronouns("You have your pen") + "'");
        System.out.println();

        System.out.println("Original:    'My mom is nice'");
        System.out.println("Transformed: '" + replacePronouns("My mom is nice") + "'");
        System.out.println();

        System.out.println("Original:    'I am learning Java'");
        System.out.println("Transformed: '" + replacePronouns("I am learning Java") + "'");
        System.out.println();

        System.out.println("Original:    'You are helping me'");
        System.out.println("Transformed: '" + replacePronouns("You are helping me") + "'");
    }
}
