2013-03-15 3 views
0

lucene 맞춤법 검사기를 사용하여 맞춤법 교정기를 작성하려고합니다. 블로그 텍스트 콘텐츠가 포함 된 단일 텍스트 파일을 제공하고 싶습니다. 문제는 사전 파일에 줄당 한 문장 씩 줄 때만 작동한다는 것입니다. 또한 제안 API는 발생 횟수에 어떠한 가중치도주지 않고 결과를 반환합니다. 다음은 소스 코드입니다lucene을 사용하는 맞춤법 검사기

public class SpellCorrector { 

     SpellChecker spellChecker = null; 

     public SpellCorrector() { 
       try { 
         File file = new File("/home/ubuntu/spellCheckIndex"); 
         Directory directory = FSDirectory.open(file); 

         spellChecker = new SpellChecker(directory); 

         StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36); 
         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer); 
         spellChecker.indexDictionary(
             new PlainTextDictionary(new File("/home/ubuntu/main.dictionary")), config, true); 
                     //Should I format this file with one sentence/word per line? 

       } catch (IOException e) { 

       } 

     } 

     public String correct(String query) { 
       if (spellChecker != null) { 
         try { 
           String[] suggestions = spellChecker.suggestSimilar(query, 5); 
           // This returns the suggestion not based on occurence but based on when it occured 

           if (suggestions != null) { 
             if (suggestions.length != 0) { 
               return suggestions[0]; 
             } 
           } 
         } catch (IOException e) { 
           return null; 
         } 
       } 
       return null; 
     } 
} 

일부 변경해야합니까?

답변

2

첫 번째 문제에 대해서는 문서화 된 사전 형식 (예 : PlainTextDictionary API)이 필요합니다. 임의의 텍스트를 전달하려는 경우 인덱스를 지정하고 대신 LuceneDictionary을 사용하거나 필요에 따라 HighFrequencyDictionary을 사용할 수 있습니다.

맞춤법 검사기는 다른 우려가있는 단어 (Levenstein Distance 기준) 간의 유사성을 기반으로 대체 단어를 제안합니다. 더 많은 인기 검색어를 추천으로 만 사용하려면 SuggestModeSpellChecker.suggestSimilar으로 전달해야합니다. 이렇게하면 제안 된 일치가 적어도 대체 할 단어만큼 인기 있고 현명한 것으로 나타납니다.

Lucene이 베스트 매치를 결정하는 방법을 오버라이드해야한다면 에 자신의 Comparator를 생성하여 SpellChecker.setComparator으로 할 수 있습니다. SuggestWord가 freq을 당신에게 공개하기 때문에, 인기로 발견 된 성냥을 쉽게 정렬해야합니다.