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.     } 

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