Lucene5学习之SpellCheck拼写纠错

简介:

   最近有点累,让这篇又姗姗来迟了,各位不好意思,让你们久等了。趁着周末一个人没什么事,继续Lucene5系列的脚步,今天主题是Suggest模块下另一个功能:拼写纠错。什么叫拼写纠错?大家还是看图吧,这样会比较形象:

 

       看完上面两张图片,我想大家应该已经知道SpellCheck是用来解决问题的了吧。其实这个功能主要目的还是为了提升用户体验问题,当用户输入的搜索关键字里包含了错别字(对于英文来说,就是单词拼写不正确),我们的搜索程序能智能检测出来并给出与用户输入的搜索关键字最相似的一组建议关键字,不过Google和百度都选择了只返回匹配度最高的一个搜索建议关键字并没有返回多个。

      知道了SpellCheck是干什么的,那接下来我们需要了解SpellCheck内部是如何工作的。首先我们需要对SpellCheck有个整体的宏观的了解,所以首先还是先查阅下SpellCheck的API文档:

      

     API里对于SpellChecker类没有太多说明,只是说这个SpellCheck功能的主类,代码作者是xxxxxx谁,这些信息对于我们来说没什么太大的价值,后面给出了一个使用示例,我们暂时也不急怎么去使用它,我们学习一个新的类或接口,我们首先需要理解它内部有哪些成员变量以及定义了哪些方法,以及每个方法的含义,这些API里都有说明,看图:


 

    定义了两个publick公开的静态成员变量,DEFAULT_ACCURACY表示默认的最小分数,SpellCheck会对字典里的每个词与用户输入的搜索关键字进行一个相似度打分,默认该值是0.5,相似度分值范围是0到1之间,数字越大表示越相似。F_WORD是对于字典文件里每一行创建索引时使用的默认域名称,默认值为:word

     然后看看有哪些方法并了解每个方法的用途:

 

clearIndex:  移除拼写检查索引中的所有Term项;

close: 关闭IndexSearcher的reader;

exist:判断用户输入的关键字是否存在于拼写检查索引中

getAccuracyaccuracy是精确度的意思,这里表示最小评分,评分越大表示与用户输入的关键字越相似

suggestSimilar:这个方法就是用来决定哪些word会被判定为比较相似的然后以数组的形式返回,这是SpellChecker的核心;

setComparator:设置比较器,既然涉及到相似度问题,那肯定有相似度大小问题,有大小必然存在比较,有比较必然需要比较器,通过比较器决定返回的建议词的顺序,因为一般需要把最相关的显示在最前面,然后依次排序显示;

setSpellIndex:设置拼写检查索引目录的

setStringDistance:设置编辑距实现

当然内部还有一些private内置函数在API里并未体现。大概了解了SpellChecker的内部设计,如果更深入了解SpellChcker则需要通过阅读它的源码:

 

