1. 程式人生 > >SimHash+漢明距離的C#實現方法

SimHash+漢明距離的C#實現方法

以下為SimHash+漢明距離的C#實現:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace chx
{
    public class SimHash
    {
        private String tokens;
        private BigInteger strSimHash;
        private int hashbits = 128;

        public
BigInteger StrSimHash { get { return strSimHash; } } public SimHash(String tokens, int hashbits) { this.tokens = tokens; this.hashbits = hashbits; this.strSimHash = simHash(); } public
SimHash(String tokens) { this.tokens = tokens; this.strSimHash = simHash(); } private BigInteger simHash() { int[] v = new int[this.hashbits]; ChxTokenizer stringTokens = new ChxTokenizer(this.tokens); while
(stringTokens.hasMoreTokens()) { String temp = stringTokens.nextToken(); BigInteger t = this.hash(temp); //Console.WriteLine("temp = {0} : {1}", temp, t); for (int i = 0; i < this.hashbits; i++) { BigInteger bitmask = BigInteger.One << i; if ((t & bitmask).Sign!=0) { v[i] += 1; } else { v[i] -= 1; } } } BigInteger fingerprint = BigInteger.Zero; for (int i = 0; i < this.hashbits; i++) { if (v[i] >= 0) { fingerprint = fingerprint + (BigInteger.Parse("1") << i); } } return fingerprint; } private BigInteger hash(string source) { if (source == null || source.Length == 0) { return BigInteger.Zero; } else { char[] sourceArray = source.ToCharArray(); BigInteger x = new BigInteger(((long)sourceArray[0]) << 7); BigInteger m = BigInteger.Parse("1000003"); BigInteger mask = BigInteger.Pow(new BigInteger(2), this.hashbits) - BigInteger.One; foreach (char item in sourceArray) { BigInteger temp = new BigInteger((long)item); x = ((x * m) ^ temp) & mask; } x = x ^ (new BigInteger(source.Length)); if (x.Equals(BigInteger.MinusOne)) { x = new BigInteger(-2); } return x; } } public int HammingDistance(SimHash other) { BigInteger m = (BigInteger.One << this.hashbits) - BigInteger.One; BigInteger x = (this.strSimHash ^ other.strSimHash) & m; int tot = 0; while (x.Sign != 0) { tot += 1; x = x & (x- BigInteger.One); } return tot; } } //簡單的分詞法,直接將中文分成單個漢。可以用其他分詞法代替 public class ChxTokenizer { private string source; private int index; private int length; public ChxTokenizer(string source) { this.source = source; this.index = 0; this.length = (source ?? "").Length; } public bool hasMoreTokens() { return index < length; } public string nextToken() { String s = source.Substring(index, 1); index++; return s; } } }

使用方法示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chx
{
    class Program
    {
        static void Main(string[] args)
        {
            Test();
        }
    private static void Test()
    {
        var s1 = "中文分詞太麻煩了,也有些中文分片語件也不錯";
        var hash1 = new SimHash(s1);
        Console.WriteLine("S1.simhash: {0}", hash1.StrSimHash);
        var s2 = "有些中文分詞太麻煩了,也有些中文分片語件也不錯";
        var hash2 = new SimHash(s2);
        Console.WriteLine("S2.simhash: {0}", hash1.StrSimHash);
        var s3 = "有些中文分詞太麻煩了";
        var hash3 = new SimHash(s3);
        Console.WriteLine("S3.simhash: {0}", hash1.StrSimHash);
        Console.WriteLine("============================");  
        Console.WriteLine("s1與s2的漢明距離:{0}",hash1.HammingDistance(hash2));
        Console.WriteLine("s1與s3的漢明距離:{0}",hash1.HammingDistance(hash3));  
    }
   }
}

相關推薦

SimHash+距離C#實現方法

以下為SimHash+漢明距離的C#實現: using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.T

距離 go實現

兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。 給出兩個整數 x 和 y,計算它們之間的漢明距離。 注意: 0 ≤ x, y < 231. 示例: 輸入: x = 1, y = 4 輸出: 2 解釋: 1 (0 0 0 1) 4 (0 1 0 0)

461.距離c++實現

問題描述: 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。 給出兩個整數 x 和 y,計算它們之間的漢明距離。 注意:0 ≤ x, y < 231. 示例: 輸入: x = 1, y = 4 輸出: 2

文字相識度演算法(餘弦相似性、簡單共有詞、編輯距離SimHash距離、Jaccard相似性係數、歐幾里得距離、曼哈頓距離

文字相似度計算在資訊檢索、資料探勘、機器翻譯、文件複製檢測等領域有著廣泛的應用。 比如輿論控制,我們假設你開發了一個微博網站,並且已經把世界上罵人的句子都已經收錄進了資料庫,那麼當一個使用者發微博時會先跟罵人句子的資料庫進行比較,如果符合裡面的句子就不讓使用者發出。

基於私鑰加密公鑰解密的RSA算法C#實現方法

第一個 inter tro 十進制 函數 軟件 產生 ++ 原創 本文實例講述了基於私鑰加密公鑰解密的RSA算法C#實現方法,是一種應用十分廣泛的算法。分享給大家供大家參考之用。具體方法如下: 一、概述 RSA算法是第一個能同時用於加密和數字簽名的算法,也易於理解和操

477. Total Hamming Distance 總的距離

blog += all out xpl bject end min which The Hamming distance between two integers is the number of positions at which the correspondi

477 Total Hamming Distance 距離總和

CP 一個數 logs lee pub 範圍 -h ble min 兩個整數的 漢明距離 指的是這兩個數字的二進制數對應位不同的數量。計算一個數組中,任意兩個數之間漢明距離的總和。示例:輸入: 4, 14, 2輸出: 6解釋: 在二進制表示中,4表示為0100,14表示為1

各種距離 歐式距離、曼哈頓距離、切比雪夫距離、閔可夫斯基距離、標準歐氏距離、馬氏距離、余弦距離距離、傑拉德距離、相關距離、信息熵

form 密碼學 一行 and gif 國際象棋 matlab 三維空間 ffi 1. 歐氏距離(Euclidean Distance) 歐氏距離是最容易直觀理解的距離度量方法,我們小學、初中和高中接觸到的兩個點在空間中的距離一般都是指歐氏距離。 二維平面上點a(x1,

統計二進制中1的個數(LeetCode 461. 距離 or LeetCode 191. 位1的個數)

des 計算 com strong problem 兩個 desc 不同的 esc 題目一 LeetCode 461.明距離(Hamming Distance) 兩個整數之間的漢明距離指的是這兩個數字對應二進制位不同的位置的數目。給出兩個整數 x 和 y,計算它們之間的漢

練習2:Hamming Distance距離

turn 異或操作 二進制位 十進制 min 範圍 get col 最終 1、鏈接地址   https://leetcode.com/problems/hamming-distance/description/ 2、題目要求   漢明距離指兩個整數的二進制表示中,對應位置數

leetcode-461-距離(hamming distance)-java

題目及測試 package pid461; /* 漢明距離 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。 給出兩個整數 x 和 y,計算它們之間的漢明距離。 注意: 0 ≤ x, y < 231. 示例: 輸入: x = 1, y = 4

(java)leetcode461 距離( Hamming Distance)

題目描述: 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。(漢明距離是使用在資料傳輸差錯控制編碼裡面的,漢明距離是一個概念,它表示兩個(相同長度)字對應位不同的數量,我們以d(x,y)表示兩個字x,y之間的漢明距離。對兩個字串進行異或運算,並統計結果為1的個數,那麼這

LeetCode-461-距離

韓明距離就是兩個二進位制數相應位置不同的個數 class Solution { public int hammingDistance(int x, int y) { int i = x ^ y; int count=0; while (i != 0)

leetcode - 461 - 距離

class Solution:     def hammingDistance(self, x, y):         """         :type x: int   &nb

距離的計算

漢明距離,作為一種衡量特徵距離的計算方法,在很多場合都有應用,其主要思想是找到兩個特徵之間的差異大小,也可以說是相似性。 我是在影象處理中用到的,專案中需要計算影象梯度方向,我選擇了四個方向,這樣就可以用二位二進位制表示,分別為 0,1,2,3,也就是 00,01,10,11,這四種情況。這樣,我

leetcode 461. 距離【Easy】【位運算】

題目: 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。 給出兩個整數 x 和 y,計算它們之間的漢明距離。 注意: 0 ≤ x, y < 231. 示例: 輸入: x = 1, y = 4

GPS定位,經緯度附近地點查詢–C 實現方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

python 刷LeetCode 之 【距離

class Solution: def hammingDistance(x, y): """ :type x: int :type y: int :rtype: int """ # 相當於

【LeetCode 簡單題】107-距離

宣告: 今天是第107道題。兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。給出兩個整數 x 和 y,計算它們之間的漢明距離。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵

Leetcode題解之其他(2)距離

題目:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/26/others/65/ 題目描述:  漢明距離 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目