All posts by matttucker1

I am currently a student at University of North Georgia majoring in Mathematics. I’m an avid fan of philosophy and sci-fi. And like to work on engineering projects in my free time.

ChatBot Digital Assistant

download

So… I’ve been working on a new project the past few days, coding a virtual assistant in Python. The idea began simply enough, I could automate certain tasks in my lab space via voice commands. For example, instead of writing lab notes why not dictate them to my computer. Well, it wasn’t long after I started working on the code that the project began to morph into something bigger.

08161702190816170217

It started when I began realizing, that in order to make this work, I need to consider the context of the speech. For example, it is natural for us to say(our end goal, a system which feels natural to use), “find the meaning of mathematics” or “find x squared plus 3 x minus 2, where x is 4;” but, they both have very different and unique expectations. “Find the meaning of mathematics” expects a definition; where as, “find x squared plus 3 x minus 2, where x is 4” expects a solution to “x^(2)+3x-2, x=4”. So here we have two very different expectations, both with their own unique set of rules for how those expectations can/should be addressed. It was with this realization, that I began to see the need to branch out into the world of machine learning. So, I set forth to Google armed with only my curiosity and a rough version of my program.

See so far I could make “computer Google machine learning” work, but without extra coding how could I make, “computer search for…, computer find…,” or even “computer what is…” work? Well, as it turns out, there are a couple different libraries/modules for Python that can help tackle this problem, Natural Language Tool Kit(NLTK) and Tensor Flow to name two. So, I know I can handle this problem now… But I bet I could create a chatbot while I’m at it. More over, wouldn’t it be cool if the chatbot could also recognize you, and respond accordingly? And so, here I am… piecing together code and writing new code in order to make my digital chatbot assistant.

With that said, let’s begin looking at the code.

images


Pt 1, the base code of the command section.
Command_BaseCode.py:

#######################################################################################################################
# [1]
#
# Author:
#           Matt Tucker
#
# Date:
#           8Aug2017-
#
# Description:
#           The following is only part of a chatbot that will analyze visual information to identify users, create user
#           profiles, imitate a personality based on user, while also acting as a virtual assistant via executing
#           a handful of basic commands.
#
#           This particular part handles the commands as they are given to the computer.
# ToDo: Create a context sensitive translation... Ex: "Get the weather." -> "What is the weather?"
#######################################################################################################################


import speech_recognition as sr
from time import ctime
from gtts import gTTS
from playsound import playsound
import webbrowser
import os
import re
import wolframalpha
import wikipedia
import smtplib
import time
import pyowm
from twilio.rest import TwilioRestClient
from PyDictionary import PyDictionary


# Keys and other user specific information
pw = "GMail PW"
add = "GMail Address"
location = "City, State"
signature = '\n' + "Name" + '\n' + "This was written via "pronoun" digital assistant"
client = wolframalpha.Client("Get Your Own")
owm = pyowm.OWM('Get Your Own')
account = "Get Your Own"
token = "Get Your Own"
client1 = TwilioRestClient(account, token)

# Replace symbols with "math jargon." To be used in calculation command!
def clean_text(text):
    text = re.sub(r" ", "", text)
    text = re.sub(r"first", "1 ", text)
    text = re.sub(r"second", "2 ", text)
    text = re.sub(r"third", "3 ", text)
    text = re.sub(r"forth", "4 ", text)
    text = re.sub(r"fifth", "5 ", text)
    text = re.sub(r"sixth", "6 ", text)
    text = re.sub(r"seventh", "7 ", text)
    text = re.sub(r"eighth", "8 ", text)
    text = re.sub(r"ninth", "9 ", text)
    text = re.sub(r"tenth", "10 ", text)
    text = re.sub(r"1st", "1 ", text)
    text = re.sub(r"2nd", "2 ", text)
    text = re.sub(r"3rd", "3 ", text)
    text = re.sub(r"4th", "4 ", text)
    text = re.sub(r"5th", "5 ", text)
    text = re.sub(r"6th", "6 ", text)
    text = re.sub(r"7th", "7 ", text)
    text = re.sub(r"8th", "8 ", text)
    text = re.sub(r"9th", "9 ", text)
    text = re.sub(r"0th", "0 ", text)
    text = re.sub(r"/", "/", text)
    text = re.sub(r"-", "-", text)
    text = re.sub(r"=", "=", text)
    text = re.sub(r"isequalto", "=", text)
    text = re.sub(r"is", "=", text)
    text = re.sub(r"equalto", "=", text)
    text = re.sub(r"equals", "=", text)
    text = re.sub(r"over", "/", text)
    text = re.sub(r"divide", "/", text)
    text = re.sub(r"times", "*", text)
    text = re.sub(r"multiply", "*", text)
    text = re.sub(r"plus", "+", text)
    text = re.sub(r"add", "+", text)
    text = re.sub(r"subtract", "-", text)
    text = re.sub(r"minus", "-", text)
    text = re.sub(r"squared", "^2 ", text)
    text = re.sub(r"tothe", "^", text)
    text = re.sub(r"sign", "sin", text)
    text = re.sub(r"cosign", "cos", text)
    text = re.sub(r"sine", "sin", text)
    text = re.sub(r"cosine", "cos", text)
    text = re.sub(r"power", "", text)
    text = re.sub(r"where", ", where ", text)
    text = re.sub(r"for", ", for ", text)
    return text


