java(jsp)搜索分詞IKAnalyzer+Lucene分詞

起源:由於現在搜索引擎可以根據搜索詞自動拆分出不同的詞來搜索信息,更好得到用戶想要搜索的信息如(深圳網站建設獨佔網絡,會被拆分爲深圳網站建設與獨佔網絡兩個不同的詞),所以我們製作網站的時候搜索信息有時候也是需要使用到分詞的。

java IKAnalyzer分詞jar包
IKAnalyzer.jar和lucene-core.jar

需要引入IKAnalyzer.cfg.xml到項目的src目錄中,下面是代碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
<properties>
<comment>IK Analyzer 擴展配置</comment>

ext.dic; stopword.dic; 需要引入stopword.dic到項目src目錄中,下面是代碼 a an and are as at be but by for if in into is it no not of on or such that the their then there these they this to was will with 需要引入ext.dic到項目的src目錄中,下面是代碼 深圳網站建設 獨佔網絡 上面的兩個詞是我們自己自定義,大家可以自定義輸入,記得ext.dic的文字編輯要跟項目是一樣如(utf-8),可以直接使用dw修改編輯 後是java代碼 public class IKAnalyzerTest { public static void main(String[] args) throws IOException { String keyWord = "深圳網站建設公司獨佔網絡技術製作分詞效果"; queryWords(keyWord); } public static void queryWords(String query) throws IOException { Configuration cfg = DefaultConfig.getInstance(); // System.out.println(cfg.getMainDictionary()); // //System.out.println(cfg.getQuantifierDicionary()); List list = new ArrayList(); StringReader input = new StringReader(query.trim()); IKSegmenter ikSeg = new IKSegmenter(input, true); for (Lexeme lexeme = ikSeg.next(); lexeme != null; lexeme = ikSeg.next()) { System.out.print(lexeme.getLexemeText()+"-"); } } } [深圳網站建設](https://www.sz886.com)[https://www.sz886.com](https://www.sz886.com)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章