2016-08-24 Michael Collison <michael.collison@linaro.org>
[official-gcc.git] / gcc / spellcheck.c
blobb37b1e469299210aae99a508a57e502dd5157f87
1 /* Find near-matches for strings.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "spellcheck.h"
26 #include "selftest.h"
28 /* The Levenshtein distance is an "edit-distance": the minimal
29 number of one-character insertions, removals or substitutions
30 that are needed to change one string into another.
32 This implementation uses the Wagner-Fischer algorithm. */
34 edit_distance_t
35 levenshtein_distance (const char *s, int len_s,
36 const char *t, int len_t)
38 const bool debug = false;
40 if (debug)
42 printf ("s: \"%s\" (len_s=%i)\n", s, len_s);
43 printf ("t: \"%s\" (len_t=%i)\n", t, len_t);
46 if (len_s == 0)
47 return len_t;
48 if (len_t == 0)
49 return len_s;
51 /* We effectively build a matrix where each (i, j) contains the
52 Levenshtein distance between the prefix strings s[0:j]
53 and t[0:i].
54 Rather than actually build an (len_t + 1) * (len_s + 1) matrix,
55 we simply keep track of the last row, v0 and a new row, v1,
56 which avoids an (len_t + 1) * (len_s + 1) allocation and memory accesses
57 in favor of two (len_s + 1) allocations. These could potentially be
58 statically-allocated if we impose a maximum length on the
59 strings of interest. */
60 edit_distance_t *v0 = new edit_distance_t[len_s + 1];
61 edit_distance_t *v1 = new edit_distance_t[len_s + 1];
63 /* The first row is for the case of an empty target string, which
64 we can reach by deleting every character in the source string. */
65 for (int i = 0; i < len_s + 1; i++)
66 v0[i] = i;
68 /* Build successive rows. */
69 for (int i = 0; i < len_t; i++)
71 if (debug)
73 printf ("i:%i v0 = ", i);
74 for (int j = 0; j < len_s + 1; j++)
75 printf ("%i ", v0[j]);
76 printf ("\n");
79 /* The initial column is for the case of an empty source string; we
80 can reach prefixes of the target string of length i
81 by inserting i characters. */
82 v1[0] = i + 1;
84 /* Build the rest of the row by considering neighbors to
85 the north, west and northwest. */
86 for (int j = 0; j < len_s; j++)
88 edit_distance_t cost = (s[j] == t[i] ? 0 : 1);
89 edit_distance_t deletion = v1[j] + 1;
90 edit_distance_t insertion = v0[j + 1] + 1;
91 edit_distance_t substitution = v0[j] + cost;
92 edit_distance_t cheapest = MIN (deletion, insertion);
93 cheapest = MIN (cheapest, substitution);
94 v1[j + 1] = cheapest;
97 /* Prepare to move on to next row. */
98 for (int j = 0; j < len_s + 1; j++)
99 v0[j] = v1[j];
102 if (debug)
104 printf ("final v1 = ");
105 for (int j = 0; j < len_s + 1; j++)
106 printf ("%i ", v1[j]);
107 printf ("\n");
110 edit_distance_t result = v1[len_s];
111 delete[] v0;
112 delete[] v1;
113 return result;
116 /* Calculate Levenshtein distance between two nil-terminated strings. */
118 edit_distance_t
119 levenshtein_distance (const char *s, const char *t)
121 return levenshtein_distance (s, strlen (s), t, strlen (t));
124 /* Given TARGET, a non-NULL string, and CANDIDATES, a non-NULL ptr to
125 an autovec of non-NULL strings, determine which element within
126 CANDIDATES has the lowest edit distance to TARGET. If there are
127 multiple elements with the same minimal distance, the first in the
128 vector wins.
130 If more than half of the letters were misspelled, the suggestion is
131 likely to be meaningless, so return NULL for this case. */
133 const char *
134 find_closest_string (const char *target,
135 const auto_vec<const char *> *candidates)
137 gcc_assert (target);
138 gcc_assert (candidates);
140 int i;
141 const char *candidate;
142 best_match<const char *, const char *> bm (target);
143 FOR_EACH_VEC_ELT (*candidates, i, candidate)
145 gcc_assert (candidate);
146 bm.consider (candidate);
149 return bm.get_best_meaningful_candidate ();
152 #if CHECKING_P
154 namespace selftest {
156 /* Selftests. */
158 /* Verify that the levenshtein_distance (A, B) equals the expected
159 value. */
161 static void
162 levenshtein_distance_unit_test_oneway (const char *a, const char *b,
163 edit_distance_t expected)
165 edit_distance_t actual = levenshtein_distance (a, b);
166 ASSERT_EQ (actual, expected);
169 /* Verify that both
170 levenshtein_distance (A, B)
172 levenshtein_distance (B, A)
173 equal the expected value, to ensure that the function is symmetric. */
175 static void
176 levenshtein_distance_unit_test (const char *a, const char *b,
177 edit_distance_t expected)
179 levenshtein_distance_unit_test_oneway (a, b, expected);
180 levenshtein_distance_unit_test_oneway (b, a, expected);
183 /* Verify that find_closest_string is sane. */
185 static void
186 test_find_closest_string ()
188 auto_vec<const char *> candidates;
190 /* Verify that it can handle an empty vec. */
191 ASSERT_EQ (NULL, find_closest_string ("", &candidates));
193 /* Verify that it works sanely for non-empty vecs. */
194 candidates.safe_push ("apple");
195 candidates.safe_push ("banana");
196 candidates.safe_push ("cherry");
198 ASSERT_STREQ ("apple", find_closest_string ("app", &candidates));
199 ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
200 ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
201 ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
203 /* The order of the vec can matter, but it should not matter for these
204 inputs. */
205 candidates.truncate (0);
206 candidates.safe_push ("cherry");
207 candidates.safe_push ("banana");
208 candidates.safe_push ("apple");
209 ASSERT_STREQ ("apple", find_closest_string ("app", &candidates));
210 ASSERT_STREQ ("banana", find_closest_string ("banyan", &candidates));
211 ASSERT_STREQ ("cherry", find_closest_string ("berry", &candidates));
212 ASSERT_EQ (NULL, find_closest_string ("not like the others", &candidates));
215 /* Test data for test_metric_conditions. */
217 static const char * const test_data[] = {
219 "foo"
220 "food",
221 "boo",
222 "1234567890123456789012345678901234567890123456789012345678901234567890"
225 /* Verify that levenshtein_distance appears to be a sane distance function,
226 i.e. the conditions for being a metric. This is done directly for a
227 small set of examples, using test_data above. This is O(N^3) in the size
228 of the array, due to the test for the triangle inequality, so we keep the
229 array small. */
231 static void
232 test_metric_conditions ()
234 const int num_test_cases = sizeof (test_data) / sizeof (test_data[0]);
236 for (int i = 0; i < num_test_cases; i++)
238 for (int j = 0; j < num_test_cases; j++)
240 edit_distance_t dist_ij
241 = levenshtein_distance (test_data[i], test_data[j]);
243 /* Identity of indiscernibles: d(i, j) > 0 iff i == j. */
244 if (i == j)
245 ASSERT_EQ (dist_ij, 0);
246 else
247 ASSERT_TRUE (dist_ij > 0);
249 /* Symmetry: d(i, j) == d(j, i). */
250 edit_distance_t dist_ji
251 = levenshtein_distance (test_data[j], test_data[i]);
252 ASSERT_EQ (dist_ij, dist_ji);
254 /* Triangle inequality. */
255 for (int k = 0; k < num_test_cases; k++)
257 edit_distance_t dist_ik
258 = levenshtein_distance (test_data[i], test_data[k]);
259 edit_distance_t dist_jk
260 = levenshtein_distance (test_data[j], test_data[k]);
261 ASSERT_TRUE (dist_ik <= dist_ij + dist_jk);
267 /* Verify levenshtein_distance for a variety of pairs of pre-canned
268 inputs, comparing against known-good values. */
270 void
271 spellcheck_c_tests ()
273 levenshtein_distance_unit_test ("", "nonempty", strlen ("nonempty"));
274 levenshtein_distance_unit_test ("saturday", "sunday", 3);
275 levenshtein_distance_unit_test ("foo", "m_foo", 2);
276 levenshtein_distance_unit_test ("hello_world", "HelloWorld", 3);
277 levenshtein_distance_unit_test
278 ("the quick brown fox jumps over the lazy dog", "dog", 40);
279 levenshtein_distance_unit_test
280 ("the quick brown fox jumps over the lazy dog",
281 "the quick brown dog jumps over the lazy fox",
283 levenshtein_distance_unit_test
284 ("Lorem ipsum dolor sit amet, consectetur adipiscing elit,",
285 "All your base are belong to us",
286 44);
287 levenshtein_distance_unit_test ("foo", "FOO", 3);
289 test_find_closest_string ();
290 test_metric_conditions ();
293 } // namespace selftest
295 #endif /* #if CHECKING_P */