Tika是一个内容抽取的工具集合(a toolkit for text extracting)。它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面。其次,Tika也提供了便利的扩展API,用来丰富其对第三方文件格式的支持。
1、侦测文档的类型,字符编码,语言,等其他现有文档的属性。
2、提取结构化的文字内容。
3、该项目的目标使用群体主要为搜索引擎以及其他内容索引和分析工具。编程语言为Java.
PDF - 通过Pdfbox
MS-* - 通过POI
HTML - 使用nekohtml将不规范的html整理成为xhtml
OpenOffice 格式 - Tika提供
Archive - zip, tar, gzip, bzip等
扫描二维码关注公众号,回复:
597818 查看本文章
RTF - Tika提供
Java class - Class解析由ASM完成
Image - 只支持图像的元数据抽取
XML
个人理解:Tika只是针对文档的文字处理,如果是图片,就只是抽取文档的类型(元数据),而不是文档的内容,例如图片、音频文件 等就只能支持元数据(metadata)的抽取
1、tika支持简单文本的提取
public class SimpleTextExtractor { public static void main(String[] args) { try { Tika tika = new Tika(); String str = tika.parseToString(new File("d:/test.log")); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } } }
2、根据文档类型,获取metadata的相关属性
public class SimpleTextExtractor { public static void main(String[] args) { MediaType type = MediaType.parse("text/plain; charset=UTF-8"); System.out.println("type: " + type.getType()); System.out.println("subtype: " + type.getSubtype()); Map<String, String> parameters = type.getParameters(); System.out.println("parameters:"); for (String name : parameters.keySet()) { System.out.println(" " + name + "=" + parameters.get(name)); } } }
显示内容:
type: text
subtype: plain
parameters:
charset=UTF-8
3、列出所有文档的类型,总共有1419个种类
public class SimpleTextExtractor { public static void main(String[] args) { MediaTypeRegistry registry = MediaTypeRegistry.getDefaultRegistry(); System.out.println(registry.getTypes().size()); for (MediaType type : registry.getTypes()) { Set<MediaType> aliases = registry.getAliases(type); System.out.println(type + ", also known as " + aliases); } } }
4、根据文档类型,获取器父类型
public class SimpleTextExtractor { public static void main(String[] args) { MediaTypeRegistry registry = MediaTypeRegistry.getDefaultRegistry(); MediaType type = MediaType.parse("image/svg+xml"); while (type != null) { System.out.println(type); type = registry.getSupertype(type); } } }
打印结果:
image/svg+xml
application/xml
text/plain
application/octet-stream
5、根据URL解析网络文件
@Test public void indexTest(){ Tika tika = new Tika();//自动根据文件类型选择Parse类 try { System.out.println(tika.parseToString(new URL("http://www.baidu.com"))); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TikaException e) { e.printStackTrace(); } }