/
TestSpellChecker.cs
496 lines (434 loc) · 17.7 KB
/
TestSpellChecker.cs
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using NUnit.Framework;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using IndexWriter = Lucene.Net.Index.IndexWriter;
using SimpleAnalyzer = Lucene.Net.Analysis.SimpleAnalyzer;
using Document = Lucene.Net.Documents.Document;
using English = Lucene.Net.Test.Util.Spell.English;
using Field = Lucene.Net.Documents.Field;
using IndexReader = Lucene.Net.Index.IndexReader;
using Directory = Lucene.Net.Store.Directory;
using LuceneDictionary = SpellChecker.Net.Search.Spell.LuceneDictionary;
using System.Collections;
using Lucene.Net.Store;
using System.Threading;
using SpellChecker.Net.Search.Spell;
using Lucene.Net.Search;
namespace SpellChecker.Net.Test.Search.Spell
{
/// <summary> Test case
///
/// </summary>
/// <author> Nicolas Maisonneuve
/// </author>
[TestFixture]
public class TestSpellChecker
{
private SpellCheckerMock spellChecker;
private Directory userindex, spellindex;
public ArrayList searchers;
private Random random = new Random();
[SetUp]
public virtual void SetUp()
{
//create a user index
userindex = new RAMDirectory();
IndexWriter writer = new IndexWriter(userindex, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
for (int i = 0; i < 1000; i++)
{
Document doc = new Document();
doc.Add(new Field("field1", English.IntToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("field2", English.IntToEnglish(i + 1), Field.Store.YES, Field.Index.ANALYZED)); // + word thousand
writer.AddDocument(doc);
}
writer.Close();
// create the spellChecker
spellindex = new RAMDirectory();
searchers = ArrayList.Synchronized(new ArrayList());
spellChecker = new SpellCheckerMock(spellindex, this);
}
[Test]
public virtual void TestBuild()
{
try
{
IndexReader r = IndexReader.Open(userindex, true);
spellChecker.ClearIndex();
Addwords(r, "field1");
int num_field1 = this.Numdoc();
Addwords(r, "field2");
int num_field2 = this.Numdoc();
Assert.AreEqual (num_field2, num_field1 + 1);
AssertLastSearcherOpen(4);
CheckCommonSuggestions(r);
CheckLevenshteinSuggestions(r);
spellChecker.setStringDistance(new JaroWinklerDistance());
spellChecker.SetAccuracy(0.8f);
CheckCommonSuggestions(r);
CheckJaroWinklerSuggestions();
spellChecker.setStringDistance(new NGramDistance(2));
spellChecker.SetAccuracy(0.5f);
CheckCommonSuggestions(r);
CheckNGramSuggestions();
}
catch (System.IO.IOException e)
{
System.Console.Error.WriteLine(e.StackTrace);
Assert.Fail();
}
}
private void CheckCommonSuggestions(IndexReader r)
{
String[] similar = spellChecker.SuggestSimilar("fvie", 2);
Assert.True(similar.Length > 0);
Assert.AreEqual(similar[0], "five");
similar = spellChecker.SuggestSimilar("five", 2);
if (similar.Length > 0)
{
Assert.False(similar[0].Equals("five")); // don't suggest a word for itself
}
similar = spellChecker.SuggestSimilar("fiv", 2);
Assert.True(similar.Length > 0);
Assert.AreEqual(similar[0], "five");
similar = spellChecker.SuggestSimilar("fives", 2);
Assert.True(similar.Length > 0);
Assert.AreEqual(similar[0], "five");
Assert.True(similar.Length > 0);
similar = spellChecker.SuggestSimilar("fie", 2);
Assert.AreEqual(similar[0], "five");
// test restraint to a field
similar = spellChecker.SuggestSimilar("tousand", 10, r, "field1", false);
Assert.AreEqual(0, similar.Length); // there isn't the term thousand in the field field1
similar = spellChecker.SuggestSimilar("tousand", 10, r, "field2", false);
Assert.AreEqual(1, similar.Length); // there is the term thousand in the field field2
}
private void CheckLevenshteinSuggestions(IndexReader r)
{
// test small word
String[] similar = spellChecker.SuggestSimilar("fvie", 2);
Assert.AreEqual(1, similar.Length);
Assert.AreEqual(similar[0], "five");
similar = spellChecker.SuggestSimilar("five", 2);
Assert.AreEqual(1, similar.Length);
Assert.AreEqual(similar[0], "nine"); // don't suggest a word for itself
similar = spellChecker.SuggestSimilar("fiv", 2);
Assert.AreEqual(1, similar.Length);
Assert.AreEqual(similar[0], "five");
similar = spellChecker.SuggestSimilar("ive", 2);
Assert.AreEqual(2, similar.Length);
Assert.AreEqual(similar[0], "five");
Assert.AreEqual(similar[1], "nine");
similar = spellChecker.SuggestSimilar("fives", 2);
Assert.AreEqual(1, similar.Length);
Assert.AreEqual(similar[0], "five");
similar = spellChecker.SuggestSimilar("fie", 2);
Assert.AreEqual(2, similar.Length);
Assert.AreEqual(similar[0], "five");
Assert.AreEqual(similar[1], "nine");
similar = spellChecker.SuggestSimilar("fi", 2);
Assert.AreEqual(1, similar.Length);
Assert.AreEqual(similar[0], "five");
// test restraint to a field
similar = spellChecker.SuggestSimilar("tousand", 10, r, "field1", false);
Assert.AreEqual(0, similar.Length); // there isn't the term thousand in the field field1
similar = spellChecker.SuggestSimilar("tousand", 10, r, "field2", false);
Assert.AreEqual(1, similar.Length); // there is the term thousand in the field field2
similar = spellChecker.SuggestSimilar("onety", 2);
Assert.AreEqual(2, similar.Length);
Assert.AreEqual(similar[0], "ninety");
Assert.AreEqual(similar[1], "one");
try
{
similar = spellChecker.SuggestSimilar("tousand", 10, r, null, false);
}
catch (NullReferenceException e)
{
Assert.True(false, "threw an NPE, and it shouldn't have");
}
}
private void CheckJaroWinklerSuggestions()
{
String[] similar = spellChecker.SuggestSimilar("onety", 2);
Assert.AreEqual(2, similar.Length);
Assert.AreEqual(similar[0], "one");
Assert.AreEqual(similar[1], "ninety");
}
private void CheckNGramSuggestions()
{
String[] similar = spellChecker.SuggestSimilar("onety", 2);
Assert.AreEqual(2, similar.Length);
Assert.AreEqual(similar[0], "one");
Assert.AreEqual(similar[1], "ninety");
}
private void Addwords(IndexReader r, System.String field)
{
long time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;
spellChecker.IndexDictionary(new LuceneDictionary(r, field));
time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - time;
//System.out.println("time to build " + field + ": " + time);
}
private int Numdoc()
{
IndexReader rs = IndexReader.Open(spellindex);
int num = rs.NumDocs();
Assert.IsTrue(num != 0);
//System.out.println("num docs: " + num);
rs.Close();
return num;
}
[Test]
public void TestClose()
{
IndexReader r = IndexReader.Open(userindex, true);
spellChecker.ClearIndex();
String field = "field1";
Addwords(r, "field1");
int num_field1 = this.Numdoc();
Addwords(r, "field2");
int num_field2 = this.Numdoc();
Assert.AreEqual(num_field2, num_field1 + 1);
CheckCommonSuggestions(r);
AssertLastSearcherOpen(4);
spellChecker.Close();
AssertSearchersClosed();
try
{
spellChecker.Close();
Assert.Fail("spellchecker was already closed");
}
catch (AlreadyClosedException e)
{
// expected
}
try
{
CheckCommonSuggestions(r);
Assert.Fail("spellchecker was already closed");
}
catch (AlreadyClosedException e)
{
// expected
}
try
{
spellChecker.ClearIndex();
Assert.Fail("spellchecker was already closed");
}
catch (AlreadyClosedException e)
{
// expected
}
try
{
spellChecker.IndexDictionary(new LuceneDictionary(r, field));
Assert.Fail("spellchecker was already closed");
}
catch (AlreadyClosedException e)
{
// expected
}
try
{
spellChecker.SetSpellIndex(spellindex);
Assert.Fail("spellchecker was already closed");
}
catch (AlreadyClosedException e)
{
// expected
}
Assert.AreEqual(4, searchers.Count);
AssertSearchersClosed();
}
/*
* tests if the internally shared indexsearcher is correctly closed
* when the spellchecker is concurrently accessed and closed.
*/
[Test]
public void TestConcurrentAccess()
{
Assert.AreEqual(1, searchers.Count);
IndexReader r = IndexReader.Open(userindex, true);
spellChecker.ClearIndex();
Assert.AreEqual(2, searchers.Count);
Addwords(r, "field1");
Assert.AreEqual(3, searchers.Count);
int num_field1 = this.Numdoc();
Addwords(r, "field2");
Assert.AreEqual(4, searchers.Count);
int num_field2 = this.Numdoc();
Assert.AreEqual(num_field2, num_field1 + 1);
int numThreads = 5 + this.random.Next(5);
SpellCheckWorker[] workers = new SpellCheckWorker[numThreads];
for (int i = 0; i < numThreads; i++)
{
SpellCheckWorker spellCheckWorker = new SpellCheckWorker(r, this);
spellCheckWorker.start();
workers[i] = spellCheckWorker;
}
int iterations = 5 + random.Next(5);
for (int i = 0; i < iterations; i++)
{
Thread.Sleep(100);
// concurrently reset the spell index
spellChecker.SetSpellIndex(this.spellindex);
// for debug - prints the internal Open searchers
// showSearchersOpen();
}
spellChecker.Close();
joinAll(workers, 5000);
for (int i = 0; i < workers.Length; i++)
{
Assert.False(workers[i].failed);
Assert.True(workers[i].terminated);
}
// 4 searchers more than iterations
// 1. at creation
// 2. ClearIndex()
// 2. and 3. during Addwords
Assert.AreEqual(iterations + 4, searchers.Count);
AssertSearchersClosed();
}
private void joinAll(SpellCheckWorker[] workers, long timeout)
{
for (int j = 0; j < workers.Length; j++)
{
long time = (long)DateTime.Now.TimeOfDay.TotalMilliseconds;
if (timeout < 0)
{
// this could be helpful if it Assert.Fails one day
Console.WriteLine("Warning: " + (workers.Length - j)
+ " threads have not joined but joinall timed out");
break;
}
workers[j].join(timeout);
timeout -= (long)DateTime.Now.TimeOfDay.TotalMilliseconds - time;
}
}
private void AssertLastSearcherOpen(int numSearchers)
{
Assert.AreEqual(numSearchers, searchers.Count);
Object[] searcherArray = searchers.ToArray();
for (int i = 0; i < searcherArray.Length; i++)
{
if (i == searcherArray.Length - 1)
{
Assert.True(
((IndexSearcher)searcherArray[i]).GetIndexReader().GetRefCount() > 0,
"expected last searcher Open but was closed");
}
else
{
Assert.False(
((IndexSearcher)searcherArray[i]).GetIndexReader().GetRefCount() > 0,
"expected closed searcher but was Open - Index: " + i);
}
}
}
private void AssertSearchersClosed()
{
Object[] searcherArray = searchers.ToArray();
for (int i = 0; i < searcherArray.Length; i++)
{
Assert.AreEqual(0, ((IndexSearcher)searcherArray[i]).GetIndexReader().GetRefCount());
}
}
private void ShowSearchersOpen()
{
int count = 0;
Object[] searcherArray = searchers.ToArray();
for (int i = 0; i < searcherArray.Length; i++)
{
if (((IndexSearcher)searcherArray[i]).GetIndexReader().GetRefCount() > 0)
++count;
}
Console.WriteLine(count);
}
private class SpellCheckWorker
{
private readonly IndexReader reader;
public bool terminated = false;
public bool failed = false;
private Thread m_thread;
private TestSpellChecker enclosingInstance;
public SpellCheckWorker(IndexReader reader, TestSpellChecker enclInstance)
: base()
{
this.reader = reader;
enclosingInstance = enclInstance;
m_thread = new Thread(run);
}
public void run()
{
try
{
while (true)
{
try
{
enclosingInstance.CheckCommonSuggestions(reader);
}
catch (AlreadyClosedException e)
{
return;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
failed = true;
return;
}
}
}
finally
{
this.terminated = true;
}
}
public void join(long timeout)
{
m_thread.Join((int)timeout);
}
public void start()
{
m_thread.Start();
}
}
public class SpellCheckerMock : SpellChecker.Net.Search.Spell.SpellChecker
{
private TestSpellChecker enclosingInstance;
ArrayList searchers = ArrayList.Synchronized(new ArrayList()); // <--New !!!!!!!
public SpellCheckerMock(Directory spellIndex, TestSpellChecker inst)
: base(spellIndex)
{
enclosingInstance = inst;
enclosingInstance.searchers = searchers; //Note: this code is invoked after createSearcher
}
public SpellCheckerMock(Directory spellIndex, StringDistance sd)
: base(spellIndex, sd)
{
}
public override IndexSearcher CreateSearcher(Directory dir)
{
IndexSearcher searcher = base.CreateSearcher(dir);
searchers.Add(searcher);
return searcher;
}
}
}
}