Lucene 分詞

分詞的基本原理:

1、分詞是用來對文本按語言特徵按算法進行過濾、分組處理的一種技術。

2、分詞的對象是文本,而不是圖像動畫腳本等等。

3、分詞的方式就是過濾和分組。

4、過濾主要把文本中那些沒有實際意義的字或詞過濾掉。

5、分組就是按照”分詞數據庫“內已添加好的詞,進行匹配。


下面來看Lucene分詞器的使用

[java] view plaincopy
  1. package com.qianyan.analyzer;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.StringReader;  
  5.   
  6. import org.apache.lucene.analysis.Analyzer;  
  7. import org.apache.lucene.analysis.SimpleAnalyzer;  
  8. import org.apache.lucene.analysis.Token;  
  9. import org.apache.lucene.analysis.TokenStream;  
  10. import org.apache.lucene.analysis.WhitespaceAnalyzer;  
  11. import org.apache.lucene.analysis.cjk.CJKAnalyzer;  
  12. import org.apache.lucene.analysis.cn.ChineseAnalyzer;  
  13. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  14.   
  15. public class TestAnalyzer {  
  16.   
  17.     public static void main(String[] args) throws IOException {  
  18.         Analyzer analyzer = new StandardAnalyzer(); //標準 過濾停用次  
  19.         //Analyzer analyzer = new SimpleAnalyzer();  //簡單 過濾空格和符號  
  20.         //Analyzer analyzer = new WhitespaceAnalyzer();  //過濾空格  
  21.         //Analyzer analyzer = new ChineseAnalyzer();  //lucene下的中文分詞器   拆分每個字符,過濾符號  
  22.         //Analyzer analyzer = new CJKAnalyzer();  //中文 兩字兩字拆分 英文和standard功能一樣  
  23.         String input = "this is test lucene analyzer class!";  
  24.         TokenStream tokenStream = analyzer.tokenStream(""new StringReader(input));  
  25.         Token token = new Token();  
  26.         while(null != tokenStream.next(token))  
  27.             System.out.println(token.term());  
  28.     }  
  29. }  

對於初學者,我們只需要掌握這些經典的分詞器就足夠了。

但在實際的開發過程中,滿足我們需要得,是一些基於lucene分詞之上的第三方中文分詞包,在這裏我們只介紹 ”庖丁分詞包“,命令借鑑了”庖丁解牛“這個成語。

庖丁解牛,我國古代成語,出自《莊子》,比喻經過反覆實踐,掌握了事物的客觀規律,做事得心應手,運用自如。

下載網址鏈接:http://code.google.com/p/paoding/

解壓後我們需要對項目添加2個jar包,解壓目錄下的paoding-analysis.jar 和lib下的 commons-logging.jar 。令把dic文件夾複製到我們的項目src目錄下。

[java] view plaincopy
  1. package com.qianyan.analyzer;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.StringReader;  
  5.   
  6. import net.paoding.analysis.analyzer.PaodingAnalyzer;  
  7.   
  8. import org.apache.lucene.analysis.Analyzer;  
  9. import org.apache.lucene.analysis.Token;  
  10. import org.apache.lucene.analysis.TokenStream;  
  11.   
  12. public class TestPaodingAnalyzer {  
  13.   
  14.     public static void main(String[] args) throws IOException {  
  15.         Analyzer analyzer = new PaodingAnalyzer();  
  16.         String input = "我愛北京天安門!";  
  17.         TokenStream ts = analyzer.tokenStream(""new StringReader(input));  
  18.         Token token = new Token();  
  19.         while(null != (token = ts.next(null)))  
  20.             System.out.println(token.term());  
  21.     }  
  22. }  

大家通過這個例子可以看到,paoding分詞器相當的強大,它的語法在此不過多介紹,有興趣的朋友可以看解壓後的中文操作手冊。

下面來看下實際中運用

首先根據paoding分詞器建立索引:

[java] view plaincopy
  1. package com.qianyan.index;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import net.paoding.analysis.analyzer.PaodingAnalyzer;  
  6.   
  7. import org.apache.lucene.analysis.Analyzer;  
  8. import org.apache.lucene.document.Document;  
  9. import org.apache.lucene.document.Field;  
  10. import org.apache.lucene.index.IndexWriter;  
  11. import org.apache.lucene.store.Directory;  
  12. import org.apache.lucene.store.FSDirectory;  
  13.   
  14. public class TestPaodingIndex {  
  15.   
  16.     public static void main(String[] args) throws IOException{  
  17.           
  18.         String[] ids = {"1""2""3""4"};  
  19.         String[] names = {"張三""李四""李五""趙六"};  
  20.         String[] addresses = {"居住在北京""南京""北京海淀""南寧"};  
  21.         String[] birthdays = {"19820720""19840203""19770409""19830130"};  
  22.         Analyzer analyzer = new PaodingAnalyzer();  
  23.         String indexDir = "E:/luceneindex";  
  24.         Directory dir = FSDirectory.getDirectory(indexDir);  
  25.         IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);  
  26.         for(int i = 0; i < ids.length; i++){  
  27.             Document document = new Document();  
  28.             document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.ANALYZED));  
  29.             document.add(new Field("name", names[i], Field.Store.YES, Field.Index.ANALYZED));  
  30.             document.add(new Field("address", addresses[i], Field.Store.YES, Field.Index.ANALYZED));  
  31.             document.add(new Field("birthday", birthdays[i], Field.Store.YES, Field.Index.ANALYZED));  
  32.             writer.addDocument(document);  
  33.         }  
  34.         writer.optimize();  
  35.         writer.close();  
  36.     }  
  37.       
  38. }  

然後來看簡單的檢索類

[java] view plaincopy
  1. package com.qianyan.search;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.lucene.document.Document;  
  6. import org.apache.lucene.index.Term;  
  7. import org.apache.lucene.search.IndexSearcher;  
  8. import org.apache.lucene.search.PrefixQuery;  
  9. import org.apache.lucene.search.ScoreDoc;  
  10. import org.apache.lucene.search.TermQuery;  
  11. import org.apache.lucene.search.TopDocs;  
  12. import org.apache.lucene.search.WildcardQuery;  
  13. import org.apache.lucene.store.Directory;  
  14. import org.apache.lucene.store.FSDirectory;  
  15.   
  16. public class TestPaodingSearch {  
  17.   
  18.     public static void main(String[] args) throws IOException {  
  19.         String indexDir = "E:/luceneindex";  
  20.         Directory dir = FSDirectory.getDirectory(indexDir);  
  21.         IndexSearcher searcher = new IndexSearcher(dir);  
  22.         ScoreDoc[] hits = null;  
  23.           
  24.         /*Term term = new Term("address", "北京"); 
  25.         TermQuery query = new TermQuery(term); 
  26.         */  
  27.           
  28.         /*Term term = new Term("name", "張"); 
  29.         PrefixQuery query = new PrefixQuery(term);*/  
  30.           
  31.         Term term = new Term("name""李*");  
  32.         WildcardQuery query = new WildcardQuery(term);  
  33.           
  34.         TopDocs topDocs = searcher.search(query, 100);  
  35.         hits = topDocs.scoreDocs;  
  36.         for(int i = 0; i < hits.length; i++){  
  37.             Document doc = searcher.doc(hits[i].doc);  
  38.             System.out.print(hits[i].score + " ");  
  39.             System.out.print(doc.get("id") + " ");  
  40.             System.out.print(doc.get("name") + " ");  
  41.             System.out.print(doc.get("address") + " ");  
  42.             System.out.println(doc.get("birthday") + " ");  
  43.         }  
  44.           
  45.         searcher.close();  
  46.         dir.close();  
  47.     }  
  48. }  

下面是來看QueryParser檢索類

[java] view plaincopy
  1. package com.qianyan.search;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import net.paoding.analysis.analyzer.PaodingAnalyzer;  
  6.   
  7. import org.apache.lucene.analysis.Analyzer;  
  8. import org.apache.lucene.document.Document;  
  9. import org.apache.lucene.queryParser.ParseException;  
  10. import org.apache.lucene.queryParser.QueryParser;  
  11. import org.apache.lucene.search.IndexSearcher;  
  12. import org.apache.lucene.search.Query;  
  13. import org.apache.lucene.search.ScoreDoc;  
  14. import org.apache.lucene.search.TopDocCollector;  
  15. import org.apache.lucene.store.Directory;  
  16. import org.apache.lucene.store.FSDirectory;  
  17.   
  18. public class TestQueryParser {  
  19.     public static void main(String[] args) throws IOException, ParseException {  
  20.         Analyzer analyzer = new PaodingAnalyzer();  
  21.         String indexDir = "E:/luceneindex";  
  22.         Directory dir = FSDirectory.getDirectory(indexDir);  
  23.         IndexSearcher searcher = new IndexSearcher(dir);  
  24.         ScoreDoc[] hits = null;  
  25.           
  26.         QueryParser parser = new QueryParser("address", analyzer);      //name爲默認字段檢索  
  27.         Query query = parser.parse("北京");             
  28.         //Query query = parser.parse("birthday:[19820720 TO 19840203]"); //中括號包含首尾,花括號不包含。TO指範圍  
  29.         //Query query = parser.parse("張~"); //前綴檢索  
  30.         //Query query = parser.parse("上海 北京");    
  31.         //Query query = parser.parse("(居住 or 北京) and 海淀");    
  32.         //Query query = parser.parse("上海 北京 AND NOT name:李四");  
  33.         //Query query = parser.parse("name:李*"); //前綴檢索  
  34.         TopDocCollector topdoc = new TopDocCollector(100);  
  35.           
  36.         searcher.search(query, topdoc);  
  37.         hits = topdoc.topDocs().scoreDocs;  
  38.           
  39.         for(int i = 0; i < hits.length; i++){  
  40.             Document doc = searcher.doc(hits[i].doc);  
  41.             //System.out.println(hits[i].score);  
  42.             System.out.print(doc.get("id") + " ");  
  43.             System.out.print(doc.get("name") + " ");  
  44.             System.out.print(doc.get("address") + " ");  
  45.             System.out.println(doc.get("birthday") + " ");  
  46.         }  
  47.           
  48.         searcher.close();  
  49.         dir.close();  
  50.     }  
  51. }  

