Guest User

Untitled

a guest
Oct 15th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. require "pg"
  2. class JaccardCalculation
  3. def initialize(word_lenght, first_document_id, second_document_id)
  4. calculate(word_lenght, first_document_id, second_document_id)
  5. end
  6.  
  7. private
  8.  
  9. def connection
  10. @connection ||= PG.connect dbname: "SMDB", user: "postgres", password: "moskieva"
  11. end
  12.  
  13. def calculate(word_lenght, first_document_id, second_document_id)
  14. puts "Word length is #{word_lenght} \nFirst document id is #{first_document_id} \nSecond document id is #{second_document_id}"
  15. jaccard = connection.exec(query(word_lenght, first_document_id, second_document_id)).getvalue(0,0)
  16. puts "Jaccard value is #{jaccard}\n\n"
  17. end
  18.  
  19. def query(word_lenght, first_document_id, second_document_id)
  20. "select (
  21. select count(*)::real from(
  22. (select word from shingles where document_id = #{first_document_id} and word_lenght = #{word_lenght})
  23. intersect
  24. (select word from shingles where document_id = #{second_document_id} and word_lenght = #{word_lenght})
  25. ) as result )
  26. /
  27. (
  28. select (select count(*) from (
  29. (select word from shingles where document_id = #{first_document_id} and word_lenght = #{word_lenght})
  30. union
  31. (select word from shingles where document_id = #{second_document_id} and word_lenght = #{word_lenght})
  32. ) as result)
  33. )"
  34. end
  35. end
  36.  
  37. JaccardCalculation.new(2, 72, 73)
  38. JaccardCalculation.new(5, 74, 75)
  39. JaccardCalculation.new(9, 76, 77)
Add Comment
Please, Sign In to add comment