Java代码   收藏代码
  1. <strong>/** 
  2.  * <p> 
  3.  *   Spell Checker class  (Main class) <br/> 
  4.  *  (initially inspired by the David Spencer code). 
  5.  * </p> 
  6.  * 
  7.  * <p>Example Usage: 
  8.  *  
  9.  * <pre class="prettyprint"> 
  10.  *  SpellChecker spellchecker = new SpellChecker(spellIndexDirectory); 
  11.  *  // To index a field of a user index: 
  12.  *  spellchecker.indexDictionary(new LuceneDictionary(my_lucene_reader, a_field)); 
  13.  *  // To index a file containing words: 
  14.  *  spellchecker.indexDictionary(new PlainTextDictionary(new File("myfile.txt"))); 
  15.  *  String[] suggestions = spellchecker.suggestSimilar("misspelt", 5); 
  16.  * </pre> 
  17.  *  
  18.  * 
  19.  */  
  20. public class SpellChecker implements java.io.Closeable {  
  21.   
  22.   /** 
  23.    * The default minimum score to use, if not specified by calling {@link #setAccuracy(float)} . 
  24.    */  
  25.   public static final float DEFAULT_ACCURACY = 0.5f;  
  26.   
  27.   /** 
  28.    * Field name for each word in the ngram index. 
  29.    */  
  30.   public static final String F_WORD = "word";  
  31.   
  32.   /** 
  33.    * the spell index 
  34.    */  
  35.   // don't modify the directory directly - see #swapSearcher()  
  36.   // TODO: why is this package private?  
  37.   Directory spellIndex;  
  38.   /** 
  39.    * Boost value for start and end grams 
  40.    */  
  41.   private float bStart = 2.0f;  
  42.   
  43.   private float bEnd = 1.0f;  
  44.   // don't use this searcher directly - see #swapSearcher()  
  45.   
  46.   private IndexSearcher searcher;  
  47.   /* 
  48.    * this locks all modifications to the current searcher. 
  49.    */  
  50.   
  51.   private final Object searcherLock = new Object();  
  52.   /* 
  53.    * this lock synchronizes all possible modifications to the 
  54.    * current index directory. It should not be possible to try modifying 
  55.    * the same index concurrently. Note: Do not acquire the searcher lock 
  56.    * before acquiring this lock! 
  57.    */  
  58.   private final Object modifyCurrentIndexLock = new Object();  
  59.   
  60.   private volatile boolean closed = false;  
  61.   // minimum score for hits generated by the spell checker query  
  62.   
  63.   private float accuracy = DEFAULT_ACCURACY;  
  64.   
  65.   private StringDistance sd;  
  66.   private Comparator<SuggestWord> comparator;</strong>  

    public static final float DEFAULT_ACCURACY = 0.5f;
    这里设置了默认的相似度值,小于这个值就不会被返回了即默认会认为相似度小于0.5的就是不相似的。

 

 

    public static final String F_WORD = "word";

    我们在创建拼写索引时默认使用的域名称

  

    Directory spellIndex:拼写索引目录

 

    private float bStart = 2.0f;

    private float bEnd = 1.0f;

    这两个分别是前缀ngram和后缀ngram的权重值,即默认认为前缀ngram权重比后缀ngram大,ngram就是按定长来分割字符串成多个Term,比如lucene,假如采用3gram,则最后返回的Term数组为:luc,uce,cen,ene,显然这里luc是前缀ngram,ene是后缀ngram,当然你也可以采用2gram,只不过这时返回的term个数更多,Term个数多自然查询更精确,但响应速度也会慢些。如果ngram这里的N取值大的话,那匹配粒度大,虽然查询精确度会下降,但响应速度也加快了。

    

    private IndexSearcher searcher; 索引查询器对象,这个没什么好说的;

    

    private final Object searcherLock = new Object();  查询锁,进行查询之间需要先占有这把锁

     

private final Object modifyCurrentIndexLock = new Object();

修改索引锁,在添加索引或更新索引时需要获取这把锁

 

private volatile boolean closed = false;

标识IndexReader是否已经关闭

 

private StringDistance sd;

编辑距实现

 

private Comparator<SuggestWord> comparator;建议词比较器,决定了最终返回的拼写建议词在SuggestWordQueue队列中的排序

 

 

setSpellIndex:

Java代码   收藏代码
  1. <strong>public void setSpellIndex(Directory spellIndexDir) throws IOException {  
  2.     // this could be the same directory as the current spellIndex  
  3.     // modifications to the directory should be synchronized   
  4.     synchronized (modifyCurrentIndexLock) {  
  5.       ensureOpen();  
  6.       if (!DirectoryReader.indexExists(spellIndexDir)) {  
  7.           IndexWriter writer = new IndexWriter(spellIndexDir,  
  8.             new IndexWriterConfig(null));  
  9.           writer.close();  
  10.       }  
  11.       swapSearcher(spellIndexDir);  
  12.     }  
  13.   }</strong>  

  先拿到modifyCurrentIndexLock索引修改锁,防止索引被修改,

 

     ensureOpen();确保索引目录能打开

     DirectoryReader.indexExists(spellIndexDir):判断指定索引目录下是否存在段文件,如果不存在则关闭IndexWrtier,然后通过swapSearcher重新打开IndexReader。

     

 

Java代码   收藏代码
  1. private static String[] formGrams(String text, int ng) {  
  2.     int len = text.length();  
  3.     String[] res = new String[len - ng + 1];  
  4.     for (int i = 0; i < len - ng + 1; i++) {  
  5.       res[i] = text.substring(i, i + ng);  
  6.     }  
  7.     return res;  
  8.   }  

   formGrams:根据提供的ng长度对text字符串进行ngram分割,返回分割后得到的gram数组

 

 

 

Java代码   收藏代码
  1. /** 
  2.    * Removes all terms from the spell check index. 
  3.    * @throws IOException If there is a low-level I/O error. 
  4.    * @throws AlreadyClosedException if the Spellchecker is already closed 
  5.    */  
  6.   public void clearIndex() throws IOException {  
  7.     synchronized (modifyCurrentIndexLock) {  
  8.       ensureOpen();  
  9.       final Directory dir = this.spellIndex;  
  10.       final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(null)  
  11.           .setOpenMode(OpenMode.CREATE));  
  12.       writer.close();  
  13.       swapSearcher(dir);  
  14.     }  
  15.   }  

    clearIndex:首先获取修改索引锁,防止索引没修改,然后ensureOpen确保索引目录能被打开,然后重新new了一个IndexWriter且索引写入模式设置为create即先前的索引将会被覆盖,然后close掉IndexWrtier,最后打开Reader重新new一个IndexWriter.这样做是为了覆盖掉先前的索引(OpenMode.CREATE是关键),即clearIndex方法的目的。

 

     

 

