使用不同的分词器, 最后得到的关键词不同, 需要的时间也不同
需要中文分词是, 用IKAnalyzer是不错的选择, 但相比时间, 我的电脑上大概分词需要800+ms
分词器工作流程:
输入文本(What's your name?)
→关键词划分(What's ; your ; name), 不同分词器分法不同
→消除停用词()
→形态还原 (What's -> What)
→转化小写(What -> what)
private long stime;
private long etime;
private Analyzer analyzer;
@Before
public void s(){
stime = System.currentTimeMillis();
}
@After
public void e(){
etime = System.currentTimeMillis();
System.out.println("使用" + analyzer.getClass().getName() + "分词, 耗时" + (etime - stime) + "ms");
}
@Test
public void test() throws Exception {
//analyzer = new SimpleAnalyzer(Version.LUCENE_35);
//analyzer = new StandardAnalyzer(Version.LUCENE_35);
analyzer = new IKAnalyzer();
analyze(analyzer, "hTTp://www.baidu.com/s?wd=Lucene中文分词");
}
private void analyze(Analyzer analyzer, String text) throws Exception {
TokenStream tokens = analyzer.reusableTokenStream("content", new StringReader(text));
OffsetAttribute offsetAttr = tokens.getAttribute(OffsetAttribute.class);
CharTermAttribute charTermAttr = tokens.getAttribute(CharTermAttribute.class);
while (tokens.incrementToken()) {
char[] charBuf = charTermAttr.buffer();
String term = new String(charBuf, 0, offsetAttr.endOffset() - offsetAttr.startOffset());
System.out.println(term + ", " + offsetAttr.startOffset() + ", " + offsetAttr.endOffset());
}
tokens.close();
// while (ts.incrementToken()) {//过时
// TermAttribute ta = ts.getAttribute(TermAttribute.class);
// System.out.println(ta.term());
// }
}