feat: 新增阅读总结模块的阅读时间和字数统计

This commit is contained in:
zhiyu1998 2024-05-30 16:28:48 +08:00
parent e2503023c6
commit 3dceb2a332
2 changed files with 31 additions and 2 deletions

View File

@ -34,7 +34,7 @@ import {
} from "../constants/constant.js"; } from "../constants/constant.js";
import { import {
downloadImg, downloadImg,
downloadMp3, downloadMp3, estimateReadingTime,
formatBiliInfo, formatBiliInfo,
getIdVideo, retryAxiosReq, getIdVideo, retryAxiosReq,
secondsToTime, secondsToTime,
@ -1569,8 +1569,11 @@ export class tools extends plugin {
.setModel(this.aiModel) .setModel(this.aiModel)
.setPrompt(SUMMARY_PROMPT) .setPrompt(SUMMARY_PROMPT)
.build(); .build();
e.reply(`识别:${name},正在为您总结,请稍等...`); e.reply(`识别:${name},正在为您总结,请稍等...`, true, { recallMsg: 60 });
const { ans: kimiAns, model } = await builder.kimi(summaryLink); const { ans: kimiAns, model } = await builder.kimi(summaryLink);
// 计算阅读时间
const stats = estimateReadingTime(kimiAns);
e.reply(`当前 ${name} 预计阅读时间: ${stats.minutes} 分钟,总字数: ${stats.words}`)
const Msg = await this.makeForwardMsg(e, [`「R插件 x ${ model }」联合为您总结内容:`,kimiAns]); const Msg = await this.makeForwardMsg(e, [`「R插件 x ${ model }」联合为您总结内容:`,kimiAns]);
await e.reply(Msg); await e.reply(Msg);
return true; return true;

View File

@ -377,4 +377,30 @@ export async function retryAxiosReq(requestFunction, retries = 3, delay = 1000)
throw error; throw error;
} }
} }
}
/**
* 统计给定文本中的字数
*
* @param {string} text - The text to count words in
* @return {number} The number of words in the text
*/
export function countWords(text) {
return text.split(/\s+/).filter(word => word.length > 0).length;
}
/**
* 根据每分钟平均单词数估计给定文本的阅读时间
*
* @param {string} text - The text for which the reading time is estimated.
* @param {number} wpm - The average words per minute for calculating reading time. Default is 200.
* @return {Object} An object containing the estimated reading time in minutes and the word count.
*/
export function estimateReadingTime(text, wpm = 200) {
const wordCount = countWords(text);
const readingTimeMinutes = wordCount / wpm;
return {
minutes: Math.ceil(readingTimeMinutes),
words: wordCount
};
} }