Lucene入門級筆記五 -- 分詞器,使用中文分詞器,擴(kuò)展詞庫,停用詞
Posted on 2011-04-17 19:25 Kevin_Zhang 閱讀(3515) 評論(1) 編輯 收藏 引用 所屬分類: 搜索引擎
1. 常見的中文分詞器有:極易分詞的(MMAnalyzer) 、"庖丁分詞"分詞器(PaodingAnalzyer)、IKAnalyzer 等等。其中 MMAnalyzer 和 PaodingAnalzyer 不支持 lucene3.0及以后版本。
使用方式都類似,在構(gòu)建分詞器時
Analyzer analyzer = new [My]Analyzer(); 

2. 這里只示例 IKAnalyzer,目前只有它支持Lucene3.0 以后的版本。 
首先需要導(dǎo)入 IKAnalyzer3.2.0Stable.jar 包

3. 示例代碼
view plaincopy to clipboardprint?
public class AnalyzerTest
{
@Test 
public void test() throws Exception
{
String text = "An IndexWriter creates and maintains an index."; 
/**//* 標(biāo)準(zhǔn)分詞器:單子分詞 */
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
testAnalyzer(analyzer, text);
String text2 = "測試中文環(huán)境下的信息檢索";
testAnalyzer(new IKAnalyzer(), text2); // 使用IKAnalyzer,詞庫分詞
}

/** *//**
* 使用指定的分詞器對指定的文本進(jìn)行分詞,并打印結(jié)果
*
* @param analyzer
* @param text
* @throws Exception
*/ 
private void testAnalyzer(Analyzer analyzer, String text) throws Exception
{
System.out.println("當(dāng)前使用的分詞器:" + analyzer.getClass());
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
tokenStream.addAttribute(TermAttribute.class);

while (tokenStream.incrementToken())
{
TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
System.out.println(termAttribute.term());
}
}
}

public class AnalyzerTest
{
@Test
public void test() throws Exception
{
String text = "An IndexWriter creates and maintains an index.";
/**//* 標(biāo)準(zhǔn)分詞器:單子分詞 */
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
testAnalyzer(analyzer, text);
String text2 = "測試中文環(huán)境下的信息檢索";
testAnalyzer(new IKAnalyzer(), text2); // 使用IKAnalyzer,詞庫分詞
}

/** *//**
* 使用指定的分詞器對指定的文本進(jìn)行分詞,并打印結(jié)果
*
* @param analyzer
* @param text
* @throws Exception
*/
private void testAnalyzer(Analyzer analyzer, String text) throws Exception
{
System.out.println("當(dāng)前使用的分詞器:" + analyzer.getClass());
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
tokenStream.addAttribute(TermAttribute.class);

while (tokenStream.incrementToken())
{
TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class);
System.out.println(termAttribute.term());
}
}
}

3. 如何擴(kuò)展詞庫:很多情況下,我們可能需要定制自己的詞庫,例如 XXX 公司,我們希望這能被分詞器識別,并拆分成一個詞。
IKAnalyzer 可以很方便的實(shí)現(xiàn)我們的這種需求。
新建 IKAnalyzer.cfg.xml
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<!-- 1,文件要是 UTF-8 編碼。2,一行寫一個詞 -->
<!--用戶可以在這里配置自己的擴(kuò)展字典-->
<entry key="ext_dict">/mydict.dic</entry>
</properties>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<!-- 1,文件要是 UTF-8 編碼。2,一行寫一個詞 -->
<!--用戶可以在這里配置自己的擴(kuò)展字典-->
<entry key="ext_dict">/mydict.dic</entry>
</properties> 

解析:
<entry key="ext_dict">/mydict.dic</entry> 擴(kuò)展了一個自己的詞典,名字叫 mydict.dic
因此我們要建一個文本文件,名為:mydict.dic (此處使用的 .dic 并非必須)
在這個文本文件里寫入:
北京XXXX科技有限公司
這樣就添加了一個詞匯。
如果要添加多個,則新起一行:
詞匯一
詞匯二
詞匯三


需要注意的是,這個文件一定要使用 UTF-8編碼

4. 停用詞:
有些詞在文本中出現(xiàn)的頻率非常高,但是對文本所攜帶的信息基本不產(chǎn)生影響,例如英文的"a、an、the、of",或中文的"的、了、著",以及各種標(biāo)點(diǎn)符號等,這樣的詞稱為停用詞(stop word)。
文本經(jīng)過分詞之后,停用詞通常被過濾掉,不會被進(jìn)行索引。在檢索的時候,用戶的查詢中如果含有停用詞,檢索系統(tǒng)也會將其過濾掉(因?yàn)橛脩糨斎氲牟樵冏址惨M(jìn)行分詞處理)。
排除停用詞可以加快建立索引的速度,減小索引庫文件的大小。
IKAnalyzer 中自定義停用詞也非常方便,和配置 "擴(kuò)展詞庫" 操作類型,只需要在 IKAnalyzer.cfg.xml 加入如下配置:
<entry key="ext_stopwords">/ext_stopword.dic</entry> 
同樣這個配置也指向了一個文本文件 /ext_stopword.dic (后綴名任意),格式如下:
也
了
仍
從




本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/wenlin56/archive/2010/12/13/6074124.aspx

