PHP code example of swatchion / simhash

1. Go to this page and download the library: Download swatchion/simhash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swatchion / simhash example snippets



Swatchion\SimHash\SimHashFactory;

// 快速相似度检查
$text1 = "这是一个测试文本";
$text2 = "这是一个测试文档";

$similarity = SimHashFactory::similarity($text1, $text2);
echo "相似度: {$similarity}%";

// 检查是否相似(默认阈值85%)
$isSimilar = SimHashFactory::isSimilar($text1, $text2);
echo $isSimilar ? "相似" : "不相似";


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);

public function __construct(int $hashBits = 64, int $kShingles = 2)
bash
composer analyse