Java代码   收藏代码
  1. /** 
  2.    * Indexes the data from the given {@link Dictionary}. 
  3.    * @param dict Dictionary to index 
  4.    * @param config {@link IndexWriterConfig} to use 
  5.    * @param fullMerge whether or not the spellcheck index should be fully merged 
  6.    * @throws AlreadyClosedException if the Spellchecker is already closed 
  7.    * @throws IOException If there is a low-level I/O error. 
  8.    */  
  9.   public final void indexDictionary(Dictionary dict, IndexWriterConfig config, boolean fullMerge) throws IOException {  
  10.     synchronized (modifyCurrentIndexLock) {  
  11.       ensureOpen();  
  12.       final Directory dir = this.spellIndex;  
  13.       final IndexWriter writer = new IndexWriter(dir, config);  
  14.       IndexSearcher indexSearcher = obtainSearcher();  
  15.       final List<TermsEnum> termsEnums = new ArrayList<>();  
  16.   
  17.       final IndexReader reader = searcher.getIndexReader();  
  18.       if (reader.maxDoc() > 0) {  
  19.         for (final LeafReaderContext ctx : reader.leaves()) {  
  20.           Terms terms = ctx.reader().terms(F_WORD);  
  21.           if (terms != null)  
  22.             termsEnums.add(terms.iterator(null));  
  23.         }  
  24.       }  
  25.         
  26.       boolean isEmpty = termsEnums.isEmpty();  
  27.   
  28.       try {   
  29.         BytesRefIterator iter = dict.getEntryIterator();  
  30.         BytesRef currentTerm;  
  31.           
  32.         terms: while ((currentTerm = iter.next()) != null) {  
  33.     
  34.           String word = currentTerm.utf8ToString();  
  35.           int len = word.length();  
  36.           if (len < 3) {  
  37.             continue// too short we bail but "too long" is fine...  
  38.           }  
  39.     
  40.           if (!isEmpty) {  
  41.             for (TermsEnum te : termsEnums) {  
  42.               if (te.seekExact(currentTerm)) {  
  43.                 continue terms;  
  44.               }  
  45.             }  
  46.           }  
  47.     
  48.           // ok index the word  
  49.           Document doc = createDocument(word, getMin(len), getMax(len));  
  50.           writer.addDocument(doc);  
  51.         }  
  52.       } finally {  
  53.         releaseSearcher(indexSearcher);  
  54.       }  
  55.       if (fullMerge) {  
  56.         writer.forceMerge(1);  
  57.       }  
  58.       // close writer  
  59.       writer.close();  
  60.       // TODO: this isn't that great, maybe in the future SpellChecker should take  
  61.       // IWC in its ctor / keep its writer open?  
  62.         
  63.       // also re-open the spell index to see our own changes when the next suggestion  
  64.       // is fetched:  
  65.       swapSearcher(dir);  
  66.     }  
  67.   }  

    先通过IndexReader读取索引目录,加载word域上的所有Term存入TermEnum集合中,然后加载字典文件(字典文件里一行一个词),遍历字典文件里的每个词,内层循环里遍历索引目录里word域上的每个Term,如果当前字典文件里的词在word域中存在(避免Term重复),则直接跳过,否则需要将字典文件里当前词写入索引,这里存入索引并不是直接把当前词存入索引,这里还有个ngram过程,通过ngram分成多个Term再写入索引的,即这句代码Document doc = createDocument(word, getMin(len), getMax(len));min和max即ngram的分割长度范围。然后writer.addDocument(doc);写入索引目录,然后释放IndexReader即releaseSearcher(indexSearcher);,如果设置了fullMerge则会强制将段文件合并为一个即writer.forceMerge(1);最后关闭IndexWriter,最后swapSearcher(dir);即IndexReader重新打开,new一个新的IndexSearcher,保证新加的document被被search到。一句话总结:indexDictionary就是将字典文件里的词进行ngram操作后得到多个词然后分别写入索引。

 

      

