楼主: Lisrelchen
2122 15

【Apache Tika】Apache Tika API Usage Examples [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-3-12 07:24:38 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Apache Tika API Usage Examples

[size=17.3333339691162px]This page provides a number of examples on how to use the various Tika APIs. All of the examples shown are also available in the Tika Example module in SVN.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Examples example apache usage ExamP

沙发
Lisrelchen 发表于 2017-3-12 07:25:20
  1. Parsing using the Tika Facade

  2. The Tika facade, provides a number of very quick and easy ways to have your content parsed by Tika, and return the resulting plain text

  3. public String parseToStringExample() throws IOException, SAXException, TikaException {
  4.     Tika tika = new Tika();
  5.     try (InputStream stream = ParsingExample.class.getResourceAsStream("test.doc")) {
  6.         return tika.parseToString(stream);
  7.     }
  8. }
复制代码

藤椅
Lisrelchen 发表于 2017-3-12 07:26:00
  1. Parsing using the Auto-Detect Parser

  2. For more control, you can call the Tika Parsers directly. Most likely, you'll want to start out using the Auto-Detect Parser, which automatically figures out what kind of content you have, then calls the appropriate parser for you.

  3. public String parseExample() throws IOException, SAXException, TikaException {
  4.     AutoDetectParser parser = new AutoDetectParser();
  5.     BodyContentHandler handler = new BodyContentHandler();
  6.     Metadata metadata = new Metadata();
  7.     try (InputStream stream = ParsingExample.class.getResourceAsStream("test.doc")) {
  8.         parser.parse(stream, handler, metadata);
  9.         return handler.toString();
  10.     }
  11. }
复制代码

板凳
Lisrelchen 发表于 2017-3-12 07:26:38
  1. Parsing to Plain Text

  2. By using the BodyContentHandler, you can request that Tika return only the content of the document's body as a plain-text string.

  3. public String parseToPlainText() throws IOException, SAXException, TikaException {
  4.     BodyContentHandler handler = new BodyContentHandler();

  5.     AutoDetectParser parser = new AutoDetectParser();
  6.     Metadata metadata = new Metadata();
  7.     try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test.doc")) {
  8.         parser.parse(stream, handler, metadata);
  9.         return handler.toString();
  10.     }
  11. }
复制代码

报纸
Lisrelchen 发表于 2017-3-12 07:27:17
  1. Parsing to XHTML

  2. By using the ToXMLContentHandler, you can get the XHTML content of the whole document as a string.

  3. public String parseToHTML() throws IOException, SAXException, TikaException {
  4.     ContentHandler handler = new ToXMLContentHandler();

  5.     AutoDetectParser parser = new AutoDetectParser();
  6.     Metadata metadata = new Metadata();
  7.     try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test.doc")) {
  8.         parser.parse(stream, handler, metadata);
  9.         return handler.toString();
  10.     }
  11. }
复制代码

地板
Lisrelchen 发表于 2017-3-12 07:27:45
  1. Fetching just certain bits of the XHTML

  2. It possible to execute XPath queries on the parse results, to fetch only certain bits of the XHTML.

  3. public String parseOnePartToHTML() throws IOException, SAXException, TikaException {
  4.     // Only get things under html -> body -> div (class=header)
  5.     XPathParser xhtmlParser = new XPathParser("xhtml", XHTMLContentHandler.XHTML);
  6.     Matcher divContentMatcher = xhtmlParser.parse("/xhtml:html/xhtml:body/xhtml:div/descendant::node()");
  7.     ContentHandler handler = new MatchingContentHandler(
  8.             new ToXMLContentHandler(), divContentMatcher);

  9.     AutoDetectParser parser = new AutoDetectParser();
  10.     Metadata metadata = new Metadata();
  11.     try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) {
  12.         parser.parse(stream, handler, metadata);
  13.         return handler.toString();
  14.     }
  15. }
复制代码

7
Lisrelchen 发表于 2017-3-12 07:29:04
  1. Streaming the plain text in chunks

  2. Sometimes, you want to chunk the resulting text up, perhaps to output as you go minimising memory use, perhaps to output to HDFS files, or any other reason! With a small custom content handler, you can do that.

  3. public List<String> parseToPlainTextChunks() throws IOException, SAXException, TikaException {
  4.     final List<String> chunks = new ArrayList<>();
  5.     chunks.add("");
  6.     ContentHandlerDecorator handler = new ContentHandlerDecorator() {
  7.         @Override
  8.         public void characters(char[] ch, int start, int length) {
  9.             String lastChunk = chunks.get(chunks.size() - 1);
  10.             String thisStr = new String(ch, start, length);

  11.             if (lastChunk.length() + length > MAXIMUM_TEXT_CHUNK_SIZE) {
  12.                 chunks.add(thisStr);
  13.             } else {
  14.                 chunks.set(chunks.size() - 1, lastChunk + thisStr);
  15.             }
  16.         }
  17.     };

  18.     AutoDetectParser parser = new AutoDetectParser();
  19.     Metadata metadata = new Metadata();
  20.     try (InputStream stream = ContentHandlerExample.class.getResourceAsStream("test2.doc")) {
  21.         parser.parse(stream, handler, metadata);
  22.         return chunks;
  23.     }
  24. }
复制代码

8
auirzxp 学生认证  发表于 2017-3-12 07:29:13
提示: 作者被禁止或删除 内容自动屏蔽

9
Lisrelchen 发表于 2017-3-12 07:29:50
  1. Translation using the Microsoft Translation API

  2. In order to use the Microsoft Translation API, you need to sign up for a Microsoft account, get an API key, then pass the key to Tika before translating.

  3. public String microsoftTranslateToFrench(String text) {
  4.     MicrosoftTranslator translator = new MicrosoftTranslator();
  5.     // Change the id and secret! See http://msdn.microsoft.com/en-us/library/hh454950.aspx.
  6.     translator.setId("dummy-id");
  7.     translator.setSecret("dummy-secret");
  8.     try {
  9.         return translator.translate(text, "fr");
  10.     } catch (Exception e) {
  11.         return "Error while translating.";
  12.     }
  13. }
复制代码

10
Lisrelchen 发表于 2017-3-12 07:30:44
  1. Language Identification

  2. Tika provides support for identifying the language of text, through the LanguageIdentifier class.

  3. public String identifyLanguage(String text) {
  4.     LanguageIdentifier identifier = new LanguageIdentifier(text);
  5.     return identifier.getLanguage();
  6. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 14:56