|
| 1 | +package similarity |
| 2 | + |
| 3 | +import ( |
| 4 | + "hash/crc32" |
| 5 | + "strconv" |
| 6 | + "unicode/utf8" |
| 7 | +) |
| 8 | + |
| 9 | +type Simhash struct { |
| 10 | +} |
| 11 | + |
| 12 | +func (s Simhash) CompareAscii(s1, s2 string) float64 { |
| 13 | + return s.CompareUtf8(s1, s2) |
| 14 | + |
| 15 | +} |
| 16 | +func (s Simhash) CompareUtf8(utf8Str1, utf8Str2 string) float64 { |
| 17 | + // 字符串长度 |
| 18 | + l1 := utf8.RuneCountInString(utf8Str1) |
| 19 | + l2 := utf8.RuneCountInString(utf8Str2) |
| 20 | + // 将字符串转换为字符数组 |
| 21 | + s1s := StrToStrs4(utf8Str1, l1) |
| 22 | + s2s := StrToStrs4(utf8Str2, l2) |
| 23 | + // 计算每个字符在字符数组中出现的次数 |
| 24 | + counts1 := make(map[string]int) |
| 25 | + counts2 := make(map[string]int) |
| 26 | + for _, s := range s1s { |
| 27 | + // 如果字符在字符数组中出现过,则计数加1 |
| 28 | + if _, ok := counts1[s]; ok { |
| 29 | + counts1[s]++ |
| 30 | + } else { |
| 31 | + // 如果字符在字符数组中没出现过,则计数设为1 |
| 32 | + counts1[s] = 1 |
| 33 | + } |
| 34 | + } |
| 35 | + for _, s := range s2s { |
| 36 | + if _, ok := counts2[s]; ok { |
| 37 | + counts2[s]++ |
| 38 | + } else { |
| 39 | + counts2[s] = 1 |
| 40 | + } |
| 41 | + } |
| 42 | + h1 := IntsToStr(Dimensionality(merge(hashcodeAndAdd(counts1)))) |
| 43 | + h2 := IntsToStr(Dimensionality(merge(hashcodeAndAdd(counts2)))) |
| 44 | + |
| 45 | + // 计算h1, h2的汉明距离 |
| 46 | + Hamming := Hamming{} |
| 47 | + //fmt.Printf("h1: %s\nh2: %s\n", h1, h2) |
| 48 | + |
| 49 | + return Hamming.CompareUtf8(h1, h2) |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +// 降维度 |
| 54 | +func Dimensionality(ins []int) []int { |
| 55 | + for i := 0; i < len(ins); i++ { |
| 56 | + if ins[i] > 0 { |
| 57 | + ins[i] = 1 |
| 58 | + } else { |
| 59 | + ins[i] = 0 |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + return ins |
| 64 | +} |
| 65 | + |
| 66 | +//合并 |
| 67 | +func merge(ins [][]int) []int { |
| 68 | + res := make([]int, len(ins[0])) |
| 69 | + lens := len(ins) |
| 70 | + for i := 0; i < lens; i++ { |
| 71 | + for j := 0; j < len(ins[i]); j++ { |
| 72 | + res[j] += ins[i][j] |
| 73 | + } |
| 74 | + } |
| 75 | + return res |
| 76 | +} |
| 77 | + |
| 78 | +// 计算hashcode并加权 |
| 79 | +func hashcodeAndAdd(counts map[string]int) [][]int { |
| 80 | + // hashmap |
| 81 | + lens := len(counts) |
| 82 | + h1 := make([][]int, lens) |
| 83 | + // 计算counts1,counts2 中每个字符的hash值, 并且将出现的次数分为5个等级, 将每个字符的hash值与出现的次数等级相乘 |
| 84 | + c1 := (lens - 1) * 4.0 |
| 85 | + j := 0 |
| 86 | + //for j := 0; j < lens; j++ { |
| 87 | + for k, v := range counts { |
| 88 | + ////计算每一个字符串的hash |
| 89 | + //for i := 0; i < len(h1); i++ { |
| 90 | + // 出现的次数除以5 |
| 91 | + c := strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(k))), 2) |
| 92 | + // 将字符串转换为数字数组 |
| 93 | + cs := Int32StrToInts(c) |
| 94 | + if v <= c1/5.0 { |
| 95 | + // 加权 |
| 96 | + h1[j] = Add(cs, 1) |
| 97 | + } else if v <= c1/5.0*2 { |
| 98 | + // 加权 |
| 99 | + h1[j] = Add(cs, 2) |
| 100 | + } else if v <= c1/5.0*3 { |
| 101 | + // 加权 |
| 102 | + h1[j] = Add(cs, 3) |
| 103 | + } else if v <= c1/5.0*4 { |
| 104 | + // 加权 |
| 105 | + h1[j] = Add(cs, 4) |
| 106 | + } else { |
| 107 | + // 加权 |
| 108 | + h1[j] = Add(cs, 5) |
| 109 | + } |
| 110 | + j++ |
| 111 | + } |
| 112 | + |
| 113 | + return h1 |
| 114 | +} |
0 commit comments