Java代码   收藏代码
  1. /** 
  2.    * Suggest similar words (optionally restricted to a field of an index). 
  3.    * 
  4.    * <p>As the Lucene similarity that is used to fetch the most relevant n-grammed terms 
  5.    * is not the same as the edit distance strategy used to calculate the best 
  6.    * matching spell-checked word from the hits that Lucene found, one usually has 
  7.    * to retrieve a couple of numSug's in order to get the true best match. 
  8.    * 
  9.    * <p>I.e. if numSug == 1, don't count on that suggestion being the best one. 
  10.    * Thus, you should set this value to <b>at least</b> 5 for a good suggestion. 
  11.    * 
  12.    * @param word the word you want a spell check done on 
  13.    * @param numSug the number of suggested words 
  14.    * @param ir the indexReader of the user index (can be null see field param) 
  15.    * @param field the field of the user index: if field is not null, the suggested 
  16.    * words are restricted to the words present in this field. 
  17.    * @param suggestMode  
  18.    * (NOTE: if indexReader==null and/or field==null, then this is overridden with SuggestMode.SUGGEST_ALWAYS) 
  19.    * @param accuracy The minimum score a suggestion must have in order to qualify for inclusion in the results 
  20.    * @throws IOException if the underlying index throws an {@link IOException} 
  21.    * @throws AlreadyClosedException if the Spellchecker is already closed 
  22.    * @return String[] the sorted list of the suggest words with these 2 criteria: 
  23.    * first criteria: the edit distance, second criteria (only if restricted mode): the popularity 
  24.    * of the suggest words in the field of the user index 
  25.    *  
  26.    */  
  27.   public String[] suggestSimilar(String word, int numSug, IndexReader ir,  
  28.       String field, SuggestMode suggestMode, float accuracy) throws IOException {  
  29.     // obtainSearcher calls ensureOpen  
  30.     final IndexSearcher indexSearcher = obtainSearcher();  
  31.     try {  
  32.       if (ir == null || field == null) {  
  33.         suggestMode = SuggestMode.SUGGEST_ALWAYS;  
  34.       }  
  35.       if (suggestMode == SuggestMode.SUGGEST_ALWAYS) {  
  36.         ir = null;  
  37.         field = null;  
  38.       }  
  39.   
  40.       final int lengthWord = word.length();  
  41.   
  42.       final int freq = (ir != null && field != null) ? ir.docFreq(new Term(field, word)) : 0;  
  43.       final int goalFreq = suggestMode==SuggestMode.SUGGEST_MORE_POPULAR ? freq : 0;  
  44.       // if the word exists in the real index and we don't care for word frequency, return the word itself  
  45.       if (suggestMode==SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX && freq > 0) {  
  46.         return new String[] { word };  
  47.       }  
  48.   
  49.       BooleanQuery query = new BooleanQuery();  
  50.       String[] grams;  
  51.       String key;  
  52.   
  53.       for (int ng = getMin(lengthWord); ng <= getMax(lengthWord); ng++) {  
  54.   
  55.         key = "gram" + ng; // form key  
  56.   
  57.         grams = formGrams(word, ng); // form word into ngrams (allow dups too)  
  58.   
  59.         if (grams.length == 0) {  
  60.           continue// hmm  
  61.         }  
  62.   
  63.         if (bStart > 0) { // should we boost prefixes?  
  64.           add(query, "start" + ng, grams[0], bStart); // matches start of word  
  65.   
  66.         }  
  67.         if (bEnd > 0) { // should we boost suffixes  
  68.           add(query, "end" + ng, grams[grams.length - 1], bEnd); // matches end of word  
  69.   
  70.         }  
  71.         for (int i = 0; i < grams.length; i++) {  
  72.           add(query, key, grams[i]);  
  73.         }  
  74.       }  
  75.   
  76.       int maxHits = 10 * numSug;  
  77.   
  78.   //    System.out.println("Q: " + query);  
  79.       ScoreDoc[] hits = indexSearcher.search(query, null, maxHits).scoreDocs;  
  80.   //    System.out.println("HITS: " + hits.length());  
  81.       SuggestWordQueue sugQueue = new SuggestWordQueue(numSug, comparator);  
  82.   
  83.       // go thru more than 'maxr' matches in case the distance filter triggers  
  84.       int stop = Math.min(hits.length, maxHits);  
  85.       SuggestWord sugWord = new SuggestWord();  
  86.       for (int i = 0; i < stop; i++) {  
  87.   
  88.         sugWord.string = indexSearcher.doc(hits[i].doc).get(F_WORD); // get orig word  
  89.   
  90.         // don't suggest a word for itself, that would be silly  
  91.         if (sugWord.string.equals(word)) {  
  92.           continue;  
  93.         }  
  94.   
  95.         // edit distance  
  96.         sugWord.score = sd.getDistance(word,sugWord.string);  
  97.         if (sugWord.score < accuracy) {  
  98.           continue;  
  99.         }  
  100.   
  101.         if (ir != null && field != null) { // use the user index  
  102.           sugWord.freq = ir.docFreq(new Term(field, sugWord.string)); // freq in the index  
  103.           // don't suggest a word that is not present in the field  
  104.           if ((suggestMode==SuggestMode.SUGGEST_MORE_POPULAR && goalFreq > sugWord.freq) || sugWord.freq < 1) {  
  105.             continue;  
  106.           }  
  107.         }  
  108.         sugQueue.insertWithOverflow(sugWord);  
  109.         if (sugQueue.size() == numSug) {  
  110.           // if queue full, maintain the minScore score  
  111.           accuracy = sugQueue.top().score;  
  112.         }  
  113.         sugWord = new SuggestWord();  
  114.       }  
  115.   
  116.       // convert to array string  
  117.       String[] list = new String[sugQueue.size()];  
  118.       for (int i = sugQueue.size() - 1; i >= 0; i--) {  
  119.         list[i] = sugQueue.pop().string;  
  120.       }  
  121.   
  122.       return list;  
  123.     } finally {  
  124.       releaseSearcher(indexSearcher);  
  125.     }  
  126.   }  

    现在来说说核心函数suggestSimilar,用来计算最后返回的建议词。首先需要说下SuggestMode的建议模式:

 

    SUGGEST_WHEN_NOT_IN_INDEX:

    意思就是只有当用户提供的搜索关键字在索引Term中不存在我才提供建议,否则我会认为用户输入的搜索关键字是正确的不需要提供建议。

    SUGGEST_MORE_POPULAR:

    表示只返回频率较高的词组,用户输入的搜索关键字首先需要经过ngram分割,创建索引的时候也需要进行分词,如果用户输入的词分割后得到的word在索引中出现的频率比索引中实际存在的Term还要高,那说明不需要进行拼写建议了。

    SUGGEST_ALWAYS:

    永远进行建议,只是返回的建议结果受numSug数量限制即最多返回几条拼写建议。

    if (suggestMode==SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX && freq > 0) {

        return new String[] { word };

      }

     这里freq>0表明用户输入的搜索关键字在Term中出现了且你配置只对搜索关键字在索引中不存在才进行拼写建议,则直接返回用户数的原词即默认用户输入是正确的,不需要进行拼写建议

    for (int ng = getMin(lengthWord); ng <= getMax(lengthWord); ng++)

    然后根据用户输入的搜索关键字的长度确定进行ngram的分割长度范围,然后对用户输入的搜索关键字进行ngram操作并把分割出的每个词使用booleanQuery链接(默认用的or链接)起来,如果是前缀ngram和后缀ngram还需要设置不同的权重,然后根据创建的Query进行检索:

