| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * This header defines the Dictionary intrinsic class. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* include our base class definition */ |
| 14 | #include "systype.h" |
| 15 | |
| 16 | /* |
| 17 | * The Dictionary intrinsic class is a specialized lookup table class |
| 18 | * designed for storing the vocabulary table for a parser. Dictionary |
| 19 | * works closely with GrammarProd to supply the vocabulary tokens for the |
| 20 | * productions. |
| 21 | * |
| 22 | * The main difference between Dictionary and a more general hash table is |
| 23 | * that Dictionary tags each vocabulary word with a type; for our purposes, |
| 24 | * the type is the vocabulary property (&noun, &adjective, etc) that |
| 25 | * associates the word with an object. |
| 26 | */ |
| 27 | intrinsic class Dictionary 'dictionary2/030001': Object |
| 28 | { |
| 29 | /* |
| 30 | * Constructor: |
| 31 | * |
| 32 | * new Dictionary() - creates a dictionary with a default comparator, |
| 33 | * which matches strings exactly (note that upper-case and lower-case |
| 34 | * letters are considered distinct) |
| 35 | * |
| 36 | * new Dictionary(compObj) - create a dictionary with the given |
| 37 | * comparator object. A comparator is an object that implements the |
| 38 | * comparator interface - see below for details. When searching the |
| 39 | * dictionary (with findWord, for example), we use the comparator |
| 40 | * object to determine if the string beings sought matches a dictionary |
| 41 | * string. |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * Set the comparator object. This defines how words are compared. |
| 46 | * The object must provide the following methods, which comprise the |
| 47 | * "comparator" interface. Note that there's no class that defines |
| 48 | * this interface; this is simply a set of methods that we define here, |
| 49 | * and which the supplied object must define. |
| 50 | * |
| 51 | * calcHash(str) - returns an integer giving the hash value of the |
| 52 | * given string. The purpose of the hash value is to arbitrarily |
| 53 | * partition the search space, so that we can search only a small |
| 54 | * subset of the dictionary when looking for a particular string. It |
| 55 | * is desirable for hash values to distribute uniformly for a given set |
| 56 | * of strings. It's also highly desirable for the hash computation to |
| 57 | * be inexpensive (i.e., to run fast), since the whole point of the |
| 58 | * hash is to reduce the amount of time it takes to find a string; if |
| 59 | * it takes longer to compute the hash value than it would to search |
| 60 | * every string in the table, then we don't come out ahead using the |
| 61 | * hash. |
| 62 | * |
| 63 | * matchValues(inputStr, dictStr) - compare the given input string with |
| 64 | * the given dictionary string, and return a result indicating whether |
| 65 | * or not they match for the purposes of the comparator. A return |
| 66 | * value of zero or nil indicates that the values do not match; any |
| 67 | * other return value indicates a match. |
| 68 | * |
| 69 | * Typically, matchValues() will return a non-zero integer to indicate |
| 70 | * a match and to encode additional information about the match using a |
| 71 | * bitwise-OR'd combination of flag values. For example, a comparator |
| 72 | * that allows case folding could use bit flag 0x0001 to indicate any |
| 73 | * match, and bit flag 0x0002 to indicate a match where the case of one |
| 74 | * or more input letters did not match the case of the corresponding |
| 75 | * letters in the dictionary string. So, a return value of 0x0001 |
| 76 | * would indicate an exact match, and 0x0003 would indicate a match |
| 77 | * with case differences. |
| 78 | * |
| 79 | * Note the asymmetry in the matchValues() arguments: we specifically |
| 80 | * designate one string as the input string and one as the dictionary |
| 81 | * string. This allows for asymmetrical comparisons, which are |
| 82 | * desirable in some cases: we sometimes want a given input string to |
| 83 | * match a given dictionary string even when the two are not identical |
| 84 | * character-by-character. For example, we might want to allow the |
| 85 | * user to type only the first six or eight characters of a string in |
| 86 | * the dictionary, to save typing; or, we might want to allow a user to |
| 87 | * enter unaccented letters and still match dictionary words containing |
| 88 | * the corresponding letters with accents. The asymmetry in the |
| 89 | * arguments is there because we often only want these "fuzzy" match |
| 90 | * rules to work in one direction; for the truncation example, we'd |
| 91 | * want an input word that's a truncated version of a dictionary word |
| 92 | * to match, but not the other way around. |
| 93 | * |
| 94 | * Important: Note that, although the hash value computation is up to |
| 95 | * the implementing object to define, we impose one requirement. It is |
| 96 | * REQUIRED that for any two strings s1 and s2, if matchValues(s1, s2) |
| 97 | * indicates a match (i.e., returns a value other than 0 or nil), then |
| 98 | * calcHash(s1) MUST EQUAL calcHash(s2). (This does NOT mean that two |
| 99 | * strings with equal hash values must be equal, or, equivalently, that |
| 100 | * two unequal strings must have different hash values. Hash |
| 101 | * collisions are explicitly allowed, so two strings that don't match |
| 102 | * can still have the same hash value.) |
| 103 | */ |
| 104 | setComparator(compObj); |
| 105 | |
| 106 | /* |
| 107 | * Find a word; returns a list giving the objects associated with the |
| 108 | * string in the dictionary. If voc_prop is specified, only objects |
| 109 | * associated with the word by the given vocabulary property are |
| 110 | * returned. We match the string using the comparator defined for the |
| 111 | * dictionary. |
| 112 | * |
| 113 | * The return value is a list consisting of pairs of entries. The |
| 114 | * first element of each pair is the matching object, and the second is |
| 115 | * gives the comparator result for matching the word. If we use a |
| 116 | * StringComparator, this will be a non-zero integer value giving |
| 117 | * information on truncation, case folding, and any equivalence |
| 118 | * mappings defined in the comparator. If the comparator is a custom |
| 119 | * object, then the second element of the pair will be whatever the |
| 120 | * custom comparator's matchValues() method returned for matching the |
| 121 | * value for that dictionary entry. |
| 122 | * |
| 123 | * The reason for giving a matchValues() return value for every |
| 124 | * individual match is that the same input string 'str' might match |
| 125 | * multiple entries in the dictionary. For example, the same string |
| 126 | * might match one word exactly and one with truncation. The match |
| 127 | * result code lets the caller determine if some matches are "better" |
| 128 | * than others, based on how the string matched for each individual |
| 129 | * object entry. |
| 130 | */ |
| 131 | findWord(str, voc_prop?); |
| 132 | |
| 133 | /* |
| 134 | * Add a word to the dictionary, associating the given object with the |
| 135 | * given string and property combination. |
| 136 | */ |
| 137 | addWord(obj, str, voc_prop); |
| 138 | |
| 139 | /* |
| 140 | * Remove the given word association from the dictionary. This |
| 141 | * removes only the association for the given object; other objects |
| 142 | * associated with the same word are not affected. |
| 143 | */ |
| 144 | removeWord(obj, str, voc_prop); |
| 145 | |
| 146 | /* |
| 147 | * Check to see if the given string 'str' is defined in the dictionary. |
| 148 | * Returns true if the word is defined, nil if not. |
| 149 | * |
| 150 | * If the 'filter' argument is provided, it gives a callback function |
| 151 | * that is invoked to determine whether or not to count a particular |
| 152 | * word in the dictionary as a match. The callback is invoked with one |
| 153 | * argument: (filter)(match), where 'match' is the result of the |
| 154 | * comparator's matchValues(str,dstr) method, where 'dstr' is a |
| 155 | * dictionary string matching 'str'. The filter function returns true |
| 156 | * if the string should be counted as a match, nil if not. The return |
| 157 | * value of isWordDefined thus will be true if the filter function |
| 158 | * returns true for at least one match, nil if not. The purpose of the |
| 159 | * filter function is to allow the caller to impose a more restrictive |
| 160 | * condition than the dictionary's current comparator does; for |
| 161 | * example, the caller might use the filter to determine if the |
| 162 | * dictionary contains any matches for 'str' that match without any |
| 163 | * truncation. |
| 164 | */ |
| 165 | isWordDefined(str, filter?); |
| 166 | |
| 167 | /* |
| 168 | * Invoke the callback func(obj, str, prop) for each word in the |
| 169 | * dictionary. Note that the callback can be invoked with a single |
| 170 | * string multiple times, since the callback is invoked once per |
| 171 | * word/object/property association; in other words, the callback is |
| 172 | * invoked once for each association created with addWord() or during |
| 173 | * compilation. |
| 174 | */ |
| 175 | forEachWord(func); |
| 176 | |
| 177 | /* |
| 178 | * Get a list of possible spelling corrections for the given word. |
| 179 | * This searches the dictionary for words that match the given word |
| 180 | * within the given maximum "edit distance". |
| 181 | * |
| 182 | * The return value is a list giving all of the words in the dictionary |
| 183 | * that match the input string within the given maximum edit distance. |
| 184 | * Any given dictionary word will appear only once in the returned |
| 185 | * list. The list is in arbitrary order. Each entry consists of a |
| 186 | * sublist, [word, dist, repl], where 'word' is a matching dictionary |
| 187 | * word, 'dist' is the edit distance between that dictionary word and |
| 188 | * the input string, and 'repl' is the number of character replacements |
| 189 | * performed. (The replacement count is included in the edit distance, |
| 190 | * but it's called out separately because some correctors treat |
| 191 | * replacements as heavier changes than other edits. A caller could |
| 192 | * use this to break ties for corrections of the same distance. |
| 193 | * Consider "book" and "box" as corrections for "bok": both have edit |
| 194 | * distance 1, but "book" has no replacements, while "box" has one.) |
| 195 | * |
| 196 | * The edit distance between two words is defined as the number of |
| 197 | * single-character insertions, deletions, replacements, and |
| 198 | * transpositions necessary to transform one word into another. For |
| 199 | * example, OPNE can be transformed into OPEN by transposing the N-E |
| 200 | * pair, for an edit distance of 1. XAEMINE can be transformed into |
| 201 | * EXAMINE by inserting an E at the beginning, and then deleting the E |
| 202 | * at the third letter, for an edit distance of 2. |
| 203 | * |
| 204 | * Choosing the maximum edit distance is essentially heuristic. Higher |
| 205 | * values make the search take longer, and yield more matches - which |
| 206 | * increases the chances that the right match will be found, but also |
| 207 | * increases the number of false matches to sift through. The |
| 208 | * literature on spelling correction suggests that 2 is a good value in |
| 209 | * practice, across a wide range of applications, based on the most |
| 210 | * frequent patterns of human typographical errors. However, you'll |
| 211 | * probably do better to vary the distance based on the word length: |
| 212 | * perhaps 1 for words up to 4 letters, 2 for 5-7 letters, and 3 for |
| 213 | * words of 8 letters or more. |
| 214 | * |
| 215 | * If the dictionary has a StringComparator object as its current |
| 216 | * comparator, the results will take into account its case folding |
| 217 | * setting, truncation length, and character mappings. These |
| 218 | * "approximations" are NOT considered to be edits, so they don't count |
| 219 | * against the maximum edit distance. Custom comparators (not of the |
| 220 | * StringComparator class) are ignored: if you use a custom comparator, |
| 221 | * this method will only find matches based on the exact text of the |
| 222 | * dictionary words. |
| 223 | */ |
| 224 | correctSpelling(str, maxEditDistance); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * We rely on certain methods defined by the comparator interface, so |
| 229 | * export those method names. |
| 230 | */ |
| 231 | property calcHash, matchValues; |
| 232 | export calcHash 'IfcComparator.calcHash'; |
| 233 | export matchValues 'IfcComparator.matchValues'; |
| 234 |