summary-words
v0.1.1
Published
Summarize repeated words and phrases in text.
Readme
summary-words
统计文本中重复出现的词语和短语。支持中英文混合文本,自动识别 URL、数字、标点符号等,并过滤被更长短语包含的重复结果。
安装
npm install summary-words快速开始
const { summary } = require('summary-words');
const result = summary({ content: '你好你好' });
// { words: [{ word: '你好', count: 2 }] }const result = summary({
content: 'abc def abc def abc def',
min: 2,
max: 3,
});
// {
// words: [
// { word: 'abc def', count: 3 },
// { word: 'abc def abc', count: 2 },
// { word: 'def abc def', count: 2 },
// ],
// }API
summary(options)
返回一个 SummaryResult 对象,包含按出现次数降序(次数相同时按首次出现位置)排列的重复短语列表。
interface SummaryOptions {
/** 待分析的文本内容(必填) */
content: string;
/** 短语包含的最小"最小词"数量,默认为 2 */
min?: number;
/** 短语包含的最大"最小词"数量,默认为 10 */
max?: number;
/** 是否忽略非标点符号(标点始终作为短语边界),默认为 true */
ignoreSymbols?: boolean;
/** 短语被返回所需的最小出现次数,默认为 2 */
minCount?: number;
}
interface SummaryWord {
word: string;
count: number;
}
interface SummaryResult {
words: SummaryWord[];
}参数说明
content:要分析的文本。min/max:短语由连续若干个"最小词"组成(英文字母序列、数字各算一个最小词;中文等文字每个字符算一个最小词),min和max限定短语长度的范围。min不能大于max,两者都必须是正整数。ignoreSymbols:为true时忽略 emoji 等非标点符号;标点符号无论如何都会结束当前短语。minCount:只有出现次数达到该值的短语才会被返回。
处理规则
- 空格只作为分隔符,不计入最小词数量。
- 标点符号和换行、制表等控制字符是短语边界,短语不会跨越它们。
- 完整的 URL(
http://、https://、www.开头)会被整体跳过,不参与统计。 - 如果一个较短短语被另一个出现次数不少于它的更长短语包含,较短短语会被过滤掉,只保留更有信息量的长短语。
示例
// 中文
summary({ content: '你好你好' });
// { words: [{ word: '你好', count: 2 }] }
// 英文(空格不计入词数)
summary({ content: 'a b a b', min: 2, max: 2 });
// { words: [{ word: 'a b', count: 2 }] }
// 包含小数与英文
summary({ content: 'abc12.5abc12.5', min: 2, max: 2 });
// { words: [{ word: 'abc 12.5', count: 2 }] }
// URL 被跳过,不参与统计
summary({ content: '见 https://example.com 和 https://example.com', min: 1, minCount: 1 });
// { words: [{ word: '见', count: 1 }, { word: '和', count: 1 }] }开发
npm run build # 编译 TypeScript 到 dist/
npm test # 运行 jest 测试License
MIT
