標籤:Lucene style blog http color java os io
抓取的網頁內容中,有大部分會是相似的,抓取時就要過濾掉,開始考慮用VSM演算法,後來發現不對,要比較太多東西了,然後就發現了simHash演算法,這個演算法的解釋我就懶得copy了,simhash演算法對於短資料的支援不好,但是,我本來就是很長的資料,用上!
源碼實現網上也有不少,但是貌似都是同樣的,裡面寫得不清不楚的,雖然效果基本能達到,但是不清楚的東西,我用來做啥?
仔細研究simhash演算法的說明後,把裡面字串的hash演算法換成的fvn-1演算法,這個在http://www.isthe.com/chongo/tech/comp/fnv/裡面有說明了,具體的那些固定數值,網站上都寫了。原先代碼裡面有些處理,和演算法不符的,也換掉了。
首先搞起IKAnalyzer,切詞並計算每個詞的頻率:
package com.cnblogs.zxub.lucene.similarity;import java.io.IOException;import java.io.Reader;import java.io.StringReader;import java.util.HashMap;import java.util.Map;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.wltea.analyzer.lucene.IKAnalyzer;public class WordsSpliter { public static Map<String, Integer> getSplitedWords(String str) throws IOException { // str = str.replaceAll("[0-9a-zA-Z]", ""); Analyzer analyzer = new IKAnalyzer(); Reader r = new StringReader(str); TokenStream ts = analyzer.tokenStream("searchValue", r); ts.addAttribute(CharTermAttribute.class); Map<String, Integer> result = new HashMap<String, Integer>(); while (ts.incrementToken()) { CharTermAttribute ta = ts.getAttribute(CharTermAttribute.class); String word = ta.toString(); if (!result.containsKey(word)) { result.put(word, 0); } result.put(word, result.get(word) + 1); } return result; }}
然後把SimHash的演算法搞上:
package com.cnblogs.zxub.lucene.similarity;import java.io.IOException;import java.math.BigInteger;import java.util.Map;import java.util.Set;public class SimHash { private static final int HASH_BITS = 64; private static final BigInteger FNV_64_INIT = new BigInteger( "14695981039346656037"); private static final BigInteger FNV_64_PRIME = new BigInteger( "1099511628211"); private static final BigInteger MASK_64 = BigInteger.ONE.shiftLeft( HASH_BITS).subtract(BigInteger.ONE); private String hash; private BigInteger signature; public SimHash(String content) throws IOException { super(); this.setFingerPrint(WordsSpliter.getSplitedWords(content)); } public String getHash() { return this.hash; } public BigInteger getSignature() { return this.signature; } private void setFingerPrint(Map<String, Integer> wordInfos) { int[] featureVector = new int[SimHash.HASH_BITS]; Set<String> words = wordInfos.keySet(); for (String word : words) { BigInteger wordhash = this.fnv1_64_hash(word); for (int i = 0; i < SimHash.HASH_BITS; i++) { BigInteger bitmask = BigInteger.ONE.shiftLeft(SimHash.HASH_BITS - i - 1); if (wordhash.and(bitmask).signum() != 0) { featureVector[i] += wordInfos.get(word); } else { featureVector[i] -= wordInfos.get(word); } } } BigInteger signature = BigInteger.ZERO; StringBuffer hashBuffer = new StringBuffer(); for (int i = 0; i < SimHash.HASH_BITS; i++) { if (featureVector[i] >= 0) { signature = signature.add(BigInteger.ONE .shiftLeft(SimHash.HASH_BITS - i - 1)); hashBuffer.append("1"); } else { hashBuffer.append("0"); } } this.hash = hashBuffer.toString(); this.signature = signature; } // fnv-1 hash演算法,將字串轉換為64位hash值 private BigInteger fnv1_64_hash(String str) { BigInteger hash = FNV_64_INIT; int len = str.length(); for (int i = 0; i < len; i++) { hash = hash.multiply(FNV_64_PRIME); hash = hash.xor(BigInteger.valueOf(str.charAt(i))); } hash = hash.and(MASK_64); return hash; } public int getHammingDistance(BigInteger targetSignature) { BigInteger x = this.getSignature().xor(targetSignature); String s = x.toString(2); return s.replaceAll("0", "").length(); } public int getHashDistance(String targetHash) { int distance; if (this.getHash().length() != targetHash.length()) { distance = -1; } else { distance = 0; for (int i = 0; i < this.getHash().length(); i++) { if (this.getHash().charAt(i) != targetHash.charAt(i)) { distance++; } } } return distance; }}
資料庫裡面存個簽名就好了,至於距離運算,本打算全部拉出來計算,後來發現oracle的bitand函數,就用它了!異或之後,轉二進位字串,把0去掉,取長度,再count一下長度小於4的,得到的結果就是很相似的內容數目了。以後再把計算改成用緩衝的去,先偷個懶。
收攤!