npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

summary-words

v0.1.2

Published

Summarize repeated words and phrases in text.

Readme

summary-words

按“最长重复子串优先”的顺序识别文本中的关键词和短语。支持中英文混合文本,自动识别 URL、数字、标点符号等。

npm

安装

npm install summary-words

快速开始

const { summary } = require('summary-words');

const result = summary({ content: '你好你好' });
// { words: [{ word: '你好', count: 2 }] }
const result = summary({ content: '甲乙丙甲乙丙丁戊丁戊' });
// {
//   words: [
//     { word: '甲乙丙', count: 2 },
//     { word: '丁戊', count: 2 },
//   ],
// }

API

summary(options)

返回一个 SummaryResult 对象,包含按出现次数降序(次数相同时按首次出现位置)排列的重复短语列表。

interface SummaryOptions {
  /** 待分析的文本内容(必填) */
  content: string;
  /** 返回短语的最小“最小词”数量,默认为 2 */
  min?: number;
  /** 返回短语的最大“最小词”数量,默认为 10 */
  max?: number;
  /** 是否忽略非标点符号(标点始终作为短语边界),默认为 true */
  ignoreSymbols?: boolean;
  /** 返回短语所需的最小出现次数,默认为 2 */
  minCount?: number;
  /** 不返回以这些字符串开头的短语,默认为 ['了', '的'] */
  notPrefixList?: string[];
  /** 不返回以这些字符串结尾的短语,默认为 ['了', '的'] */
  notSuffixList?: string[];
}

interface SummaryWord {
  word: string;
  count: number;
}

interface SummaryResult {
  words: SummaryWord[];
}

参数说明

  • content:要分析的文本。
  • min / max:限制最终返回短语的最小词数量范围。它们不会限制最长重复子串的识别或后续剔除流程。英文字母序列、数字各算一个最小词;中文等文字每个字符算一个最小词。
  • ignoreSymbols:为 true 时忽略 emoji 等非标点符号;标点符号无论如何都会结束当前短语。
  • minCount:限制最终返回短语所需的最少、非重叠出现次数;不会改变关键词识别与剔除的流程。
  • notPrefixList:不返回以列表中任一字符串开头的候选短语,默认为 ['了', '的']。传入空数组可关闭此前缀过滤。
  • notSuffixList:不返回以列表中任一字符串结尾的候选短语,默认为 ['了', '的']。传入空数组可关闭此后缀过滤。

处理规则

  1. 使用最小词进行匹配:空格只作分隔符,不计入最小词数量;标点、换行、制表等控制字符是短语边界;完整 URL 会整体跳过。
  2. 每一轮先在所有未剔除的文本片段中,寻找全文出现至少两次、且不匹配 notPrefixList 或 notSuffixList 的最长子串。
  3. 同一轮中所有并列最长的短语都会记录。每个短语只采用互不重叠的出现位置;不同短语的选中位置也不能重叠。发生冲突时,按出现次数降序、首次出现位置升序、短语键升序确定优先级。
  4. 每轮选中的所有位置都会被替换为边界占位,避免相邻文本拼接生成新的短语;然后继续在剩余文本片段中搜索。
  5. 找不到长度不少于 2 且具有至少两次非重叠出现的子串时停止。最后才应用 min、max 与 minCount 过滤,并按次数和首次位置排序。

示例

// 中文
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: [] }

开发

npm run build   # 编译 TypeScript 到 dist/
npm test        # 运行 jest 测试

License

MIT