public void TestInternalLevenshteinDistance() { DirectSpellChecker spellchecker = new DirectSpellChecker(); Directory dir = NewDirectory(); RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, new MockAnalyzer(Random(), MockTokenizer.KEYWORD, true), Similarity, TimeZone); String[] termsToAdd = { "metanoia", "metanoian", "metanoiai", "metanoias", "metanoið‘" }; for (int i = 0; i < termsToAdd.Length; i++) { Document doc = new Document(); doc.Add(NewTextField("repentance", termsToAdd[i], Field.Store.NO)); writer.AddDocument(doc); } IndexReader ir = writer.Reader; String misspelled = "metanoix"; SuggestWord[] similar = spellchecker.SuggestSimilar(new Term("repentance", misspelled), 4, ir, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX); assertTrue(similar.Length == 4); IStringDistance sd = spellchecker.Distance; assertTrue(sd is LuceneLevenshteinDistance); foreach (SuggestWord word in similar) { assertTrue(word.Score == sd.GetDistance(word.String, misspelled)); assertTrue(word.Score == sd.GetDistance(misspelled, word.String)); // LUCNENET TODO: Perhaps change this to word.ToString()? } ir.Dispose(); writer.Dispose(); dir.Dispose(); }
public void TestGetDistance() { float d = sd.GetDistance("al", "al"); assertTrue(d == 1.0f); d = sd.GetDistance("martha", "marhta"); assertTrue(d > 0.961 && d < 0.962); d = sd.GetDistance("jones", "johnson"); assertTrue(d > 0.832 && d < 0.833); d = sd.GetDistance("abcvwxyz", "cabvwxyz"); assertTrue(d > 0.958 && d < 0.959); d = sd.GetDistance("dwayne", "duane"); assertTrue(d > 0.84 && d < 0.841); d = sd.GetDistance("dixon", "dicksonx"); assertTrue(d > 0.813 && d < 0.814); d = sd.GetDistance("fvie", "ten"); assertTrue(d == 0f); float d1 = sd.GetDistance("zac ephron", "zac efron"); float d2 = sd.GetDistance("zac ephron", "kai ephron"); assertTrue(d1 > d2); d1 = sd.GetDistance("brittney spears", "britney spears"); d2 = sd.GetDistance("brittney spears", "brittney startzman"); assertTrue(d1 > d2); }
public void TestGetDistance() { float d = sd.GetDistance("al", "al"); assertEquals(d, 1.0f, 0.001); d = sd.GetDistance("martha", "marhta"); assertEquals(d, 0.6666, 0.001); d = sd.GetDistance("jones", "johnson"); assertEquals(d, 0.4285, 0.001); d = sd.GetDistance("abcvwxyz", "cabvwxyz"); assertEquals(d, 0.75, 0.001); d = sd.GetDistance("dwayne", "duane"); assertEquals(d, 0.666, 0.001); d = sd.GetDistance("dixon", "dicksonx"); assertEquals(d, 0.5, 0.001); d = sd.GetDistance("six", "ten"); assertEquals(d, 0, 0.001); float d1 = sd.GetDistance("zac ephron", "zac efron"); float d2 = sd.GetDistance("zac ephron", "kai ephron"); assertEquals(d1, d2, 0.001); d1 = sd.GetDistance("brittney spears", "britney spears"); d2 = sd.GetDistance("brittney spears", "brittney startzman"); assertTrue(d1 > d2); }
/// <summary> /// Suggest similar words (optionally restricted to a field of an index). /// <para> /// As the Lucene similarity that is used to fetch the most relevant n-grammed terms /// is not the same as the edit distance strategy used to calculate the best /// matching spell-checked word from the hits that Lucene found, one usually has /// to retrieve a couple of numSug's in order to get the true best match. /// </para> /// <para> /// I.e. if numSug == 1, don't count on that suggestion being the best one. /// Thus, you should set this value to <b>at least</b> 5 for a good suggestion. /// </para> /// </summary> /// <param name="word"> the word you want a spell check done on </param> /// <param name="numSug"> the number of suggested words </param> /// <param name="ir"> the indexReader of the user index (can be null see field param) </param> /// <param name="field"> the field of the user index: if field is not null, the suggested /// words are restricted to the words present in this field. </param> /// <param name="suggestMode"> /// (NOTE: if indexReader==null and/or field==null, then this is overridden with SuggestMode.SUGGEST_ALWAYS) </param> /// <param name="accuracy"> The minimum score a suggestion must have in order to qualify for inclusion in the results </param> /// <exception cref="System.IO.IOException"> if the underlying index throws an <see cref="System.IO.IOException"/> </exception> /// <exception cref="ObjectDisposedException"> if the <see cref="SpellChecker"/> is already disposed </exception> /// <returns> string[] the sorted list of the suggest words with these 2 criteria: /// first criteria: the edit distance, second criteria (only if restricted mode): the popularity /// of the suggest words in the field of the user index /// </returns> public virtual string[] SuggestSimilar(string word, int numSug, IndexReader ir, string field, SuggestMode suggestMode, float accuracy) { // obtainSearcher calls ensureOpen IndexSearcher indexSearcher = ObtainSearcher(); try { if (ir == null || field == null) { suggestMode = SuggestMode.SUGGEST_ALWAYS; } if (suggestMode == SuggestMode.SUGGEST_ALWAYS) { ir = null; field = null; } int lengthWord = word.Length; int freq = (ir != null && field != null) ? ir.DocFreq(new Term(field, word)) : 0; int goalFreq = suggestMode == SuggestMode.SUGGEST_MORE_POPULAR ? freq : 0; // if the word exists in the real index and we don't care for word frequency, return the word itself if (suggestMode == SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX && freq > 0) { return(new string[] { word }); } BooleanQuery query = new BooleanQuery(); string[] grams; string key; for (int ng = GetMin(lengthWord); ng <= GetMax(lengthWord); ng++) { key = "gram" + ng; // form key grams = FormGrams(word, ng); // form word into ngrams (allow dups too) if (grams.Length == 0) { continue; // hmm } if (bStart > 0) // should we boost prefixes? { Add(query, "start" + ng, grams[0], bStart); // matches start of word } if (bEnd > 0) // should we boost suffixes { Add(query, "end" + ng, grams[grams.Length - 1], bEnd); // matches end of word } for (int i = 0; i < grams.Length; i++) { Add(query, key, grams[i]); } } int maxHits = 10 * numSug; // System.out.println("Q: " + query); ScoreDoc[] hits = indexSearcher.Search(query, null, maxHits).ScoreDocs; // System.out.println("HITS: " + hits.length()); SuggestWordQueue sugQueue = new SuggestWordQueue(numSug, comparer); // go thru more than 'maxr' matches in case the distance filter triggers int stop = Math.Min(hits.Length, maxHits); SuggestWord sugWord = new SuggestWord(); for (int i = 0; i < stop; i++) { sugWord.String = indexSearcher.Doc(hits[i].Doc).Get(F_WORD); // get orig word // don't suggest a word for itself, that would be silly if (sugWord.String.Equals(word, StringComparison.Ordinal)) { continue; } // edit distance sugWord.Score = sd.GetDistance(word, sugWord.String); if (sugWord.Score < accuracy) { continue; } if (ir != null && field != null) // use the user index { sugWord.Freq = ir.DocFreq(new Term(field, sugWord.String)); // freq in the index // don't suggest a word that is not present in the field if ((suggestMode == SuggestMode.SUGGEST_MORE_POPULAR && goalFreq > sugWord.Freq) || sugWord.Freq < 1) { continue; } } sugQueue.InsertWithOverflow(sugWord); if (sugQueue.Count == numSug) { // if queue full, maintain the minScore score accuracy = sugQueue.Top.Score; } sugWord = new SuggestWord(); } // convert to array string string[] list = new string[sugQueue.Count]; for (int i = sugQueue.Count - 1; i >= 0; i--) { list[i] = sugQueue.Pop().String; } return(list); } finally { ReleaseSearcher(indexSearcher); } }