Lucene索引前对doc pdf html文件的预处理
来源:互联网 发布:ajax与servlet交互JSON 编辑:程序博客网 时间:2024/05/17 02:00
初学Lucene,写下点小小的心得:
Lucene提供的API能够方便地对文本文档创建索引,但当需要为像.doc 、.pdf 这样的非文本文档创建索引时就必须先把文档转换为纯文本。
对.pdf文件的处理
lib:PDFBox
PDFBox是一个在java环境中对pdf文件进行解析处理的开源软件,同时它也提供了一个丰富的类库支持对pdf文件的操作。PDFBox为使用Lucene的开发者专门提供了LucenePDFDocument类,它的static方法getDocument(ps:该方法被重载三次)能够直接返回一个Lucene的Document类型结果。所以在为一个pdf文件(例子中为File类型实例pdfFile)创建索引时只要写下如下语句就可以了:
getDocument方法的这种形式很好,接下来对.doc以及对.html文件的处理也参照这种形式。
对.doc文件的处理
lib:tm-extractors-0.4
这原是Apache的POI类库的一部分——HWPF,POI提供了一组操作MS-word/excel等文件的方法,在最近的release版本中HWPF被移出,需要下载独立的tm-extractors-0.4。下面的代码中实现了static方法getDocument(File)返回Lucene的Document类型结果,主要通过调用WordExtractor类的成员方法extractor,该方法能返回一个包含所解析doc文件内容的String类型实例。
public static Document getDocument(File doc)...{
String docPath = doc.getAbsolutePath();
String title = doc.getName();
InputStream inputStream = null;
Reader contents = null;
Document document = new Document();
try
...{
inputStream = new FileInputStream(doc);
}
catch (FileNotFoundException e)
...{
e.printStackTrace();
}
WordExtractor extractor = new WordExtractor();
try...{
contents = new StringReader(extractor.extractText(inputStream));
}
catch(Exception e)...{
e.printStackTrace();
}
document.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
document.add(new Field("contents", contents));
document.add(new Field("path", docPath, Field.Store.YES, Field.Index.NO));
return document;
}
}
HTML文件虽然是文本,但是由于其中包含的标记无法被Lucene识别,导致也会被编入索引,而用户在搜索时并不希望搜索到这些标签,所以在对HTML文件创建所以前必须对其进行去标签的处理。
对HTML的处理(去标签)
lib:htmlparser
原本Lucene的DEMO中也附带了一个HtmlParser,只是这个html解析器功能比较弱。另外不知道是不是笔者使用上的错误,在使用索引文件管理器Luke查看时发现,DEMO带的HtmlParser的成员方法getReader所返回的字符流的内容并非是html文件的全部文本内容,而仅仅是标题内容。
在下面的例子中,笔者使用了更为强大的htmlparser,同样在代码中定义了static方法getDocument(File)返回Document类型。
public static Document getDocument(File html)...{
String htmlPath = html.getAbsolutePath();
String text = "";
Parser parser = null;
try...{
parser = new Parser(htmlPath);
}
catch(ParserException e)...{
e.printStackTrace();
}
try...{
parser.setEncoding("UTF-8");
}
catch(ParserException e)...{
e.printStackTrace();
}
HtmlPage visitor = new HtmlPage(parser);
try...{
parser.visitAllNodesWith(visitor);
}
catch(ParserException e)...{
e.printStackTrace();
}
NodeList nodes = visitor.getBody();
int size = nodes.size();
for(int i=0;i<size;i++)...{
Node node = nodes.elementAt(i);
text += node.toPlainTextString();
}
String title = visitor.getTitle();
Reader contents = new StringReader (text);
Document document = new Document();
document.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
document.add(new Field("contents", contents));
document.add(new Field("path", htmlPath, Field.Store.YES, Field.Index.NO));
return document;
}
}
第一次发文,写得比较粗糙,有不对的地方请指出。
- Lucene索引前对doc pdf html文件的预处理
- Lucene索引前对doc pdf html文件的预处理
- Lucene索引doc pdf html
- Lucene对pdf、word、html等文件的处理
- lucene索引word/pdf/html/txt文件及检索(搜索引擎)
- lucene索引word/pdf/html/txt文件及检索(搜索引擎)
- lucene对文件做简单的索引
- lucene实现pdf,doc,xls,ppt,htm,html等格式文件的检索
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
- Lucene 课程设计 检索.doc,.pdf,.html,.execl,.txt格式文件
- lucene pdf+doc+ppt+xls+txt+多层文件
- 使用Lucene对预处理后的文档进行创建索引(可运行)
- lucene的索引文件
- lucene的索引文件
- lucene索引结构(对lucene索引段中的各个类型的文件有所解释)
- [lucene]关于对xml的lucene索引
- Lucene对索引的查询
- Solrj创建doc/pdf/txt文件索引,高亮查询
- MD5算法研究
- 在首页显示部分说明
- 防火墙导致的不能登录Windows域
- 本文介绍了如何从XML文件中读取数据绑定到DropDownList。
- JSP调用Java Bean在网页上动态生成柱状图
- Lucene索引前对doc pdf html文件的预处理
- 今天似乎不是很顺利
- DWR使用体会3:加载信息DWRUtil.useLoadingMessage
- 集成产品开发(IPD)初探
- javascript实现消息提示型信息窗口
- javascript实现消息提示型信息窗口
- 关于strrchr函数的用法
- C知识(1)
- 试试看