/// <summary> /// Use a different index as the spell checker index or re-open /// the existing index if <code>spellIndex</code> is the same value /// as given in the constructor. /// </summary> /// <param name="spellIndexDir">spellIndexDir the spell directory to use </param> /// <throws>AlreadyClosedException if the Spellchecker is already closed</throws> /// <throws>IOException if spellchecker can not open the directory</throws> virtual public void SetSpellIndex(Directory spellIndexDir) { // this could be the same directory as the current spellIndex // modifications to the directory should be synchronized lock (modifyCurrentIndexLock) { EnsureOpen(); if (!IndexReader.IndexExists(spellIndexDir)) { IndexWriter writer = new IndexWriter(spellIndexDir, null, true, IndexWriter.MaxFieldLength.UNLIMITED); writer.Close(); } SwapSearcher(spellIndexDir); } }
/// <summary> Index a Dictionary</summary> /// <param name="dict">the dictionary to index /// </param> /// <throws> IOException </throws> public virtual void IndexDictionary(Dictionary dict) { IndexReader.Unlock(spellindex); IndexWriter writer = new IndexWriter(spellindex, new WhitespaceAnalyzer(), !IndexReader.IndexExists(spellindex)); writer.SetMergeFactor(300); writer.SetMaxBufferedDocs(150); System.Collections.IEnumerator iter = dict.GetWordsIterator(); while (iter.MoveNext()) { System.String word = (System.String)iter.Current; int len = word.Length; if (len < 3) { continue; // too short we bail but "too long" is fine... } if (this.Exist(word)) { // if the word already exist in the gramindex continue; } // ok index the word Document doc = CreateDocument(word, GetMin(len), GetMax(len)); writer.AddDocument(doc); } // close writer writer.Optimize(); writer.Close(); // close reader reader.Close(); reader = null; }