fork download
  1. #!/usr/bin/ruby -w
  2.  
  3.  
  4. class Page
  5. @@count = 0
  6. attr_reader :id
  7. attr_accessor :keywords
  8.  
  9. def initialize()
  10. @id = @@count + 1
  11. @keywords = Hash.new
  12. @@count += 1
  13. end
  14. end
  15.  
  16.  
  17. class Query
  18. @@count = 0
  19. attr_reader :id
  20. attr_accessor :keywords
  21.  
  22. def initialize()
  23. @id = @@count + 1
  24. @keywords = Hash.new
  25. @@count += 1
  26. end
  27. end
  28.  
  29.  
  30. class PageStrength
  31. attr_reader :pageid
  32. attr_accessor :strength
  33. def initialize(id, strength)
  34. @pageid = id
  35. @strength = strength
  36. end
  37. end
  38.  
  39.  
  40. class SearchRanking
  41. # pages, to contain all page object
  42. # queries, to contain all query objects
  43. @@pages = []
  44. @@queries = []
  45.  
  46. def parse
  47. # parses the standard input (file in this case)
  48. # and builds array of Page and Query objects
  49. begin
  50. file = File.new("input", "r")
  51. while (line = file.gets)
  52. if line.start_with?('P')
  53. keywords = line.split()
  54. # create Page object
  55. page = Page.new()
  56. keywords.delete(keywords[0])
  57. keyword_index_pair = keywords.zip((0...8))
  58. keyword_index_pair.each do |keyword, index|
  59. page.keywords[keyword.downcase] = 8-index
  60. end
  61. @@pages.push(page)
  62. elsif line.start_with?('Q')
  63. keywords = line.split()
  64. # create Query object
  65. query = Query.new()
  66. keywords.delete(keywords[0])
  67. keyword_index_pair = keywords.zip((0...8))
  68. keyword_index_pair.each do |keyword, index|
  69. query.keywords[keyword.downcase] = 8 - index
  70. end
  71. @@queries.push(query)
  72. end
  73. end
  74. file.close
  75. rescue => err
  76. puts "Exception: #{err}"
  77. err
  78. end
  79. end
  80.  
  81. # def rank_pages:
  82. # # calculates the strength of each page against each query and
  83. # # displays the ranking of pages based on decreasing order of strength
  84. # @@queries.each do |query|
  85. # page_strengths = []
  86. # @@pages.each do |page|
  87. # strength = 0
  88. # query.keywords.each do |qkey|
  89. # if page.keywords.has_key?(qkey)
  90. # strength = strength + query.keywords[qkey]*page.keywords[qkey]
  91. # end
  92. # end
  93. # pstrength = PageStrength.new(page.id, strength)
  94. # page_strengths.push(pstrength)
  95. # end
  96. # # ranking pages: sorting in descending order based on strength value.
  97. # page_strengths.sort(reverse=True, key=lambda page_strengths: page_strengths.strength)
  98. # # work on the above ===========******================
  99.  
  100. # end
  101. # end
  102. end
  103.  
  104. sr = SearchRanking.new()
  105. sr.parse()
Success #stdin #stdout #stderr 0.02s 7468KB
stdin
Standard input is empty
stdout
Exception: No such file or directory - input
stderr
prog.rb:72: warning: mismatched indentations at 'end' with 'if' at 52