swatchion/simhash
Composer 安装命令:
composer require swatchion/simhash
包简介
A PHP implementation of SimHash algorithm for fast document similarity detection
README 文档
README
一个高效的 PHP 8.0+ SimHash 算法实现库,用于快速文档相似性检测和去重。
功能特点
- 🚀 高性能的 SimHash 算法实现
- 📊 支持文本相似度计算和比较
- 🔧 灵活的配置选项(哈希位数、K-shingles)
- 🌍 支持中文和多语言文本处理
- 📦 标准 Composer 包,易于集成
- ✅ 完整的单元测试覆盖
- 📖 详细的文档和示例
安装
使用 Composer 安装:
composer require swatchion/simhash
快速开始
基本使用
<?php require_once 'vendor/autoload.php'; use Swatchion\SimHash\SimHashFactory; // 快速相似度检查 $text1 = "这是一个测试文本"; $text2 = "这是一个测试文档"; $similarity = SimHashFactory::similarity($text1, $text2); echo "相似度: {$similarity}%"; // 检查是否相似(默认阈值85%) $isSimilar = SimHashFactory::isSimilar($text1, $text2); echo $isSimilar ? "相似" : "不相似";
高级使用
<?php use Swatchion\SimHash\SimHash; // 创建自定义配置的SimHash实例 $simHash = new SimHash(64, 2); // 64位哈希,2-shingles // 生成哈希值 $hash1 = $simHash->hash($text1); $hash2 = $simHash->hash($text2); // 计算汉明距离 $distance = $simHash->hammingDistance($hash1, $hash2); // 转换为十六进制 $hexHash = $simHash->toHex($hash1);
API 参考
SimHash 类
构造函数
public function __construct(int $hashBits = 64, int $kShingles = 2)
主要方法
hash(string $text): string- 生成文本的SimHash值similarity(string $text1, string $text2): float- 计算两个文本的相似度百分比isSimilar(string $text1, string $text2, float $threshold = 85.0): bool- 判断两个文本是否相似hammingDistance(string $hash1, string $hash2): int- 计算两个哈希值的汉明距离toHex(string $binaryHash): string- 转换为十六进制fromHex(string $hexHash): string- 从十六进制转换回二进制
SimHashFactory 类
便捷的静态方法:
SimHashFactory::create(): SimHash- 创建默认实例SimHashFactory::similarity(string $text1, string $text2): float- 快速相似度计算SimHashFactory::isSimilar(string $text1, string $text2, float $threshold = 85.0): bool- 快速相似性判断
算法原理
SimHash 是一种局部敏感哈希算法,具有以下特点:
- 相似文档生成相似的哈希值
- 通过汉明距离快速比较相似性
- 适合大规模文档去重和相似性检测
性能特点
- 时间复杂度: O(n),其中 n 是文本长度
- 空间复杂度: O(1)
- 支持大规模文档处理
- 内存占用低
开发
运行测试
composer test
代码分析
composer analyse
许可证
MIT License
贡献
欢迎提交 Issue 和 Pull Request!
swatchion/simhash 适用场景与选型建议
swatchion/simhash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「text」 「document」 「hash」 「SimHash」 「comparison」 「similarity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 swatchion/simhash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swatchion/simhash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 swatchion/simhash 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Invalidity check of documents in the database of Ministry of the Interior of the Czech Republic
Article CSS-ID Frontend Output Optimization
♺ Laravel Nova Hashids card. Convert your ids.
Common libraries used by Zimbra Api
Text file manipulation
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-28