IKAnalyzer的下載和使用


IKAnalyzer下載地址

鏈接:https://pan.baidu.com/s/1bNqXh8B7suT1rAUm_ZZ7gw 
提取碼:c08j

文件夾結構如下

 

 

 

 

在Lucene中默認的分析器StandardAnalyzer對於漢字進行分析的時候是拆成一個字,一個字的,每個字算上一個詞

        //用於配置分詞器
        IndexWriterConfig config = new IndexWriterConfig();

在IndexWriterConfig的構造方法中使用的是StandardAnalyzer

    public IndexWriterConfig() {
        this(new StandardAnalyzer());
    }

我們要使用中文的分詞器的話就要把他替換掉,

先來測試一下使用默認的StandardAnalyzer分詞效果

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;

public class MyTokenStream {
    public static void main(String[] args) throws IOException {
        //創建一個標准分析器對象
        Analyzer analyzer=new StandardAnalyzer();
        //獲取tokenStream對象
        //參數1域名 2要分析的文本內容
        TokenStream tokenStream=analyzer.tokenStream("","test a lucene 程序,杯莫停");
        //添加引用,用於獲取每個關鍵詞
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        //添加一個偏移量的引用,記錄了關鍵詞的開始位置以及結束位置
        OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
        //將指針調整到列表的頭部
        tokenStream.reset();
        //遍歷關鍵詞列表,incrementToken判斷是否結束
        while (tokenStream.incrementToken()) {
            System.out.println("開始--->"+offsetAttribute.startOffset());
            System.out.println(charTermAttribute);
            System.out.println("結束--->"+offsetAttribute.endOffset());
        }
        tokenStream.close();
    }
}

測試結果如下

 

 

"程序"一詞被分成了兩個單獨的字

接下來使用第三方的IKAnalyzer,導入maven依賴或者關聯jar包

    <!-- https://mvnrepository.com/artifact/com.jianggujin/IKAnalyzer-lucene -->
                    <dependency>
                        <groupId>com.jianggujin</groupId>
                        <artifactId>IKAnalyzer-lucene</artifactId>
                        <version>8.0.0</version>
                    </dependency>

然后將下面標紅線的代碼進行修改

  public static void main(String[] args) throws IOException {
        //創建一個標准分析器對象
        Analyzer analyzer=new IKAnalyzer();
        //獲取tokenStream對象
        //參數1域名 2要分析的文本內容
        TokenStream tokenStream=analyzer.tokenStream("","test a lucene 程序,杯莫停");
        //添加引用,用於獲取每個關鍵詞
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        //添加一個偏移量的引用,記錄了關鍵詞的開始位置以及結束位置
        OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
        //將指針調整到列表的頭部
        tokenStream.reset();
        //遍歷關鍵詞列表,incrementToken判斷是否結束
        while (tokenStream.incrementToken()) {
            System.out.println("開始--->"+offsetAttribute.startOffset());
            System.out.println(charTermAttribute);
            System.out.println("結束--->"+offsetAttribute.endOffset());
        }
        tokenStream.close();
    }

 

 

 

 

 可以看出來分析器把"程序"當成一個詞語來看待了,但是"杯莫停",還是拆成了單個字,而且"杯"字還沒有進行顯示,因為"杯"字在停用詞列表里

 

 

 接下來在擴展詞典里添加一個詞語"杯莫停"

 

 測試結果

 

 TIP:在自定義擴展詞典和停用詞詞典的過程當中,千萬不要使用windows記事本編輯,因為windows記事本是UTF-8+BOM編碼,文件保存的格式應當是UTF-8

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM