Tika文本提取工具的使用(word、pdf、excel等)



分类: 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 plain copy print ?

  1. package org.lucene.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import org.apache.lucene.document.Document;
  8. import org.apache.lucene.document.Field;
  9. import org.apache.lucene.index.CorruptIndexException;
  10. import org.apache.lucene.index.IndexWriter;
  11. import org.apache.lucene.index.IndexWriterConfig;
  12. import org.apache.lucene.store.Directory;
  13. import org.apache.lucene.store.FSDirectory;
  14. import org.apache.lucene.store.LockObtainFailedException;
  15. import org.apache.lucene.util.Version;
  16. import org.apache.tika.Tika;
  17. import org.apache.tika.exception.TikaException;
  18. import org.apache.tika.metadata.Metadata;
  19. import org.apache.tika.parser.AutoDetectParser;
  20. import org.apache.tika.parser.ParseContext;
  21. import org.apache.tika.parser.Parser;
  22. import org.apache.tika.sax.BodyContentHandler;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.SAXException;
  25. import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
  26. public class IndexUtil {
  27. /**
  28. * 直接读取pdf建立索引,结果是索引建立成功了,但是索引存储的数据却是乱的
  29. */
  30. public void index() {
  31. try {
  32. File f = new File("F:文档资料lucene_in_action中文版.pdf");
  33. Directory dir = FSDirectory.open(new File("f:/lucene"));
  34. IndexWriter writer = new IndexWriter(dir,new IndexWriterConfig(Version.LUCENE_35, new MMSegAnalyzer()));
  35. writer.deleteAll();
  36. Document doc = new Document();
  37. doc.add(new Field("content",new Tika().parse(f)));
  38. writer.addDocument(doc);
  39. writer.close();
  40. } catch (CorruptIndexException e) {
  41. e.printStackTrace();
  42. } catch (LockObtainFailedException e) {
  43. e.printStackTrace();
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. /**
  51. * 根据Tika得到文档的内容,这种比下面那种获取的要简单很多,
  52. * 据tika的文档上说,效率没有下面的那种高,可能封装的比较多
  53. * @param f
  54. * @return
  55. * @throws IOException
  56. * @throws TikaException
  57. */
  58. public String tikaTool(File f) throws IOException, TikaException {
  59. Tika tika = new Tika();
  60. Metadata metadata = new Metadata();
  61. metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
  62. metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
  63. String str = tika.parseToString(new FileInputStream(f),metadata);
  64. for(String name:metadata.names()) {
  65. System.out.println(name+":"+metadata.get(name));
  66. }
  67. return str;
  68. }
  69. /**
  70. * 根据Parser得到文档的内容
  71. * @param f
  72. * @return
  73. */
  74. public String fileToTxt(File f) {
  75. Parser parser = new AutoDetectParser();//自动检测文档类型,自动创建相应的解析器
  76. InputStream is = null;
  77. try {
  78. Metadata metadata = new Metadata();
  79. metadata.set(Metadata.AUTHOR, "空号");//重新设置文档的媒体内容
  80. metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
  81. is = new FileInputStream(f);
  82. ContentHandler handler = new BodyContentHandler();
  83. ParseContext context = new ParseContext();
  84. context.set(Parser.class,parser);
  85. parser.parse(is,handler, metadata,context);
  86. for(String name:metadata.names()) {
  87. System.out.println(name+":"+metadata.get(name));
  88. }
  89. return handler.toString();
  90. } catch (FileNotFoundException e) {
  91. e.printStackTrace();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. } catch (SAXException e) {
  95. e.printStackTrace();
  96. } catch (TikaException e) {
  97. e.printStackTrace();
  98. } finally {
  99. try {
  100. if(is!=null) is.close();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. return null;
  106. }
  107. }

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 plain copy print ?

  1. package org.lucene.test;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.tika.exception.TikaException;
  5. import org.junit.Test;
  6. import org.lucene.util.IndexUtil;
  7. public class TestIndex {
  8. @Test
  9. public void testIndex() {
  10. IndexUtil iu = new IndexUtil();
  11. iu.index();
  12. }
  13. @Test
  14. public void testTika01() {
  15. IndexUtil iu = new IndexUtil();
  16. System.out.println(iu.fileToTxt(new File("F:文档资料lucene_in_action中文版.pdf")));
  17. }
  18. @Test
  19. public void testToka02() throws IOException, TikaException {
  20. IndexUtil iu = new IndexUtil();
  21. System.out.println(iu.tikaTool(new File("F:文档资料初级SQL开发指南.doc")));
  22. }
  23. }

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是否单实例