发布于 2015-04-25 06:55:28 | 215 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Lucene教程,程序狗速度看过来!

Apache Lucene全文检索引擎工具包

Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。


示例代码

/*
 * 利用Lucene和 XPDF 来处理pdf文件
 * */
package pdfbox;


import java.io.File;
import java.io.IOException;


public class Pdf2Test {
//PDF文件名
private File pdffile;

//转换器的存放位置,默认为E:\xpdf下
private String CONVERTOR_STORED_PATH = "E:\xpdf";

//转换器的名称, 默认为pdftotext
private String CONVERTOR_NAME = "pdftotext";

//构造函数,参数为pdf文件的路径
public Pdf2Test(String pdffile) throws IOException {
this(new File(pdffile));
}


//构造函数,参数为PDF文件对象
public Pdf2Test(File file){
this.pdffile = pdffile;
}

//将PDF转化为文本文档
public void toTextFile() throws IOException{
toTextFile(pdffile, true);
}

//将PDF转化为文本文档,参数为目标文件的路径,默认使用PDF文件的布局
public void toTextFile(String targetfile) throws IOException{
toTextFile(new File(targetfile), true);
}

//将PDF转化为文本文,参数一维目标文件的路径
//参数2位true,则表示使用PDF文件中的布局
public void toTestFile(String targetfile, boolean isLayout) throws IOException{
toTextFile(new File(targetfile), isLayout);
}


//将PDF转化为文本文档,参数为目标文件
public void toTextFile(File targetfile) throws IOException{
toTextFile(targetfile, true);

}

//将PDF转换为文档,参数一维目标文件
//参数二位true,表示使用了PDF文件中的布局
public void toTextFile(File targetfile, boolean isLayout) throws IOException{
String[] cmd = getCmd(targetfile, isLayout);
Process p = Runtime.getRuntime().exec(cmd);
}

//获取转换器
public String getCONVERTOR_STORED_PATH(){
return CONVERTOR_STORED_PATH;
}


//设置PDF转换器的路径
public void setCONVERTOR_STORED_PATH(String path){
if(!path.trim().endsWith("\"))
path = path.trim() + "\";
this.CONVERTOR_STORED_PATH = path;
}

//解析命令行参数
private String[] getCmd(File targetfile, boolean isLayout){
//命令字符
String command = CONVERTOR_STORED_PATH + CONVERTOR_NAME;

//PDF文件的绝对路劲
String source_absolutePath = pdffile.getAbsolutePath();

//输出文本文件的绝对路径
String target_absolutePath = targetfile.getAbsolutePath();

//保持原来的layout
String layout = "-layout";

//设置编码方式
String encoding = "-enc";
String character = "GBK";

//设置不打印任何消息和错误
String mistake = "-q";


//页面之间不加入分页
String nopagebrk = "-nopgbrk";

//如果isLayout为false,则设置不保持原来的layout
if(!isLayout)
layout = "";
return new String[]{
command, layout, encoding, character, mistake, nopagebrk, source_absolutePath, target_absolutePath
};

}

public static void main(String[] args) {
// TODO Auto-generated method stub
try{
//参数输入PDF文件的存放位置
Pdf2Test p2t = new Pdf2Test("E:\Lucene项目\C语言代码.pdf");

//设定转换器的位置
p2t.setCONVERTOR_STORED_PATH("E:\xpdftest\xpdf");

//设置文本文件存放的位置
p2t.toTextFile("E:\Lucene项目\XPDF\");
}catch(Exception e){
e.printStackTrace();
}
}


}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务