當前位置:成語大全網 - 新華字典 - 如何簡便使用ikanalyzer

如何簡便使用ikanalyzer

IKAnalyzer 用來對壹段文集進行分詞

IKAnalyzer.cfg.xml 文件必須放在classpath的根目錄下,可以在源碼中修改這個配置文件的位置,但是沒有必要去修改。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "/dtd/properties.dtd">

<properties>

<comment>IK Analyzer 擴展配置</comment>

<entry key="ext_dict">/ikdic/ext_keyword.dic;</entry>//在classpath根目錄下的ikdic包下的ext_keyword.dic,為擴展字典

<entry key="ext_stopwords">/ikdic/ext_stopword.dic</entry> //在classpath根目錄下的ikdic包下的ext_stopword.dic,為停止字典

</properties>

至於調用是很簡單的:

package org.bdp.util;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.util.ArrayList;

import java.util.List;

import org.wltea.analyzer.core.IKSegmenter;

import org.wltea.analyzer.core.Lexeme;

public class CZIKAnaUtil {

public static void main(String[] args) {

String str = "文嘉(1501-1583)";

IKAnalysis(str);

}

public static List<String> IKAnalysis(String str) {

List<String> keywordList = new ArrayList<String>();

try {

byte[] bt = str.getBytes();

InputStream ip = new ByteArrayInputStream(bt);

Reader read = new InputStreamReader(ip);

IKSegmenter iks = new IKSegmenter(read,true);//true開啟只能分詞模式,如果不設置默認為false,也就是細粒度分割

Lexeme t;

while ((t = iks.next()) != null) {

keywordList.add(t.getLexemeText());

}

} catch (IOException e) {

e.printStackTrace();

}

/* for (int i = 0; i < keyWordList.size() - 1; i++) {

for (int j = keyWordList.size() - 1; j > i; j--) {

if (keyWordList.get(j).equals(keyWordList.get(i))) {

keyWordList.remove(j);

}

}

}

Collections.sort(keyWordList, new Comparator<String>() {

@Override

public int compare(String o1, String o2) {

return o2.length() - o1.length();

}

})*/

System.out.println(keywordList);

return keywordList;

}

}