ScoreDoc[] hits = indexSearcher.search(query, null, maxHits).scoreDocs;

 然后遍历实际命中的结果集for (int i = 0; i < stop; i++),这里的stop即结果集的实际长度,取出每个Term与当前用户输入的关键字进行比对,如果equals则直接pass,因为需要排除自身,即这句代码:

    if (sugWord.string.equals(word)) {

          continue;

    }

sugWord.score = sd.getDistance(word,sugWord.string);

返回用户输入关键字和索引中当前Term的相似度,这个取决于你Distance实现,默认实现是LevensteinDistance即计算编辑距。

if (sugWord.score < accuracy) {

          continue;

}

如果相似度小于设置的默认值则也不返回

if ((suggestMode==SuggestMode.SUGGEST_MORE_POPULAR && goalFreq > sugWord.freq) || sugWord.freq < 1) {

            continue;

 }

在SUGGEST_MORE_POPULAR 模式下,如果用户输入的关键字都比索引中的Term的出现频率高,那也直接跳过不返回(用户输入的关键字出现频率高,说明该关键字匹配度高啊,通过该关键字搜索自然会有结果集返回,自然不需要进行拼写检查啊)

sugQueue.insertWithOverflow(sugWord);

        if (sugQueue.size() == numSug) {

          accuracy = sugQueue.top().score;

 

}

