Lucene本身的StandardAnalyzer提供了中文分詞接口,不過(guò)其采用的為1-gram.
這種分詞方法雖然不會(huì)損失任何索引信息,但是造成的索引垃圾太多,用戶(hù)得到的查詢(xún)結(jié)果中垃圾也是很多.
經(jīng)過(guò)認(rèn)真研究了Lucene的Analysis包,我寫(xiě)了一個(gè)TjuChineseAnalyzer,效果不錯(cuò).
簡(jiǎn)要介紹一下:
TjuChineseAnlyzer 的功能強(qiáng)大,在中文分詞方面使用JNI調(diào)用ICTCLAS(中國(guó)科學(xué)院,算法為隱馬爾可夫模型)的java接口. 所以其在中文方面性能上同與ICTCLAS.其在英文分詞上采用了Lucene的StopAnalyzer,可以去除 stopWords,而且可以不區(qū)分大小寫(xiě),過(guò)濾掉各類(lèi)標(biāo)點(diǎn)符號(hào).完全兼容Lucene的底層分詞格式.
核心代碼如下:
/**
*Author:zhangbufeng
* TjuAILab (天津大學(xué)人工智能實(shí)驗(yàn)室)
* 2005.9.23
*
* TjuChineseAnlyzer的功能相當(dāng)強(qiáng)大,在中文分詞方面由于其調(diào)用的為ICTCLAS的java接口.
* 所以其在中文方面性能上同與ICTCLAS.其在英文分詞上采用了Lucene的StopAnalyzer,可以去除
* stopWords,而且可以不區(qū)分大小寫(xiě),過(guò)濾掉各類(lèi)標(biāo)點(diǎn)符號(hào).
*/
public final class TjuChineseAnalyzer extends Analyzer {
private Set stopWords;
/** An array containing some common English words that are not usually useful
for searching. */
//可以在此擴(kuò)展English stop words和Chinese stop words
public static final String[] ENGLISH_STOP_WORDS = {
"a", "an", "and", "are", "as", "at", "be", "but", "by",
"for", "if", "in", "into", "is", "it",
"no", "not", "of", "on", "or", "s", "such",
"t", "that", "the", "their", "then", "there", "these",
"they", "this", "to", "was", "will", "with",
"我","我們"
};
/** Builds an analyzer which removes words in ENGLISH_STOP_WORDS. */
public TjuChineseAnalyzer() {
stopWords = StopFilter.makeStopSet(ENGLISH_STOP_WORDS);
}
/** Builds an analyzer which removes words in the provided array. */
public TjuChineseAnalyzer(String[] stopWords) {
this.stopWords = StopFilter.makeStopSet(stopWords);
}
/** Filters LowerCaseTokenizer with StopFilter. */
public TokenStream tokenStream(String fieldName, Reader reader) {
try{
ICTCLAS splitWord = new ICTCLAS();
String inputString = FileIO.readerToString(reader);
String resultString = splitWord.paragraphProcess(inputString);
return new StopFilter(new LowerCaseTokenizer(new StringReader(resultString)),stopWords);
}
catch (IOException e){
System.out.println("轉(zhuǎn)換出錯(cuò)");
return null;
}
}
}
進(jìn)行測(cè)試:
我用Junit進(jìn)行了測(cè)試,效果很好.同時(shí)我用TjuChineseAnalyzer進(jìn)行索引,查詢(xún),測(cè)試
效果都很好.
下邊給出用TjuChineseAnalyzer切分的詞語(yǔ)
原文:
體育訊 在被尤文淘汰之后,皇馬主帥博斯克拒絕接受媒體對(duì)球隊(duì)后防線(xiàn)的批評(píng),同時(shí)還為自己排出的首發(fā)陣容進(jìn)行了辯護(hù)。“失利是全隊(duì)的責(zé)任,而不僅僅是后防線(xiàn)該受指責(zé),”博斯克說(shuō),“我并不認(rèn)為我們踢得一塌糊涂。”“我們進(jìn)入了半決賽,而且在晉級(jí)的道路上一路奮
戰(zhàn)。即使是今天的比賽我們也有幾個(gè)翻身的機(jī)會(huì),但我們面對(duì)的對(duì)手非常強(qiáng)大,他們踢得非常好。”“我們的球迷應(yīng)該為過(guò)去幾個(gè)賽季里我們?cè)诠谲姳械谋憩F(xiàn)感到驕傲。”博斯克還說(shuō)。對(duì)于博斯克在首發(fā)中排出了久疏戰(zhàn)陣的坎比亞索,賽后有記者提出了質(zhì)疑,認(rèn)為完全應(yīng)該將隊(duì)內(nèi)的另一
名球員帕文派遣上場(chǎng)以加強(qiáng)后衛(wèi)線(xiàn)。對(duì)于這一疑議,博斯克拒絕承擔(dān)所謂的“責(zé)任”,認(rèn)為球隊(duì)的首發(fā)沒(méi)有問(wèn)題。“我們按照整個(gè)賽季以來(lái)的方式做了,對(duì)于人員上的變化我沒(méi)有什么可說(shuō)的。”對(duì)于球隊(duì)在本賽季的前景,博斯克表示皇馬還有西甲聯(lián)賽的冠軍作為目標(biāo)。“皇家馬德里在冠軍
杯中戰(zhàn)斗到了最后,我們?cè)诼?lián)賽中也將這么做。”
A Java User Group is a group of people who share a common interest in Java technology and meet on a regular basis to share technical ideas and information. The actual structure of a JUG can vary greatly - from a small number of friends and coworkers meeting informally in the evening, to a large group of companies based in the same geographic area. Regardless of the size and focus of a particular JUG, the sense of community spirit remains the same.
切分結(jié)果如下:
體育 訊 在 被 尤文淘汰 之后 皇 馬 主帥 博斯克 拒絕 接受 媒體 對(duì) 球隊(duì) 后防線(xiàn) 的 批評(píng) 同時(shí) 還 為 自己 排 出 的 首發(fā) 陣容 進(jìn)行 了 辯護(hù)失利 是 全隊(duì) 的 責(zé)任 而 不僅僅 是 后防線(xiàn) 該 受 指責(zé) 博斯克 說(shuō) 并 不 認(rèn)為 踢 得 一塌糊涂 進(jìn)入 了 半決賽 而且 在 晉級(jí)的 道路 上 一路 奮 戰(zhàn) 即使 是 今天 的 比賽 也 有 幾 個(gè) 翻身 的 機(jī)會(huì) 但 面對(duì) 的 對(duì)手 非常 強(qiáng)大 他們 踢 得 非常 好的 球迷 應(yīng)該 為 過(guò)去 幾 個(gè) 賽季 里 在 冠軍杯 中 的 表現(xiàn) 感到 驕傲 博斯克 還 說(shuō) 對(duì)于 博斯克 在 首發(fā) 中 排 出 了 久疏 戰(zhàn) 陣 的 坎比亞索 賽后 有 記者 提出 了 質(zhì)疑 認(rèn)為 完全 應(yīng)該 將 隊(duì) 內(nèi) 的 另 一 名 球員 帕 文 派遣 上場(chǎng) 以 加強(qiáng)后衛(wèi) 線(xiàn) 對(duì)于 這 一 疑 議 博斯克 拒絕 承擔(dān) 所謂 的 責(zé)任 認(rèn)為 球隊(duì) 的 首發(fā) 沒(méi)有 問(wèn)題 按照 整個(gè) 賽季 以來(lái) 的 方式 做 了對(duì)于 人員 上 的 變化 沒(méi)有 什么 可 說(shuō) 的 對(duì)于 球隊(duì) 在 本 賽季 的 前景 博斯克 表示 皇 馬 還有 西 甲 聯(lián)賽 的 冠軍 作為目標(biāo) 皇家 馬德里 在 冠軍 杯 中 戰(zhàn)斗 到 了 最后 在 聯(lián)賽 中 也 將 這么 做 java user group group people who share common interest java technology meet regular basis share technical ideas information actual structure jug can vary greatly from small number friends coworkers meeting informally evening large group companies based same geographic area regardless size focus particular jug sense community spirit remains same
評(píng)析:從結(jié)果可以看出其切分效果,過(guò)濾掉了stop words(包括英文和中文).去除所有的標(biāo)點(diǎn)符號(hào).