Tika文本提取工具的使用(word、pdf、excel等)
2014-01-10 11:17
561 查看
分类:
lucene 2013-05-03 23:42
554人阅读 评论(1)
收藏
举报
Tika是Apache的Lucene项目下面的子项目,在lucene的应用中可以使用tika获取大批量文档中的内容来建立索引,非常方便,也很容易使用~
Tika的缺点就是都是依赖外部的jar包,导致jar包的重量太大,lucene的核心包只有1M,tika约20M,tika依赖的外部的jar包有多样的功能,比如PDFBox和Apache POI能获取文档的字体,布置和内置图片信息,而Tika只是获取文本信息。但是这些外部的jar包又没有把获取文本信息的抽离出一个单独的jar包。
1、Tika的作用
工程结构:
2、Tika的工具类
[java]
view plaincopyprint?
package org.lucene.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
public class IndexUtil {
/**
* 直接读取pdf建立索引,结果是索引建立成功了,但是索引存储的数据却是乱的
*/
public void index() {
try {
File f = new File("F:\\文档资料\\lucene_in_action中文版.pdf");
Directory dir = FSDirectory.open(new File("f:/lucene"));
IndexWriter writer = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_35, new MMSegAnalyzer()));
writer.deleteAll();
Document doc = new Document();
doc.add(new Field("content",new Tika().parse(f)));
writer.addDocument(doc);
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据Tika得到文档的内容,这种比下面那种获取的要简单很多,
* 据tika的文档上说,效率没有下面的那种高,可能封装的比较多
* @param f
* @return
* @throws IOException
* @throws TikaException
*/
public String tikaTool(File f) throws IOException, TikaException {
Tika tika = new Tika();
Metadata metadata = new Metadata();
metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
String str = tika.parseToString(new FileInputStream(f),metadata);
for(String name:metadata.names()) {
System.out.println(name+":"+metadata.get(name));
}
return str;
}
/**
* 根据Parser得到文档的内容
* @param f
* @return
*/
public String fileToTxt(File f) {
Parser parser = new AutoDetectParser();//自动检测文档类型,自动创建相应的解析器
InputStream is = null;
try {
Metadata metadata = new Metadata();
metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
is = new FileInputStream(f);
ContentHandler handler = new BodyContentHandler();
ParseContext context = new ParseContext();
context.set(Parser.class,parser);
parser.parse(is,handler, metadata,context);
for(String name:metadata.names()) {
System.out.println(name+":"+metadata.get(name));
}
return handler.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
} finally {
try {
if(is!=null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
3、测试类
[java]
view plaincopyprint?
package org.lucene.test; import java.io.File; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.junit.Test; import org.lucene.util.IndexUtil; public class TestIndex { @Test public void testIndex() { IndexUtil iu = new IndexUtil(); iu.index(); } @Test public void testTika01() { IndexUtil iu = new IndexUtil(); System.out.println(iu.fileToTxt(new File("F:\\文档资料\\lucene_in_action中文版.pdf"))); } @Test public void testToka02() throws IOException, TikaException { IndexUtil iu = new IndexUtil(); System.out.println(iu.tikaTool(new File("F:\\文档资料\\初级SQL开发指南.doc"))); } }
工程路径:http://download.csdn.net/detail/wxwzy738/5328383
更多0
上一篇:【JAVA】BitSet的源码研究
下一篇:关于lucene的IndexSearcher是否单实例
分类:
lucene 2013-05-03 23:42
554人阅读 评论(1)
收藏
举报
Tika是Apache的Lucene项目下面的子项目,在lucene的应用中可以使用tika获取大批量文档中的内容来建立索引,非常方便,也很容易使用~
Tika的缺点就是都是依赖外部的jar包,导致jar包的重量太大,lucene的核心包只有1M,tika约20M,tika依赖的外部的jar包有多样的功能,比如PDFBox和Apache POI能获取文档的字体,布置和内置图片信息,而Tika只是获取文本信息。但是这些外部的jar包又没有把获取文本信息的抽离出一个单独的jar包。
1、Tika的作用
工程结构:
2、Tika的工具类
[java]
view plaincopyprint?
package org.lucene.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
public class IndexUtil {
/**
* 直接读取pdf建立索引,结果是索引建立成功了,但是索引存储的数据却是乱的
*/
public void index() {
try {
File f = new File("F:\\文档资料\\lucene_in_action中文版.pdf");
Directory dir = FSDirectory.open(new File("f:/lucene"));
IndexWriter writer = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_35, new MMSegAnalyzer()));
writer.deleteAll();
Document doc = new Document();
doc.add(new Field("content",new Tika().parse(f)));
writer.addDocument(doc);
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据Tika得到文档的内容,这种比下面那种获取的要简单很多,
* 据tika的文档上说,效率没有下面的那种高,可能封装的比较多
* @param f
* @return
* @throws IOException
* @throws TikaException
*/
public String tikaTool(File f) throws IOException, TikaException {
Tika tika = new Tika();
Metadata metadata = new Metadata();
metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
String str = tika.parseToString(new FileInputStream(f),metadata);
for(String name:metadata.names()) {
System.out.println(name+":"+metadata.get(name));
}
return str;
}
/**
* 根据Parser得到文档的内容
* @param f
* @return
*/
public String fileToTxt(File f) {
Parser parser = new AutoDetectParser();//自动检测文档类型,自动创建相应的解析器
InputStream is = null;
try {
Metadata metadata = new Metadata();
metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
is = new FileInputStream(f);
ContentHandler handler = new BodyContentHandler();
ParseContext context = new ParseContext();
context.set(Parser.class,parser);
parser.parse(is,handler, metadata,context);
for(String name:metadata.names()) {
System.out.println(name+":"+metadata.get(name));
}
return handler.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
} finally {
try {
if(is!=null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
package org.lucene.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.Version; import org.apache.tika.Tika; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.Parser; import org.apache.tika.sax.BodyContentHandler; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import com.chenlb.mmseg4j.analysis.MMSegAnalyzer; public class IndexUtil { /** * 直接读取pdf建立索引,结果是索引建立成功了,但是索引存储的数据却是乱的 */ public void index() { try { File f = new File("F:\\文档资料\\lucene_in_action中文版.pdf"); Directory dir = FSDirectory.open(new File("f:/lucene")); IndexWriter writer = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_35, new MMSegAnalyzer())); writer.deleteAll(); Document doc = new Document(); doc.add(new Field("content",new Tika().parse(f))); writer.addDocument(doc); writer.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 根据Tika得到文档的内容,这种比下面那种获取的要简单很多, * 据tika的文档上说,效率没有下面的那种高,可能封装的比较多 * @param f * @return * @throws IOException * @throws TikaException */ public String tikaTool(File f) throws IOException, TikaException { Tika tika = new Tika(); Metadata metadata = new Metadata(); metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容 metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); String str = tika.parseToString(new FileInputStream(f),metadata); for(String name:metadata.names()) { System.out.println(name+":"+metadata.get(name)); } return str; } /** * 根据Parser得到文档的内容 * @param f * @return */ public String fileToTxt(File f) { Parser parser = new AutoDetectParser();//自动检测文档类型,自动创建相应的解析器 InputStream is = null; try { Metadata metadata = new Metadata(); metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容 metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); is = new FileInputStream(f); ContentHandler handler = new BodyContentHandler(); ParseContext context = new ParseContext(); context.set(Parser.class,parser); parser.parse(is,handler, metadata,context); for(String name:metadata.names()) { System.out.println(name+":"+metadata.get(name)); } return handler.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (TikaException e) { e.printStackTrace(); } finally { try { if(is!=null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
3、测试类
[java]
view plaincopyprint?
package org.lucene.test; import java.io.File; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.junit.Test; import org.lucene.util.IndexUtil; public class TestIndex { @Test public void testIndex() { IndexUtil iu = new IndexUtil(); iu.index(); } @Test public void testTika01() { IndexUtil iu = new IndexUtil(); System.out.println(iu.fileToTxt(new File("F:\\文档资料\\lucene_in_action中文版.pdf"))); } @Test public void testToka02() throws IOException, TikaException { IndexUtil iu = new IndexUtil(); System.out.println(iu.tikaTool(new File("F:\\文档资料\\初级SQL开发指南.doc"))); } }
package org.lucene.test; import java.io.File; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.junit.Test; import org.lucene.util.IndexUtil; public class TestIndex { @Test public void testIndex() { IndexUtil iu = new IndexUtil(); iu.index(); } @Test public void testTika01() { IndexUtil iu = new IndexUtil(); System.out.println(iu.fileToTxt(new File("F:\\文档资料\\lucene_in_action中文版.pdf"))); } @Test public void testToka02() throws IOException, TikaException { IndexUtil iu = new IndexUtil(); System.out.println(iu.tikaTool(new File("F:\\文档资料\\初级SQL开发指南.doc"))); } }
工程路径:http://download.csdn.net/detail/wxwzy738/5328383
更多0
上一篇:【JAVA】BitSet的源码研究
下一篇:关于lucene的IndexSearcher是否单实例
相关文章推荐
- Tika文本提取工具的使用(word、pdf、excel等)
- 使用Tika、Luke工具解析多种类型(word、pdf、txt 等)索引文件
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 简单明了 c#web 导出 excel,word,pdf, 只包含了文本导出。
- 使用pdfbox实现pdf文本提取和合并功能示例
- .net 下如何将文档文件(Word, Pdf等) 中的文本提取出来(转)
- 提取文档中的文本内容(使用PDFBox处理PDF文档)
- 使用Jsoup.jar写的工具类(java提取Html中的文本)
- JAVA提取Word,Excel,PPT,PDF,TXT等文档文字内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- .net 下如何将文档文件(Word, Pdf等) 中的文本提取出来
- 使用PageOffice实现文档(word,excel,pdf)在线预览编辑
- 自制 Word、Excel 批转 PDF 工具
- 数据分析技术 使用SQL和EXCEL工具 第2版 pdf
- 自制 Word、Excel 批转 PDF 工具
- 使用MSOffice .NET API 将文档(Word \ Excel \ PowerPoint \ Visio \ text \ XML \ RTF \ CSV等)转换为PDF
- 如何使用免费PDF控件从PDF文档中提取文本和图片
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- Java读取Word/Excel/PDF可以使用的第三方包
- 在JAVA项目中,使用JasperReports与iReport,直接连接打印机可打印(word excel xml pdf )文档