Mark Ryan and Tim Cocks
| 1 | 2 (abc) | 3 (def) |
| 4 (ghi) | 5 (jkl) | 6 (mno) |
| 7 (pqrs) | 8 (tuv) | 9 (wxyz) |
| * | space | # |
Without predictive text, the user is required to press the appropriate key a number of times for a particular letter to be shown. For example, the letter 'u' is produced by pressing the '8' key twice. This makes entering text a slow and cumbersome process. The problem becomes even more apparent if we consider the word 'hello'. With this method, the user must press 4, 4, 3, 3, 5, 5, 5, then pause, then 5, 5, 5, 6, 6 and finally another 6.
Predictive Text (also known as T9) is a system that aims to reduce the number of key pressings necessary to enter text. It aims to allow the user to press each key only once. This allows the word 'hello' to be typed in 5 key presses instead of 12. It does this by restricting the available words to those in a dictionary.
Unfortunately, predictive text technology is not perfect. One problem concerns what should happen when a given key sequence could correspond to more than one word in the dictionary. For example, the sequence '228' could be interpreted as 'act', 'bat' or 'cat'. We call these words "predictive text clashes". Other clashes well-known to users of predictive text include "of" and "me"; "home", "gone" and "good", etc.
On Nokia phones, the * key is used to cycle between alternative words for the same numeric string. Thus, if entering "4663" produces "gone" when you meant "home", you press *. This moves on to the next word; if it's still not "home" you have to press * again. Normally, the words are arranged so that the more common words (like "home") will occur earlier in the list, and less common words (like "hoof") will occur later.
In these exercises, you are asked to develop a
Java program for working with predictive text. For simplicity, we
assume that the user does not need punctuation or numerals. Also, you
need not attempt to order words by how common they are.
You may also limit your solutions to using only lower-case letters.
PredText to convert
some strings to their
corresponding numeric signature. The strings will be given on the
command
line. For example,
java PredText university of birminghamwill deliver the output
8648377489 63 2476464426Notes
StringBuffer
instead of String to avoid creating lots of strings which
later get thrown away. Predictive text solves this by restricting
the available possibilities to the
words occurring in a dictionary. This reduces the number of
possibilities considerably. For example, for most dictionaries the
numeric string '7733428' is reduced to only one possibility - it is
the word 'predict'.
Unix systems usually have a dictionary of words in /usr/dict/words
or /usr/share/dict/words. If you're working on a non-unix
system, you will need to download such a
file from a unix system in order to do the remainder of these
exercises.
Write a program which takes a numeric string argument, and prints out all the words in the dictionary that match it. For example,
java PredText 7733428would probably just print the word "predict", while
java PredText 4663would print out a large number of words including: gone, good, goof, home, hone, hood, hoof.
Notes
Modify your PredText program so
it reads in the words and stores them in an appropriate data structure
from Java's Collections
Framework. (To use the classes in the Collections framework,
you need to import java.util.*.)
From your data structure, compute the largest set of clashes. For example, if the set of 7 words
gone, good, goof, home, hone, hood, hoof
forms one of the largest sets of clashes, your program could print that set. (Probably you will find a set of clashes bigger than this one.)
The following remarks allude to my solution, but you may well find a
more elegant way of doing things. Feel free to diverge from my way of
doing things.
class WordSig implements ComparableThe idea is that you will store objects of this class, e.g. in an
{
String word;
String sig;
public WordSig (...) {...}
public int compareTo(Object ws){ ... }
public boolean equals(Object ws) { ... }
...
}
ArrayList. We will sort this list,
in order of the
numeric signatures. In order to sort an list, the objects stored
in it must implement the Comparable interface. That means
they must have a compareTo(Object) method which returns
-1, 0 or 1 according to whether this object is less than,
equal to, or greater than the argument object in the intended
ordering. Collections.sort(). You can search
the sorted list with Collections.binarySearch(). Find out
more about
Collections and the comparable interface in Horstmann/Cornell "Core
Java". PredText.java. To ensure your GUI is reliable
and does not interfere with the existing code, you can use the Model
View Controller (MVC) model of GUI development. MVC is a good
discipline for building GUIs, because it encourages separation of
concerns.
MVC also helps ensure that the user interface code is separate from the
program
code. This makes it easy to change the user interface without changing
the
underlying program, or change the program without changing the GUI.
We encapsulate the original program in an object which we call the model. It contains accessor and mutator methods which act as an interface to the program. For every GUI component that can change the model, such as buttons or text entry fields, we create a control object. Control objects translate the user's actions into the model's mutator methods. Every GUI component that displays the object (such as status lights or text output fields) is represented by a view object. View objects observe the model, and change themselves when the model changes.

View classes typically extend GUI output classes, and implement Observer. The model class extends
observable; thus, the view classes observe the model. The model is the
only part of the GUI that interfaces with the underlying program.
Control classes extend GUI
input classes. Some books say that the control classes should implement
listeners; but I think it is more natural for the model to implement
the listeners. Thus, the flow of information is: the user drives
the control classes. The model listens to them, and updates the
underlying program. The views observe the model, and display
appropriate information.
You should decide what your GUI will look like
before you start writing
it. An example layout is shown below:

| Design: ((no design needed for this very simple program.)) | 0 |
| Coding: Is there a
self-contained method which does the conversion?
The method should return the string rather than printing it directly. Is the code nicely laid out and properly indented? |
10 |
| Testing: Has the student tried it on a variety
of strings, including empty string and incorrect inputs? Also try it on several expected strings, such as "meet me for a drink tonight"; in that case you should get 6338 63 367 2 37465 8664448. |
10 |
| Total | 20 |
| Design: ((no significant design
needed for this very simple program.)) |
0 |
| Coding: are there
appropriate methods? Do they return their results instead of printing
them? Is the code nicely laid out and properly indented? Are the variable names sensible? Variable and method names should begin with a lowercase letter, and class names should begin with an uppercase letter. Does the same program work for part 1 and part 2, by detecting the kind of input? |
10 |
| Testing: Has
the student tried it on a variety of strings, including empty string
and incorrect inputs? Try it on several numeric strings, such as 7733428 (probably gives only "predict") and 4663 (probably gives gone, good, goof, home, hone, hood, hoof). |
10 |
| Total | 20 |
| Design: Has an appropriate set of
classes been used, with meaningful relations between them? |
6 |
| Coding: Does the program build an appropriate
data structure from the
Collections framework? Does the program follow the student's written design? |
7 |
| Testing: Does
the program print a large set of clashes? Check that the
words printed are really clashes! What happens if the dictionary is empty, or missing, or has no clashes? |
7 |
| Total | 20 |
| Design: Is there an appropriate set of view and controller classes? Does the model properly encapsulate the underlying program? | 10 |
| Coding: Is the code reasonably commented and could it be maintained by a different programmer, if necessary? | 10 |
| Testing: Has the
student devised an appropriate set of test cases, including multiple
pressings of *, input which doesn't match any word, etc. |
10 |
| Functionality: Basic behaviour: is it possible to
enter a numeric string and
get a
corresponding word to appear? More complex behaviour: can the user press 0 for a space, and go on to enter a next word? Can the user press * to cycle between clashes? |
10 |
| Total | 40 |