条件符合那就把当前索引中的Term存入拼写建议队列中,如果队列满了则把队列顶部的score(即相似度)缓存到accuracy即该值就表示了当前最小的相似度值,因为当队列满了,自然是要把相似度最小的给移除啊,处于顶部的自然是最小的,所以你懂的。

sugWord = new SuggestWord();然后把SuggestWord重置进入索引中下一个Term匹配过程如此循环。

for (int i = sugQueue.size() - 1; i >= 0; i--) {

        list[i] = sugQueue.pop().string;

}

最后把队列里的word一个个pop从队列中弹出,因为是倒着赋值的(从数组尾部赋值的),那自然最终返回的数组里相似度高的在前面,相似度低的处于数组尾部。

 

关键重点是sugWord.score = sd.getDistance(word,sugWord.string);

判断两个词的相似度,默认实现是LevensteinDistance,至于LevensteinDistance算法实现自己去看LevensteinDistance源码吧。

其实lucene还内置了另外几种相似度实现:JaroWinklerDistance和NGramDistance。

Distance实现可以在构造SpellChecker时通过其构造函数进行自定义,

Java代码   收藏代码
  1. /** 
  2.    * Use the given directory as a spell checker index. The directory 
  3.    * is created if it doesn't exist yet. 
  4.    * @param spellIndex the spell index directory 
  5.    * @param sd the {@link StringDistance} measurement to use  
  6.    * @throws IOException if Spellchecker can not open the directory 
  7.    */  
  8.   public SpellChecker(Directory spellIndex, StringDistance sd) throws IOException {  
  9.     this(spellIndex, sd, SuggestWordQueue.DEFAULT_COMPARATOR);  
  10.   }  

    另外一个重点是comparator,在构造SpellChecker时如果没有显式设置comparator,则默认构造的是SuggestWordQueue.DEFAULT_COMPARATOR即SuggestWordScoreComparator,SuggestWordScoreComparator的比较规则是:

 先比较socre值即相似度值(这个相似度值是有Distance类决定的),如果score值相等,则再来比较freq即索引中出现频率,频率高则相似度高,如果还是分不出高下,则直接比较两个字符串吧,字符串比较其实就是从左至右逐个字符比较他们的ASCII码。

 

