當前位置:成語大全網 - 英語詞典 - java分詞中 用戶自定義詞典有什麽要求

java分詞中 用戶自定義詞典有什麽要求

import?java.io.Reader;

import?java.io.StringReader;

import?org.apache.lucene.analysis.*;

import?org.apache.lucene.analysis.cjk.CJKAnalyzer;

import?org.apache.lucene.analysis.cn.ChineseAnalyzer;

import?org.apache.lucene.analysis.standard.StandardAnalyzer;

import?org.mira.lucene.analysis.MIK_CAnalyzer;

public?class?JeAnalyzer?{

public?static?void?testStandard(String?testString)?{

try?{

Analyzer?analyzer?=?new?StandardAnalyzer();

Reader?r?=?new?StringReader(testString);

StopFilter?sf?=?(StopFilter)?analyzer.tokenStream("",?r);

System.err.println("=====standard?analyzer====");

Token?t;

while?((t?=?sf.next())?!=?null)?{

System.out.println(t.termText());

}

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

public?static?void?testCJK(String?testString)?{

try?{

Analyzer?analyzer?=?new?CJKAnalyzer();

Reader?r?=?new?StringReader(testString);

StopFilter?sf?=?(StopFilter)?analyzer.tokenStream("",?r);

System.err.println("=====cjk?analyzer====");

Token?t;

while?((t?=?sf.next())?!=?null)?{

System.out.println(t.termText());

}

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

public?static?void?testChiniese(String?testString)?{

try?{

Analyzer?analyzer?=?new?ChineseAnalyzer();

Reader?r?=?new?StringReader(testString);

TokenFilter?tf?=?(TokenFilter)?analyzer.tokenStream("",?r);

System.err.println("=====chinese?analyzer====");

Token?t;

while?((t?=?tf.next())?!=?null)?{

System.out.println(t.termText());

}

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

public?static?String?transJe(String?testString,?String?c1,?String?c2)?{

String?result?=?"";

try?{

Analyzer?analyzer?=?new?MIK_CAnalyzer();

Reader?r?=?new?StringReader(testString);

TokenStream?ts?=?(TokenStream)?analyzer.tokenStream("",?r);

Token?t;

while?((t?=?ts.next())?!=?null)?{

result?+=?t.termText()?+?",";

}

}?catch?(Exception?e)?{

e.printStackTrace();

}

return?result;

}

public?static?void?main(String[]?args)?{

try?{

String?testString?=?"中文分詞的方法其實不局限於中文應用,也被應用到英文處理,如手寫識別,單詞之間的空格就很清楚,中文分詞方法可以幫助判別英文單詞的邊界";

System.out.println("測試的語句"+testString);

String?sResult[]?=?transJe(testString,?"gb2312",?"utf-8").split(",");

for?(int?i?=?0;?i?<?sResult.length;?i++)?{

System.out.println(sResult[i]);

}

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

}

jar包?

lucene-analyzers-2.4.1.jar

lucene-core-2.4.1.jar

IKAnalyzer2.0.2OBF.jar