top of page
Search

Final Project: Code V.2

  • Yavneeka Patel
  • Mar 9, 2015
  • 5 min read

This is the second draft, it allows the computer to randomly choose an answer given a question held in it's array, and ensures that the answers do not get repeated in a row.

Issues with this version that I ran into:

-Was unable to get Spanish to read, even though English would work

Where I'd like to go from here:

-clean up code (add more functions) to make it less messy in the main especially

-allow the computer to not have to take in the exact string to elicit a response

-create a ranking system, that allows computer to choose the best version to submit

-text to speech

-prevent user from inputting the same phrase twice

----------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

import java.util.Vector;

/**

* This class holds the second prototype for the chatterbot

* Goals:

*Have computer respond to certain questions based on user input question

*Have more options on what the computer can respond with

***Randomly choose one of three options

***Ensure response does not repeat

*

* References:

*Minesweeper (CS 101)

*Chatterbot in C++ Tutorial: http://www.instructables.com/id/A-Learning-Chatterbot-in-C/

*

* @author patel22y

*/

public class Chatterbot2 {

/***************************** CLASS PROPERTIES **************************/

//holds the value the user inputs

private static Scanner userInput;

//holds the question, response choices

private static String responseDataBase[][] = {

{

//if asked "Hola":

"Hola",

//respond with one of the following three options:

"¿Cómo te llamas?",

"¿Cómo estás?",

"¡Hola!",

},

{

//if asked "Como estas"

"Cómo estás",

//respond with one of the following three options

"Estoy bien, gracias.",

"¿Cómo crees que soy?",

"¡Buenísimo!"

},

{

//if asked ""where is the bathroom?":

"Dónde está el baño",

//respond with one of the following three options

"El baño está en la biblioteca",

"No tengo ni idea de dónde podría ser.",

"¡No! El baño ha desaparecido"

},

{

//if asked "what do you like?"

"Que te gustan",

//respond with one of the following three options

"Me gusta las piña coladas y quede atrapado en la lluvia",

"Me gusta el tenis. ¿Te gusta el tenis?",

"Me gusta el champán"

}

};

//create a string that holds all punctuation

private final static String puncs = "?!.;,¿¡";

//there is one input

private final static int INPUT_NUM = 1;

//there are three possible responses

private static int RESP_NUM = 3;

/***************************** INSTANCE METHODS **********************/

/**

* Blank constructor

*/

public Chatterbot2() {

//does nothing

}

/***************************** CLASS METHODS **********************/

/**

* Check if there is punctuation at each spot

* @param ch

* @return

*/

private static boolean isPunctuation(char ch) {

//Print out what the punctuation is at each spot:

//System.out.println (puncs.indexOf(ch) != -1);

//return this value

return puncs.indexOf(ch) != -1;

}

/**

* Finds a match in the response data base using the Vector (http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html)

* I wanted to try using the vector because I've never used it before, and I needed something that is able to change it's values

* based on the input (i.e. there will be different responses inside based on what is inputted in)

* @param str

* @return

*/

private static Vector<String> findMatch(String cleanedUpInput) {

//make sure the right input is coming in

//System.out.println ("The str is: " + cleanedUpInput);

//create a vector of type string called result

//instantiate it with 3 spots (for the 3 possible responses)

Vector<String> result = new Vector<String>(RESP_NUM);

//for loop that goes through and adds each possible response for an input into the new Vector array thing

for(int i = 0; i < responseDataBase.length; ++i) {

//print out what the value in the data base is at spot [i][0]

//System.out.println ("the value in the data base is " + responseDataBase[i][0]);

//if the input equals the first thing each of the response data base array portions

//System.out.println ("does this match the input " + responseDataBase[i][0].equalsIgnoreCase(cleanedUpInput));

if(responseDataBase[i][0].equalsIgnoreCase(cleanedUpInput)) {

for(int j = INPUT_NUM; j <= RESP_NUM; ++j) {

//go through this portion and add in the remaining response choices into the result vector

result.add(responseDataBase[i][j]);

}

//if we make it in here, that means a match has been found so we can stop

//we don't have to go through the rest of the array

break;

}

}

//return the result vector with the possible response choices added to it.

return result;

}

/**

* Removes punctuation & extra spaces

* @param originalInput

* @return

**/

private static String cleanString(String originalInput) {

//Create an empty string that will hold the end cleaned up version of the input

String temp = "";

//variable of the previous character is instantiated to 0

char prevChar = 0;

//for loop that goes through each character of the originalInput and gets rid of punctuation & spaces

for(int i = 0; i < originalInput.length(); ++i) {

//if the character i in the originalInput is a space and the previousChar is a space

if(originalInput.charAt(i) == ' ' && prevChar == ' ' ) {

//skip the current iteration of the loop

continue;

}

//if there isn't punctuation in the originalInput at the i spot

else if(!isPunctuation(originalInput.charAt(i))) {

//add the character to the temporary string

temp += (originalInput.charAt(i));

}

//the previous character equals the character at the i spot in this iteration

prevChar = originalInput.charAt(i);

}

//return the cleaned up version of the input

return temp;

}

/**

* The main function

* Must throw exception because I am taking in user input

* @param args

*/

public static void main(String[] args)throws Exception {

//create a blank string

String responseString = "";

int counter = 0;

//create a boolean called isRunning that is initialized to true

//i want to use this boolean to have my loop continue as long as the user does not type "QUIT"

boolean isRunning = true;

//while isRunning = true

while(isRunning) {

//start by printing out the instruction line, and the >>> to indicate the user response goes there

System.out.print("---------------" + "\n" + "Escriba algo | Write something:" + "\n" + ">>>");

//Creates the userInput which is a new Scanner

userInput = new Scanner (System.in);

//Sets the input String equal to the user input

String input = userInput.nextLine();

//clean up the input

input = cleanString(input);

//set the vector to length of respNum

Vector <String> output = new Vector <String> (RESP_NUM);

//set the output equal to the possible response choices

output = findMatch(input);

//if the user input says "I Quit" (Renuncio in Spanish)

if(input.equals("Renuncio")){

//Have the computer acknowledge that they are leaving

System.out.println("¡Hasta luego!");

//stop the while loop by setting isRunning to false

isRunning = false;

}

//else if the output vector was unable to find the input and hold the responses, output would equal 0

else if(output.size() == 0) {

//print out to user "I don't understand"

System.out.println("No intiendo.");

}

//otherwise

else {

//just keep going!

}

//add the element at the random number to the output (loop around the indices)

responseString = output.elementAt(counter%RESP_NUM);

counter++;

//Print out the response string

System.out.println("> " + responseString);

}

}

}


 
 
 

Recent Posts

See All
The End

So we've made it to the very end. Through all the ups and downs and we're all still standing. It was an amazing semester and I made some...

 
 
 
Final Code: Dictionary

/** *Java Speech Grammar Format (JSGF) file *This file holds all the words that the program will recognize **/...

 
 
 
Final Code: Dialogue File

/* * Copyright 2013 Carnegie Mellon University. * Portions Copyright 2004 Sun Microsystems, Inc. * Portions Copyright 2004 Mitsubishi...

 
 
 

Comments


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
  • Facebook Classic
  • Twitter Classic
  • Google Classic
  • RSS Classic

© 2023 by TOKYO DESIGN. Proudly created with Wix.com

bottom of page