# Responsible for vocalising responses
def speak(audioString):
    # This is only here to aid in problem solving, "did it hear me correctly?"
    print(audioString)
    tts = gTTS(text=audioString, lang='en-uk')
    tts.save("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/audio.mp3")
    playsound("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/audio.mp3")


# Responsible for "listening to" and returning the users comments
def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        # speak("I'm listening")
        audio = r.listen(source)
    # Speech recognition
    data = ""
    try:
        data = r.recognize_google(audio)
        if data == "end program":
            speak("Goodbye!")
            exit()
        # This is only here to aid in problem solving, "did it hear me correctly?"
        print("You said: " + data)
    except sr.UnknownValueError:
        speak("Can you please repeat that")
        recordAudio()
    except sr.RequestError as e:
        speak("Can you please repeat that?".format(e))
        recordAudio()
    data = data.lower()
    return data


# This is the function responsible for executing commands
def command(data):
    # Request time
    if "time" in data:
        speak(ctime())
    # Request map info
    if "show me where a city is" in data:
        speak("Sure thing Mathew, what is the name of the city?")
        city = recordAudio()
        speak("Now, what state is it in?")
        state = recordAudio()
        speak("Hold on Mathew, I will show you where " + city + ", " + state + " is.")
        speak("chromium-browser https://www.google.us/maps/place/" + city + "," + state + "/&")
    # Request data search
    if ("google" or "search") in data:
        data = data.split("Google " or "search ")
        search = " ".join(str(x) for x in data)
        speak("Googling " + search)
        url = "https://www.google.co.in/search?q=" + search + "&oq=" + search + "&gs_l=serp.12..0i71l8.0.0.0.6391.0.0.0.0.0.0.0.0..0.0....0...1c..64.serp..0.0.0.UiQhpfaBsuU"
        webbrowser.open_new(url)
    # Resquest calculations be done
    if ("calculate" or "find") in data:
        if "calculate" in data:
            data = data.split("calculate ")
        else:
            data = data.split("find ")
        search = " ".join(str(x) for x in data)
        _search = clean_text(search)
        speak("Calculating " + _search)
        try:
            res = client.query(_search)
            answer = next(res.results).text
            speak(answer)
        except:
            speak("Sorry, I was unable to get an answer for you")
    # Request info from Wikipedia
    if "tell me about" in data:
        data = data.split("tell me about ")
        search = " ".join(str(x) for x in data)
        speak("Searching...")
        try:
            res = (wikipedia.summary(search))
            speak("Sir, I found " + data + " for you: " + res)
        except:
            speak("I was unable to find anything")
    # Get weather
    if "weather" in data:
        data = data.split("weather")
        search = " ".join(str(x) for x in data)
        if "today" in search:
            fc = owm.weather_at_place(location)
            w = fc.get_weather()
        else:
            fc = owm.daily_forecast(location, limit=3)
            w = fc.get_weather()
        client1.messages.create(from_='(507) 607-7398', to='(507) 382-0499', body = ('\n' + "Here's the forecast you requested:" + '\n' + w.get_detailed_status()+ '\n' + str(w.get_temperature('fahrenheit'))))
    # Request an email be written and sent
    if ("email") in data:
        speak("Certainly sir, who would you like me this to?")
        targ = recordAudio()
        speak("What would you like to say in your email sir?")
        mat = recordAudio()
        mail = smtplib.SMTP('smtp.gmail.com', 587)
        try:
            mail.ehlo()
        except:
            mail.helo()
        mail.starttls()
        mail.login(add, pw)
        mail.sendmail(add, targ, mat)
        mail.close()
    # Request definition, synonym, or antonym
    if "definition" in data:
        data = data.split("definition of")
        search = " ".join(str(x) for x in data)
        dictionary = PyDictionary()
        speak(str((dictionary.meaning(search))))
    if "synonym" in data:
        data = data.split("synonym of")
        search = " ".join(str(x) for x in data)
        dictionary = PyDictionary()
        speak(str((dictionary.synonym(search))))
    if "antonym" in data:
        data = data.split("definition of")
        search = " ".join(str(x) for x in data)
        dictionary = PyDictionary()
        speak(str((dictionary.antonym(search))))
