private static void Main(string[] args) { var ramDirectory = new RAMDirectory(); var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(ramDirectory); var ms = new MemoryStream(); var sw = new StreamWriter(ms); sw.WriteLine("Book"); sw.WriteLine("Bath"); sw.WriteLine("Bed"); sw.WriteLine("Make"); sw.WriteLine("Model"); sw.WriteLine("Vacum"); sw.WriteLine("Wending machine"); sw.Flush(); ms.Position = 0; spellChecker.setStringDistance(new JaroWinklerDistance()); spellChecker.SetAccuracy(0.3f); spellChecker.IndexDictionary(new PlainTextDictionary(ms), CancellationToken.None); var indexReader = IndexReader.Open(ramDirectory, true); var termEnum = indexReader.Terms(); while (termEnum.Next()) { Console.WriteLine(termEnum.Term); } var suggestSimilar = spellChecker.SuggestSimilar("both", 10); foreach (var s in suggestSimilar) { Console.WriteLine(s); } }
public string[] GetSuggestedWords(string spellIndex, string term, int maxCount) { FSDirectory dir = FSDirectory.Open(spellIndex); var spell = new SpellChecker.Net.Search.Spell.SpellChecker(dir); spell.SetAccuracy(0.6f); spell.setStringDistance(new LevenshteinDistance()); return(spell.SuggestSimilar(term, maxCount)); }
public SuggestionQueryResult ExecuteSuggestionQuery(string indexName, SuggestionQuery suggestionQuery) { if (suggestionQuery == null) throw new ArgumentNullException("suggestionQuery"); if (string.IsNullOrWhiteSpace(suggestionQuery.Term)) throw new ArgumentNullException("suggestionQuery.Term"); if (string.IsNullOrWhiteSpace(indexName)) throw new ArgumentNullException("indexName"); if (string.IsNullOrWhiteSpace(suggestionQuery.Field)) throw new ArgumentNullException("suggestionQuery.Field"); if (suggestionQuery.MaxSuggestions <= 0) suggestionQuery.MaxSuggestions = 10; if (suggestionQuery.Accuracy <= 0 || suggestionQuery.Accuracy > 1) suggestionQuery.Accuracy = 0.5f; suggestionQuery.MaxSuggestions = Math.Min(suggestionQuery.MaxSuggestions, _database.Configuration.MaxPageSize); var currentSearcher = _database.IndexStorage.GetCurrentIndexSearcher(indexName); IndexSearcher searcher; using(currentSearcher.Use(out searcher)) { var indexReader = searcher.GetIndexReader(); var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(new RAMDirectory(), GetStringDistance(suggestionQuery)); try { spellChecker.IndexDictionary(new LuceneDictionary(indexReader, suggestionQuery.Field)); spellChecker.SetAccuracy(suggestionQuery.Accuracy); var suggestions = spellChecker.SuggestSimilar(suggestionQuery.Term, suggestionQuery.MaxSuggestions, indexReader, suggestionQuery.Field, true); return new SuggestionQueryResult { Suggestions = suggestions }; } finally { spellChecker.Close(); // this is really stupid, but it doesn't handle this in its close method! GC.SuppressFinalize(spellChecker); } } }
public SuggestionQueryIndexExtension(Index indexInstance, WorkContext workContext, string key, StringDistance distanceType, bool isRunInMemory, string field, float accuracy) { _indexInstance = indexInstance; this.workContext = workContext; this.field = field; if (isRunInMemory) { directory = new RAMDirectory(); } else { directory = FSDirectory.Open(new DirectoryInfo(key)); } spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(directory, null); spellChecker.SetAccuracy(accuracy); spellChecker.setStringDistance(distanceType); _operationText = "Suggestions for " + field + " " + distanceType + " (" + accuracy + ")"; }
public SuggestionQueryResult Query(SuggestionQuery suggestionQuery, IndexReader indexReader) { if (suggestionQuery.Accuracy.HasValue == false) { throw new InvalidOperationException("SuggestionQuery.Accuracy must be specified."); } if (suggestionQuery.Distance.HasValue == false) { throw new InvalidOperationException("SuggestionQuery.Distance must be specified."); } spellChecker.setStringDistance(SuggestionQueryRunner.GetStringDistance(suggestionQuery.Distance.Value)); spellChecker.SetAccuracy(suggestionQuery.Accuracy.Value); if (suggestionQuery.Term.StartsWith("<<") && suggestionQuery.Term.EndsWith(">>")) { return(QueryOverMultipleWords(suggestionQuery, indexReader, suggestionQuery.Term.Substring(2, suggestionQuery.Term.Length - 4))); } if (suggestionQuery.Term.StartsWith("(") && suggestionQuery.Term.EndsWith(")")) { return(QueryOverMultipleWords(suggestionQuery, indexReader, suggestionQuery.Term.Substring(1, suggestionQuery.Term.Length - 2))); } string[] suggestions = spellChecker.SuggestSimilar(suggestionQuery.Term, suggestionQuery.MaxSuggestions, indexReader, suggestionQuery.Field, true); return(new SuggestionQueryResult { Suggestions = suggestions }); }