<commit_before>/**
 * A program to carry on conversations with a human user.
 */
import java.util.Scanner;
public class Magpie
{
    /**
     * Gives a response to a user statement
     *
     * @param statement - the user statement
     * @return a response based on the rules given
     */
    public String getResponse(String statement)
    {
        if (statement.length() == 0)
        {
            return "Say something, please.";
        }

        if (findKeyword(statement, "no") >= 0)
        {
            return "Why so negative?";
        }

        if (findKeyword(statement, "mother") >= 0
                || findKeyword(statement, "father") >= 0
                || findKeyword(statement, "sister") >= 0
                || findKeyword(statement, "brother") >= 0)
        {
            return "Tell me more about your family.";
        }

        /*** --------------- Activity 2 --------------
         * Add new responses here.
         *
         * 1. Add response for: "cat" or "dog"
         * 2. Add response for: teacher name "Haas"
         * 3. Handle empty string
         *
         ***/

        /** <<< Add your code for Activity 2 here >>> **/

        if (findKeyword(statement, "dog") >= 0 || findKeyword(statement, "cat") >= 0) {
            return "Tell me more about your pets.";
        }
        if (findKeyword(statement, "Haas") >= 0) {
            return "He sounds like a good teacher.";
        }
        if (statement.trim().length() == 0) {
            return "Say something, please.";
        }

        /***
         * Responses that Transform Statements
         */
        if (findKeyword(statement, "I want to") >= 0)
        {
            return transformIWantToStatement(statement);
        }

        if (findKeyword(statement, "I want") >= 0)
        {
            return transformIWantStatement(statement);
        }

        if (findKeyword(statement, "you") != -1 &&
                findKeyword(statement, "you") < findKeyword(statement, "me"))
        {
            return transformYouMeStatement(statement);
        }

        if (findKeyword(statement, "I") != -1 &&
                findKeyword(statement, "I") < findKeyword(statement, "you"))
        {
            return transformIYouStatement(statement);
        }

        return getRandomResponse();
    }

    /*** --------------- Activity 2 --------------
     * Pick a random response to use if nothing else fits.
     * @return a random string from the following options:
     *    "Interesting, tell me more."
     *    "Hmmm."
     *    "Do you really think so?"
     *    "You don't say."
     */
    private String getRandomResponse()
    {
        /** <<< Add your code for Activity 2 here >>> **/
        String[] responses = {
                "Interesting, tell me more.",
                "Hmmm.",
                "Do you really think so?",
                "You don't say.",
                "How does that make you feel?"
        };
        return responses[(int) Math.floor(Math.random() * responses.length)];
    }

    /**
     * runs the method: findKeyword passing in a default value of 0
     */
    private int findKeyword(String statement, String goal)
    {
        return findKeyword (statement, goal, 0);
    }

    /**
     * This method returns the index of a given goal in a longer string.
     *
     * @param statement the string to search
     * @param goal the string to search for
     * @param startPos the character of the string to begin the search at
     * @return the index of the first occurrence of goal in statement or -1 if it's not found
     */
    private int findKeyword(String statement, String goal, int startPos)
    {
        /*** --------------- Activity 3 --------------
         * Improve this method so that it only detects a keyword if it
         * is not within another word.
         * A keyword should not have letters around it.
         */

        /** convert to lower case **/
        statement = statement.toLowerCase();
        goal = goal.toLowerCase();

        /** <<< Add your code for Activity 3 here >>> **/
        int tentativeIndex = statement.indexOf(goal, startPos);
        if (tentativeIndex == -1) return -1;

        String letters = "abcdefghijklmnopqrstuvwxyz";
        // Is there a letter before it?
        boolean isLetterBefore = false;
        if (tentativeIndex > 0)
             isLetterBefore = letters.contains(statement.substring(tentativeIndex-1, tentativeIndex));

        // Is there a letter after the end?
        String charAfter;
        boolean isLetterAfter = false;
        if (tentativeIndex + goal.length() < statement.length()) {
            charAfter = statement.substring(tentativeIndex + goal.length(), tentativeIndex + goal.length() + 1);
            isLetterAfter = letters.contains(charAfter);
        }

        if (tentativeIndex == -1 || isLetterBefore || isLetterAfter) return -1;
        else return tentativeIndex;
    }

