2017-07-03 131 views
0

我想索引我的拼寫檢查程序使用目錄爲RAMDirectory而不是使用FSDirectory。由於我已經創建了索引,我只是想使用該用戶索引來索引拼寫檢查器,但我沒有收到任何建議。 exist也返回0,所以我猜索引不正確創建。Lucene:使用RAMDictionary拼寫檢查器似乎不工作

try{ 

     StandardAnalyzer analyzer = new StandardAnalyzer(); 
     IndexWriterConfig config = new IndexWriterConfig(analyzer); 
     Directory directory = new RAMDirectory(); 

     IndexWriter indexWriter = new IndexWriter(directory,config); 

     JSONArray documentArray = new JSONArray(); 
     String[] fieldArray = {"field"}; 
     JSONObject documentObj= new JSONObject(); 
     documentObj.put("field", "KARNATAKA"); 

     documentArray.put(documentObj); 

     JSONObject documentObj1= new JSONObject(); 
     documentObj1.put("field", "KERALA"); 

     documentArray.put(documentObj1); 

     for (int i = 0; i < documentArray.length(); i++) 
     { 
      JSONObject docObj = documentArray.getJSONObject(i); 
      Document doc = new Document(); 
      for (int j = 0; j < fieldArray.length; j++) 
      { 
       doc.add(new Field(fieldArray[j], docObj.getString(fieldArray[j]), org.apache.lucene.document.TextField.TYPE_STORED)); 
      } 
      indexWriter.addDocument(doc); 
      } 


     indexWriter.commit(); 
     indexWriter.close(); 


     DirectoryReader ireader = DirectoryReader.open(directory); 
     SpellChecker spellChecker = new SpellChecker(directory); 
     spellChecker.clearIndex(); 

     spellChecker.indexDictionary(new LuceneDictionary(ireader, "field"), new IndexWriterConfig(new StandardAnalyzer()),true); 

     String[] suggestions = spellChecker.suggestSimilar("KANATAKA", 5); 

     System.out.println(spellChecker.exist("Karnataka".toUpperCase())); 
     if(suggestions.length > 0) 
      System.out.println(suggestions[0]); 
     spellChecker.close(); 




    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

請幫我可能是錯的

回答

1

文檔,這樣做對SpellChecker.clearIndex()說:

去除拼寫檢查索引中的所有條款。

我看起來並不像你想要做的那樣。我會刪除該行,或者更好,只需使用拼寫檢查器索引的新目錄,而不是使用與源索引相同的目錄。

+0

建議使用拼寫檢查器的專用索引 – dom