# ToDO #########################################################################
# ToDO #                                                                       #
# ToDO #   Add dictation. Remember to offer a few locations to store file.     #
# ToDO #                                                                       #
# ToDO #########################################################################
    # Request a program be opened, use similar process to take down dictations in .txt file but it will nee a seperate
    # calling to recordAudio for what it types.
    if "open" in data:
        data = data.split("open ")
        program = " ".join(str(x) for x in data)
        speak("Opening " + program)
        # Special note, use: os.system("open -a path/program.app") to open program so it should look like:
        # if "name" in program:
        #    os.system("open -a path/name.app")
        if "iTunes" in program:
            os.system("open -a program.app")
        if "Matlab" in program:
            os.system("open -a MATLAB_R2017a.app")
        if "VLC" in program:
            os.system("open -a program.app")
        if "slicer" in program:
            os.system("open -a Slic3r.app")
        if "pycharm" in program:
            os.system("open -a 'PyCharm CE.app.app'")
        if "ip scanner" in program:
            os.system("open -a 'IP Scanner.app'")
        if "Arduino" in program:
            os.system("open -a Arduino.app")
        if "eclipse" in program:
            os.system("open -a Eclipse.app")
        if "AutoCad" in program:
            os.system("open -a 'AutoCAD 2016.app'")
        if "terminal" in program:
            os.system("open -a Terminal.app")
        if "latex" in program:
            os.system("open -a texmaker.app")
    # Opens a file that acts as a list of impromptu commands
    if "new command" in data:
        speak("Remember, you have to type this for yourself. I can't type it for you.")
        os.system("open -a '/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/command.txt'")
        # Searches file of impromptu commands for correct command
    else:
        coms = open("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/command.txt", "r")
        for line in coms:
            if re.match(data, line):
                os.system(next(coms))
            else:
                speak("I'm sorry, I can't do that.")
        coms.close()


# Where the magic happens...
def init():
    while 1:
        data = recordAudio()
        # Exit commands
        if "that will be all" in data:
            speak("Certainly sir.")
            break
        if "no" in data:
            speak("Certainly sir.")
            break
        if "thank you for your help" in data:
            speak("Certainly sir.")
            break
        # Operation Commands
        else:
            command(data)
            data = ""
            speak("will there be anything else sir?")


# Pause program
def pause():
    i = 1
    while i == 1:
        k = input("Press a button to continue: ")
        if not k == "":
            break


# Initialization...
time.sleep(1)
playsound("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/Computer_Magic-Microsift-1901299923.mp3")
while 1:
    speak("Hello Mathew, what can I do for you?")
    init()

 


Pt 2., the base code for the facial recognition section…(Still under construction)
FacialRecognition_BaseCode.py

