![[SimpleParser.java]] ```java package simpleparser; import java.util.Scanner; public class SimpleParser {     String[] verbs = {"run", "walk", "talk", "eat"};     String[] nouns = {"ball", "dog", "john", "jane"};     String[] adjectives = {"red", "big", "small"};     String[] adverbs = {"quickly", "slowly"};     String[] pronouns = {"i", "you", "he", "she"};     String[] articles = {"the", "a"};     SimpleParser() {         inputLoop();     }     public static void main(String[] args) {         new SimpleParser();     }     public void inputLoop() {         Scanner scan = new Scanner(System.in);         String s;         do {             s = scan.nextLine();             String tokens = tokenize(s);             System.out.println(tokens);             if (tokens.equals(" a n v")) //ie adjective, noun, verb             {                 System.out.println("Well formed sentence!");             } else {                 System.out.println("Not a well formed sentence");             }         } while (!s.equals("end"));     }     public String tokenize(String s) {         String[] parts = s.split("\\s+");  //split into words.         //note s+ to take account of multiple spaces.         String tokens = "";         for (String word : parts) //iterate through each word         {             boolean isRecognised = false;             if (checkList(verbs, word)) {                 isRecognised = true;                 tokens = tokens + " v";  //note I included a space!             }             if (checkList(adjectives, word)) {                 isRecognised = true;                 tokens = tokens + " a";             }             if (checkList(nouns, word)) {                 isRecognised = true;                 tokens = tokens + " n";             }             if (!isRecognised) {                 tokens = tokens + " u";                 System.out.println("I don't know what a " + word + " is!");             }         }         return tokens;     }     public boolean checkList(String[] list, String word) {         for (String s : list) {             if (word.equals(s)) {                 return true;             }         }         return false;     } } ``` ![[FlexibleParser.java]]![[Parser.java]] ## Exercise 1. Use the simple parser class to write a program that will accept sentences in the following form 1. The dog runs 2. I see the dog 3. Jim pats the dog 4. The dog bites jim 2. Use the Flexible Parser class as a basis for a program that will recognise the following sentences as well formed 1. The quick brown fox jumped over the lazy dog 2. Is you is or is you ain't my baby? 3. My hovercraft is full of eels 3. An adventure game accepts commands in the form verb noun. Some example commands are **take key** | **go east** | **drop shove**l. The game will also allow compound commands such as **take key and open door** or **drop shovel, go east**.  Finally, the game will ignore articles.  Write a parser that will convert sentences such as **take the key and go east** into the instructions **take key |  go east** ## Extension Below is the code for a simple computer psychotherapist: ```java Scanner scan = new Scanner(System.in); String input = ""; boolean isNotDone = true; System.out.println("How are you?"); do {             input = scan.nextLine();             String output = "I don't know what you mean";             if (input.startsWith("I")) {                 output = input.replaceFirst("I", "Why do you") + " ?";             }             if (input.equalsIgnoreCase("Bye")) {                 output = "Bye!";                 isNotDone = false;             }             System.out.println(output); } while (isNotDone); ``` 1. Run the code and see what it does 2. Combine the code with flexible parser class. Have the psychotherapist recognise sentences of the form "I feel happy because I take exercise" and "I feel sad because I am lonely".  Have it give replies of the form "People who take exercise are happy" and "People who are lonely are sad." 3. Now take a look at the word switcher code.  Add this to your psychotherapist code from above.  Have your psychotherapist respond appropriately if people include greetings (hello|Hi|hiya) or goodbyes such as (bye|goodbye|see you)