nodes: 0, module; 1, function_definition; 2, function_name:rank_items; 3, parameters; 4, block; 5, identifier:self; 6, identifier:userid; 7, identifier:user_items; 8, identifier:selected_items; 9, default_parameter; 10, expression_statement; 11, comment:# check if selected_items contains itemids that are not in the model(user_items); 12, if_statement; 13, comment:# calculate the relevance scores; 14, expression_statement; 15, expression_statement; 16, comment:# remove items that are not in the selected_items; 17, expression_statement; 18, expression_statement; 19, comment:# returned items should be equal to input selected items; 20, for_statement; 21, return_statement; 22, identifier:recalculate_user; 23, False; 24, comment:""" Rank given items for a user and returns sorted item list """; 25, boolean_operator; 26, block; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, identifier:itemid; 32, identifier:selected_items; 33, block; 34, identifier:ret; 35, comparison_operator:max(selected_items) >= user_items.shape[1]; 36, comparison_operator:min(selected_items) < 0; 37, raise_statement; 38, identifier:liked_vector; 39, subscript; 40, identifier:recommendations; 41, call; 42, identifier:best; 43, call; 44, identifier:ret; 45, list_comprehension; 46, if_statement; 47, call; 48, subscript; 49, call; 50, integer:0; 51, call; 52, identifier:user_items; 53, identifier:userid; 54, attribute; 55, argument_list; 56, identifier:sorted; 57, argument_list; 58, identifier:rec; 59, for_in_clause; 60, if_clause; 61, comparison_operator:itemid not in recommendations.indices; 62, block; 63, identifier:max; 64, argument_list; 65, attribute; 66, integer:1; 67, identifier:min; 68, argument_list; 69, identifier:IndexError; 70, argument_list; 71, identifier:liked_vector; 72, identifier:dot; 73, attribute; 74, call; 75, keyword_argument; 76, identifier:rec; 77, identifier:best; 78, comparison_operator:rec[0] in selected_items; 79, identifier:itemid; 80, attribute; 81, expression_statement; 82, identifier:selected_items; 83, identifier:user_items; 84, identifier:shape; 85, identifier:selected_items; 86, string:"Some of selected itemids are not in the model"; 87, identifier:self; 88, identifier:similarity; 89, identifier:zip; 90, argument_list; 91, identifier:key; 92, lambda; 93, subscript; 94, identifier:selected_items; 95, identifier:recommendations; 96, identifier:indices; 97, call; 98, attribute; 99, attribute; 100, lambda_parameters; 101, unary_operator; 102, identifier:rec; 103, integer:0; 104, attribute; 105, argument_list; 106, identifier:recommendations; 107, identifier:indices; 108, identifier:recommendations; 109, identifier:data; 110, identifier:x; 111, subscript; 112, identifier:ret; 113, identifier:append; 114, tuple; 115, identifier:x; 116, integer:1; 117, identifier:itemid; 118, unary_operator; 119, float:1.0
code: def rank_items(self, userid, user_items, selected_items, recalculate_user=False):
          """ Rank given items for a user and returns sorted item list """
          # check if selected_items contains itemids that are not in the model(user_items)
          if max(selected_items) >= user_items.shape[1] or min(selected_items) < 0:
              raise IndexError("Some of selected itemids are not in the model")
  
          # calculate the relevance scores
          liked_vector = user_items[userid]
          recommendations = liked_vector.dot(self.similarity)
  
          # remove items that are not in the selected_items
          best = sorted(zip(recommendations.indices, recommendations.data), key=lambda x: -x[1])
          ret = [rec for rec in best if rec[0] in selected_items]
  
          # returned items should be equal to input selected items
          for itemid in selected_items:
              if itemid not in recommendations.indices:
                  ret.append((itemid, -1.0))
          return ret