#######################################################################################################################
# [2]
#
# Author:
#           Matt Tucker
#
# Date:
#           9Aug2017-
#
# Description:
#           The following is only part of a chatbot that will analyze visual information to identify users, create user
#           profiles, imitate a personality based on user, while also acting as a virtual assistant via executing
#           a handful of basic commands.
#
#           This particular part handles the facial recognition procedure.
#
# Note of Acknowledgement:
#           This program was built using the Facial Recognition example from,
#           "http://rpihome.blogspot.com/2015/03/face-detection-with-raspberry-pi.html" as a base/outline. As such much
#           of the basic underlining structure may appear similar or even be the same. And for that reason, I would
#           like to extend a thank you to the author.
#
# ToDo:     This program needs to be modified to work with RPi and with video.
#######################################################################################################################
import cv2
i = 1
while i < 5:
    title = "faces/" + str(i) + ".jpeg"
    # Load an image from file
    image = cv2.imread(title, 1)
    # Load a cascade file for detecting faces
    face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_alt.xml')
    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Look for faces in the image using the loaded cascade file
    faces = face_cascade.detectMultiScale(gray, 1.1, 5)
    print "Found "+str(len(faces))+" face(s)"
    # Draw a rectangle around every found face
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
    # Adds text to the image
    # Come back to this later and improve description
    cv2.putText(image, "Image: " + str(i), (0, 10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, 8, bottomLeftOrigin = False)
    # Save the result image
    result = "results/result" + str(i) + ".jpg"
    cv2.imwrite(result, image)
    i += 1

 


Pt 3.1 the chatbot section. This part of the code is still in flux. This is a test of one chatbot recipe. There will be others until I decide on one, which will be named Chatbot_BaseCode.py
Chat_Test.py

#######################################################################################################################
# [3_1]
#
# Author:
#           Matt Tucker
#
# Date:
#           14Aug2017-
#
# Description:
#           The following is only part of a chatbot that will analyze visual information to identify users, create user
#           profiles, imitate a personality based on user, while also acting as a virtual assistant via executing
#           a handful of basic commands.
#
#           This particular part handles the ChatBot.
#
# Author's Note:
#           I'm using this to test out different approaches to creating a Chatbot before I commit to a chat bot to be
#           used in "ChatBot_BaseCode.py".
#######################################################################################################################

from gtts import gTTS
from playsound import playsound
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer



# Responsible for vocalising responses
def speak(audioString):
    # This is only here to aid in problem solving, "did it hear me correctly?"
    print(audioString)
    tts = gTTS(text=audioString, lang='en-uk')
    tts.save("srcs/audio.mp3")
    playsound("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/audio.mp3")

chatbot = ChatBot("Tod")
chatbot.set_trainer(ListTrainer)
chatbot.train("/Users/Tuck/Documents/PyCharm_PythonPrograms/ChatBot_Test/srcs/trainingdata.txt")

print "Moving to response now..."
response = str(chatbot.get_response("What is death?"))
speak(response)

 

As a funny note, I appeared to have turned my computer into Marvin from The Hitchhiker’s Guide to the Galaxy using this code:
Screen Shot 2017-08-14 at 11.00.12 PM


I will be back at a later date to better explain what the code is doing, and some of the theory behind. I will also be posting the code on GitHub when I am done with it.

Cat and Mouse II

A thought has occurred to me, it may be interesting to constrict the “cat’s” available moves to only those towards the “mouse” -along the shortest line between the two; similarly the mouse towards the “goal.” Moreover, let’s reconstruct the game so that both the cat and mouse exist on a graph. In particular let’s say a Cayley graph Γ(G,S) and restrict their movements to the edges of the graph. If we maintain that the goal of the mouse is to reach the vertex mapped by max{x|x∈G∩S*} where S* ⊂ S, and the cat’s goal is to “catch” the mouse; how then does this change the dynamics of the game? What is now the optimal strategy for a mouse starting on any given vertex, not max{x|x∈G∩S*}? What about the cat’s?

I will have to come back to this thought later.

Thoughts: …the more I reread this the more I see other interesting questions about this game. For example, there are several ways to turn this into a game of incomplete information. And what happens as we add or subtract vertices or add players? What if we change the rate of motions(e.g. cat 2 vertices to the mouse’s 1, or 3:1, 3:2… ect. -I sense a possible covering problem here)

Note to self: I suspect this may “reduce” nicely to a coloring problem. I should also go back and look at these older projects and expand on them… make them interesting.

LaTex Keyboard

So, for anyone who knows me… It is no secret I am always looking for ways to shorten the time required to do a task. So when I saw an .stl file for creating a personalized 12 key keyboard(http://www.thingiverse.com/thing:1043977), I instantly saw a chance to cut down some of the typing required in LaTex. The following it an account of what follows:

Day one-
Research similar projects.

…and do Real Analysis h/w.


I’m sorry to say, but I suspect this project is going to get put on the back burner for awhile.

…To be continued!

Twitter Bot

Ok… So… Trump’s administration has made it known that they do not believe climate science. As a result, I have taken it upon myself to collect a MONSTER list of links to climate research to be used in a Twitter Bot.

Step 0: Create a .txt file that is nothing more than a list of links to climate change data!

Step 1: Set up one of my RaspberryPi’s to work headlessly.

Step 2: PYTHON!

Step 3: Setup Twitter Bot page.

Step 4: RELEASE THE BOT!

Step 5: …hmmm… so I’ve got the code to work. But… it seems after about 4 posts(2 hrs), the bot stops working…. This is annoying. Scratch head and return.
…Best guess, the server is timing out. Worst guess… Twitter bots work on pizza and beer and mine has neither.

*Will return to make this into a tutorial.*


16Feb17

I just discovered how to fix the “timing out” problem. I had a “derp.” need to use:

crontab -e

It will let me run the code at set times. So if I rewrite the code to select a line at random… I should be in the clear. But it also means I need to figure out how to deal with the exceptions/errors it sends back when a tweet is repeated.


17Feb17

Bot appears to be working:

https://twitter.com/alt_climate


19Feb17

The Twitter Bot is most definitely working!

https://twitter.com/alt_climate

You can find the tutorial and examples that I am putting together here:
https://github.com/AltClimate/Twitter_Bot (the tutorial has been moved to- https://github.com/MattTucker22689)


6Jul17

I should probably note, after tinkering with the bot to have it send tweets to @realDonaldTrump for/in solidarity with #MarchForScience, Twitter canned the account temporarily. As a result, I returned the bot to its original state… but quickly got bored with it, and took the Pi that was executing the code offline. I also closed the Github page and moved the tutorial to:
https://github.com/MattTucker22689

That said, I am now in the very slow and tedious process of rewriting the code. It is my intent to write it in such a way as to incorporate a machine learning algorithm to sift thru searches and find legitimate research on the subject of climate change and post that… in short, no more list!

I may also add a feature to have the bot respond to Tweets sent to it… But that will likely be a feature I add later. In the meantime, I’ll be… Coding, getting ready for the SIAM Conference in Pittsburgh (I will be giving a poster presentation -come visit!), and reading up on Genetics and O-Chem.

R2D2 Project(On Going)

Though I am a math major, I often times find myself working on fun computer science, engineering, and robotic projects in my spare time. The most recent of which has been turning an 18″ R2D2 toy I bought from WalMart into a functioning robot.


To begin the project I tore the toy R2D2 apart to see what I had to work with. It became clear right away, that the manufacturer had at some point planned on making the toy robotic as there were many places to mount speakers, motors, and LEDS.  Continue reading R2D2 Project(On Going)

Pursue and Evade: A Look at “Cat and Mouse” thru Game Theory

The point of the attached powerpoint was to act as a class project demonstrating how it might be possible to populate a game matrix with the appropriate payoffs, assuming one can model/simulate the game on a computer.

CatAndMouse

It should be understood, that in order to model/simulate a game via a computer, the games rules must be well defined and understood.


The following is the code used:

 

import java.awt.*;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;

public class GameTheory2 extends JPanel
{
 public static void main(String[] args)
 {
 int[] catpost = Start();
 int[] mousepost = Start();
 String m = Arrays.toString(mousepost);
 String c = Arrays.toString(catpost);
 Random rn= new Random();
 int turn;
 int[] tempmouse;
 int[] tempcat;
 int count=1;

 while(mousepost!=catpost && mousepost[0]>=0 && mousepost[0]<=500 &&mousepost[1]>=0 && mousepost[1]<=500 && count<1000)
 {
 int direction[]=Choise(catpost, mousepost);
 tempmouse=mousepost;//To be used later
 tempcat=catpost;//To be used later
 catpost[0] = catpost[0]+30*direction[0];//new catpost
 catpost[1] = catpost[1]+30*direction[1];//new catpost
 mousepost[0] = mousepost[0]+10*direction[0];//new mousepost
 mousepost[1] = mousepost[1]+10*direction[1];//new mousepost
 
 m=m+" "+Arrays.toString(mousepost);
 c=c+" "+Arrays.toString(catpost);
 count++;
 }
 System.out.println(count+" Cat: "+c);
 System.out.println(count+" Mouse: "+m);
 
 
 /////////////////////***GUI***/////////////////////
 /*
 JFrame frame=new JFrame("Pursuit and Evation");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 JPanel main_window = new JPanel();
 main_window.setPreferredSize(new Dimension(1000, 1000));
 frame.getContentPane().add(main_window);
 frame.pack();
 frame.setVisible(true);
 */
 
 }
 public static int[] Start()//Initializing location of the Character
 {
 int[] post= new int[2];//Array with x,y location
 Random rn= new Random();
 int x = rn.nextInt(501);//Random x-post
 int y = rn.nextInt(501);//Random y-post
 post[0]=x;//sets x location
 post[1]=y;//sets y location
 return post;//returns x,y location
 
 }
 public static int[] Choise(int[] catpost, int[] mousepost)//Defines cardinal direction of motion---Unit Vector
 {
 int direction[]={0,0};
 Random rn= new Random();
 int cardinal;
 if(catpost[0]<mousepost[0] && catpost[1]<mousepost[1])//If cat is above and left of mouse
 {
 cardinal = rn.nextInt(2);
 if(cardinal==0)//move X
 {
 direction[0]=1;
 }
 else//move Y
 {
 direction[1]=1;
 }
 
 }
 else if (catpost[0]>mousepost[0] && catpost[1]<mousepost[1])//If cat is above and right of mouse
 {
 cardinal = rn.nextInt(2);
 if(cardinal==0)
 {
 direction[0]=-1;
 }
 else
 {
 direction[1]=1;
 }
 
 }
 else if (catpost[0]==mousepost[0] && catpost[1]<mousepost[1])//If cat is directly above mouse
 {
 cardinal = rn.nextInt(3);
 if(cardinal==0)//Move X
 {
 direction[0]=1;
 
 }
 else if(cardinal==1)//Move X
 {
 direction[0]=-1;
 
 }
 else//Move Y
 {
 direction[1]=1;
 
 }
 
 }
 else if (catpost[0]<mousepost[0] && catpost[1]>mousepost[1])//If cat is below and left of mouse
 {
 cardinal = rn.nextInt(2);
 if(cardinal==0)
 {
 direction[0]=1;
 }
 else
 {
 direction[1]=-1;
 }
 
 }
 else if (catpost[0]>mousepost[0] && catpost[1]>mousepost[1])//If cat is below and right of mouse
 {
 cardinal = rn.nextInt(2);
 if(cardinal==0)
 {
 direction[0]=-1;
 }
 else
 {
 direction[1]=-1;
 }
 
 }
 else if (catpost[0]==mousepost[0] && catpost[1]>mousepost[1])//If cat is directly below of mouse
 {
 cardinal = rn.nextInt(3);
 if(cardinal==0)
 {
 direction[0]=1;
 
 }
 else if(cardinal==1)
 {
 direction[0]=-1;
 
 }
 else
 {
 direction[1]=-1;
 
 }
 
 }
 else if(catpost[0]<mousepost[0] && catpost[1]==mousepost[1])//If cat is directly to the left of mouse
 {
 cardinal = rn.nextInt(3);
 if(cardinal==0)
 {
 direction[0]=1;
 
 }
 else if(cardinal==1)
 {
 direction[1]=1;
 
 }
 else
 {
 direction[1]=-1;
 
 }
 
 }
 else if (catpost[0]>mousepost[0] && catpost[1]==mousepost[1])//If cat is directly to the right of mouse
 {
 cardinal = rn.nextInt(3);
 if(cardinal==0)
 {
 direction[0]=-1;
 
 }
 else if(cardinal==1)
 {
 direction[1]=1;
 
 }
 else
 {
 direction[1]=-1;
 
 }
 
 }
 return direction;
 }
}