Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. from math import sqrt
  2. from collections import Counter
  3.  
  4. def forSearch():
  5. words = {'bit':{1:3,2:4,3:19,4:0},'shoe':{1:0,2:0,3:0,4:0},'dog':{1:3,2:0,3:4,4:5}, 'red':{1:0,2:0,3:15,4:0}}
  6. search = {'bit':1,'dog':3,'shoe':5}
  7. num_files = 4 # TODO: , figure this out programatically
  8.  
  9. file_relevancy = Counter()
  10. c = sqrt(sum([x**2 for x in search.values()]))
  11. for i in range(1, num_files+1):
  12. words_ith_val = [words[x][i] for x in search.keys() ]
  13. a = sum([search[key] * words[key][i] for key in search.keys()])
  14. b = sqrt(sum([x**2 for x in words_ith_val]))
  15. file_relevancy[i] = (a / (b * c))
  16.  
  17. return [x[0] for x in file_relevancy.most_common(num_files)]
  18.  
  19. print forSearch()
  20.  
  21. for i in range(1, num_files+1):
  22. if corresponding key in words cannot be found
  23. insert it and make its value = 0
  24. words_ith_val = [words[x][i] for x in search.keys() ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement