




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





The due date is Friday, February 10, 2006 by 11:59:59pm. See the syllabus for late policies and academic integrity policies. See below for submission guidelines.
Spell checking is the process of verifying that a particular word is spelled properly according to some dic- tionary. Spell checkers are used in many applications, including word processors (such as Microsoft Word), electronic dictionaries, and optical character recognition (OCR) systems that need to turn images of printed text (or even handwriting) into coherent text. Spell checking itself is trivial, requiring only a simple lookup in a dictionary. However, most applications of spell checking also require that the spell checker provide a list of potentially correct spellings (“near matches”) when the word was spelled improperly. For instance, if I type “speling” into an online dictionary, it will provide suggestions of similar words that I may have meant to type, including “spelling”, “spoiling”, “sapling”, and “splendid”. Your task is to implement a spell checker that determines if a given word is spelled correctly based on a dictionary lookup. When the word is not spelled correctly, it will provide a list of similar-sounding words based on your implementation of the Metaphone algorithm, and ordered based on their edit distance from the string that the user typed. We could use only the edit distance to find near matches, but by first using the Metaphone algorithm we achieve better results because it can find the word you are looking for even if you have no idea how to spell it. Additionally, we can isolate the set of near matches more quickly using the Metaphone algorithm.
Your program will be run from the command line, and will take a rule file (rules.txt, to be described later), a dictionary file containing about 10,000 words (dictionary.txt, from the web page), and some words. It will check the spelling of each word. If the word is spelled properly, it will print “Word ‘[the word]’ is spelled correctly.” Otherwise, it will print the word and its pronunciation, followed by a list of like-sounding words ordered by the edit distance (shown in brackets). For example, if your program is named spellcheck.exe, here is a sample program run:
spellcheck.exe rules.txt dictionary.txt speling supress save
Word ‘speling’ not in dictionary. It sounds like ‘SPLN’. Perhaps you meant: spelling [1] sapling [2] spellings [2] spoiling [2] splendid [5]
Word ‘supress’ not in dictionary. It sounds like ‘SPRS’.
Perhaps you meant: suppress [1] spares [3] suppressed [3] suppresses [3] surprise [3] surprises [3] suppressing [4] surprised [4] spurious [5] surprising [5] surprisingly [7]
Word ‘save’ is spelled correctly.
Read on to learn about the Metaphone algorithm, which will be used to isolate the set of near matches; and an edit distance algorithm, which will be used to rank the results (notice that “spelling” comes before “spoiling”). Fig. 1 gives an overview of the spell-checking process. Please write your program to mimic the output given exactly. We will provide more example output on the project web page.
input word Metaphone −→ Metaphone dictionary lookup −→ set of edit distance −→ ordered list pronunciation homophones of homophones
Figure 1: Overview: Sequence of Transformations for Spell Checking
The Metaphone algorithm, created by Lawrence Philips, takes a word and returns a very rough approximation of the sound of that word. The rough approximation eliminates the distinction between some characters; for instance, ‘S’ and ‘Z’ sound very similar, so the Metaphone algorithm maps them to the same sound (“s”). Some letters may make many different types of sounds: ‘C’, for instance, may make the “sh” sound (denoted by ‘X’) when it is part of “cia” (as in “social”), the “s” sound when it is following by ‘C’, ‘I’, or ‘Y’ (as in “since”), or even the “k” sound when it is by itself, or followed by a ‘K’ (as in “clack”). The Metaphone algorithm maps every sound in a word down to one of the following sounds: , B, X, S, K, J, T, F, H, L, M, N, P, R, @, W, Y. The ‘’ represents the sound of a vowel at the beginning of the word, ‘X’ represents the “sh” sound, and ‘@’ represents the “th” sound. All of the other consonant sounds in the list sound like the consonant. Vowels not at the beginning of the word are considered silent. See Fig. 2(a) for some examples of strings generated by the Metaphone algorithm for some common words. We (arbitrarily) limit the length of the Metaphone output strings to 4 characters, which has been found to produce better results in many cases than using longer strings.
To implement the Metaphone algorithm, you will use a table-driven algorithm to transform the input string into its Metaphone pronunciation. Fig. 2(b) contains several of the transformation rules used in the Meta- phone algorithm. The complete set of rules will be provided on the project web page in the file rules.txt. Each of the rules contains a pattern that should be matched to the input word. Patterns are first matched at the first character of the string. When the first matching pattern is found (the order of the rules is signif- icant), it first consumes part of the input string and then the pronunciation for that pattern is added to the
You should never have a case where no pattern matches. Also, you can ignore any errors in the input, and assume that no word contains any punctuation characters or numbers: only characters a-z (lower or upper case) will be used to make words.
Once you have implemented the Metaphone algorithm, you can determine the set of words in a dictionary that “sound like” a particular word (i.e., homophones) just by calculating the Metaphone pronunciations of every word in the dictionary. Those with the same metaphone pronunciation as the given word sound like the word, and should be returned as results.
Once you have determined the set of homophones for the word, you need to determine the order in which those results should be presented. Since the user probably mistyped the word, we want results that are close from a typing perspective. For instance, “spelling” is closer to “speling” than is “sapling”, so we would like to present “spelling” as the first alternative if the user types the word “speling”. To do this, we will compute the edit distance between the typed word and each of the real words that sound like the typed word. The edit distance is a measure of how many steps it takes to transform one string into another, so closer matches will have smaller edit distances. When two homophones have the same edit distance from the word typed, order them alphabetically. The edit distance between string S 1 and S 2 is the minimum number of symbol deletions and replacements required to transform S 1 and S 2 into a common string.
Example 1 Given: S 1 = agttgtagct S 2 = agtgctact To transform S 1 and S 2 into a common string we can apply three deletions.
The result is the string agtgtact. The absolute minimum number of deletions/replacements required to transform S 1 and S 2 into a common string is three. See for yourself: Try to transform the strings using only two deletions/replacements. You will see that it is impossible.
Consider the following naive approach to computing the edit distance between two strings:
This naive approach is not only tedious to implement, but is also very inefficient. The implementation involves a process that redundantly computes many sub-problems. To make the algorithm more efficient, we will use a technique called dynamic programming, which systematically records the answers to sub-problems in a table. Dynamic programming eliminates the redundant computation of sub-problems through the use of additional memory. The dynamic programming approach is recursive. We compute the edit distance of two strings incre- mentally by first computing the edit distance of the prefixes of the two strings. In turn, the edit distance of each prefix can be computed from the edit distance of even smaller prefixes. This process will eventually continue until the prefix size is one. At this point, the problem of determining edit distance is as simple as comparing the first two symbols in the strings. To perform this recursive computation, we must build a table where each table cell corresponds to the edit distance of a pair of prefixes. The initial table is shown in Figure 3(a). One string Y is aligned across the top and the other X is aligned down the left. Each cell of the table T can be identified using indices. For example, T [i][j] would correspond to the cell in row i and column j. Row zero and column zero are always initialized as shown in the figure. The initialization actually has meaning. For example, the value zero in T [0][0] indicates that the edit distance of two empty strings is zero. The value five in T [0][5] indicates that the edit distance between Y [0,... , 4] = agtac and the empty string is five. And the value nine in T [9][0] indicates that the edit distance between X[0,... , 8] = gtatcgtat and the empty string is nine. In general, the value T [i][j] represents the edit distance between the strings Y [0,... , j-1] and X[0,... , i-1].
0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9
a g t a c g t c a t
g t a t c g t a t
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
Column index j
Row index i
of T[i][j]
Neighbors
X = g t a t c g t a t Y = a g t a c g t c a t
Figure 3: The initial dynamic programming table.
Computing the Table
memory required is 2(n+1). This is described as a linear memory algorithm because the amount of memory is linearly proportional to the length of the input strings, which is a very nice feature.^1
Your submission must include only your source code (.h and .cpp suffix) and a brief report as a readme.txt file. Your submission should NOT include any files with a .exe, .dsp, .dsw, .ncb, or .opt suffix. Every file should have your name in a comment line at the top. Your readme.txt file should have a brief description of your program design, the breakdown of the files, which compiler you used (VC++ .NET will be taken as the default), a summary of what you think works and fails in your program. Exact details of the web-based submission procedure you must follow will be posted on the project web page. Briefly, you should submit a single zip, tar, or gzip file containing your source code files and readme.txt file. You can submit your project multiple times; only the most recent project submission will be graded. The most recent project submission will also be used to compute late days, if any.
(^1) You are not required to implement this optimization.
A project that does not follow the submission guidelines will receive a 10 point deduction. Proper submission is entirely your responsibility. Contact your TA if you have any doubts whatsoever about your submission. Do NOT submit your project via email. Be sure to keep copies of your files, and do not change them after submitting. Don’t even copy them. It is a good idea to keep your submission files in a private (protected) directory on RCS to have a valid timestamp. After grades are posted, you have exactly one week to resolve all problems. After that week is up, all grades are final.