麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Lucene 4.x Spellcheck使用說明

2019-11-14 23:36:07
字體:
來源:轉載
供稿:網友
Lucene 4.x Spellcheck使用說明

  Spellcheck是Lucene新版本的功能,在介紹spellcheck之前,我們需要弄清楚Spellcheck支持幾種數據源。Spellcheck構造函數需要傳入Dictionary接口:

  

package org.apache.lucene.search.spell;/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either exPRess or implied. * See the License for the specific language governing permissions and * limitations under the License. */import java.io.IOException;import org.apache.lucene.search.suggest.InputIterator;/** * A simple interface representing a Dictionary. A Dictionary * here is a list of entries, where every entry consists of * term, weight and payload. *  */public interface Dictionary {  /**   * Returns an iterator over all the entries   * @return Iterator   */  InputIterator getEntryIterator() throws IOException;}

  常用的Dictionary主要有以下幾種,常用的主要有基于文本型的和基于lucene索引構建的:

  

  下面是我測試用的一段代碼,代碼包括索引構建和索引查詢:

  

package com.tianditu.com.search;import java.io.File;import java.io.IOException;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.search.spell.LuceneDictionary;import org.apache.lucene.search.spell.SpellChecker;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.MMapDirectory;import org.apache.lucene.util.Version;public class GlobalSuggest {//拼寫檢查構建的索引private  final String SPELL_CHECK_FOLDER = "c://spellcheck//";//根據已有的索引private final String GLOBAL_PINYIN_SUGGEST = "O://searchwork_custom//data_index//pinyin2008//";//構建索引public void testIndexPinyin2008() throws IOException{long start = System.currentTimeMillis();//北京吉威時代軟件股份有限公司//String indexDir ="O://searchwork_custom//data_index//GlobalIndex//";Directory direct = new MMapDirectory(new File(GLOBAL_PINYIN_SUGGEST));LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name");ld.getEntryIterator();Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER));SpellChecker sc = new SpellChecker(spd);//sc.inIndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null);//往spellcheck目錄下寫索引--------------sc.indexDictionary(ld, iwc, true);sc.close();long end = System.currentTimeMillis();System.out.println("索引完畢,耗時:"+(end-start)+"ms");}public void testIndex() throws IOException{long start = System.currentTimeMillis();//北京吉威時代軟件股份有限公司String indexDir ="O://searchwork_custom//data_index//GlobalIndex//";Directory direct = new MMapDirectory(new File(indexDir));LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name");ld.getEntryIterator();Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER));SpellChecker sc = new SpellChecker(spd);//sc.inIndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null);sc.indexDictionary(ld, iwc, true);sc.close();long end = System.currentTimeMillis();System.out.println("索引完畢,耗時:"+(end-start)+"ms");}public void testSearch(String wd) throws IOException{//構建DirectoryDirectory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER));//實例化 spellcheck組件SpellChecker sc = new SpellChecker(spd);//根據輸入關鍵字  獲得N條最相近的幾率 第三個鄙視精確度 越大越匹配 安裝實際需要調整String[] suggests = sc.suggestSimilar(wd, 10,0.6f);if(suggests!=null){for(String Word:suggests){System.out.println("Dou you mean:"+word);}}}/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {GlobalSuggest spellcheck = new GlobalSuggest();//spellcheck.testIndexPinyin2008();spellcheck.testSearch("beijing京鴨");//spellcheck.testSearch("beijng");}}

  其中索引構建處代碼:

  

//構建索引public void testIndexPinyin2008() throws IOException{long start = System.currentTimeMillis();//北京吉威時代軟件股份有限公司//String indexDir ="O://searchwork_custom//data_index//GlobalIndex//";Directory direct = new MMapDirectory(new File(GLOBAL_PINYIN_SUGGEST));LuceneDictionary ld = new LuceneDictionary(DirectoryReader.open(direct), "name");ld.getEntryIterator();Directory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER));SpellChecker sc = new SpellChecker(spd);//sc.inIndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_30,null);//往spellcheck目錄下寫索引--------------sc.indexDictionary(ld, iwc, true);sc.close();long end = System.currentTimeMillis();System.out.println("索引完畢,耗時:"+(end-start)+"ms");}

  此處代碼,就是根據已有的索引來構建Spellcheck所需的索引。

Spellcheck查詢索引代碼片段如下:

  

//構建DirectoryDirectory spd = FSDirectory.open(new File(SPELL_CHECK_FOLDER));//實例化 spellcheck組件SpellChecker sc = new SpellChecker(spd);//根據輸入關鍵字  獲得N條最相近的幾率 第三個鄙視精確度 越大越匹配 安裝實際需要調整String[] suggests = sc.suggestSimilar(wd, 10,0.6f);if(suggests!=null){for(String word:suggests){System.out.println("Dou you mean:"+word);}}

 相關算法:默認是LevensteinDistance 。

     

 查詢樣例:

    1、查詢漢字,有錯別字情況:

    

    2、查詢拼音:

    

    3、拼音漢字夾雜:

    

(備注:發現問題了,拼音和漢字夾雜的情況不行,如果想使用,需要進行某種處理。)

    4、如果處理一長串漢字,中間夾雜錯別字:

    

  總結:看來spellcheck能力還是有限,如果需要用還可能改造。

      

  

  

  

    


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲精品一区二区三区免 | 国产精品免费看 | 一区二区三区日本在线观看 | 杏美月av| 欧美xxxxx视频 | 国产一区日韩精品 | 成人在线观看一区 | 国产精品成人免费一区久久羞羞 | 黄色免费在线电影 | 成人不卡在线观看 | 亚洲影视中文字幕 | 欧美日韩色 | 久久久精彩 | 国产午夜精品久久久久婷 | 91av在线免费观看 | 色婷婷久久久久久 | 最近中文字幕一区二区 | 成人羞羞在线观看网站 | 成人综合免费视频 | 国产在线一级片 | 国内精品视频饥渴少妇在线播放 | 亚洲一区中文字幕 | 性视频久久 | 成人福利软件 | 国产精品一区二区三区在线 | 久久久久亚洲精品国产 | 欧美一极视频 | 国产精品久久久久久影院8一贰佰 | 日日操操 | 久久国产精品久久久久久电车 | 伦一区二区三区中文字幕v亚洲 | 人人看人人舔 | 久久αv| 热re91久久精品国产99热 | 狠狠干夜夜草 | 少妇一级淫片免费放正片 | 一区二区三级视频 | 精品中文视频 | 永久免费在线观看av | 国产精品免费一区二区三区四区 | 国产精品久久久久久久久久大牛 |