Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. def insert_hash_compare():
  2.  
  3. # input value
  4. value_mongo1 = request.form['id1']
  5. value_mongo2 = request.form['id2']
  6.  
  7. # collection mongo
  8. online_news_hash = mongo_insert.db.online_news_hash
  9.  
  10. # get hash1 & hash2
  11. compare_hash1 = online_news_hash.find_one({'_id' : value_mongo1})
  12. compare_hash2 = online_news_hash.find_one({'_id' : value_mongo2})
  13.  
  14. hash1 = compare_hash1['hash']
  15. hash2 = compare_hash2['hash']
  16.  
  17. dec1 = int(hash1, 16);
  18. dec2 = int(hash2, 16);
  19. bin1 = bin(dec1)
  20. bin2 = bin(dec2)
  21. xor_bin = int(bin1, 2) ^ int(bin2, 2)
  22. distance = bin(xor_bin).count("1")
  23. divider = len(hash1) * 4
  24.  
  25. result = 1 - (distance / float(divider))
  26.  
  27. hash_accuracy = {
  28. 'accuracy': float(result)
  29. }
  30.  
  31. return json.dumps(hash_accuracy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement