108 lines
3.5 KiB
TypeScript
108 lines
3.5 KiB
TypeScript
|
|
/**
|
|||
|
|
* 测试脚本:按章分块服务
|
|||
|
|
*
|
|||
|
|
* 用法:
|
|||
|
|
* npx ts-node scripts/test-structure-chunking.ts [文件路径]
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
import fs from 'fs';
|
|||
|
|
import path from 'path';
|
|||
|
|
import { structureChunkingService } from '../src/services/structureChunkingService';
|
|||
|
|
|
|||
|
|
// 测试文本:多章节
|
|||
|
|
const TEST_TEXT_CHAPTERS = `
|
|||
|
|
第一章 绪论
|
|||
|
|
|
|||
|
|
人工智能(Artificial Intelligence,AI)是计算机科学的一个分支,
|
|||
|
|
它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器。
|
|||
|
|
|
|||
|
|
人工智能的发展可以追溯到20世纪50年代。1956年,在达特茅斯会议上,
|
|||
|
|
"人工智能"这一术语首次被提出。
|
|||
|
|
|
|||
|
|
第二章 机器学习基础
|
|||
|
|
|
|||
|
|
监督学习是机器学习的一种方法,其中训练数据既包含输入数据,也包含期望的输出结果。
|
|||
|
|
模型通过学习输入和输出之间的映射关系来进行预测。
|
|||
|
|
|
|||
|
|
无监督学习是另一种机器学习方法,训练数据只包含输入数据,没有对应的输出标签。
|
|||
|
|
模型需要自己发现数据中的结构和模式。
|
|||
|
|
|
|||
|
|
第 3 章 深度学习
|
|||
|
|
|
|||
|
|
深度学习是机器学习的一个子领域,使用多层神经网络来学习数据的层次化表示。
|
|||
|
|
卷积神经网络(CNN)特别适合处理图像数据,而循环神经网络(RNN)则适合处理序列数据。
|
|||
|
|
`;
|
|||
|
|
|
|||
|
|
// 测试文本:无章节结构
|
|||
|
|
const TEST_TEXT_NO_STRUCTURE = `
|
|||
|
|
这是一段没有明确结构的文本。
|
|||
|
|
|
|||
|
|
它只是一些段落的集合,没有章节标题,也没有编号。
|
|||
|
|
|
|||
|
|
用户可能会上传这样的文本,我们需要能够处理它。
|
|||
|
|
`;
|
|||
|
|
|
|||
|
|
async function main() {
|
|||
|
|
console.log('='.repeat(60));
|
|||
|
|
console.log('按章分块服务测试');
|
|||
|
|
console.log('='.repeat(60));
|
|||
|
|
console.log();
|
|||
|
|
|
|||
|
|
const filePath = process.argv[2];
|
|||
|
|
let testTexts: { name: string; text: string }[] = [];
|
|||
|
|
|
|||
|
|
if (filePath) {
|
|||
|
|
const absolutePath = path.resolve(filePath);
|
|||
|
|
if (!fs.existsSync(absolutePath)) {
|
|||
|
|
console.error(`错误:文件不存在 - ${absolutePath}`);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
const fileContent = fs.readFileSync(absolutePath, 'utf-8');
|
|||
|
|
testTexts.push({ name: `文件: ${path.basename(filePath)}`, text: fileContent });
|
|||
|
|
} else {
|
|||
|
|
testTexts = [
|
|||
|
|
{ name: '多章节', text: TEST_TEXT_CHAPTERS },
|
|||
|
|
{ name: '无结构', text: TEST_TEXT_NO_STRUCTURE },
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (const { name, text } of testTexts) {
|
|||
|
|
console.log('-'.repeat(60));
|
|||
|
|
console.log(`测试: ${name}`);
|
|||
|
|
console.log(`输入长度: ${text.length} 字符`);
|
|||
|
|
console.log('-'.repeat(60));
|
|||
|
|
|
|||
|
|
const startTime = Date.now();
|
|||
|
|
const result = await structureChunkingService.parseAsync(text);
|
|||
|
|
const duration = Date.now() - startTime;
|
|||
|
|
|
|||
|
|
console.log(`成功: ${result.success}`);
|
|||
|
|
console.log(`模式: ${result.pattern || '无'}`);
|
|||
|
|
console.log(`分块数: ${result.chunks.length}`);
|
|||
|
|
console.log(`字符数: ${result.totalCharacters}`);
|
|||
|
|
console.log(`耗时: ${duration}ms`);
|
|||
|
|
console.log();
|
|||
|
|
|
|||
|
|
if (result.chunks.length > 0) {
|
|||
|
|
console.log('分块详情:');
|
|||
|
|
for (const chunk of result.chunks) {
|
|||
|
|
const preview = chunk.content.substring(0, 50).replace(/\n/g, ' ');
|
|||
|
|
console.log(` [${chunk.order + 1}] ${chunk.title}`);
|
|||
|
|
console.log(` ${preview}${chunk.content.length > 50 ? '...' : ''} (${chunk.content.length} 字符)`);
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
console.log('未检测到章级结构');
|
|||
|
|
}
|
|||
|
|
console.log();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('='.repeat(60));
|
|||
|
|
console.log('测试完成');
|
|||
|
|
console.log('='.repeat(60));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
main().catch((err) => {
|
|||
|
|
console.error('测试失败:', err);
|
|||
|
|
process.exit(1);
|
|||
|
|
});
|