
Security News
PyPI Expands Trusted Publishing to GitLab Self-Managed as Adoption Passes 25 Percent
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads
textrank4zh-ts
Advanced tools
TextRank算法的TypeScript实现,专门用于中文文本的关键词提取和摘要生成。这是对原Python版本TextRank4ZH的TypeScript重写,完全支持浏览器环境。
npm install textrank4zh-ts
<!-- 通过 CDN 引入 (推荐使用具体版本号) -->
<script type="module">
import { TextRankKeyword, TextRankSentence } from 'https://unpkg.com/textrank4zh-ts@latest/dist/index.mjs';
// 使用库...
</script>
🔗 浏览器在线演示 - 无需安装,直接在浏览器中体验
⚡ Web Worker 演示 - 后台线程处理,性能优化演示
🔧 构建集成示例 - 各种构建方式和部署示例
🚀 完整演示中心 - 所有功能和示例的综合展示
import { TextRankKeyword } from 'textrank4zh-ts';
const text = '北京是中华人民共和国的首都,是全国政治中心、文化中心';
const tr4w = new TextRankKeyword();
// 同步分析
const result = tr4w.analyze(text, {
lower: true,
window: 2
});
if (result.ok) {
// 获取关键词
const keywords = tr4w.getKeywords(10, 1);
keywords.forEach(item => {
console.log(`${item.word}: ${item.weight.toFixed(4)}`);
});
// 获取关键短语
const keyphrases = tr4w.getKeyphrases(10, 2);
console.log('关键短语:', keyphrases);
} else {
console.error('分析失败:', result.error!.message);
}
import { TextRankKeyword } from 'textrank4zh-ts';
const text = '北京是中华人民共和国的首都,是全国政治中心、文化中心...'; // 大量文本
const tr4w = new TextRankKeyword();
// 异步分析,支持进度回调,不阻塞主线程
const result = await tr4w.analyzeAsync(text, {
lower: true,
window: 2,
// 进度回调
onProgress: (progress) => {
console.log(`${progress.phase}: ${progress.progress.toFixed(1)}% - ${progress.message}`);
if (progress.details?.iterations) {
console.log(`PageRank迭代: ${progress.details.iterations}/${progress.details.maxIterations}`);
}
},
// 异步配置
timeSlice: 5, // 5ms时间片
maxContinuousTime: 16, // 60fps保护
priority: 'background' // 后台任务
});
if (result.ok) {
const keywords = tr4w.getKeywords(10, 1);
console.log('关键词:', keywords);
} else {
console.error('异步分析失败:', result.error!);
}
import { TextRankSentence } from 'textrank4zh-ts';
const text = `
北京是中华人民共和国的首都,是全国政治中心、文化中心。
上海是中华人民共和国直辖市,是中国最大的经济中心。
深圳是中国改革开放的前沿城市,经济发展迅速。
`;
const tr4s = new TextRankSentence();
// 分析文本
tr4s.analyze(text, {
lower: true,
source: 'all_filters'
});
// 获取关键句子
const sentences = tr4s.getKeySentences(2);
sentences.forEach(item => {
console.log(`权重: ${item.weight.toFixed(4)}`);
console.log(`句子: ${item.sentence}`);
});
// 生成摘要
const summary = tr4s.getSummary(2, 10, true);
console.log('摘要:', summary);
import { TextRankSentence } from 'textrank4zh-ts';
const longText = `
北京是中华人民共和国的首都,全国政治中心、文化中心、国际交往中心、科技创新中心...
// 大量文本内容
`;
const tr4s = new TextRankSentence();
// 异步分析,避免阻塞主线程
const result = await tr4s.analyzeAsync(longText, {
lower: true,
source: 'no_stop_words',
onProgress: (progress) => {
console.log(`句子分析进度: ${progress.progress}% - ${progress.message}`);
if (progress.phase === 'pagerank' && progress.details) {
console.log(`句子图计算: ${progress.details.processedItems}/${progress.details.totalItems}`);
}
},
// 针对大文本优化的配置
timeSlice: 3, // 较小的时间片
maxContinuousTime: 10, // 更短的连续执行时间
priority: 'normal' // 普通优先级
});
if (result.ok) {
// 获取关键句子
const sentences = tr4s.getKeySentences(3);
console.log('重要句子:', sentences);
// 生成摘要
const summary = tr4s.getSummary(3, 15, true);
console.log('文本摘要:', summary);
} else {
console.error('异步分析失败:', result.error!);
}
// 使用自定义相似度函数的异步分析
const customSimilarity = (words1: string[], words2: string[]): number => {
// 自定义Jaccard相似度
const set1 = new Set(words1);
const set2 = new Set(words2);
const intersection = new Set([...set1].filter(x => set2.has(x)));
const union = new Set([...set1, ...set2]);
return union.size === 0 ? 0 : intersection.size / union.size;
};
const customResult = await tr4s.analyzeWithSimilarityFuncAsync(
longText,
customSimilarity,
{
lower: true,
onProgress: (progress) => {
console.log(`自定义相似度分析: ${progress.progress}%`);
}
}
);
if (customResult.ok) {
const customSummary = tr4s.getSummary(3);
console.log('自定义相似度摘要:', customSummary);
}
import { TextRankUniversalClient, WorkerType } from 'textrank4zh-ts';
// 创建通用客户端 - 自动选择最佳Worker类型
const client = new TextRankUniversalClient('./textrank.worker.js', {
timeout: 30000, // 超时时间
maxConcurrent: 10, // 最大并发任务数
preferredWorkerType: 'auto', // 'shared' | 'dedicated' | 'auto'
fallbackToSync: true, // 允许降级到同步模式
syncScheduling: { // 同步模式调度配置
timeSlice: 5, // 5ms时间片
maxContinuousTime: 16, // 16ms最大连续执行(60fps保护)
priority: 'background' // 后台任务优先级
}
});
// 检查当前使用的Worker类型和状态
const status = client.getStatus();
console.log(`当前模式: ${status.type}, 可用: ${status.available}`);
// 智能关键词分析 - 自动选择执行模式
const keywordResult = await client.analyzeKeywords(text, {
window: 2,
lower: true
}, {
keywords: { num: 10 },
keyphrases: { keywordsNum: 15 }
});
if (keywordResult.success) {
console.log('关键词:', keywordResult.data.keywords);
console.log('执行时间:', keywordResult.duration);
} else {
console.error('分析失败:', keywordResult.error);
}
// 并行处理多个文档
const texts = ['文档1', '文档2', '文档3'];
const results = await Promise.all(
texts.map(text => client.analyzeKeywords(text))
);
// 获取详细状态(包含调度器信息)
const detailedStatus = await client.getDetailedStatus();
if (detailedStatus.schedulerStatus) {
console.log('调度器状态:', detailedStatus.schedulerStatus);
console.log('主线程繁忙程度:', detailedStatus.mainThreadBusyness);
}
// 根据主线程繁忙程度自动优化调度策略
await client.optimizeSyncScheduling();
// 检查浏览器支持情况
if (TextRankUniversalClient.supportsWorkerType(WorkerType.SHARED)) {
console.log('支持 SharedWorker');
}
const recommended = TextRankUniversalClient.getRecommendedWorkerType();
console.log('推荐Worker类型:', recommended);
// 清理资源
client.terminate();
new TextRankKeyword(config?: SegmentationConfig)
同步分析文本并提取关键词,返回 Result 类型以支持函数式错误处理。
参数:
text: 要分析的文本config.window: 滑动窗口大小 (默认: 2)config.lower: 是否转换为小写 (默认: false)config.vertexSource: 构建图节点的词源 (默认: 'all_filters')config.edgeSource: 构建图边的词源 (默认: 'no_stop_words')config.pageRankConfig: PageRank算法配置返回值:
Result<void, TextRankError>: 成功返回 Ok(void),失败返回 Err(TextRankError)异步分析文本并提取关键词,推荐用于大文本处理。使用主线程调度器避免阻塞UI,支持进度回调。
参数:
text: 要分析的文本config: 包含所有同步配置选项,plus:config.onProgress: 进度回调函数 (progress: AnalysisProgress) => voidconfig.timeSlice: 时间片大小(毫秒,默认: 5ms)config.maxContinuousTime: 最大连续执行时间(毫秒,默认: 16ms for 60fps)config.yieldInterval: 让出控制权间隔(迭代次数,默认: 100)config.priority: 任务优先级 'background' | 'normal' | 'user-blocking' (默认: 'background')返回值:
Promise<Result<void, TextRankError>>: 异步返回分析结果使用场景:
获取关键词列表。
参数:
num: 返回的关键词数量 (默认: 6)wordMinLen: 关键词最小长度 (默认: 1)获取关键短语列表。
参数:
keywordsNum: 用于构造短语的关键词数量 (默认: 12)minOccurNum: 短语最少出现次数 (默认: 2)new TextRankSentence(config?: SegmentationConfig)
同步分析文本并计算句子重要性。
参数:
text: 要分析的文本config.lower: 是否转换为小写 (默认: false)config.source: 计算相似度的词源 (默认: 'no_stop_words')config.pageRankConfig: PageRank算法配置异步分析文本并计算句子重要性,推荐用于大文本处理。
参数:
text: 要分析的文本config: 包含所有同步配置选项,plus:config.onProgress: 进度回调函数config.timeSlice: 时间片大小(默认: 5ms)config.maxContinuousTime: 最大连续执行时间(默认: 16ms)config.yieldInterval: 让出控制权间隔(默认: 100)config.priority: 任务优先级(默认: 'background')返回值:
Promise<Result<void, TextRankError>>: 异步返回分析结果使用自定义相似度函数同步分析文本。
参数:
text: 要分析的文本similarityFunc: 自定义相似度计算函数config: 其他配置参数使用自定义相似度函数异步分析文本。
参数:
text: 要分析的文本similarityFunc: 自定义相似度计算函数 (words1: string[], words2: string[]) => numberconfig: 异步配置参数(同analyzeAsync)返回值:
Promise<Result<void, TextRankError>>: 异步返回分析结果获取关键句子列表。
参数:
num: 返回的句子数量 (默认: 6)sentenceMinLen: 句子最小长度 (默认: 6)生成摘要文本。
参数:
num: 摘要句子数量 (默认: 3)sentenceMinLen: 句子最小长度 (默认: 6)sortByIndex: 是否按原文顺序排序 (默认: true)获取所有句子的权重分布,用于调试和分析。
new TextRankUniversalClient(workerUrl: string, options?: WorkerOptions)
三级智能降级架构:
智能关键词分析,自动选择最佳执行模式。
参数:
text: 要分析的文本config: 关键词分析配置options.keywords: 关键词选项 {num?, wordMinLen?}options.keyphrases: 关键短语选项 {keywordsNum?, minOccurNum?}返回: WorkerResult<{keywords?, keyphrases?, duration}>
智能句子分析和摘要生成。
参数:
text: 要分析的文本config: 句子分析配置options.sentences: 句子选项 {num?, sentenceMinLen?}options.summary: 摘要选项 {num?, sentenceMinLen?, sortByIndex?}返回: WorkerResult<{sentences?, summary?, duration}>
获取当前Worker状态和类型。
返回: {type: WorkerType, supported: boolean, available: boolean, connectionCount?}
获取详细状态,包含主线程调度器信息。
根据主线程繁忙程度自动优化同步模式调度策略。
获取当前待处理任务数量。
终止Worker并清理资源,支持SharedWorker连接计数。
检查浏览器是否支持指定Worker类型。
获取当前环境推荐的Worker类型。
interface PageRankConfig {
alpha?: number; // 阻尼因子 (默认: 0.85)
maxIterations?: number; // 最大迭代次数 (默认: 100)
tolerance?: number; // 收敛阈值 (默认: 1e-6)
}
interface AsyncAnalysisConfig {
onProgress?: ProgressCallback; // 进度回调函数
timeSlice?: number; // 时间片大小(毫秒,默认: 5ms)
maxContinuousTime?: number; // 最大连续执行时间(毫秒,默认: 16ms)
yieldInterval?: number; // 让出控制权间隔(迭代次数,默认: 100)
priority?: 'background' | 'normal' | 'user-blocking'; // 任务优先级(默认: 'background')
}
interface AsyncTextRankKeywordConfig extends TextRankKeywordConfig, AsyncAnalysisConfig {}
interface AsyncTextRankSentenceConfig extends TextRankSentenceConfig, AsyncAnalysisConfig {}
interface AnalysisProgress {
phase: 'segmentation' | 'graph_building' | 'pagerank' | 'sorting' | 'complete';
progress: number; // 0-100进度百分比
message: string; // 当前阶段描述
details?: { // 可选的详细信息
totalItems?: number; // 总处理项目数
processedItems?: number; // 已处理项目数
iterations?: number; // 当前迭代次数
maxIterations?: number; // 最大迭代次数
};
}
interface SegmentationConfig {
stopWordsFile?: string; // 停用词文件路径
allowSpeechTags?: string[]; // 允许的词性标签
delimiters?: string[]; // 句子分隔符
}
interface WorkerOptions {
timeout?: number; // 任务超时时间(毫秒,默认: 30000)
maxConcurrent?: number; // 最大并发任务数(默认: 10)
preferredWorkerType?: 'shared' | 'dedicated' | 'auto'; // 首选Worker类型(默认: 'auto')
fallbackToSync?: boolean; // 是否允许降级到同步模式(默认: true)
syncScheduling?: { // 同步模式调度配置
timeSlice?: number; // 时间片大小(毫秒,默认: 5ms)
maxContinuousTime?: number; // 最大连续执行时间(默认: 16ms,60fps)
idleTimeout?: number; // requestIdleCallback超时(默认: 50ms)
yieldInterval?: number; // 让出控制权间隔(默认: 1000次迭代)
priority?: 'background' | 'normal' | 'user-blocking'; // 任务优先级(默认: 'background')
};
}
interface TextRankError {
type: ErrorType; // 错误类型枚举
message: string; // 错误消息
cause?: Error; // 原始错误
context?: Record<string, any>; // 错误上下文
}
enum ErrorType {
INITIALIZATION_ERROR = 'INITIALIZATION_ERROR', // 初始化错误
WORKER_ERROR = 'WORKER_ERROR', // Worker 错误
COMPUTATION_ERROR = 'COMPUTATION_ERROR', // 计算错误
SERIALIZATION_ERROR = 'SERIALIZATION_ERROR', // 序列化错误
VALIDATION_ERROR = 'VALIDATION_ERROR', // 验证错误
NETWORK_ERROR = 'NETWORK_ERROR', // 网络错误
TIMEOUT_ERROR = 'TIMEOUT_ERROR', // 超时错误
UNSUPPORTED_ERROR = 'UNSUPPORTED_ERROR' // 不支持的操作
}
interface WorkerResult {
id: string; // 任务ID
success: boolean; // 执行是否成功
data?: any; // 结果数据
error?: string; // 错误信息
duration?: number; // 执行时长(毫秒)
}
interface WorkerStatus {
type: WorkerType; // 当前Worker类型
supported: boolean; // 是否支持
available: boolean; // 是否可用
connectionCount?: number; // SharedWorker连接数
}
本项目采用 typescript-result 库实现函数式错误处理,完全消除传统 try/catch 异常处理的问题。
import { TextRankKeyword } from 'textrank4zh-ts';
import { handleResult, logError } from 'textrank4zh-ts/utils';
const tr4w = new TextRankKeyword();
// analyze 方法返回 Result<void, TextRankError>
const result = tr4w.analyze('测试文本');
// 方式1:使用 handleResult 辅助函数
handleResult(
result,
() => {
// 成功处理逻辑
const keywords = tr4w.getKeywords();
console.log('关键词:', keywords);
},
(error) => {
// 错误处理逻辑
logError(error, 'KeywordAnalysis');
}
);
// 方式2:直接检查 Result
if (result.ok) {
console.log('分析成功');
} else {
const error = result.error!;
console.error(`错误: ${error.type} - ${error.message}`);
// 查看错误上下文
if (error.context) {
console.error('上下文:', error.context);
}
// 查看原始错误
if (error.cause) {
console.error('原因:', error.cause);
}
}
enum ErrorType {
INITIALIZATION_ERROR, // 初始化错误 - 分词器、Worker等初始化失败
WORKER_ERROR, // Worker 错误 - Worker创建、通信失败
COMPUTATION_ERROR, // 计算错误 - 算法执行过程中的错误
SERIALIZATION_ERROR, // 序列化错误 - 数据传输序列化失败
VALIDATION_ERROR, // 验证错误 - 输入参数验证失败
NETWORK_ERROR, // 网络错误 - Worker脚本加载失败
TIMEOUT_ERROR, // 超时错误 - 任务执行超时
UNSUPPORTED_ERROR // 不支持的操作 - 浏览器API不支持
}
import { TextRankUniversalClient } from 'textrank4zh-ts';
const client = new TextRankUniversalClient('./worker.js');
try {
const result = await client.analyzeKeywords('测试文本');
if (result.success) {
console.log('分析成功:', result.data);
} else {
// Worker 内部错误
console.error('Worker 错误:', result.error!);
}
} catch (error) {
// 客户端错误(网络、超时等)
console.error('客户端错误:', error.message);
}
import { createError, ErrorType, safeSync } from 'textrank4zh-ts/utils';
// 创建自定义错误
const customError = createError(
ErrorType.VALIDATION_ERROR,
'自定义验证失败',
undefined,
{ input: 'invalid data', expected: 'string' }
);
// 安全执行函数
const result = safeSync(() => {
// 可能抛出异常的代码
return JSON.parse(invalidJson);
}, ErrorType.SERIALIZATION_ERROR, { data: 'context info' });
if (!result.ok) {
console.error('执行失败:', result.error!);
}
import { mapResult, chainResult, withDefault } from 'textrank4zh-ts/utils';
const tr4w = new TextRankKeyword();
// 链式处理
const finalResult = chainResult(
tr4w.analyze('测试文本'),
() => {
// 分析成功后获取关键词
const keywords = tr4w.getKeywords(5);
return Result.ok(keywords);
}
);
// 映射结果
const wordCount = mapResult(finalResult, keywords => keywords.length);
// 提供默认值
const count = withDefault(wordCount, 0);
console.log('关键词数量:', count);
# 安装依赖
npm install
# 开发构建
npm run dev
# 完整构建
npm run build
# 运行测试
npm test
# 代码检查和格式化
npm run lint
npm run format
src/
├── core/ # 核心算法实现
│ ├── segmentation.ts # 文本分词和句子分割
│ ├── textrank-keyword.ts # 关键词提取
│ └── textrank-sentence.ts # 句子摘要
├── utils/ # 工具函数
│ ├── async-analysis.ts # 异步分析执行器
│ ├── main-thread-scheduler.ts # 主线程调度器
│ └── result-helpers.ts # Result类型辅助函数
├── worker/ # Worker 多线程支持
└── types/ # TypeScript 类型定义
| 特性 | Node.js 版本 | 浏览器版本 |
|---|---|---|
| 中文分词 | nodejieba (C++ 绑定) | 内置轻量级分词器 |
| 外部依赖 | 需要 C++ 编译 | 零外部依赖 |
| 性能 | 高性能 | 良好性能 |
| 安装大小 | 较大 | 轻量级 |
| 运行环境 | Node.js only | 浏览器 + Node.js |
| 停用词 | 外部文件 | 内置数据 |
| 部署难度 | 较复杂 | 简单 |
A: 检查文件路径和 CORS 设置。Worker 文件需要与页面同源或正确配置 CORS。
// 确保路径正确
const client = new TextRankUniversalClient('./dist/index.worker.js');
// 检查浏览器开发者工具中的网络请求
A: 确认 index.iife.js 正确加载,全局变量名为 TextRank4ZH。
<script src="./dist/index.iife.js"></script>
<script>
console.log(window.TextRank4ZH); // 检查是否存在
const { TextRankKeyword } = window.TextRank4ZH;
</script>
A: 检查是否有不必要的依赖被打包,考虑使用 external 配置排除。当前构建产物大小合理:
A: 确认使用正确的文件格式并检查 package.json 配置。
// ES Module (推荐)
import { TextRankKeyword } from 'textrank4zh-ts';
// CommonJS
const { TextRankKeyword } = require('textrank4zh-ts');
A: 确认项目包含类型定义文件 dist/index.d.ts,并检查 TypeScript 配置。
// tsconfig.json
{
"compilerOptions": {
"moduleResolution": "node",
"types": ["node"]
}
}
A: 项目需要 ES2020+ 支持。对于老版本浏览器,考虑使用 Babel 转译:
// 检查浏览器支持
if (typeof Worker === 'undefined') {
console.warn('当前浏览器不支持 Worker');
// 使用同步模式
}
TextRank算法基于Google的PageRank算法,将文本中的词语或句子看作图中的节点,通过计算节点的重要性来提取关键词或关键句子。
typescript-result@^3.5.2 - 函数式错误处理MIT License
欢迎提交Issue和Pull Request!
本项目参考了TextRank4ZH的实现思路,特此致谢。
FAQs
适用于中文文本关键词提取和摘要生成的 TextRank 算法 TypeScript 实现(兼容浏览器)
The npm package textrank4zh-ts receives a total of 12 weekly downloads. As such, textrank4zh-ts popularity was classified as not popular.
We found that textrank4zh-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
PyPI adds Trusted Publishing support for GitLab Self-Managed as adoption reaches 25% of uploads

Research
/Security News
A malicious Chrome extension posing as an Ethereum wallet steals seed phrases by encoding them into Sui transactions, enabling full wallet takeover.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.