欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lucene-索引word文档

程序员文章站 2022-05-15 17:43:34
...

1、通过POI项目来索引。

public class POIWordDocHandler implements DocumentHandler{

public Document getDocument(InputStream is)throws DocumentHandlerException{

StringbodyText=null;

try{//提取文本字段,接收word文档的inputstream对象,允许把原文写入writer类,从wrtier类提取

WordDocument wd=new wordDocument(is);

StringWriterdocTextWriter=new StringWriter();

wd.writeAllText(bew PrintWriter(docTextWriter));

docTextWriter.close();

bodyText=docTextWriter.toString();

}

catch(Exception e){

throw new DocumentHandlerException("cannot extracttext from a word document",e);

}

if((bodyText!=null)&&(bodyText.trim().length()>0)){

Document doc=new Document();

doc.add(Field.UnStored("body",bodyText));

return doc;

}

returnnull;

}

public static void main() throwsException{

POIWordDocHandler handler=new POIWordDocHandler();

Documentdoc=handler.getDocument(new FileInputStream(newFile(args[0])));

System.out.println(doc);

}

}

2、使用TextMining.org包API,支持从WORD6/95

public class TextMiningWordDocHandler implementsDocumentHandler{

publicDocument getDocument throws DocumentHandlerException(){

String bodyText=null;

try{

bodyText=newWordExtractor().extractText(is);//从InputStream对象中提取文本

}

catch (Exception e){

throw new DocumentHandlerException("cannot extract text from a worddocument",e);

}

 

if((bodyText!=null)&&(bodyText.trim().length()>0)){

Document doc=new Document();

doc.add(Field.unStored("body",bodyText));

return doc;

}

return null;

}

 

publicstatic void main(String[] args) throws Exception{

TextMiningWordDocHandler handler=newTextMiningWordDocHandler();

Document doc=handler.getDocument(new FileInputStream(newFile(args[0])));

System.out.println(doc);

}

}

相关标签: lucene