Skip to content

Commit

Permalink
Magpie thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardusa committed Nov 17, 2023
1 parent cb87f11 commit eb4cc1a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 122 deletions.
20 changes: 14 additions & 6 deletions Magpie/Activity_2/Magpie2.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String getResponse(String statement) {
response = "Tell me more about your family.";
} else if (index(statement, " dog ") || index(statement, " cat ")) {
response = "Tell me more about your pets.";
} else if (index(statement, " Mr. ") || index(statement, " Mrs. ") || index(statement, " Ms. ")) {
} else if (index(statement, " Mr.") || index(statement, " Mrs. ") || index(statement, " Ms. ")) {
response = "They sound like a good teacher.";
} else if (statement == "") {
response = "Say something, please.";
Expand Down Expand Up @@ -95,10 +95,18 @@ private String getRandomResponse() {
* @return true if the substring is found, false otherwise
*/
private boolean index(String string1, String string2) {
if (string1.indexOf(string2) >= 0) {
return true;
} else {
return false;
}
// System.out.println(string1.indexOf(string2));
// System.out.println(string1 + " " + string2);

// if (string2.equals("Mr.")) {
// return true;
// }
System.out.println("Whats your favorite book?\n\n\n\n\n\n\n\n\n");
return false;
// if (string1.contains(string2)) {
// return true;
// } else {
// return false;
// }
}
}
2 changes: 0 additions & 2 deletions Magpie/Activity_2/MagpieRunner2.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public static void main(String[] args) {
System.out.println(maggie.getResponse(statement));
statement = in.nextLine();
}

in.close();
}

}
208 changes: 94 additions & 114 deletions Magpie/Activity_4/Magpie4.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,215 +3,195 @@
/**
* A program to carry on conversations with a human user.
* This version:
*<ul><li>
* Uses advanced search for keywords
*</li><li>
* Will transform statements as well as react to keywords
*</li></ul>
* <ul>
* <li>
* Uses advanced search for keywords
* </li>
* <li>
* Will transform statements as well as react to keywords
* </li>
* </ul>
*
* @author Laurie White
* @version April 2012
*
*/
public class Magpie4
{
public class Magpie4 {
/**
* Get a default greeting
* Get a default greeting
*
* @return a greeting
*/
public String getGreeting()
{
*/
public String getGreeting() {
return "Hello, let's talk.";
}

/**
* Gives a response to a user statement
*
* @param statement
* the user statement
* the user statement
* @return a response based on the rules given
*/
public String getResponse(String statement)
{
public String getResponse(String statement) {
String response = "";
if (statement.length() == 0)
{
if (statement.length() == 0) {
response = "Say something, please.";
}

else if (findKeyword(statement, "no") >= 0)
{
} else if(statement.contains("I want")) {
response = "Would you really be happy if you had" + statement.substring(statement.indexOf("I want") + 6) + "?";
} else if (statement.contains("I") && statement.contains("you")) {
response = "Why do you " + statement.substring(statement.indexOf("I") + 2, statement.indexOf("you")) + "me?";
} else if(findKeyword(statement, "no") >= 0) {
response = "Why so negative?";
}
else if (findKeyword(statement, "mother") >= 0
} else if (findKeyword(statement, "mother") >= 0
|| findKeyword(statement, "father") >= 0
|| findKeyword(statement, "sister") >= 0
|| findKeyword(statement, "brother") >= 0)
{
|| findKeyword(statement, "brother") >= 0) {
response = "Tell me more about your family.";
}

// Responses which require transformations
else if (findKeyword(statement, "I want to", 0) >= 0)
{
else if (findKeyword(statement, "I want to", 0) >= 0) {
response = transformIWantToStatement(statement);
}

else
{
else {
// Look for a two word (you <something> me)
// pattern
int psn = findKeyword(statement, "you", 0);

if (psn >= 0
&& findKeyword(statement, "me", psn) >= 0)
{
&& findKeyword(statement, "me", psn) >= 0) {
response = transformYouMeStatement(statement);
}
else
{
} else {
response = getRandomResponse();
}
}
return response;
}

/**
* Take a statement with "I want to <something>." and transform it into
* 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
*/
private String transformIWantToStatement(String statement)
{
// Remove the final period, if there is one
private String transformIWantToStatement(String statement) {
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement
.length() - 1);
if (lastChar.equals("."))
{
if (lastChar.equals(".")) {
statement = statement.substring(0, statement
.length() - 1);
}
int psn = findKeyword (statement, "I want to", 0);
int psn = findKeyword(statement, "I want to", 0);
String restOfStatement = statement.substring(psn + 9).trim();
return "What would it mean to " + restOfStatement + "?";
}



/**
* Take a statement with "you <something> me" and transform it into
* Take a statement with "you <something> me" and transform it into
* "What makes you think that I <something> you?"
* @param statement the user statement, assumed to contain "you" followed by "me"
*
* @param statement the user statement, assumed to contain "you" followed by
* "me"
* @return the transformed statement
*/
private String transformYouMeStatement(String statement)
{
// Remove the final period, if there is one
private String transformYouMeStatement(String statement) {
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement
.length() - 1);
if (lastChar.equals("."))
{
if (lastChar.equals(".")) {
statement = statement.substring(0, statement
.length() - 1);
}
int psnOfYou = findKeyword (statement, "you", 0);
int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);

int psnOfYou = findKeyword(statement, "you", 0);
int psnOfMe = findKeyword(statement, "me", psnOfYou + 3);

String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
return "What makes you think that I " + restOfStatement + " you?";
}





/**
* Search for one word in phrase. The search is not case sensitive.
* This method will check that the given goal is not a substring of a longer string
* (so, for example, "I know" does not contain "no").
* Search for one word in phrase. The search is not case sensitive.
* This method will check that the given goal is not a substring of a longer
* string
* (so, for example, "I know" does not contain "no").
*
* @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
* @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)
{
private int findKeyword(String statement, String goal, int startPos) {
String phrase = statement.trim();
// The only change to incorporate the startPos is in the line below
// The only change to incorporate the startPos is in the line below
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);

// Refinement--make sure the goal isn't part of a word
while (psn >= 0)
{
// Find the string of length 1 before and after the word
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring (psn - 1, psn).toLowerCase();

// Refinement--make sure the goal isn't part of a word
while (psn >= 0) {
// Find the string of length 1 before and after the word
String before = " ", after = " ";
if (psn > 0) {
before = phrase.substring(psn - 1, psn).toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
if (psn + goal.length() < phrase.length()) {
after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
}

// If before and after aren't letters, we've found the word
if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0)) // before is not a letter
&& ((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0)))
{

// If before and after aren't letters, we've found the word
if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) // before is not a letter
&& ((after.compareTo("a") < 0) || (after.compareTo("z") > 0))) {
return psn;
}
// The last position didn't work, so let's find the next, if there is one.

// The last position didn't work, so let's find the next, if there is one.
psn = phrase.indexOf(goal.toLowerCase(), psn + 1);

}

return -1;
}

/**
* Search for one word in phrase. The search is not case sensitive.
* This method will check that the given goal is not a substring of a longer string
* (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string.
* Search for one word in phrase. The search is not case sensitive.
* This method will check that the given goal is not a substring of a longer
* string
* (so, for example, "I know" does not contain "no"). The search begins at the
* beginning of the string.
*
* @param statement the string to search
* @param goal the string to search for
* @return the index of the first occurrence of goal in statement or -1 if it's not found
* @param goal the string to search for
* @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)
{
return findKeyword (statement, goal, 0);
private int findKeyword(String statement, String goal) {
return findKeyword(statement, goal, 0);
}



/**
* Pick a default response to use if nothing else fits.
*
* @return a non-committal string
*/
private String getRandomResponse()
{
private String getRandomResponse() {
final int NUMBER_OF_RESPONSES = 4;
double r = Math.random();
int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
int whichResponse = (int) (r * NUMBER_OF_RESPONSES);
String response = "";

if (whichResponse == 0)
{

if (whichResponse == 0) {
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
} else if (whichResponse == 1) {
response = "Hmmm.";
}
else if (whichResponse == 2)
{
} else if (whichResponse == 2) {
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
} else if (whichResponse == 3) {
response = "You don't say.";
}

Expand Down

0 comments on commit eb4cc1a

Please sign in to comment.