IKAnalyzer使用停用詞詞典進行分詞


@Test
// 測試分詞的效果,以及停用詞典是否起作用
public void test() throws IOException {
	String text = "老爹我們都愛您。";
	Configuration configuration = DefaultConfig.getInstance();
	configuration.setUseSmart(true);
	IKSegmenter ik = new IKSegmenter(new StringReader(text), configuration);
	Lexeme lexeme = null;
	while ((lexeme = ik.next()) != null) {
		System.out.println(lexeme.getLexemeText());
	}
}

第二個樣例

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

import javax.imageio.stream.FileImageInputStream;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;



public class TestStopWords {
	public static void main(String[] args) throws IOException {
		String keyWords = "2012年那個歐洲杯四強賽";
		InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("data/stopword.txt")));
		IKSegmenter ikSegmenter = new IKSegmenter(isr, true);
		Lexeme lexeme = null;
		while((lexeme=ikSegmenter.next())!= null){
			System.out.println(lexeme.getLexemeText());
		}
	}
}	

程序的執行結果是:

載入擴展停止詞典:stopword.dic
載入擴展停止詞典:chinese_stopwords.dic
老爹
都愛

IKAnalyzer.cfg.xml的配置例如以下:

<?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">stopword.dic;chinese_stopwords.dic</entry>
</properties>

注意點:

1、停用詞詞典必須是UTF-8編碼。

2、這里非常多跟我一樣的新手沒辦法成功的原因就是被無bom的UTF-8格式給折磨的,IK作者自己也這樣說了。

3、假設你不知道啥叫無BOM,也不確定自己的文件是不是UTF-8無bom,那么請在第一行使用回車換行,從第二行開始加入停止詞。

4、該配置文件以及停用詞詞典均存放在src文件夾以下就可以。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM