lucene文档解析

 

    利用PDFBOX进行PDF转成文本,可以直接生成LUCENE所需的DOCUMENT。

    PDFBOX1.6中没有包含LucenePDFDocument类,需另下载pdfbox-lucene-1.6.0.jar

   下载http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-lucene/1.6.0

 

doc = LucenePDFDocument.getDocument(file);

    单独使用

 

	public void parsePdfToText(String file) {
		PDDocument doc = null;
		OutputStreamWriter osw = null;
		try {
			try {
				URL url = new URL(file);
				doc = PDDocument.load(url);
			} catch (Exception e) {
				try {
					doc = PDDocument.load(file);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			osw = new OutputStreamWriter(
					new FileOutputStream("D:\\"+System.currentTimeMillis() + ".txt"),"UTF-8");
			PDFTextStripper pts = new PDFTextStripper();
			pts.setSortByPosition(true);
			pts.setStartPage(1);
			pts.setEndPage(Integer.MAX_VALUE);
			pts.setAddMoreFormatting(true);
			pts.writeText(doc, osw);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null != osw){
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();   
				}
			}
			if(null != doc){
				try {
					doc.close();
				} catch (IOException e) {
					e.printStackTrace();   
				}
			}
		}
	}

    word,excel可用poi处理。

 

public static String parseWord(String path) {
		StringBuffer sb = new StringBuffer();
		try {
			HWPFDocument doc = new HWPFDocument(new FileInputStream(path));
			Range range = doc.getRange();
			int rangeNum = range.numParagraphs();
			for (int i = 0; i < rangeNum; i++) {
				Paragraph pg = range.getParagraph(i);
				sb.append(pg.text());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString().trim();
	}

   lius是一个基于lucene的框架,其中有着许多文档转换类。

 

/**
	 * 
	 * parseWordToText:word转为txt使用lius
	 * 引入tm-extractors-0.4.jar和Lius-1.0.jar
	 * 注意tm....jar包要放在lius前面,好像是两个包中有类的冲突问题
	 * (右击->BuildPath->Order and Export中进行调整),否则报java.lang.NoSuchMethodError: 
	 * org.apache.poi.poifs.filesystem.POIFSFileSystem.getRoot()
	 * Lorg/apache/poi/poifs/filesystem/DirectoryEntry
	 * http://stardust1900.wordpress.com/2009/12/04/poi%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98/
	 * @param  @param name    设定文件   
	 * @return void    DOM对象   
	 * @throws    
	 * @since  search1.0
	 */
	public void parseWordToText(String name){
		WordIndexer wi = new WordIndexer(); 
		File file = new File(name);
		try {
			Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\a.txt"));
			wi.setStreamToIndex(new FileInputStream(file));
			writer.write(wi.getContent());
		} catch (Exception e1) {
			e1.printStackTrace();   
		}
	}
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值