TextRank4ZH-TS 使用 unbuild 构建工具,生成多种格式以适应不同的使用场景和部署环境。
ES Module 格式,适用于现代构建工具
CommonJS 格式,适用于 Node.js 环境
IIFE 格式,可直接在浏览器中使用
TypeScript 类型定义文件
DedicatedWorker 独立文件
SharedWorker 独立文件
<script src="./dist/index.iife.js"></script>
<script>
const { TextRankKeyword } = window.TextRank4ZH;
const tr4w = new TextRankKeyword();
// 使用库...
</script>
import { TextRankKeyword, TextRankSentence } from 'textrank4zh-ts';
// 或从 CDN 导入
import { TextRankKeyword } from 'https://unpkg.com/textrank4zh-ts@latest/dist/index.mjs';
const tr4w = new TextRankKeyword();
const result = tr4w.analyze(text);
// package.json
{
"dependencies": {
"textrank4zh-ts": "^0.1.0"
}
}
// main.js
import { TextRankKeyword } from 'textrank4zh-ts';
const { TextRankKeyword, TextRankSentence } = require('textrank4zh-ts');
const tr4w = new TextRankKeyword();
const result = tr4w.analyze('中文测试文本');
if (result.ok) {
const keywords = tr4w.getKeywords(10);
console.log('关键词:', keywords);
}
{
"name": "my-project",
"type": "commonjs",
"dependencies": {
"textrank4zh-ts": "^0.1.0"
}
}
<script type="module">
import { TextRankKeyword } from 'https://unpkg.com/textrank4zh-ts@latest/dist/index.mjs';
const tr4w = new TextRankKeyword();
// 使用库...
</script>
<script type="module">
import { TextRankKeyword } from 'https://cdn.jsdelivr.net/npm/textrank4zh-ts@latest/dist/index.mjs';
</script>
<!-- IIFE 版本 -->
<script src="https://cdn.jsdelivr.net/npm/textrank4zh-ts@latest/dist/index.iife.js"></script>
根据您的项目环境选择合适的集成方式:
使用 ES Module 或 IIFE 格式,支持现代浏览器 (ES2020+)
使用 CommonJS 或 ES Module 格式,支持 Node.js 16+
支持 Webpack、Vite、Rollup 等主流构建工具
使用独立的 Worker 文件,支持多线程处理