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 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 }); }