Created
March 29, 2010 15:53
-
-
Save ultraspeed/347994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
This is my current matching algorithm. It's very database-heavy - with this number of records... | |
>> User.count | |
=> 25 | |
>> Kink.count | |
=> 76 | |
>> Rating.count | |
=> 1900 | |
...it takes about 3000ms to return on my Mac. This is a bit ugly, and it'll only get worse with more users, kinks and the ratings that'll then be produced. | |
HALP. | |
I can be contacted at [email protected] (Jabber and MSN are also available at this address). | |
=end | |
def top | |
matches = {} # Initialise empty Hash | |
my_ratings = @current_user.ratings # Load this user's ratings into my_ratings as my_rating | |
User.all.each do |iterated_user| # Iterate through all users as iterated_user | |
kinks_in_common = 0 # # Initialise number of common interests to 0 for this user | |
iterated_user_diff_factor = 0 # # Initialise difference factor to 0 for this user | |
my_ratings.each do |iterated_my_rating| # # Iterate through all of my ratings | |
if (their_rating = Rating.find_by_user_id_and_kink_id(iterated_user.id, iterated_my_rating.kink_id)) # # # If they have a rating for the interest that my_rating is about... | |
kinks_in_common += 1 # # # # Add 1 to the common interest tally | |
iterated_user_diff_factor += (their_rating.rating_as_top - iterated_my_rating.rating_as_bottom).abs # # # # Add any difference in rating to the difference factor one way round | |
iterated_user_diff_factor += (their_rating.rating_as_bottom - iterated_my_rating.rating_as_top).abs # # # # Add any difference in rating to the difference factor the other way round | |
end # # # End If | |
end # # Next of my ratings, or stop iterating through my ratings | |
if kinks_in_common == 0 # # # If the two users have nothing in common... | |
iterated_user_match_percentage = 0 # # # # We can't calculate any match, so set the match percentage to 0 | |
else # # # Else (if the users have at least one interest in common) | |
iterated_user_match_factor = (kinks_in_common * 6) - iterated_user_diff_factor # # # # Take the difference between the maximum possible difference factor (ratings are integers between 0 and 6 inclusive) and the actual difference factor to get the match factor | |
iterated_user_match_percentage = ((iterated_user_match_factor.to_f / (kinks_in_common * 6)) * 100).to_i # # # # Convert the match factor to a percentage match (integer 0-100 inclusive) | |
end # # # End If | |
matches[iterated_user.id] = iterated_user_match_percentage # # Push the user ID and the match percentage integer into the matches Hash | |
end # End iterating through all users | |
matches.delete(@current_user.id) # Remove my user ID from the matches Hash | |
@topmatches = matches # Pop the matches Hash into the class variable that the view will use | |
# @topmatches returns a Hash of uid => match_percentage. # Remind myself what the hell I've just done | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment