CSCI 136 |
In this assignment, you will be building a predictive keyboard similar to ones you may have seen on your mobile phone.
|
Overview.
The keyboard learns to make predictions by text given at startup and by things the user subsequently types.
As a user types, the program predicts the most likely word given the currently entered word prefix.
So for example, if the user has typed th, the keyboard might predict the.
Your phone's keyboard (e.g. the iPhone keyboard on the right) may offer a selection of the most likely word completions. The keyboard you will develop in this assignment will predict just the single most likely word. Often keyboards look at the last few words to help predict the current word. For example the iPhone keyboard on the right suspects the next word is "science" due to the previous word being "computer". In this assignment, we will taking a simpler approach of predicting the completion based only on the relative frequency of words observed in some training text (ignoring the context provided by previous words). |
There are 9 total words in this text. Ignoring case and punctuation, the word the appears twice. The unigram probability of the is thus P(the) = 2/9. The unigram probability of cat is P(cat) = 1/9. The other six words: is, in, corner, of, their, garage all also have a probability of 1/9. If a word has never been seen, its probability is zero: P(zebra) = 0/9 = 0.0.The cat is in the corner of their garage.
Prediction class. The brains of this whole operation is the class WordPredictor. This class learns how to make word predictions based on being shown training data. It can learn from all the words in a file (via the train() method) or from a single individual word (via the trainWord() method). After new training data is added, the build() method must be called so the class can recompute the most likely word for all possible prefixes. Here is the API for the WordPredictor class:public class Word ------------------------------------------------------------------------------------------------------------------------------ Word(String word, double prob) // Create a new entry given a word and probability Word(String word, double prob, String filename) // Create a new entry given a word, probability, and audio filename String getAudioFilename() // Getter for audio filename String getWord() // Getter for the word double getProbability() // Getter for the probability boolean matchesPattern(String regularExpression) // Does this word match the given regular expression pattern?
Training the model. Model training occurs in the train() and trainWord() methods. train() should parse out each word in the specified file on disk. If the file cannot be read, it should print out an error, "Could not open training file: file.txt". All training words are converted to lowercase and stripped of any characters that are not a-z or the single apostrophe. During training, you need to update the instance variables:public class WordPredictor ----------------------------------------------------------------------------------------- void train(String trainingFile) // Train the unigram model on all the words in the given file void trainWord(String word) // Train on a single word, convert to lowercase, strip anything not a-z or ' long getTrainingCount() // Get the number of total words we've trained on int getWordCount(String word) // Get the number of times we've seen this word (0 if never) void build() // Recompute the word probabilities and prefix mapping Word getBest(String prefix) // Return the most likely Word object given a word prefix
The wordToCount instance variable is a map where the keys are the unique words encountered in the training data. The values are the integer count of how many times we've seen each word in the data. Only words seen in the training data will have an entry in the HashMap. The total instance variable tracks how many words of training data we have seen. That is, total should equal the sum of all integer counts stored in your map. Training is cumulative for repeated calls to train() and trainWord(), so you just keep increasing the counts stored in your wordToCount map and your total count of training words.private HashMap<String, Integer> wordToCount; private long total;
A Map collection in Java can map multiple keys to the same value. This is exactly what we need in this case since a set of strings (such as wh, wha, whal and whale) may all need to map to a single Word object. You need to ensure that each prefix maps to its most likely word. So even if the word thespian was encountered first in the training data, for any sensible training text, th should probably map to the and not thespian.private HashMap<String, Word> prefixToEntry;
Predictive keyboard interface. Product marketing has mocked up a screenshot for your new predictive keyboard product:% java WordPredictor bad1 = null training words = 202 bad2 = null Could not open training file: thisfiledoesnotexist.txt training words = 202 count, the = 10 count, me = 5 count, zebra = 0 count, ishmael = 1 count, savage = 0 count, the = 32 count, me = 5 count, zebra = 0 count, ishmael = 1 count, savage = 1 a -> and (prob 0.039171 audio '') ab -> about (prob 0.004608 audio '') b -> bird (prob 0.004608 audio '') be -> before (prob 0.002304 audio '') t -> the (prob 0.073733 audio '') th -> the (prob 0.073733 audio '') archang -> archangelic (prob 0.002304 audio '') training words = 434 before, b -> bird (prob 0.004608 audio '') before, pn -> null after, b -> bird (prob 0.004577 audio '') after, pn -> pneumonoultramicroscopicsilicovolcanoconiosis (prob 0.002288 audio '') training words = 437 a -> and (prob 0.030079 audio '') ab -> about (prob 0.001473 audio '') b -> but (prob 0.008580 audio '') be -> be (prob 0.004891 audio '') t -> the (prob 0.067571 audio '') th -> the (prob 0.067571 audio '') archang -> archangel (prob 0.000033 audio '') training words = 209778 elapsed time (s) : 0.3830 heap memory used (MB) : 17.8877 max memory (MB) : 123.9375 Random load test: elapsed time (s) : 3.4390 heap memory used (MB) : 13.8707 max memory (MB) : 123.9375 Hit % = 30.04277
In particular, note the last requirement. This requirement means you can in fact start your keyboard with no data and it will learn words as you go. You can also train the model on some data by providing file(s) on the command line and your keyboard will still learn new words and update its model based on what it sees you type. How cool is that? This program should sell like hot cakes! The video to the right shows the keyboard starting without any training data. Notice how it eventually starts making predictions based on what it has seen the user type. |
Do I need to follow the prescribed APIs? Yes. You may not add public methods to the API; however, you may add private methods (which are only accessible in the class in which they are declared). For example, in our PredictiveKeyboard class, we made a private helper method private String normalize(String text). This method's converts a word of training data and make it lower case and strip any invalid characters. This helped simplify our trainWord() method.
I seem to have repeated code in my train() and trainWord() methods. Is that okay? No, recall our mantra: Repeated Code is Evil™. You should probably refactor your code so the train() method makes use of the trainWord() method.
How do I test if a HashMap contains a certain key value? You can use the containsKey() method to check whether a particular key exists. For further details, check out the HashMap documentation.
How do I test a char variable to see if it is the backspace key? The backspace key is \b.
How do I test a char variable to see if it is the return key? The return key is \n.
How do I represent the char value for a single apostrophe? Yeah that tripped us up at first too. You need to escape the apostrophe with a backslash in the char literal since apostrophes are used to start and end the literal. So for example this code (ch == '\'') tests if the char variable ch is equal to an apostrophe.
Why did you use a long type for the total count of training words but an Integer for the HashMap value? Well we used a long since we were worried somebody might train our keyboard on over 2^31 - 1 (a little more than 2 billion) words of data. We used an Integer instead of a Long in the HashMap since even with a huge amount of training data, it is unlikely we'd see any particular word over 2 billion times. This saved us memory in our HashMap instance variable.
Is there a way to clear out all the data in a HashMap so I can re-fill it based on new training data? Yes. You can call the clear() method on the HashMap object. You could also reinstantiate the object and let the Java garbage collector deal with the old map. Probably the earlier is somewhat more efficient.
How do I normalize a word so it only contains the letters a-z plus apostrophe and is lower case? There are are variety of ways to do this. You might want to investigate the String methods charAt, lowerCase, and/or replaceAll. Check out the String javadocs for details.
Extra credit ideas:
|
Page last updated: February 13, 2015