其实Compartor比较器也是可以通过构造函数重载进行指定的,如果默认的比较器满足不了你的要求,请自定义你自己的比较器,你懂的。

 

      还有一个重点就是Dictionary实现,它有3个实现:

     1. PlainTextDictionary:即文本字典文件,即从文本文件中读取内容来构建索引,不过要求一行只能一个词,内部会对每一行的词进行ngram分割然后才写入索引中

   2.LuceneDictionary:即直接以已经存在的索引中的某个域的值作为字典

   3.HighFrequencyDictionary:跟LuceneDictionary类似,它跟LuceneDictionary区别就是它限制了只有出现在各document中的次数满足一定数量时才会被写入索引,这个数量通过构造函数的thresh参数决定。

   Dictionary实现类通过SpellChecker的indexDictionary方法指定。

      按照惯例下面还是来段示例代码吧:

      

Java代码   收藏代码
  1. package com.yida.framework.lucene5.spellcheck;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.nio.file.Paths;  
  6.   
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  8. import org.apache.lucene.document.Document;  
  9. import org.apache.lucene.document.Field.Store;  
  10. import org.apache.lucene.document.TextField;  
  11. import org.apache.lucene.index.IndexReader;  
  12. import org.apache.lucene.index.IndexWriter;  
  13. import org.apache.lucene.index.IndexWriterConfig;  
  14. import org.apache.lucene.search.IndexSearcher;  
  15. import org.apache.lucene.search.spell.PlainTextDictionary;  
  16. import org.apache.lucene.search.spell.SpellChecker;  
  17. import org.apache.lucene.store.Directory;  
  18. import org.apache.lucene.store.RAMDirectory;  
  19.   
  20. /** 
  21.  * 拼写检查测试 
  22.  * @author Lanxiaowei 
  23.  * 
  24.  */  
  25. public class SpellCheckTest {  
  26.     private static String dicpath = "C:/dictionary.dic";  
  27.     private Document document;  
  28.     private Directory directory = new RAMDirectory();  
  29.     private IndexWriter indexWriter;  
  30.     /**拼写检查器*/  
  31.     private SpellChecker spellchecker;  
  32.     private IndexReader indexReader;  
  33.     private IndexSearcher indexSearcher;  
  34.       
  35.     /** 
  36.      * 创建测试索引 
  37.      * @param content 
  38.      * @throws IOException 
  39.      */  
  40.     public void createIndex(String content) throws IOException {  
  41.         IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());  
  42.         indexWriter = new IndexWriter(directory, config);  
  43.         document = new Document();  
  44.         document.add(new TextField("content", content,Store.YES));  
  45.         try {  
  46.             indexWriter.addDocument(document);  
  47.             indexWriter.commit();  
  48.             indexWriter.close();  
  49.         } catch (IOException e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.       
  54.     public void search(String word, int numSug) {  
  55.         directory = new RAMDirectory();  
  56.         try {  
  57.             IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());  
  58.             spellchecker = new SpellChecker(directory);  
  59.             //初始化字典目录  
  60.             //最后一个fullMerge参数表示拼写检查索引是否需要全部合并  
  61.             spellchecker.indexDictionary(new PlainTextDictionary(Paths.get(dicpath)),config,true);  
  62.             //这里的参数numSug表示返回的建议个数  
  63.             String[] suggests = spellchecker.suggestSimilar(word, numSug);  
  64.             if (suggests != null && suggests.length > 0) {  
  65.                 for (String suggest : suggests) {  
  66.                     System.out.println("您是不是想要找:" + suggest);  
  67.                 }  
  68.             }  
  69.         } catch (IOException e) {  
  70.             e.printStackTrace();  
  71.         }  
  72.     }  
  73.     public static void main(String[] args) throws IOException {  
  74.         SpellCheckTest spellCheckTest = new SpellCheckTest();  
  75.         spellCheckTest.createIndex("《屌丝男士》不是传统意义上的情景喜剧,有固定时长和单一场景,以及简单的生活细节。而是一部具有鲜明网络特点,舞台感十足,整体没有剧情衔接,固定的演员演绎着并不固定角色的笑话集。");  
  76.         spellCheckTest.createIndex("屌丝男士的拍摄构想,首先源于“屌丝文化”在中国的刮起的现象级春风,红透了整片天空,全中国上下可谓无人不屌丝,无人不爱屌丝。");  
  77.         spellCheckTest.createIndex("德国的一部由女演员玛蒂娜-希尔主演的系列短剧,凭借其疯癫荒诞、自high耍贱、三俗无下限的表演风格,在中国取得了巨大成功,红火程度远远超过了德国。不仅位居国内各个视频网站的下载榜和点播榜高位,且在微博和媒体间,引发了坊间热议和话题传播。网友们更是形象地将其翻译为《屌丝女士》,对其无比热衷。于是我们决定着手拍一部属于中国人,带强烈国人屌丝色彩的《屌丝男士》。");  
  78.           
  79.         String word = "吊丝男士";  
  80.         spellCheckTest.search(word, 5);  
  81.     }  
  82. }  

 

 

     在C盘新建一个dictionary.dic文件,编辑内容如图:

     

     OK,有关SpellCheck拼写纠错功能就说到这儿了,如果还有哪里不清楚的请联系我,demo源码请在最底下的附件里下载。

 

       如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,

