用lucene.net根据关键字检索本地word文档
2020-12-13 01:50
                         标签:winform   Lucene   style   blog   class   code    目前在做一个winform小软件,其中有一个功能是能根据关键字检索本地保存的word文档。第一次是用com读取word方式(见上一篇文章),先遍历文件夹下的word文档,读取每个文档时循环关键字查找,结果可想而知效率很慢。检索结果是一条接一条显示出来的o(>_
 大致了解了下,我是用C#的,所以要用lucene.net框架,还需要有分词器,lucene.net可以在nuget组件管理中搜到,直接下载到项目中,分词器nuget上也可以搜到。。大概的思路是:遍历文件夹,读取文档内容,进行分词、建索引…… 代码:   一、声明索引文件位置,名称,分词器 StandardAnalyzer分词器与ChineseAnalyzer分词器的区别: 对“123木头人”这句话进行分词,StandardAnalyzer的分词结果:1 2 3 木 头 人;ChineseAnalyzer的分词结果:木 头 
人,把数字过滤了。   二、遍历文件夹及文件,创建索引   三、根据关键字检索   作者:goodgirlmia 用lucene.net根据关键字检索本地word文档,搜素材,soscw.com 用lucene.net根据关键字检索本地word文档 标签:winform   Lucene   style   blog   class   code    原文地址:http://www.cnblogs.com/goodgirlmia/p/3712116.html
1 string filesDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files");
2  static   string indexDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index");
3   // Analyzer analyzer = new Lucene.Net.Analysis.Cn.ChineseAnalyzer();
4 static Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);//用standardAnalyzer分词器


  1         /// 
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
 78                  //doct.ActiveWindow.Selection.WholeStory();
 79                  //doct.ActiveWindow.Selection.Copy();
 80                  //IDataObject data = Clipboard.GetDataObject();
 81                  ////读出的内容赋给content变量
 82                  //string content = data.GetData(DataFormats.Text).ToString();
 83                  string content = doct.Content.Text;
 84                  FileInfo fi = new FileInfo(file.ToString());
 85                  string createTime = fi.CreationTime.ToString();
 86                  string filemark = files[i].DirectoryName + createTime;
 87                  //关闭word
 88                  object missingValue = Type.Missing;
 89                  object miss = System.Reflection.Missing.Value;
 90                  object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
 91                  doct.Close(ref saveChanges, ref missingValue, ref missingValue);
 92                  wordapp.Quit(ref saveChanges, ref miss, ref miss);
 93                  //  StreamReader reader = new StreamReader(fileInfo.FullName);读取txt文件的方法,如读word会出现乱码,不适用于word的读取
 94                  Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
 95 
 96                  writer.DeleteDocuments(new Term("filemark", filemark)); //当索引文件中含有与filemark相等的field值时,会先删除再添加,以防出现重复  
 97                  doc.Add(new Lucene.Net.Documents.Field("filemark", filemark, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NOT_ANALYZED));   //不分词建索引             
 98                  doc.Add(new Lucene.Net.Documents.Field("FileName", filename, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); //ANALYZED分词建索引
 99                  doc.Add(new Lucene.Net.Documents.Field("Content", content, Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED));
100                  doc.Add(new Lucene.Net.Documents.Field("Path", file.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
101                  writer.AddDocument(doc);
102                  writer.Optimize();//优化索引
103              }
104              writer.Dispose();
105          }

 1         /// 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
上一篇:C++实验六;
下一篇:java按位操作符和位移操作符