下面我們來學習Paoding對文件的索引

[java] view plaincopy
  1. package com.qianyan.file;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8.   
  9. import net.paoding.analysis.analyzer.PaodingAnalyzer;  
  10.   
  11. import org.apache.lucene.analysis.Analyzer;  
  12. import org.apache.lucene.document.Document;  
  13. import org.apache.lucene.document.Field;  
  14. import org.apache.lucene.index.IndexWriter;  
  15. import org.apache.lucene.store.Directory;  
  16. import org.apache.lucene.store.FSDirectory;  
  17.   
  18. public class TestFileIndex {  
  19.    
  20.     public static void main(String[] args) throws IOException {  
  21.         String dataDir = "E:\\lucenedata";  
  22.         String indexDir = "E:\\luceneindex";  
  23.           
  24.         File[] files = new File(dataDir).listFiles();  
  25.         Analyzer analyzer = new PaodingAnalyzer();  
  26.         Directory dir = FSDirectory.getDirectory(indexDir);  
  27.         IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);  
  28.         for(int i = 0; i < files.length; i++){  
  29.             StringBuffer strBuffer = new StringBuffer();  
  30.             String line = "";  
  31.             FileInputStream fs = new FileInputStream(files[i].getCanonicalPath());  
  32.             BufferedReader reader = new BufferedReader(new InputStreamReader(fs));  
  33.             line = reader.readLine();  
  34.             while(null != line){  
  35.                 strBuffer.append(line).append("\n");  
  36.                 line = reader.readLine();  
  37.             }  
  38.             Document document = new Document();  
  39.             document.add(new Field("fileName", files[i].getName(), Field.Store.YES, Field.Index.ANALYZED));  
  40.             document.add(new Field("contents", strBuffer.toString(), Field.Store.YES, Field.Index.ANALYZED));  
  41.             writer.addDocument(document);  
  42.             fs.close();  
  43.             reader.close();  
  44.         }  
  45.         writer.close();  
  46.         dir.close();  
  47.     }  
  48. }  

然後是對之前索引的檢索類:

[java] view plaincopy
  1. package com.qianyan.file;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.lucene.document.Document;  
  6. import org.apache.lucene.index.Term;  
  7. import org.apache.lucene.search.IndexSearcher;  
  8. import org.apache.lucene.search.ScoreDoc;  
  9. import org.apache.lucene.search.TermQuery;  
  10. import org.apache.lucene.search.TopDocs;  
  11. import org.apache.lucene.search.WildcardQuery;  
  12. import org.apache.lucene.store.Directory;  
  13. import org.apache.lucene.store.FSDirectory;  
  14.   
  15. public class TestSearch {  
  16.   
  17.     public static void main(String[] args) throws IOException {  
  18.         String indexDir = "E:/luceneindex";  
  19.         Directory dir = FSDirectory.getDirectory(indexDir);  
  20.         IndexSearcher searcher = new IndexSearcher(dir);  
  21.         ScoreDoc[] hits = null;  
  22.           
  23.         Term term = new Term("fileName""星期");  
  24.         TermQuery query = new TermQuery(term);  
  25.               
  26.           
  27.         TopDocs topDocs = searcher.search(query, 100);  
  28.         hits = topDocs.scoreDocs;  
  29.         for(int i = 0; i < hits.length; i++){  
  30.             Document doc = searcher.doc(hits[i].doc);  
  31.             System.out.print(hits[i].score + " ");  
  32.             System.out.println(doc.get("fileName") + " ");  
  33.             System.out.println(doc.get("contents") + " ");  
  34.         }  
  35.           
  36.         searcher.close();  
  37.         dir.close();  
  38.     } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章