或者加裙
一起交流学习!

转载:http://iamyida.iteye.com/blog/2206107

目录
相关文章
|
7月前
|
Web App开发 数据建模 Java
|
7月前
|
自然语言处理 API 索引
Elasticsearch汉字补全和拼写纠错
Elasticsearch汉字补全和拼写纠错
139 0
|
8月前
|
Java 索引
Elasticsearch之实战掌握误拼写时的fuzzy模糊搜索技术
Elasticsearch之实战掌握误拼写时的fuzzy模糊搜索技术
|
11月前
|
自然语言处理 Python
使用有限状态机原理实现英文分词
使用有限状态机原理实现英文分词
43 0
|
11月前
|
自然语言处理
NLP项目(二)——拼写纠错
NLP项目(二)——拼写纠错
|
自然语言处理 算法
中文文本处理分词的二元模型
中文文本处理分词的二元模型
133 1
中文文本处理分词的二元模型
|
Java Maven Android开发
给定一个汉字句子,可以输出句子的读音。借鉴第三方库:pinyin4j 。
给定一个汉字句子,可以输出句子的读音。借鉴第三方库:pinyin4j 。
给定一个汉字句子,可以输出句子的读音。借鉴第三方库:pinyin4j 。
|
人工智能 PHP 开发者
贝叶斯拼写纠错实例 | 学习笔记
快速学习贝叶斯拼写纠错实例
102 0
贝叶斯拼写纠错实例 | 学习笔记
部分常用分词工具使用整理
以下分词工具均能在Python环境中直接调用(排名不分先后)。1、jieba(结巴分词) 免费使用2、HanLP(汉语言处理包) 免费使用3、SnowNLP(中文的类库) 免费使用4、FoolNLTK(中文处理工具包) 免费使用5、Jiagu(甲骨NLP) 免费使用6、pyltp(哈工大语言云) 商用需要付费7、THULAC(清华中文词法分析工具包) 商用需要付费8、NLPIR(汉语分词系统) 付费使用 1、jieba(结巴分词)“结巴”中文分词:做最好的 Python 中文分词组件。
2087 0
|
索引 JavaScript 数据安全/隐私保护
教科书式的正则匹配
正则在js中是一类比较特殊的对象,它可以匹配各个场景需要的格式验证,例如邮箱、手机号、用户登录名、密码等等,似乎无处不在,在常见的字符串检索或替换中,我们需要提供一种模式表示检索或替换的规则,来匹配一系列符合某个句法规则的字符串。
926 0

热门文章

最新文章