1. 程式人生 > >使用IKAnalyzer實現中文分詞&去除中文停用詞

使用IKAnalyzer實現中文分詞&去除中文停用詞

1、簡介:IKAnalyzer是一個開源的,基於java語言開發的輕量級的中文分詞工具包。這裡使用的是IKAnalyzer2012。

2、IK Analyzer 2012特性:

(1)採用了特有的“正向迭代最細粒度切分演算法“,支援細粒度和智慧分詞兩種切分模式;
(2)在系統環境:Core2 i7 3.4G雙核,4G記憶體,window 7 64位, Sun JDK 1.6_29 64位 普通pc環境測試,IK2012具有160萬字/秒(3000KB/S)的高速處理能力。
(3)2012版本的智慧分詞模式支援簡單的分詞排歧義處理和數量詞合併輸出。
(4)採用了多子處理器分析模式,支援:英文字母、數字、中文詞彙等分詞處理,相容韓文、日文字元。
(5)優化的詞典儲存,更小的記憶體佔用。支援使用者詞典擴充套件定義。特別的,在2012版本,詞典支援中文,英文,數字混合詞語。

3、測試與使用:

public class Demo {

	//停用詞詞表
	public static final String stopWordTable = "StopWordTable.txt";

	public static void main(String[] args) throws IOException {
		
		//讀入停用詞檔案
		BufferedReader StopWordFileBr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(stopWordTable))));
		//用來存放停用詞的集合
		Set<String> stopWordSet = new HashSet<String>();
		//初如化停用詞集
		String stopWord = null;
		for(; (stopWord = StopWordFileBr.readLine()) != null;){
			stopWordSet.add(stopWord);
		}
		//測試文字
		String text="不同於計算機,人類一睜眼就能迅速看到和看明白一個場景,因為人的大腦皮層至少有一半以上海量神經元參與了視覺任務的完成。";  
		//建立分詞物件
		StringReader sr=new StringReader(text);  
        IKSegmenter ik=new IKSegmenter(sr, false);  
        Lexeme lex=null;  
        //分詞
        while((lex=ik.next())!=null){
        	//去除停用詞
        	if(stopWordSet.contains(lex.getLexemeText())) {
        		continue;
        	}
            System.out.print(lex.getLexemeText()+"|");  
        } 
        //關閉流
        StopWordFileBr.close();
	}
}