private static void doSpellCheckerIndexing(string LuceneIndexDir, string SpellCheckerIndexDir) { try { // http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/search/spell/SpellChecker.html FSDirectory spellCheckerIndexDir = FSDirectory.GetDirectory(SpellCheckerIndexDir, false); FSDirectory indexDir = FSDirectory.GetDirectory(LuceneIndexDir, false); SpellChecker.Net.Search.Spell.SpellChecker spellchecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellCheckerIndexDir); spellchecker.ClearIndex(); // SpellChecker.Net.Search.Spell.SpellChecker spellchecker = new SpellChecker.Net.Search.Spell.SpellChecker (global::Lucene.Net.Store.Directory SpellChecker(spellIndexDirectory); IndexReader r = IndexReader.Open(indexDir); try { // To index a field of a user index: Dictionary dict = new SpellChecker.Net.Search.Spell.LuceneDictionary(r, "title"); spellchecker.IndexDictionary(dict); } finally { r.Close(); } } catch (Exception ex) { Console.Write("Could not create spell-checking index" + ex.Message); } }
public void CreateFullTextIndex(IEnumerable <SearchResult> dataList, string path) { var directory = FSDirectory.Open(new DirectoryInfo(path)); var analyzer = new StandardAnalyzer(_version); //var analyzer = new WhitespaceAnalyzer(); using (var writer = new IndexWriter(directory, analyzer, create: true, mfl: IndexWriter.MaxFieldLength.UNLIMITED)) { foreach (var post in dataList) { writer.AddDocument(MapPostToDocument(post)); } writer.Optimize(); writer.Commit(); writer.Dispose(); directory.Dispose(); //change here } var indexReader = IndexReader.Open(FSDirectory.Open(path), readOnly: true); // Create the SpellChecker //Directory d = new Directory();d.Delete(path + "\\Spell"); spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(FSDirectory.Open(path + "\\Spell")); // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title")); }
public static string[] SuggestSilmilarWords(string term, int count = 10) { IndexReader indexReader = IndexReader.Open(FSDirectory.Open(_luceneDir), true); // Create the SpellChecker var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(FSDirectory.Open(_luceneDir + "\\Spell")); // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Title))); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, StronglyTyped.PropertyName <LuceneSearchModel>(x => x.Description))); //Suggest Similar Words return(spellChecker.SuggestSimilar(term, count, null, null, true)); }
public static string[] SuggestSilmilarWords(string term, int count = 10) { IndexReader indexReader = IndexReader.Open(FSDirectory.Open(_luceneDir), true); // Create the SpellChecker var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(FSDirectory.Open(_luceneDir + "\\Spell")); // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Body")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "SubTitle")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Keywords")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description")); //Suggest Similar Words return(spellChecker.SuggestSimilar(term, count, null, null, true)); }
public void SpellingSuggestion() { log.Debug("Building the word index"); // Create a "Did you mean?" dictionary (the words are extracted from the search index) Directory wordDirectory = GetWordDirectory(); Directory wordIndex = new RAMDirectory(); SpellChecker.Net.Search.Spell.SpellChecker checker = new SpellChecker.Net.Search.Spell.SpellChecker(wordIndex); checker.ClearIndex(); IndexReader reader = IndexReader.Open(wordDirectory); // Add words to spell checker index checker.IndexDictionary(new LuceneDictionary(reader, SpellChecker.Net.Search.Spell.SpellChecker.F_WORD)); // Suggest similar words SuggestAndVerify(checker, "nhibrenate", "nhibernate"); SuggestAndVerify(checker, "dreiven", "driven"); SuggestAndVerify(checker, "inyection", "injection"); }
public static string[] SuggestSilmilarWords(string term, int count = 10) { IndexReader indexReader = IndexReader.Open(FSDirectory.Open(_luceneDir), true); // Create the SpellChecker var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(FSDirectory.Open(_luceneDir + "\\Spell")); // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Name")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Author")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Publisher")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "ISBN")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description")); //Suggest Similar Words return spellChecker.SuggestSimilar(term, count, null, null, true); }
public virtual long CreateIndex() { try { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); if (System.IO.Directory.Exists(_indexFilesPath)) { try { using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) directory.ClearLock(directory.GetLockId()); } catch { } FileUtils.DeleteDirRecursively(new DirectoryInfo(_indexFilesPath)); } System.IO.Directory.CreateDirectory(_indexFilesPath); if (System.IO.Directory.Exists(_spellFilesPath)) { try { using (var directory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath))) directory.ClearLock(directory.GetLockId()); } catch { } FileUtils.DeleteDirRecursively(new DirectoryInfo(_spellFilesPath)); } System.IO.Directory.CreateDirectory(_spellFilesPath); var allPosts = _postService.GetAsQueryable().Where(p => p.Published) .Include(p => p.Descriptions) .Include(p => p.Tags) .Include(p => p.Categories); var languages = _languagesService.GetAsEnumerable(); var analyzer = new StandardAnalyzer(Version); using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) { using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { foreach (var post in allPosts) { writer.AddDocument(MapPost(post, post.PostType));//Default values foreach (var language in languages.OrderByDescending(p => p.IsDefault)) { var localizedMap = MapPost(post, language, post.PostType); if (localizedMap != null) { writer.AddDocument(localizedMap); //Localized values } } } writer.Optimize(); writer.Commit(); } } using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath))) { using (var spellDirectory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath))) { using (var indexReader = IndexReader.Open(directory, readOnly: true)) { using (var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellDirectory)) { // Create SpellChecker Index spellChecker.ClearIndex(); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title")); spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description")); spellChecker.Close(); } } } } watch.Stop(); MethodCache.ExpireTag(CacheTags.Search); return(watch.ElapsedMilliseconds); } catch (Exception e) { MethodCache.ExpireTag(CacheTags.Search); _eventPublisher.Publish(new CreateSearchIndexesFailEvent(e)); throw; } }