-
-
Save benmcredmond/0dec520b6ab2ce7c59d5 to your computer and use it in GitHub Desktop.
This file contains 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
@cluster_centers = [rand_point(), rand_point()] | |
15.times do | |
@clusters = [[], []] | |
@posts.each do |post| | |
min_distance, min_point = nil, nil | |
@cluster_centers.each.with_index do |center, i| | |
if distance(center, post) < min_distance | |
min_distance = distance(center, post) | |
min_point = i | |
end | |
end | |
@clusters[min_point] << post | |
end | |
@cluster_centers = @clusters.map do |post| | |
average(posts) | |
end | |
end |
This file contains 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
def similar_posts(post) | |
title_keywords = post.title.split(' ') | |
Post.all.to_a.sort |post1, post2| | |
post1_title_intersection = post1.body.split(' ') & title_keywords | |
post2_title_intersection = post2.body.split(' ') & title_keywords | |
post2_title_intersection.length <=> post1_title_intersection.length | |
end[0..9] | |
end |
This file contains 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
@posts = Post.all | |
@words = @posts.map do |p| | |
p.body.split(' ') | |
end.flatten.uniq | |
@vectors = @posts.map do |p| | |
@words.map do |w| | |
p.body.include?(w) ? 1 : 0 | |
end | |
end |
What is rand_point().
The definition of the method is missing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I bet you will get error "ArgumentError: comparison of Fixnum with nil failed" on this line https://gist.github.com/benmcredmond/0dec520b6ab2ce7c59d5#file-kmeans-rb-L10