In my high school AP computer science class we're making a very basic chatbot that replies with predefined answers. I decided to have a bit more fun with itMy program is basically this answer on stackoverflow. The dictionary is being created correctly using several popular works of literature, but my response method is producing very poor results. I know I shouldn't be expecting much from such a simple program, but I'm fairly certain it's suppose to be better than this.package me.rabrg.chatbot;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.text.BreakIterator;import java.util.ArrayList;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Random;import java.util.Scanner;import java.util.TreeMap;public final class Chatbot {    private final Random random = new Random();    private final BreakIterator sentenceIterator = BreakIterator.getSentenceInstance(Locale.US);    private final BreakIterator wordIterator = BreakIterator.getWordInstance(Locale.US);    private final Map<String, List<String>> dictionary = new TreeMap<String, List<String>>();    public static void main(final String[] args) {        final Chatbot chatbot = new Chatbot();        chatbot.addDictionary(new File(mobydick.txt), new File(gatsby.txt), new File(ulysses.txt), new File(portraitartistyoungman.txt),                new File(bravenewworld.txt), new File(mockingbird.txt), new File(1984.txt));        System.out.println(Loaded);        try (final Scanner scanner = new Scanner(System.in)) {            for (;;) {                System.out.println(chatbot.getResponse(scanner.nextLine()));            }        }    }    private void addDictionary(final File... files) {        for (final File file : files) {            final StringBuffer stringBuffer = new StringBuffer();            String line;            try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {                while ((line = reader.readLine()) != null)                    stringBuffer.append(line.toLowerCase() + ' ');            } catch (final IOException e) {                e.printStackTrace();            }            addDictionary(stringBuffer.toString());        }    }    private void addDictionary(final String string) {        sentenceIterator.setText(string);        String sentence;        for (int startSentence = sentenceIterator.first(), endSentence = sentenceIterator.next(); endSentence != BreakIterator.DONE;                startSentence = endSentence, endSentence = sentenceIterator.next()) {            sentence = string.substring(startSentence, endSentence).trim();            // TODO: Add quotation support            if (sentence.contains(\))                continue;            if (sentence.endsWith(?) || sentence.endsWith(!) || sentence.endsWith(.))                sentence = sentence.substring(0, sentence.length() - 1);            wordIterator.setText(sentence);            String word, lastWord = null, lastLastWord = null, key;            for (int startWord = wordIterator.first(), endWord = wordIterator.next();                    endWord != BreakIterator.DONE; startWord = endWord, endWord = wordIterator.next()) {                if (!Character.isLetterOrDigit(sentence.charAt(startWord)))                    continue;                word = sentence.substring(startWord, endWord);                if (lastWord != null && lastLastWord != null) {                    key = lastLastWord + ' ' + lastWord;                    List<String> values = dictionary.get(key);                    if (values == null)                        values = new ArrayList<String>();                    if (!values.contains(word))                        values.add(word);                    dictionary.put(key, values);                }                lastLastWord = lastWord;                lastWord = word;            }        }    }    private String getResponse(String input) {        input = input.toLowerCase();        wordIterator.setText(input);        String word = null, lastWord = null, key = null;        for (int startWord = wordIterator.first(), endWord = wordIterator.next(); endWord != BreakIterator.DONE;                startWord = endWord, endWord = wordIterator.next()) {            if (!Character.isLetterOrDigit(input.charAt(startWord)))                continue;            word = input.substring(startWord, endWord);            if (lastWord == null)                lastWord = word;            else                break;        }        String response = ;        for (int i = 0; i < random.nextInt(8) + 1; i++) {            List<String> values = dictionary.get(lastWord + ' ' + word);            if (values == null)                break;            final String value = values.get(random.nextInt(values.size()));            response += value + ' ';            lastWord = word;            word = value;        }        return response.trim();    }}Example run:> where do you livethey you> what is your favorite colourwrong god's going beauty happening> my favorite colour is bluefavorite> i love drinking ketchupmy youDid I not implement the response method correctly? What could I change to get better results?
Markov chain chatbot producing poor results
java;ai;markov chain