最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

使用lucene索引和检索中文文件

日期:08-13    来源:中国设计秀    作者:cnwebshow.com

一. 我本来的程序mFT中国设计秀
mFT中国设计秀
    其实我本来的程序挺简单, 完全修改自Demo里面的SearchFiles和IndexFiles. 唯一不同的是引用了SmartCN的分词器.mFT中国设计秀
mFT中国设计秀
    我把修改那一点的代码贴出来.mFT中国设计秀
mFT中国设计秀
    IndexhChinese.java:mFT中国设计秀
mFT中国设计秀
Date start = new Date();try {  IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR),           new SmartChineseAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);  indexDocs(writer, docDir);  System.out.PRintln("Indexing to directory '" +INDEX_DIR+ "'...");  System.out.println("Optimizing...");  //writer.optimize();  writer.close();   Date end = new Date();  System.out.println(end.getTime() - start.getTime() + " total milliseconds"); } mFT中国设计秀
    SearchChinese.javamFT中国设计秀
mFT中国设计秀
Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_CURRENT); BufferedReader in = null;if (queries != null) {  in = new BufferedReader(new FileReader(queries));} else {  in = new BufferedReader(new InputStreamReader(System.in, "GBK"));}mFT中国设计秀
     在这里, 我制定了输入的查询是采用GBK编码的.mFT中国设计秀
mFT中国设计秀
     然后我充满信心的运行后......发现无法检索出中文, 里面的英文检索是正常的.mFT中国设计秀
mFT中国设计秀
mFT中国设计秀
mFT中国设计秀
二. 发现问题.mFT中国设计秀
mFT中国设计秀
     于是我就郁闷了, 由于对于java与lucene都是太熟悉, 而且用的3.0.0版外面的讨论又不是太多, 就瞎折腾了一会儿, 发现我如果把文件的格式另存为ansi就可以检索中文了(以前是utf-8的), 看来是文件编码的问题, 摸索了一下, 在indexChinese.java中发现了如下的代码:mFT中国设计秀
mFT中国设计秀
static void indexDocs(IndexWriter writer, File file)  throws IOException {  // do not try to index files that cannot be read  if (file.canRead()) {    if (file.isDirectory()) {      String[] files = file.list();      // an IO error could occur      if (files != null) {        for (int i = 0; i < files.length; i++) {          indexDocs(writer, new File(file, files[i]));        }      }    } else {      System.out.println("adding " + file);      try {        writer.addDocument(FileDocument.Document(file));      }      // at least on windows, some temporary files raise this exception with an "access denied" message      // checking if the file can be read doesn't help      catch (FileNotFoundException fnfe) {        ;      }    }  }mFT中国设计秀
     重点在于这一句:mFT中国设计秀
mFT中国设计秀
try {  writer.addDocument(FileDocument.Document(file));}mFT中国设计秀
    读取文件的代码应该就在这里面, 跟踪进去:mFT中国设计秀
mFT中国设计秀
public static Document Document(File f)     throws java.io.FileNotFoundException, UnsupportedEncodingException {  Document doc = new Document();   doc.add(new Field("path", f.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));   doc.add(new Field("modified",      DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE),      Field.Store.YES, Field.Index.NOT_ANALYZED));    doc.add(new Field("contents", FileReader(f)));   // return the document  return doc;} private FileDocument() {}}mFT中国设计秀
mFT中国设计秀
     这是Lucene的一个内部类, 作用就是从一个文本文件中获取内容, 生成的Document默认有3个域: path, modified, content, 而content就是文件的文本内容了. 看来是FileReader(f), 这个函数出了问题了, 根本没有制定采用什么编码进行读取啊, 于是把这儿简单的修改了一下.mFT中国设计秀
mFT中国设计秀
FileInputStream fis=new FileInputStream(f);//   按照 UTF-8 编码方式将字节流转化为字符流InputStreamReader isr=new InputStreamReader(fis,"UNICODE");//   从字符流中获取文本并进行缓冲BufferedReader br=new BufferedReader(isr); doc.add(new Field("contents", br));mFT中国设计秀
     至于那个"Unicode"可以修改为支持的所有编码,

本文引用地址:/bc/article_46138.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明