-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
public static BigInteger simHash(Map<String,Integer> terms) throws IOException {
// 定义特征向量/数组
int[] v = new int[hashbits];
//遍历每一个单词
for(Entry<String, Integer> entry :terms.entrySet()){
String key = entry.getKey();
//将每一个分词hash为一组固定长度的数列.比如 64bit 的一个整数.
BigInteger t = hash(key);
for (int i = 0; i < hashbits; i++) {
// 建立一个长度为64的整数数组(假设要生成64位的数字指纹,也可以是其它数字),
// 对每一个分词hash后的数列进行判断,如果是1000...1,那么数组的第一位和末尾一位加1,
// 中间的62位减一,也就是说,逢1加1,逢0减1.一直到把所有的分词hash数列全部判断完毕.
// 可以理解成没个词的权重都为1
BigInteger bitmask = new BigInteger("1").shiftLeft(i);
// signum 符号位,负数是为-1,零时为0,正数是为1
// 这里是应该计算整个文档的所有特征的向量和
//TODO 这里实际使用中需要 +- 权重,比如词频,而不是简单的 +1/-1,
Integer termFreq = entry.getValue();
if (t.and(bitmask).signum() != 0) {
v[i] += entry.getValue();
} else {
v[i] -= entry.getValue();
}
}
}
BigInteger fingerprint = new BigInteger("0");
for (int i = 0; i < hashbits; i++) {
if (v[i] >= 0) {
fingerprint = fingerprint.add(new BigInteger("1").shiftLeft(i));
}
}
return fingerprint;
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels