自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

全文內容推薦引擎之中文分詞

數(shù)據(jù)庫
基于內容的推薦引擎有兩種實現(xiàn)途徑,一種是根據(jù)條目的元數(shù)據(jù)(可以將元數(shù)據(jù)理解為屬性),另一種是根據(jù)條目的文本描述信息。本系列中將先描述基于條目描述信息的全文檢索實現(xiàn)方式,然后描述基于元數(shù)據(jù)的內容推薦引擎實現(xiàn)方式。

基于內容的推薦引擎有兩種實現(xiàn)途徑,一種是根據(jù)條目的元數(shù)據(jù)(可以將元數(shù)據(jù)理解為屬性),另一種是根據(jù)條目的文本描述信息。本系列中將先描述基于條目描述信息的全文檢索實現(xiàn)方式,然后描述基于元數(shù)據(jù)的內容推薦引擎實現(xiàn)方式。

對于基于條目文本描述信息的內容推薦引擎,目前有很多資料可以參考,基本步聚是先對文本內容進行分詞,包括提取出單詞、去掉常用詞如的地得、加入同意詞、對英語還有去掉復數(shù)形式和過去分詞形式等;第二步是計算各個詞在每篇文章中的出現(xiàn)頻率,以及在所有文章中的出現(xiàn)頻率,即TF/IDF;第三步計算文章向量;***是利用自動聚類算法,對條目進行聚類,這樣就可以實現(xiàn)向用戶推薦同類產品的需求了。

但是在這里有一個非常重要的問題沒有解決,就是中文分詞的問題,這些文章中絕大部分都是以英文為背景的,而英文分詞方面,分出單詞很簡單,只需要空格作為分隔符就可以了,而中文中詞與詞之間沒有空格,其次是英文中單復數(shù)、過去分詞等比較多,需要還原成單數(shù)現(xiàn)在式,但是中文中這個問題基本不存在,再有就是英文需要在分詞后識別長的詞組,而中文這一步也不需進行。

針對以上這些難題,在我的項目中,采用了MMSeg4j中文分詞模塊,這個項目集成了據(jù)說是搜狗輸入法的10萬多詞庫(大家知道中文分詞的關鍵是中文詞庫)。

另外,我還希望中文分詞可以在全文檢索引擎和全文內容推薦引擎共用,由于全文檢索引擎采用了Apache Lucene 3.x版本,需要中文分詞模塊符合Lucene的體系架構,幸運的是MMSeg4j提供了Lucene所需的Tokenizer實現(xiàn)類,同時還需要重點解決如下問題:

  • 由于打開索引文件比較慢,所以整個程序共享一個indexer和searcher
  • 考慮到準實時性需求,采用了Lucene新版本中reopen機制,每次查詢前讀入索引增量
  • 采用Lucene默鎖機制

在項目中我定義了全文檢索引擎類:

  1. public class FteEngine { 
  2.  
  3.   public static void initFteEngine(String _indexPathname) { 
  4.     indexPathname = _indexPathname; 
  5.   } 
  6.  
  7.   public static FteEngine getInstance() { // Singleton模式 
  8.     if (null == engine) { 
  9.       engine = new FteEngine(); 
  10.     } 
  11.     return engine; 
  12.   } 
  13.  
  14.   public IndexWriter getIndexWriter() { 
  15.     return writer; 
  16.   } 
  17.  
  18.   public IndexSearcher getIndexSearcher() { 
  19.     try { 
  20.       IndexReader newReader = reader.reopen(); // 讀入新增加的增量索引內容,滿足實時索引需求 
  21.       if (!reader.equals(newReader)) { 
  22.         reader.close(); 
  23.         reader = newReader; 
  24.       } 
  25.       searcher = new IndexSearcher(reader); 
  26.     } catch (CorruptIndexException e) { .... 
  27.  
  28.     } catch (IOException e) {.... 
  29.     } 
  30.     return searcher; 
  31.   } 
  32.  
  33.   public Analyzer getAnalyzer() { 
  34.     return analyzer; 
  35.   } 
  36.  
  37.   public void stop() { 
  38.     try { 
  39.       if (searcher != null) { 
  40.         searcher.close(); 
  41.       } 
  42.       reader.close(); 
  43.     writer.close(); 
  44.     indexDir.close(); 
  45.     } catch (IOException e) {.... 
  46.     } 
  47.   } 
  48.  
  49.   private FteEngine() { 
  50.     analyzer = new MMSegAnalyzer(); // 初始化中文分詞模塊,會讀入中文字典 
  51.     IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer); 
  52.     iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); 
  53.     try { 
  54.       indexDir = FSDirectory.open(new File(indexPathname)); 
  55.       writer = new IndexWriter(indexDir, iwc); // writer和reader整個程序共用 
  56.       reader = IndexReader.open(writer, true); 
  57.     } catch (CorruptIndexException e) {...... 
  58.     } catch (LockObtainFailedException e) {...... 
  59.     } catch (IOException e) {..... 
  60.  
  61.     } 
  62.   } 
  63.   private static FteEngine engine = null; 
  64.   private static String indexPathname = null; 
  65.   private Directory indexDir = null; 
  66.   private IndexWriter writer = null; 
  67.   private IndexSearcher searcher = null; 
  68.   private Analyzer analyzer = null; 
  69.   private IndexReader reader = null; 
  70.  
  71. 具體中文分詞可以使用如下代碼: 
  72.  
  73. FteEngine fteEngine = FteEngine.getInstance(); 
  74. Analyzer analyzer = fteEngine.getAnalyzer(); 
  75. String text = "測試2011年如java有意見 分岐其中華人民共合國,oracle咬死獵人的狗!"
  76. TokenStream tokenStrm = analyzer.tokenStream("contents"new StringReader(text)); 
  77. OffsetAttribute offsetAttr = tokenStrm.getAttribute(OffsetAttribute.class); 
  78. CharTermAttribute charTermAttr = tokenStrm.getAttribute(CharTermAttribute.class); 
  79. String term = null; 
  80. int i = 0; 
  81. int len = 0; 
  82. char[] charBuf = null; 
  83. try { 
  84.   while (tokenStrm.incrementToken()) { 
  85.   charBuf = charTermAttr.buffer(); 
  86.   for (i = (charBuf.length - 1); i >= 0; i--) { 
  87.     if (charBuf[i] > 0) { 
  88.       len = i + 1; 
  89.       break
  90.     } 
  91.   } 
  92.   //term = new String(charBuf, offsetAttr.startOffset(), offsetAttr.endOffset()); 
  93.   term = new String(charBuf, 0, offsetAttr.endOffset() - offsetAttr.startOffset()); 
  94.   System.out.println(term); 
  95. catch (IOException e) { 
  96.   // TODO Auto-generated catch block 
  97.   e.printStackTrace(); 

打印的內容如下:

測試 2011 年 如 java 有 意見 分 岐 其中 華 人民 共 合 國 oracle 咬 死 獵人 的 狗

當我們在缺省詞庫中加入單詞:分岐 中華人民共合國后,那么分詞結果可以變?yōu)椋?/p>

測試 2011 年 如 java 有 意見 分岐 其 中華人民共合國 oracle 咬 死 獵人 的 狗

由此可見,可以通過完善中文詞庫,得到越來越好的中文分詞效果。

原文鏈接:http://www.cnblogs.com/yantao7589/archive/2011/08/16/2140399.html

【編輯推薦】

  1. 代號:Denali,SQL Server再出擊
  2. 說說SQL Server編年史
  3. 簡單說說SQL Server上的加密術
  4. 擦亮自己的眼睛去看SQL Server
責任編輯:艾婧 來源: 閆濤的博客
相關推薦

2023-08-21 19:37:21

得物DGraph引擎

2025-04-08 02:30:00

2022-09-07 08:16:09

MySQL索引

2024-08-22 12:35:37

2012-12-31 12:02:56

百度推薦引擎數(shù)據(jù)架構

2012-12-28 13:16:35

大數(shù)據(jù)架構

2024-03-07 10:46:13

人工智能?

2011-06-03 16:04:05

SEO分詞

2021-04-12 08:17:12

ElasticSear分詞中文

2015-07-13 11:39:25

SphinxSQL

2024-02-20 09:00:00

2018-12-28 09:48:11

SolrElasticSear搜索

2017-08-17 16:42:38

Elastic 全文搜索服務器

2021-04-12 10:38:17

ElasticSearSolrJava

2009-04-22 14:19:32

Oracle中文索引基礎

2011-06-30 18:33:09

分詞

2012-03-16 10:07:30

IK AnalyzerJava

2023-12-14 15:27:12

中文分詞Python

2011-02-25 14:32:54

LBS

2016-09-18 23:56:51

Java開源中文分詞器
點贊
收藏

51CTO技術棧公眾號