由于C2C的商城要实现一个简单的商品关键词搜索,建立关键词和商品的映射表需要先对商品标题自动提取关键词,
故用了lucene Analyzer\IKAnalyzer3.2.3做测试,对标题进行关键词切分.
感觉还是IKAnalyzer的分词比较贴切些。
直接附上jsp测试代码:
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" buffer="2kb" session="false" autoFlush="true"%>
<%@page import="java.io.*,
org.apache.lucene.analysis.*,
org.apache.lucene.analysis.TokenStream,
org.apache.lucene.analysis.tokenattributes.TermAttribute,
org.wltea.analyzer.lucene.IKAnalyzer,
org.apache.lucene.util.*
"%>
<%
IKAnalyzer ka = new IKAnalyzer(true);
String s = "发布IKAnalyzer中文分词器 - Java、咖啡与茶";
Reader r = new StringReader(s);
TokenStream ts = (TokenStream)ka.tokenStream("title", r);
ts.addAttribute(TermAttribute.class);
while (ts.incrementToken()) {
TermAttribute ta =ts.getAttribute(TermAttribute.class);
out.print(ta.term());
out.print(" | ");
}
%>
IKAnalyzer.cfg.xml放在 WEB-INF/classes/下,自定义的字典文件也在这个目录下
测试效果如:
当然要用在商城里,我还需要扩展一个商品品牌相关的字典
在此十分感谢作者提供了这么好的一个插件.