溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用IKAnalyzer與Lucene怎么實現(xiàn)一個中文分詞功能

發(fā)布時間:2020-12-08 16:22:49 來源:億速云 閱讀:345 作者:Leah 欄目:編程語言

利用IKAnalyzer與Lucene怎么實現(xiàn)一個中文分詞功能?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

步驟如下:

step1:準備相關(guān)的Jar依賴,lucene-core-5.1.0.jar、ik.jar,然后新建項目,引入相關(guān)依賴項目結(jié)構(gòu)如下:

IkDemo-src
     -con.funnyboy.ik
-IKAnalyzer.cfg.xml
     -stopword.dic
-ext.dic
-Reference Libraries
     -lucene-core-5.1.0.jar
     -ik.jar

IKAnalyzer.cfg.xml:配置擴展詞典以及停止詞典 內(nèi)容如下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!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>

其中的ext.dic配置自己的擴展字典,stopword.dic配置自己的擴展停止詞字典

step2:通過java代碼驗證測試

public class MyIkTest {
  public static String str = "中國人民銀行我是中國人";
  public static void main(String[] args) { 
     MyIkTest test = new MyIkTest(); 
     test.wordCount("", str);
   }
   private void wordCount(String arg,String content) {
     Analyzer analyzer = new IKAnalyzer(true); // IK實現(xiàn)分詞 true:用最大詞長分詞 false:最細粒度切分 
    StringReader reader = null; 
    TokenStream ts = null; try { 
      reader = new StringReader(content); 
      ts = analyzer.tokenStream(arg,reader); 
      CharTermAttribute term = ts.addAttribute(CharTermAttribute.class); 
      ts.reset(); 
      Map<String, Integer> map = new HashMap<String, Integer>(); //統(tǒng)計 
      while (ts.incrementToken()) { 
        String str = term.toString(); 
        Object o = map.get(str); 
        if (o == null) { 
          map.put(str, new Integer(1)); 
         } else { 
          Integer i = new Integer(((Integer) o).intValue() + 1); 
           map.put(str, i); 
        } 
       } 
      List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(map.entrySet()); 
      Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() { 
        public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) { 
          return (o2.getValue() - o1.getValue()); 
        } });  
       for (int k=0;k<list.size();k++) { 
        Entry<String, Integer> it=list.get(k); 
        String word = it.getKey().toString(); 
        System.err.println(word+"["+it.getValue()+"]"); 
       }  
    } catch (Exception e) {
     } finally { 
      if(reader != null){ 
         reader.close(); 
      } 
      if (analyzer != null) { 
        analyzer.close(); 
      } 
     } 
   }
  }

執(zhí)行程序測試結(jié)果如下:

中國人民銀行[1]

中國人[1]

我[1]

3、配置說明

a、如何自定義配置擴展詞典和停止詞典 IKAnalyzer.cfg.xml中定義了擴展詞典和停止詞典,如果有多好個可以通過;配置多個。擴展詞典是指用戶可以根據(jù)自己定義的詞義實現(xiàn)分詞,比如人名在默認的詞典中并未實現(xiàn),需要自定義實現(xiàn)分詞,卡可以通過在ext.dic中新增自定義的詞語。停止詞是指對于分詞沒有實際意義但出現(xiàn)頻率很高的詞,比如嗎、乎等語氣詞,用戶也可以通過在stopword.dic中自定義相關(guān)的停止詞。

b、關(guān)于最大詞長分詞和最小粒度分詞的區(qū)分 在IKAnalyzer構(gòu)造方法中可以通過提供一個標示來實現(xiàn)最大詞長分詞和最小粒度分詞,true為最大詞長分詞,默認是最小粒度分詞。對"中國人民銀行我是中國人"分別測試結(jié)果如下:

最大詞長分詞結(jié)果如下:

中國人民銀行[1]

中國人[1]

我[1]

最小粒度分詞結(jié)果如下:

國人[2]
中國人[2]
中國[2]
人民[1]
中國人民銀行[1]
我[1]
人民銀行[1]
中國人民[1]
銀行[1]

關(guān)于利用IKAnalyzer與Lucene怎么實現(xiàn)一個中文分詞功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI