1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/** @file api_spelling.cc
* @brief Test the spelling correction suggestion API.
*/
/* Copyright (C) 2007,2008,2009,2010,2011 Olly Betts
* Copyright (C) 2007 Lemur Consulting Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include "api_spelling.h"
#include <xapian.h>
#include "apitest.h"
#include "testsuite.h"
#include "testutils.h"
#include <string>
using namespace std;
// Test add_spelling() and remove_spelling(), which remote dbs support.
DEFINE_TESTCASE(spell0, spelling || remote) {
Xapian::WritableDatabase db = get_writable_database();
db.add_spelling("hello");
db.add_spelling("cell", 2);
db.commit();
db.add_spelling("zig");
db.add_spelling("ch");
db.add_spelling("hello", 2);
db.remove_spelling("hello", 2);
db.remove_spelling("cell", 6);
db.commit();
db.remove_spelling("hello");
db.remove_spelling("nonsuch");
db.remove_spelling("zzzzzzzzz", 1000000);
db.remove_spelling("aarvark");
db.remove_spelling("hello");
db.commit();
db.remove_spelling("hello");
return true;
}
// Test basic spelling correction features.
DEFINE_TESTCASE(spell1, spelling) {
Xapian::WritableDatabase db = get_writable_database();
// Check that the more frequent term is chosen.
db.add_spelling("hello");
TEST_EQUAL(db.get_spelling_suggestion("cell"), "hello");
db.add_spelling("cell", 2);
TEST_EQUAL(db.get_spelling_suggestion("hell"), "cell");
db.commit();
Xapian::Database dbr(get_writable_database_as_database());
TEST_EQUAL(db.get_spelling_suggestion("hell"), "cell");
TEST_EQUAL(dbr.get_spelling_suggestion("hell"), "cell");
// Check suggestions for single edit errors to "zig".
db.add_spelling("zig");
// Transpositions:
TEST_EQUAL(db.get_spelling_suggestion("izg"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zgi"), "zig");
// Substitutions:
TEST_EQUAL(db.get_spelling_suggestion("sig"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zog"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zif"), "zig");
// Deletions:
TEST_EQUAL(db.get_spelling_suggestion("ig"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zg"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zi"), "zig");
// Insertions:
TEST_EQUAL(db.get_spelling_suggestion("azig"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("zaig"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("ziag"), "zig");
TEST_EQUAL(db.get_spelling_suggestion("ziga"), "zig");
// Check suggestions for single edit errors to "ch".
db.add_spelling("ch");
// Transpositions:
TEST_EQUAL(db.get_spelling_suggestion("hc"), "ch");
// Substitutions - we don't handle these for two character words:
TEST_EQUAL(db.get_spelling_suggestion("qh"), "");
TEST_EQUAL(db.get_spelling_suggestion("cq"), "");
// Deletions would leave a single character, and we don't handle those.
TEST_EQUAL(db.get_spelling_suggestion("c"), "");
TEST_EQUAL(db.get_spelling_suggestion("h"), "");
// Insertions:
TEST_EQUAL(db.get_spelling_suggestion("qch"), "ch");
TEST_EQUAL(db.get_spelling_suggestion("cqh"), "ch");
TEST_EQUAL(db.get_spelling_suggestion("chq"), "ch");
// Check assorted cases:
TEST_EQUAL(db.get_spelling_suggestion("shello"), "hello");
TEST_EQUAL(db.get_spelling_suggestion("hellot"), "hello");
TEST_EQUAL(db.get_spelling_suggestion("acell"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("cella"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("acella"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("helo"), "hello");
TEST_EQUAL(db.get_spelling_suggestion("cll"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("helol"), "hello");
TEST_EQUAL(db.get_spelling_suggestion("clel"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("ecll"), "cell");
TEST_EQUAL(db.get_spelling_suggestion("cll"), "cell");
// Check that edit distance 3 isn't found by default:
TEST_EQUAL(db.get_spelling_suggestion("shelolx"), "");
TEST_EQUAL(db.get_spelling_suggestion("celling"), "");
TEST_EQUAL(db.get_spelling_suggestion("dellin"), "");
// Check that edit distance 3 is found if specified:
TEST_EQUAL(db.get_spelling_suggestion("shelolx", 3), "hello");
TEST_EQUAL(db.get_spelling_suggestion("celling", 3), "cell");
TEST_EQUAL(db.get_spelling_suggestion("dellin", 3), "cell");
// Make "hello" more frequent than "cell" (3 vs 2).
db.add_spelling("hello", 2);
TEST_EQUAL(db.get_spelling_suggestion("hell"), "hello");
db.commit();
TEST_EQUAL(db.get_spelling_suggestion("cello"), "hello");
db.remove_spelling("hello", 2);
TEST_EQUAL(db.get_spelling_suggestion("hell"), "cell");
// Test "over-removing".
db.remove_spelling("cell", 6);
TEST_EQUAL(db.get_spelling_suggestion("cell"), "hello");
db.commit();
TEST_EQUAL(db.get_spelling_suggestion("cell"), "hello");
db.remove_spelling("hello");
TEST_EQUAL(db.get_spelling_suggestion("cell"), "");
// Test removing words not in the table.
db.remove_spelling("nonsuch");
db.remove_spelling("zzzzzzzzz", 1000000);
db.remove_spelling("aarvark");
// Try removing word which was present but no longer is.
db.remove_spelling("hello");
db.commit();
db.remove_spelling("hello");
return true;
}
// Test spelling correction for Unicode.
DEFINE_TESTCASE(spell2, spelling) {
Xapian::WritableDatabase db = get_writable_database();
// Check that a UTF-8 sequence counts as a single character.
db.add_spelling("h\xc3\xb6hle");
db.add_spelling("ascii");
TEST_EQUAL(db.get_spelling_suggestion("hohle", 1), "h\xc3\xb6hle");
TEST_EQUAL(db.get_spelling_suggestion("hhle", 1), "h\xc3\xb6hle");
TEST_EQUAL(db.get_spelling_suggestion("\xf0\xa8\xa8\x8f\xc3\xb6le", 2), "h\xc3\xb6hle");
TEST_EQUAL(db.get_spelling_suggestion("hh\xc3\xb6l"), "h\xc3\xb6hle");
TEST_EQUAL(db.get_spelling_suggestion("as\xc3\xb6\xc3\xb7i"), "ascii");
TEST_EQUAL(db.get_spelling_suggestion("asc\xc3\xb6i\xc3\xb7i"), "ascii");
db.commit();
Xapian::Database dbr(get_writable_database_as_database());
TEST_EQUAL(dbr.get_spelling_suggestion("hohle", 1), "h\xc3\xb6hle");
TEST_EQUAL(dbr.get_spelling_suggestion("hhle", 1), "h\xc3\xb6hle");
TEST_EQUAL(dbr.get_spelling_suggestion("\xf0\xa8\xa8\x8f\xc3\xb6le", 2), "h\xc3\xb6hle");
TEST_EQUAL(dbr.get_spelling_suggestion("hh\xc3\xb6l"), "h\xc3\xb6hle");
TEST_EQUAL(dbr.get_spelling_suggestion("as\xc3\xb6\xc3\xb7i"), "ascii");
TEST_EQUAL(dbr.get_spelling_suggestion("asc\xc3\xb6i\xc3\xb7i"), "ascii");
return true;
}
// Test spelling correction with multi databases
DEFINE_TESTCASE(spell3, spelling) {
Xapian::WritableDatabase db1 = get_writable_database();
// We can't just call get_writable_database() since it would delete db1
// which doesn't work at all under __WIN32__ and will go wrong elsewhere if
// changes to db1 are committed.
Xapian::WritableDatabase db2 = get_named_writable_database("spell3", "");
db1.add_spelling("hello");
db1.add_spelling("cell", 2);
db2.add_spelling("hello", 2);
db2.add_spelling("helo");
Xapian::Database db;
db.add_database(db1);
db.add_database(db2);
TEST_EQUAL(db.get_spelling_suggestion("hello"), "");
TEST_EQUAL(db.get_spelling_suggestion("hell"), "hello");
TEST_EQUAL(db1.get_spelling_suggestion("hell"), "cell");
TEST_EQUAL(db2.get_spelling_suggestion("hell"), "hello");
// Test spelling iterator
Xapian::TermIterator i(db1.spellings_begin());
TEST_EQUAL(*i, "cell");
TEST_EQUAL(i.get_termfreq(), 2);
++i;
TEST_EQUAL(*i, "hello");
TEST_EQUAL(i.get_termfreq(), 1);
++i;
TEST(i == db1.spellings_end());
i = db2.spellings_begin();
TEST_EQUAL(*i, "hello");
TEST_EQUAL(i.get_termfreq(), 2);
++i;
TEST_EQUAL(*i, "helo");
TEST_EQUAL(i.get_termfreq(), 1);
++i;
TEST(i == db2.spellings_end());
i = db.spellings_begin();
TEST_EQUAL(*i, "cell");
TEST_EQUAL(i.get_termfreq(), 2);
++i;
TEST_EQUAL(*i, "hello");
TEST_EQUAL(i.get_termfreq(), 3);
++i;
TEST_EQUAL(*i, "helo");
TEST_EQUAL(i.get_termfreq(), 1);
++i;
TEST(i == db.spellings_end());
return true;
}
// Regression test - check that appending works correctly.
DEFINE_TESTCASE(spell4, spelling) {
Xapian::WritableDatabase db = get_writable_database();
db.add_spelling("check");
db.add_spelling("pecks", 2);
db.commit();
db.add_spelling("becky");
db.commit();
TEST_EQUAL(db.get_spelling_suggestion("jeck", 2), "pecks");
return true;
}
// Regression test - used to segfault with some input values.
DEFINE_TESTCASE(spell5, spelling) {
const char * target = "\xe4\xb8\x80\xe4\xba\x9b";
Xapian::WritableDatabase db = get_writable_database();
db.add_spelling(target);
db.commit();
string s = db.get_spelling_suggestion("\xe4\xb8\x8d", 3);
TEST_EQUAL(s, target);
return true;
}
// Test basic spelling correction features.
DEFINE_TESTCASE(spell6, spelling) {
Xapian::WritableDatabase db = get_writable_database();
// Check that the more frequent term is chosen.
db.add_spelling("hello", 2);
db.add_spelling("sell", 3);
TEST_EQUAL(db.get_spelling_suggestion("hell"), "sell");
db.commit();
Xapian::Database dbr(get_writable_database_as_database());
TEST_EQUAL(db.get_spelling_suggestion("hell"), "sell");
TEST_EQUAL(dbr.get_spelling_suggestion("hell"), "sell");
return true;
}
// Test suggestions when there's an exact match.
DEFINE_TESTCASE(spell7, spelling) {
Xapian::WritableDatabase db = get_writable_database();
// Check that the more frequent term is chosen.
db.add_spelling("word", 57);
db.add_spelling("wrod", 3);
db.add_spelling("sword", 56);
db.add_spelling("words", 57);
db.add_spelling("ward", 58);
db.commit();
TEST_EQUAL(db.get_spelling_suggestion("ward"), "");
TEST_EQUAL(db.get_spelling_suggestion("words"), "word");
TEST_EQUAL(db.get_spelling_suggestion("sword"), "word");
TEST_EQUAL(db.get_spelling_suggestion("wrod"), "word");
return true;
}
/// Regression test - repeated trigrams cancelled in 1.2.5 and earlier.
DEFINE_TESTCASE(spell8, spelling) {
Xapian::WritableDatabase db = get_writable_database();
// kin and kin used to cancel out in "skinking".
db.add_spelling("skinking", 2);
db.add_spelling("stinking", 1);
db.commit();
TEST_EQUAL(db.get_spelling_suggestion("scimkin", 3), "skinking");
return true;
}
|