@shelf/tika-text-extract

中文文档
3.0.0发布于 2022/01/19MITCommonJS

Extract text from documents with Apache Tika

原始使用文档
文档目录15 节
  1. What?
  2. Why?
  3. Install
  4. Note
  5. Usage
  6. Execute Tika with a custom path to Java binary
  7. Execute Tika with Java version less than 9
  8. Execute Tika V1
  9. Note
  10. API
  11. tte.startServer(artifactPath)
  12. tte.extract(fileInput)
  13. Publish
  14. How to run tika-text-extract
  15. License

Tika Text Extract

Extract text from any document by Apache Tika

CircleCI npm David npm

What?

The Apache Tika™ toolkit detects and extracts metadata and text from over a thousand different file types (such as PPT, XLS, and PDF). All of these file types can be parsed through a single interface, making Tika useful for search engine indexing, content analysis, translation, and much more.

Why?

This was mainly built for convenience usage in AWS Lambda environment.

If you want to use Tika from node.js you are left with these options:

  • Spawn a CLI - no, extremely inefficient to pay for Java startup time
  • Start HTTP Server
  • Use Java ?

Spawning a Tika as CLI is extremely inefficient. Using Java API from node.js is tedious. This module starts a Tika HTTP Server to stream files to and return a string of extracted text.

Requires java to be present on the system.

Install

$ yarn add @shelf/tika-text-extract

Note

By default in tika-text-extract version 3 use tika-server greater than 2.

Usage

import {readFileSync} from 'fs';
import tte from '@shelf/tika-text-extract';

await tte.startServer('/tmp/tika-server-standard-2.2.1.jar');
const testFile = readFileSync('./README.md');

const extractedText = await tte.extract(testFile);

Execute Tika with a custom path to Java binary

const options = {executableJavaPath: '/bin/jre/java'};

await tte.startServer('/tmp/tika-server-standard-2.2.1.jar', options);
// The next command will be executed:
// /bin/jre/java -jar /tmp/tika-server-standard-2.2.1.jar -noFork

Execute Tika with Java version less than 9

By default, the library does not support Java versions less than 9. In order to use it with Java 8, pass an option to startServer function

const options = {alignWithJava8: true};

await tte.startServer('/tmp/tika-server-standard-2.2.1.jar', options);
// The next command will be executed:
// java -jar /tmp/tika-server-standard-2.2.1.jar -noFork

Execute Tika V1

By default in tika-text-extract version 3 use apache-tika greater than 2. To use tika-server less than 2, pass an option to startServer function

const options = {useTikaV1: true};

await tte.startServer('/tmp/tika-server-1.25.jar', options);
// The next command will be executed:
// /bin/jre/java --add-modules=java.xml.bind,java.activation -Duser.home=/tmp -jar /tmp/tika-server-1.25.jar

Note

If you don't use this option with apache-tika less than 2. You will get an error

API

You can see debug messages by setting env var DEBUG=tika-text-extract

tte.startServer(artifactPath)

Params: artifactPath - path to your tika-server.jar file.

Returns: Promise resolved when server is started. Rejects in case of error.

tte.extract(fileInput)

Params: fileInput - Buffer, String, Stream or Promise of file to extract text from.

Returns: Promise resolved with extracted text.

Publish

$ git checkout master
$ yarn version
$ yarn publish
$ git push origin master --tags

How to run tika-text-extract

Download Java, you can accomplish it with these commands:

mkdir java

docker run --rm -v "$PWD"/java:/lambda/opt lambci/yumda:2 yum install -y java-1.8.0-openjdk-headless.x86_64

Move java folder inside tika-text-extract. Download tika-server which you want to use. You can find it in an archive After that you can run this command:

docker run --rm \
-v "$PWD":/var/task \
-v "$PWD/java":/opt/java \
-v "$PWD/tika":/../layer/tika/ \
lambci/lambda:nodejs12.x basic-usage.handler

License

MIT © Shelf

版本与发布记录

@shelf/tika-text-extract 共发布 3 个版本,首次发布于 2021/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2022/01/194 年前
2.5.12021/06/225 年前
2.5.02021/06/225 年前

关于 @shelf/tika-text-extract 的常见问题

@shelf/tika-text-extract 是什么?

使用Apache Tika从文档中提取文本

如何安装 @shelf/tika-text-extract?

在项目目录运行 npm install @shelf/tika-text-extract,或使用 pnpm add / yarn add / bun add。

@shelf/tika-text-extract 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/shelf__tika-text-extract 是否存在。

@shelf/tika-text-extract 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@shelf/tika-text-extract 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@shelf/tika-text-extract 可以商用吗?

@shelf/tika-text-extract 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@shelf/tika-text-extract 还在维护吗?

最新版本 3.0.0 发布于 2022/01/19(4 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。