1.新建一個測試Lucene提供的分詞器的maven項目LuceneAnalyzerjava
2. 在pom.xml裏面引入以下依賴git
<!-- lucene 核心模塊 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>7.3.0</version> </dependency> <!-- Lucene提供的中文分詞器模塊,lucene-analyzers-smartcn:Lucene 的中文分詞器 SmartChineseAnalyzer --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-smartcn</artifactId> <version>7.3.0</version> </dependency>
3. 新建一個標準分詞器StandardAnalyzer的測試類LuceneStandardAnalyzerTestgithub
package com.luceneanalyzer.use.standardanalyzer; import java.io.IOException; 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; /** * Lucene core模塊中的 StandardAnalyzer英文分詞器使用 * 英文分詞效果好,中文分詞效果很差 * @author THINKPAD * */ public class LuceneStandardAnalyzerTest { private static void doToken(TokenStream ts) throws IOException { ts.reset(); CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class); while (ts.incrementToken()) { System.out.print(cta.toString() + "|"); } System.out.println(); ts.end(); ts.close(); } public static void main(String[] args) throws IOException { String etext = "Analysis is one of the main causes of slow indexing. Simply put, the more you analyze the slower analyze the indexing (in most cases)."; String chineseText = "張三說的確實在理。"; // Lucene core模塊中的 StandardAnalyzer 英文分詞器 try (Analyzer ana = new StandardAnalyzer();) { TokenStream ts = ana.tokenStream("coent", etext); System.out.println("標準分詞器,英文分詞效果:"); doToken(ts); ts = ana.tokenStream("content", chineseText); System.out.println("標準分詞器,中文分詞效果:"); doToken(ts); } catch (IOException e) { } } }
運行效果:apache
標準分詞器,英文分詞效果: analysis|one|main|causes|slow|indexing|simply|put|more|you|analyze|slower|analyze|indexing|most|cases| 標準分詞器,中文分詞效果: 張|三|說|的|確|實|在|理|
4. 新建一個Lucene提供的中文分詞器SmartChineseAnalyzer的測試類app
package com.luceneanalyzer.use.smartchineseanalyzer; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; /** * Lucene提供的中文分詞器模塊,lucene-analyzers-smartcn:Lucene 的中文分詞器 SmartChineseAnalyzer * 中英文分詞效果都很差 * * @author THINKPAD * */ public class LuceneSmartChineseAnalyzerTest { private static void doToken(TokenStream ts) throws IOException { ts.reset(); CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class); while (ts.incrementToken()) { System.out.print(cta.toString() + "|"); } System.out.println(); ts.end(); ts.close(); } public static void main(String[] args) throws IOException { String etext = "Analysis is one of the main causes of slow indexing. Simply put, the more you analyze the slower analyze the indexing (in most cases)."; String chineseText = "張三說的確實在理。"; // Lucene 的中文分詞器 SmartChineseAnalyzer try (Analyzer smart = new SmartChineseAnalyzer()) { TokenStream ts = smart.tokenStream("content", etext); System.out.println("smart中文分詞器,英文分詞效果:"); doToken(ts); ts = smart.tokenStream("content", chineseText); System.out.println("smart中文分詞器,中文分詞效果:"); doToken(ts); } } }
運行效果:maven
smart中文分詞器,英文分詞效果: analysi|is|on|of|the|main|caus|of|slow|index|simpli|put|the|more|you|analyz|the|slower|analyz|the|index|in|most|case| smart中文分詞器,中文分詞效果: 張|三|說|的|確實|在|理|
IKAnalyzer是開源、輕量級的中文分詞器,應用比較多ide
最早是做爲lucene上使用而開發,後來發展爲獨立的分詞組件。只提供到Lucene 4.0版本的支持。咱們在4.0之後版本Lucene中使用就須要簡單集成一下。測試
須要作集成,是由於Analyzer的createComponents方法API改變了this
IKAnalyzer提供兩種分詞模式:細粒度分詞和智能分詞spa
集成步驟
一、找到 IkAnalyzer包體提供的Lucene支持類,比較IKAnalyzer的createComponets方法。
4.0及以前版本的createComponets方法:
@Override protected TokenStreamComponents createComponents(String fieldName, final Reader in) { Tokenizer _IKTokenizer = new IKTokenizer(in, this.useSmart()); return new TokenStreamComponents(_IKTokenizer); }
最新的createComponets方法:
protected abstract TokenStreamComponents createComponents(String fieldName);
二、照這兩個類,建立新版本的, 類裏面的代碼直接複製,修改參數便可。
1.新建一個maven項目IkanalyzerIntegrated
2. 在pom.xml裏面引入以下依賴
<!-- lucene 核心模塊 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>7.3.0</version> </dependency> <!-- ikanalyzer 中文分詞器 --> <dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artifactId> <version>2012_u6</version> <!--排除掉裏面舊的lucene包,由於咱們要重寫裏面的分析器和分詞器 --> <exclusions> <exclusion> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> </exclusion> <exclusion> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> </exclusion> <exclusion> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> </exclusion> </exclusions> </dependency>
3. 重寫分析器
package com.study.lucene.ikanalyzer.Integrated; import org.apache.lucene.analysis.Analyzer; /** * 由於Analyzer的createComponents方法API改變了須要從新實現分析器 * @author THINKPAD * */ public class IKAnalyzer4Lucene7 extends Analyzer { private boolean useSmart = false; public IKAnalyzer4Lucene7() { this(false); } public IKAnalyzer4Lucene7(boolean useSmart) { super(); this.useSmart = useSmart; } public boolean isUseSmart() { return useSmart; } public void setUseSmart(boolean useSmart) { this.useSmart = useSmart; } @Override protected TokenStreamComponents createComponents(String fieldName) { IKTokenizer4Lucene7 tk = new IKTokenizer4Lucene7(this.useSmart); return new TokenStreamComponents(tk); } }
4. 重寫分詞器
package com.study.lucene.ikanalyzer.Integrated; import java.io.IOException; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; import org.apache.lucene.analysis.tokenattributes.TypeAttribute; import org.wltea.analyzer.core.IKSegmenter; import org.wltea.analyzer.core.Lexeme; /** * 由於Analyzer的createComponents方法API改變了須要從新實現分詞器 * @author THINKPAD * */ public class IKTokenizer4Lucene7 extends Tokenizer { // IK分詞器實現 private IKSegmenter _IKImplement; // 詞元文本屬性 private final CharTermAttribute termAtt; // 詞元位移屬性 private final OffsetAttribute offsetAtt; // 詞元分類屬性(該屬性分類參考org.wltea.analyzer.core.Lexeme中的分類常量) private final TypeAttribute typeAtt; // 記錄最後一個詞元的結束位置 private int endPosition; /** * @param in * @param useSmart */ public IKTokenizer4Lucene7(boolean useSmart) { super(); offsetAtt = addAttribute(OffsetAttribute.class); termAtt = addAttribute(CharTermAttribute.class); typeAtt = addAttribute(TypeAttribute.class); _IKImplement = new IKSegmenter(input, useSmart); } /* * (non-Javadoc) * * @see org.apache.lucene.analysis.TokenStream#incrementToken() */ @Override public boolean incrementToken() throws IOException { // 清除全部的詞元屬性 clearAttributes(); Lexeme nextLexeme = _IKImplement.next(); if (nextLexeme != null) { // 將Lexeme轉成Attributes // 設置詞元文本 termAtt.append(nextLexeme.getLexemeText()); // 設置詞元長度 termAtt.setLength(nextLexeme.getLength()); // 設置詞元位移 offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition()); // 記錄分詞的最後位置 endPosition = nextLexeme.getEndPosition(); // 記錄詞元分類 typeAtt.setType(nextLexeme.getLexemeTypeString()); // 返會true告知還有下個詞元 return true; } // 返會false告知詞元輸出完畢 return false; } /* * (non-Javadoc) * * @see org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader) */ @Override public void reset() throws IOException { super.reset(); _IKImplement.reset(input); } @Override public final void end() { // set final offset int finalOffset = correctOffset(this.endPosition); offsetAtt.setOffset(finalOffset, finalOffset); } }
5. 新建一個IKAnalyzer的測試類IKAnalyzerTest
package com.study.lucene.ikanalyzer.Integrated; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; /** * IKAnalyzer分詞器集成測試: * 細粒度切分:把詞分到最細 * 智能切分:根據詞庫進行拆分符合咱們的語言習慣 * * @author THINKPAD * */ public class IKAnalyzerTest { private static void doToken(TokenStream ts) throws IOException { ts.reset(); CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class); while (ts.incrementToken()) { System.out.print(cta.toString() + "|"); } System.out.println(); ts.end(); ts.close(); } public static void main(String[] args) throws IOException { String etext = "Analysis is one of the main causes of slow indexing. Simply put, the more you analyze the slower analyze the indexing (in most cases)."; String chineseText = "張三說的確實在理。"; /** * ikanalyzer 中文分詞器 由於Analyzer的createComponents方法API改變了 須要咱們本身實現 * 分析器IKAnalyzer4Lucene7和分詞器IKTokenizer4Lucene7 */ // IKAnalyzer 細粒度切分 try (Analyzer ik = new IKAnalyzer4Lucene7();) { TokenStream ts = ik.tokenStream("content", etext); System.out.println("IKAnalyzer中文分詞器 細粒度切分,英文分詞效果:"); doToken(ts); ts = ik.tokenStream("content", chineseText); System.out.println("IKAnalyzer中文分詞器 細粒度切分,中文分詞效果:"); doToken(ts); } // IKAnalyzer 智能切分 try (Analyzer ik = new IKAnalyzer4Lucene7(true);) { TokenStream ts = ik.tokenStream("content", etext); System.out.println("IKAnalyzer中文分詞器 智能切分,英文分詞效果:"); doToken(ts); ts = ik.tokenStream("content", chineseText); System.out.println("IKAnalyzer中文分詞器 智能切分,中文分詞效果:"); doToken(ts); } } }
運行結果:
IKAnalyzer中文分詞器 細粒度切分,英文分詞效果: analysis|is|one|of|the|main|causes|of|slow|indexing.|indexing|simply|put|the|more|you|analyze|the|slower|analyze|the|indexing|in|most|cases| IKAnalyzer中文分詞器 細粒度切分,中文分詞效果: 張三|三|說的|的確|的|確實|實在|在理| IKAnalyzer中文分詞器 智能切分,英文分詞效果: analysis|is|one|of|the|main|causes|of|slow|indexing.|simply|put|the|more|you|analyze|the|slower|analyze|the|indexing|in|most|cases| IKAnalyzer中文分詞器 智能切分,中文分詞效果: 張三|說的|確實|在理|
一、在類目錄下建立IK的配置文件:IKAnalyzer.cfg.xml
二、在配置文件中增長配置擴展停用詞文件的節點: <entry key=「ext_stopwords」>my_ext_stopword.dic</entry> 若有多個,以「;」間隔
<?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_stopwords">my_ext_stopword.dic</entry> </properties>
三、在類目錄下建立咱們的擴展停用詞文件 my_ext_stopword.dic,編輯該文件加入停用詞,一行一個
四、目錄結構以下:
5.新建測試類ExtendedIKAnalyzerDicTest.java
package com.study.lucene.ikanalyzer.Integrated.ext; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import com.study.lucene.ikanalyzer.Integrated.IKAnalyzer4Lucene7; /** * 擴展 IKAnalyzer的詞典測試 * * */ public class ExtendedIKAnalyzerDicTest { private static void doToken(TokenStream ts) throws IOException { ts.reset(); CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class); while (ts.incrementToken()) { System.out.print(cta.toString() + "|"); } System.out.println(); ts.end(); ts.close(); } public static void main(String[] args) throws IOException { String chineseText = "厲害了個人國一經播出,受到各方好評,強烈激發了國人的愛國之情、自豪感!"; // IKAnalyzer 細粒度切分 try (Analyzer ik = new IKAnalyzer4Lucene7();) { TokenStream ts = ik.tokenStream("content", chineseText); System.out.println("IKAnalyzer中文分詞器 細粒度切分,中文分詞效果:"); doToken(ts); } // IKAnalyzer 智能切分 try (Analyzer ik = new IKAnalyzer4Lucene7(true);) { TokenStream ts = ik.tokenStream("content", chineseText); System.out.println("IKAnalyzer中文分詞器 智能切分,中文分詞效果:"); doToken(ts); } } }
運行結果:
未加停用詞以前:
加停用詞以後:
一、在類目錄下IK的配置文件:IKAnalyzer.cfg.xml 中增長配置擴展詞文件的節點: <entry key="ext_dict">ext.dic</entry> 若有多個,以「;」間隔
<?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">my_ext_stopword.dic</entry> </properties>
二、在類目錄下建立擴展詞文件 ext.dic,編輯該文件加入新詞,一行一個
三、目錄結構以下:
4.運行前面的測試類測試類ExtendedIKAnalyzerDicTest.java查看運行效果
運行結果:
未加新詞以前:
加新詞以後:
源碼獲取地址:
https://github.com/leeSmall/SearchEngineDemo