    /** --- This method is complete!!! --
     * Take a statement with "I want to <something>." and transform it into
     * "What would it mean to <something>?"
     * @param statement the user statement, assumed to contain "I want to"
     * @return the transformed statement
     */<commit_msg>Allow finding matches beyond the first<commit_after>/**
 * A program to carry on conversations with a human user.
 */
import java.util.Scanner;
public class Magpie
{
    /**
     * Gives a response to a user statement
     *
     * @param statement - the user statement
     * @return a response based on the rules given
     */
    public String getResponse(String statement)
    {
        if (statement.length() == 0)
        {
            return "Say something, please.";
        }

        if (findKeyword(statement, "no") >= 0)
        {
            return "Why so negative?";
        }

        if (findKeyword(statement, "mother") >= 0
                || findKeyword(statement, "father") >= 0
                || findKeyword(statement, "sister") >= 0
                || findKeyword(statement, "brother") >= 0)
        {
            return "Tell me more about your family.";
        }

        /*** --------------- Activity 2 --------------
         * Add new responses here.
         *
         * 1. Add response for: "cat" or "dog"
         * 2. Add response for: teacher name "Haas"
         * 3. Handle empty string
         *
         ***/

        /** <<< Add your code for Activity 2 here >>> **/

        if (findKeyword(statement, "dog") >= 0 || findKeyword(statement, "cat") >= 0) {
            return "Tell me more about your pets.";
        }
        if (findKeyword(statement, "Haas") >= 0) {
            return "He sounds like a good teacher.";
        }
        if (statement.trim().length() == 0) {
            return "Say something, please.";
        }

        /***
         * Responses that Transform Statements
         */
        if (findKeyword(statement, "I want to") >= 0)
        {
            return transformIWantToStatement(statement);
        }

        if (findKeyword(statement, "I want") >= 0)
        {
            return transformIWantStatement(statement);
        }

        if (findKeyword(statement, "you") != -1 &&
                findKeyword(statement, "you") < findKeyword(statement, "me"))
        {
            return transformYouMeStatement(statement);
        }

        if (findKeyword(statement, "I") != -1 &&
                findKeyword(statement, "I") < findKeyword(statement, "you"))
        {
            return transformIYouStatement(statement);
        }

        return getRandomResponse();
    }

    /*** --------------- Activity 2 --------------
     * Pick a random response to use if nothing else fits.
     * @return a random string from the following options:
     *    "Interesting, tell me more."
     *    "Hmmm."
     *    "Do you really think so?"
     *    "You don't say."
     */
    private String getRandomResponse()
    {
        /** <<< Add your code for Activity 2 here >>> **/
        String[] responses = {
                "Interesting, tell me more.",
                "Hmmm.",
                "Do you really think so?",
                "You don't say.",
                "How does that make you feel?"
        };
        return responses[(int) Math.floor(Math.random() * responses.length)];
    }

    /**
     * runs the method: findKeyword passing in a default value of 0
     */
    private int findKeyword(String statement, String goal)
    {
        return findKeyword (statement, goal, 0);
    }

    /**
     * This method returns the index of a given goal in a longer string.
     *
     * @param statement the string to search
     * @param goal the string to search for
     * @param startPos the character of the string to begin the search at
     * @return the index of the first occurrence of goal in statement or -1 if it's not found
     */
    private int findKeyword(String statement, String goal, int startPos)
    {
        /*** --------------- Activity 3 --------------
         * Improve this method so that it only detects a keyword if it
         * is not within another word.
         * A keyword should not have letters around it.
         */

        /** convert to lower case **/
        statement = statement.toLowerCase();
        goal = goal.toLowerCase();
        String letters = "abcdefghijklmnopqrstuvwxyz";

        for (int tentativeIndex=0; tentativeIndex <= statement.length()-goal.length(); tentativeIndex++) {
            if (statement.substring(tentativeIndex, tentativeIndex+goal.length()).equals(goal)) {
                // Is there a letter before it?
                boolean isLetterBefore = false;
                if (tentativeIndex > 0)
                    isLetterBefore = letters.contains(statement.substring(tentativeIndex-1, tentativeIndex));

                // Is there a letter after the end?
                String charAfter;
                boolean isLetterAfter = false;
                if (tentativeIndex + goal.length() < statement.length()) {
                    charAfter = statement.substring(tentativeIndex + goal.length(), tentativeIndex + goal.length() + 1);
                    isLetterAfter = letters.contains(charAfter);
                }

                if (!isLetterBefore && !isLetterAfter) return tentativeIndex;
            }
        }
        return -1;
    }

    /** --- This method is complete!!! --
     * Take a statement with "I want to <something>." and transform it into
     * "What would it mean to <something>?"
     * @param statement the user statement, assumed to contain "I want to"
     * @return the transformed statement
     */
{'fraction_non_alphanumeric': 0.12406085649887302, 'fraction_numerical': 0.005916604057099925, 'mean_word_length': 1.743880443184746, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5860224', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2698, 'n_words': 939}