標籤:mkt cve hapi wpc vfio srp encoding kdbg pbm
1.分析器的執行過程:
第一個 TokenFilter將大寫字母變為小寫,第二次分詞去掉空格並換行,第三次去掉不要的 and或者標點符號
2.中文分詞器介紹:
(1)lucene內建的
(2)第三方的
3.查看標準分詞器對中文的分詞效果:
最終決定使用的是IKAnalyzer()分詞器,支援中文擴充與停用。
1 // 查看標準分析器的分詞效果 2 @Test 3 public void testTokenStream() throws Exception { 4 // 建立一個標準分析器對象 5 // Analyzer analyzer = new StandardAnalyzer(); 6 // Analyzer analyzer = new CJKAnalyzer(); 7 // Analyzer analyzer = new SmartChineseAnalyzer(); 8 Analyzer analyzer = new IKAnalyzer(); 9 // 獲得tokenStream對象10 // 第一個參數:網域名稱,可以隨便給一個11 // 第二個參數:要分析的常值內容12 // TokenStream tokenStream = analyzer.tokenStream("test",13 // "The Spring Framework provides a comprehensive programming and configuration model.");14 TokenStream tokenStream = analyzer.tokenStream("test",15 "高富帥可以用二維表結構來邏輯表達實現的資料");16 // 添加一個引用,可以獲得每個關鍵詞17 CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);18 // 添加一個位移量的引用,記錄了關鍵詞的開始位置以及結束位置19 OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);20 // 將指標調整到列表的頭部21 tokenStream.reset();22 // 遍曆關鍵詞列表,通過incrementToken方法判斷列表是否結束23 while (tokenStream.incrementToken()) {24 // 關鍵詞的起始位置25 System.out.println("start->" + offsetAttribute.startOffset());26 // 取關鍵詞27 System.out.println(charTermAttribute);28 // 結束位置29 System.out.println("end->" + offsetAttribute.endOffset());30 }31 tokenStream.close();32 }
(1)標準分詞器:StandardAnalyzer
一個一個分詞
(2)中日韓分詞器 CJKAnalyzer
兩個兩個分詞
(3) 智能中文分詞器 SmartChineseAnalyzer
擴充性差,不支援擴充。
(4) 最終擴充性好的分詞器 IKAnalyzer
4. IKAnalyzer分詞器使用方法:
Jar包:
設定檔:
(1)ext.dic存放擴充的詞彙(分詞算一個,擴充新詞彙),比如:
(2)stopword.dic存放停用的詞彙(分詞不算一個),比如:
(3) IKAnalyzer.cfg.xml 主設定檔:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 擴充配置</comment> <!--使用者可以在這裡配置自己的擴充字典 --> <entry key="ext_dict">ext.dic;</entry> <!--使用者可以在這裡配置自己的擴充停止詞字典--> <entry key="ext_stopwords">stopword.dic;</entry> </properties>
(4)效果:
對於漢字 "高富帥可以用二維表結構來邏輯表達實現的資料" 的分詞效果:
載入擴充詞典:ext.dic載入擴充停止詞典:stopword.dicstart->0高富帥end->3start->3可以用end->6start->3可以end->5start->6二維表end->9start->6二維end->8start->9結構end->11start->12邏輯end->14start->14表達end->16start->16實現end->18start->19資料end->21
lucene中文分詞——(四)