全文内容推荐引擎之中文分词
基于内容的推荐引擎有两种实现途径,一种是根据条目的元数据(可以将元数据理解为属性),另一种是根据条目的文本描述信息。本系列中将先描述基于条目描述信息的全文检索实现方式,然后描述基于元数据的内容推荐引擎实现方式。
对于基于条目文本描述信息的内容推荐引擎,目前有很多资料可以参考,基本步聚是先对文本内容进行分词,包括提取出单词、去掉常用词如的地得、加入同意词、对英语还有去掉复数形式和过去分词形式等;第二步是计算各个词在每篇文章中的出现频率,以及在所有文章中的出现频率,即TF/IDF;第三步计算文章向量;***是利用自动聚类算法,对条目进行聚类,这样就可以实现向用户推荐同类产品的需求了。
但是在这里有一个非常重要的问题没有解决,就是中文分词的问题,这些文章中绝大部分都是以英文为背景的,而英文分词方面,分出单词很简单,只需要空格作为分隔符就可以了,而中文中词与词之间没有空格,其次是英文中单复数、过去分词等比较多,需要还原成单数现在式,但是中文中这个问题基本不存在,再有就是英文需要在分词后识别长的词组,而中文这一步也不需进行。
针对以上这些难题,在我的项目中,采用了MMSeg4j中文分词模块,这个项目集成了据说是搜狗输入法的10万多词库(大家知道中文分词的关键是中文词库)。
另外,我还希望中文分词可以在全文检索引擎和全文内容推荐引擎共用,由于全文检索引擎采用了Apache Lucene 3.x版本,需要中文分词模块符合Lucene的体系架构,幸运的是MMSeg4j提供了Lucene所需的Tokenizer实现类,同时还需要重点解决如下问题:
- 由于打开索引文件比较慢,所以整个程序共享一个indexer和searcher
- 考虑到准实时性需求,采用了Lucene新版本中reopen机制,每次查询前读入索引增量
- 采用Lucene默锁机制
在项目中我定义了全文检索引擎类:
- public class FteEngine {
- public static void initFteEngine(String _indexPathname) {
- indexPathname = _indexPathname;
- }
- public static FteEngine getInstance() { // Singleton模式
- if (null == engine) {
- engine = new FteEngine();
- }
- return engine;
- }
- public IndexWriter getIndexWriter() {
- return writer;
- }
- public IndexSearcher getIndexSearcher() {
- try {
- IndexReader newReader = reader.reopen(); // 读入新增加的增量索引内容,满足实时索引需求
- if (!reader.equals(newReader)) {
- reader.close();
- reader = newReader;
- }
- searcher = new IndexSearcher(reader);
- } catch (CorruptIndexException e) { ....
- } catch (IOException e) {....
- }
- return searcher;
- }
- public Analyzer getAnalyzer() {
- return analyzer;
- }
- public void stop() {
- try {
- if (searcher != null) {
- searcher.close();
- }
- reader.close();
- writer.close();
- indexDir.close();
- } catch (IOException e) {....
- }
- }
- private FteEngine() {
- analyzer = new MMSegAnalyzer(); // 初始化中文分词模块,会读入中文字典
- IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);
- iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
- try {
- indexDir = FSDirectory.open(new File(indexPathname));
- writer = new IndexWriter(indexDir, iwc); // writer和reader整个程序共用
- reader = IndexReader.open(writer, true);
- } catch (CorruptIndexException e) {......
- } catch (LockObtainFailedException e) {......
- } catch (IOException e) {.....
- }
- }
- private static FteEngine engine = null;
- private static String indexPathname = null;
- private Directory indexDir = null;
- private IndexWriter writer = null;
- private IndexSearcher searcher = null;
- private Analyzer analyzer = null;
- private IndexReader reader = null;
- }
- 具体中文分词可以使用如下代码:
- FteEngine fteEngine = FteEngine.getInstance();
- Analyzer analyzer = fteEngine.getAnalyzer();
- String text = "测试2011年如java有意见 分岐其中华人民共合国,oracle咬死猎人的狗!";
- TokenStream tokenStrm = analyzer.tokenStream("contents", new StringReader(text));
- OffsetAttribute offsetAttr = tokenStrm.getAttribute(OffsetAttribute.class);
- CharTermAttribute charTermAttr = tokenStrm.getAttribute(CharTermAttribute.class);
- String term = null;
- int i = 0;
- int len = 0;
- char[] charBuf = null;
- try {
- while (tokenStrm.incrementToken()) {
- charBuf = charTermAttr.buffer();
- for (i = (charBuf.length - 1); i >= 0; i--) {
- if (charBuf[i] > 0) {
- len = i + 1;
- break;
- }
- }
- //term = new String(charBuf, offsetAttr.startOffset(), offsetAttr.endOffset());
- term = new String(charBuf, 0, offsetAttr.endOffset() - offsetAttr.startOffset());
- System.out.println(term);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
打印的内容如下:
测试 2011 年 如 java 有 意见 分 岐 其中 华 人民 共 合 国 oracle 咬 死 猎人 的 狗
当我们在缺省词库中加入单词:分岐 中华人民共合国后,那么分词结果可以变为:
测试 2011 年 如 java 有 意见 分岐 其 中华人民共合国 oracle 咬 死 猎人 的 狗
由此可见,可以通过完善中文词库,得到越来越好的中文分词效果。
原文链接:http://www.cnblogs.com/yantao7589/archive/2011/08/16/2140399.html
【编辑推荐】
相关文章
- 从本体论开始说起——运营商关系图谱的构建及应用
- 如何成为一名数据科学家?
- 从未见过的堂兄杀了人,你的DNA是关键证据
- 20个安全可靠的免费数据源,各领域数据任你挑
- 20个安全可靠的免费数据源,各领域数据任你挑
- 阿里云李飞飞:All in Cloud时代,云原生数据库优势明显
- 基于Hadoop生态系统的一高性能数据存储格式CarbonData(性能篇)
- 大数据告诉你:10年漫威,到底有多少角色
- TigerGraph:实时图数据库助力金融风控升级
- Splunk利用Splunk Connected Experiences和Splunk Business Flow 扩大数据访问
- 大数据开发常见的9种数据分析手段
- 以免在景区看人,我爬了5W条全国景点门票数据...
- 【实战解析】基于HBase的大数据存储在京东的应用场景
- 数据科学家告诉你哪些计算机科学书籍是你应该看的
- Kafka作为大数据的核心技术,你了解多少?
- Spring Boot 整合 Redis 实现缓存操作
- 大数据学习必须掌握的五大核心技术有哪些?
- 基于Antlr在Apache Flink中实现监控规则DSL化的探索实践
- 甲骨文再次被Gartner评为分析型数据管理解决方案魔力象限领导者
- 爬取吴亦凡微博102118条转发数据,扒一扒流量的真假