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 🙏

© 2025 – Pkg Stats / Ryan Hefner

edw

v1.1.1

Published

EDW 是一个基于编辑距离算法的文本相似度计算库,支持中文分词和拼音匹配,适用于多种文本匹配场景。

Readme

edw (Edit Distance Weighting) - 文本相似度计算库

EDW 是一个基于编辑距离算法的文本相似度计算库,支持中文分词和拼音匹配,适用于多种文本匹配场景。

功能特点

  • 多级别相似度计算:字符级、词语级、拼音级
  • 自定义权重配置:可调整插入、删除、替换操作的权重
  • 预设权重模板:提供常见场景的权重配置
  • 高性能优化:采用缓存机制和内存池优化
  • 中文支持:集成 jieba 分词和 pinyin-pro 拼音转换
  • 链式调用 API:提供流畅的编程接口

安装

npm install edw

快速开始

import edw from 'edw';

// 基本文本相似度计算
console.log(edw.similarity('北京', '南京')); // 0.5

// 词语级相似度计算
console.log(edw.similarity('我喜欢苹果', '我喜欢香蕉', 'word')); // 0.6

// 拼音级相似度计算
console.log(edw.similarity('重庆', '崇庆', 'pinyin')); // 1.0

// 批量计算相似度
const cities = ['北京', '南京', '上海', '广州'];
const results = edw.batch(cities, '京');
console.log(results);
// [
//   { text: '北京', similarity: 0.5 },
//   { text: '南京', similarity: 0.5 },
//   { text: '上海', similarity: 0 },
//   { text: '广州', similarity: 0 }
// ]

API 说明

核心函数

similarity(s1, s2, level?, config?)

计算两个文本的相似度

参数:

  • s1: 第一个文本字符串
  • s2: 第二个文本字符串
  • level: 计算级别 ('char' | 'word' | 'pinyin'),默认为 'char'
  • config: 权重配置对象

返回值:0-1 之间的相似度值

distance(s1, s2, level?, config?)

计算两个文本的编辑距离

参数与返回值同 similarity 函数

batch(targets, query, level?, config?)

批量计算目标文本与查询文本的相似度

参数:

  • targets: 目标文本数组
  • query: 查询文本
  • level: 计算级别
  • config: 权重配置对象

返回值:按相似度降序排列的结果数组

权重配置

权重配置对象可以包含以下属性:

const config = {
  insert: 1,           // 插入操作权重
  delete: 1,           // 删除操作权重
  substitute: 1,       // 替换操作权重
  position: (pos, char) => 1, // 位置权重函数
  charType: (char) => 1,      // 字符类型权重函数
  usePinyinFallback: true     // 是否使用拼音兜底匹配
};

预设权重

// 重视开头字符
edw.similarity('北京大学', '南京大学', 'char', edw.PRESET_WEIGHTS.frontHeavy);

// 重视中文字符
edw.similarity('北京大学', '南京大学', 'char', edw.PRESET_WEIGHTS.chineseHeavy);

// 重视数字字符
edw.similarity('版本1.0', '版本2.0', 'char', edw.PRESET_WEIGHTS.numberHeavy);

链式调用 API

// 使用链式调用
const result = edw.edw
  .q('北京大学')
  .setLevel('word')
  .use('chineseHeavy')
  .similarity('南京大学');

// 批量计算
const results = edw.edw
  .q('京')
  .batch(['北京', '南京', '上海']);

便捷函数

// 字符级相似度
edw.charSimilarity('北京', '南京');

// 词语级相似度
edw.wordSimilarity('我喜欢苹果', '我喜欢香蕉');

// 拼音级相似度
edw.pinyinSimilarity('重庆', '崇庆');

// 编辑距离
edw.editDistance('北京', '南京');

// 快速批量计算
edw.quickBatch(['北京', '南京'], '京');

性能优化

EDW 采用了多种性能优化技术:

  1. LRU 缓存机制:缓存计算结果避免重复计算
  2. 内存池:重用数组对象减少垃圾回收
  3. 算法优化:针对不同情况采用最优算法
  4. 快速路径:对特殊情况进行快速处理

使用场景

  • 搜索引擎中的文本匹配
  • 数据清洗和去重
  • 推荐系统中的内容匹配
  • 智能客服中的问题匹配
  • 地址匹配和标准化

许可证

MIT