前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >apache-tika从ppt-pdf-xls读取文本

apache-tika从ppt-pdf-xls读取文本

作者头像
阿超
发布2024-03-04 09:27:19
1480
发布2024-03-04 09:27:19
举报
文章被收录于专栏:快乐阿超快乐阿超

你若要喜爱自己的价值,你就得给世界创造价值。——歌德

代码仓库:

GitHub - apache/tika: The Apache Tika toolkit detects and extracts metadata and text from over a thousand different file types (such as PPT, XLS, and PDF).

官网:

https://tika.apache.org/

快速开始:

Apache Tika – Getting Started with Apache Tika

代码语言:javascript
复制
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.tika.example;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.xml.sax.ContentHandler;

import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.langdetect.optimaize.OptimaizeLangDetector;
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;

/**
 * Demonstrates how to call the different components within Tika: its
 * {@link Detector} framework (aka MIME identification and repository), its
 * {@link Parser} interface, its {@link org.apache.tika.language.LanguageIdentifier} and other goodies.
 * <p>
 * It also shows the "easy way" via {@link AutoDetectParser}
 */
public class MyFirstTika {
    public static void main(String[] args) throws Exception {
        String filename = args[0];
        TikaConfig tikaConfig = TikaConfig.getDefaultConfig();

        Metadata metadata = new Metadata();
        String text = parseUsingComponents(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);

        System.out.println("-------------------------");

        metadata = new Metadata();
        text = parseUsingAutoDetect(filename, tikaConfig, metadata);
        System.out.println("Parsed Metadata: ");
        System.out.println(metadata);
        System.out.println("Parsed Text: ");
        System.out.println(text);
    }

    public static String parseUsingAutoDetect(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        System.out.println("Handling using AutoDetectParser: [" + filename + "]");

        AutoDetectParser parser = new AutoDetectParser(tikaConfig);
        ContentHandler handler = new BodyContentHandler();
        TikaInputStream stream = TikaInputStream.get(new File(filename), metadata);
        parser.parse(stream, handler, metadata, new ParseContext());
        return handler.toString();
    }

    public static String parseUsingComponents(String filename, TikaConfig tikaConfig,
                                              Metadata metadata) throws Exception {
        MimeTypes mimeRegistry = tikaConfig.getMimeRepository();

        System.out.println("Examining: [" + filename + "]");

        metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, filename);
        System.out.println(
                "The MIME type (based on filename) is: [" + mimeRegistry.detect(null, metadata) +
                        "]");

        InputStream stream = TikaInputStream.get(new File(filename));
        System.out.println(
                "The MIME type (based on MAGIC) is: [" + mimeRegistry.detect(stream, metadata) +
                        "]");

        stream = TikaInputStream.get(new File(filename));
        Detector detector = tikaConfig.getDetector();
        System.out.println("The MIME type (based on the Detector interface) is: [" +
                detector.detect(stream, metadata) + "]");

        LanguageDetector langDetector = new OptimaizeLangDetector().loadModels();
        LanguageResult lang =
                langDetector.detect(FileUtils.readFileToString(new File(filename), UTF_8));

        System.out.println("The language of this content is: [" + lang.getLanguage() + "]");

        // Get a non-detecting parser that handles all the types it can
        Parser parser = tikaConfig.getParser();
        // Tell it what we think the content is
        MediaType type = detector.detect(stream, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.toString());
        // Have the file parsed to get the content and metadata
        ContentHandler handler = new BodyContentHandler();
        parser.parse(stream, handler, metadata, new ParseContext());

        return handler.toString();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档