Java Magpie Chatbot Project

AbdulSamad Olagunju / January 07, 2022

5 min read

Tutorials

This is a very simplistic chatbot application. Let’s go over what it’s supposed to do. I have also included the project documents.

In the file Magpie2.java, I begin the file by writing import java.util.*. This imports a bunch of important functions we would like to use. I create a class called Magpie2.

In the class Magpie2, I have some functions.

Let us begin by discussing the first one, GreetingUser().

Aside: Personally, I prefer C++ and Python to Java because of how much more intuitive they are. For example, to input text you have to create an object scan from Scanner. This feels longwinded to me compared to C++ or Python.

Anyways, we print out an introductory message using System.out.println(). We ask the user about the kind of music that they like.

We then receive a response from the user.

The function called OutputResponse() will take in the response from the user, and give several different sentences according to the length of the response.

We rely on the findKeyword() function to find out if the user responds using a certain word.

Take a look at the findKeyword() function and try to figure out what is going on. It is trying to cut up the main string and look for the substring you want.

We can then create a response based on that. In addition, we take a look at the length of the response from the user via the .length() function. This way, we can know if the user responds with a short or long response. We give the user a random question if their response does not include “you” or “I want to.”

The ChooseRandomQuestion() function is what we use to generate a random question for the user, and get their response.

This project gives you a little bit of insight into the kind of technology being used in Natural Language Processing and AI processing of human languages.

Magpie2.java

Magpie2.java
//Abdul-Samad Olagunju
import java.util.*;

public class Magpie2 {

	public String GreetingUser()
	{
		Scanner scan = new Scanner(System.in);
		
		System.out.println("AI: Hi. How are you doing?");
		String response = scan.nextLine();

		response = "AI: Wow, I feel the same. Tell me, Do you like to listen to pop, rock, or rap?";
		return response;
	}
	public String OutputResponse(String userresponse)
	{
		String response = "";
		if( (findKeyword(userresponse, "pop", 0) != -1) || (findKeyword(userresponse, "rock", 0) != -1) || (findKeyword(userresponse, "rap", 0) != -1))
		{
			response = "AI: You have good taste in music. How do you feel when listening to music?";
		}
		else if(userresponse.length() == 0)
		{
			response = "Can you respond to me.";
		}
		else if(findKeyword(userresponse, "I want to", 0) >= 0)
		{
			response = IWantTo(userresponse);
		}
		else if((findKeyword(userresponse, "you", 0) >= 0) && (findKeyword(userresponse, "me", 0) >= 0))
		{
			response = transformYouMe(userresponse);
		}
		else 
		{
			response = ChooseRandomQuestion();
		}
		return response;
	}
	public String ChooseRandomQuestion()
	{
		Random generator = new Random();
		String otherresponse = "";
		int num = generator.nextInt(3) + 1;
		if(num == 1)
		{ 
			otherresponse = "AI: Hmmm. Very Interesting... Is there something about me that you’ve always wanted to know but have never asked?";
		}
		if(num == 2)
		{
			otherresponse = "AI: Ok. Off on a tangent...Do you have any regrets?";
		}
		if(num == 3)
		{
			otherresponse = "AI: That's ok. Random question: How has your life been different than what you’d imagined?";
		}
		return otherresponse;
	}
	public String IWantTo(String userresponse)
	{
		String response;
		userresponse = userresponse.trim();
		String lastChar = userresponse.substring(userresponse.length() - 1);
		if (lastChar.equals("."))
		{
			userresponse = userresponse.substring(0, userresponse.length() - 1);
		}
		int psn = findKeyword (userresponse, "I want to", 0);
		String something = userresponse.substring(psn + 9).trim();
		response = "Would you be really happy if you had " + something + "?";
		return response;
	}
	public String transformYouMe(String userresponse)
	{
		String response = "";
		userresponse = userresponse.trim();
		String lastChar = userresponse.substring(userresponse.length() - 1);
		if (lastChar.equals("."))
		{
			userresponse = userresponse.substring(0, userresponse.length() - 1);
		}
		
		int You = findKeyword (userresponse, "you", 0);
		int Me = findKeyword (userresponse, "me", You + 3);
		
		String something = userresponse.substring(You + 3, Me).trim();
		response = "What makes you think that I " + something + " you?";
		return response;
	}
	private int findKeyword(String statement, String goal, int startPos)
	{
		String phrase = statement.trim();
		int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
		while (psn >= 0)
		{
			String before = " ", after = " ";
			if (psn > 0)
			{
				before = phrase.substring (psn - 1, psn).toLowerCase();
			}
			if (psn + goal.length() < phrase.length())
			{
				after = phrase.substring(psn + goal.length(),
				psn + goal.length() + 1).toLowerCase();
			}
			/* determine the values of psn, before, and after at this point in the method. */
			if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0))
			&&
			((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0)))
			{
				return psn;
			}
			psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
		}
		 return -1;
	}
	}

MagpieRunner.java

In this file, we just start running the Magpie program, calling on the GreetingUser() function and the OutputResponse() function.

Magpie2.java
//Abdul-Samad Olagunju
import java.util.Scanner;
public class MagpieRunner {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Magpie2 maggie = new Magpie2();
		System.out.println(maggie.GreetingUser());
		Scanner scan = new Scanner (System.in);
		String userentry = scan.nextLine();
		int counter = 0;
		 
		while(counter == 0)
		{
			System.out.println(maggie.OutputResponse(userentry));
			userentry = scan.nextLine();
			if(userentry.equals("Goodbye") || userentry.equals("Bye") || userentry.equals("bye"))
			{
				counter = 1;
			}
		}
		
	}
}

Hopefully you got to see some new functions involved in NLP. Thanks for reading!