这是我用来寻找推荐系统的算法
要获取 current_user
的以下内容:current_user.followings
要获取 current_user
的关注者:current_user.followers
- S = current_user.followings
- L = (s in S) UNION(s.followings)
- T = (l in L) UNION(l.followings)
- R = T-S
算法在This Paper ,第 5 页。
为了从 R 中推荐一个用户而不是另一个用户,我使用以下公式评估 R 中的每个元素:
score(person) = (occurences(person)/R.count) * (followers(person)/followees(person) * retweets(person)/tweets(person).count)
得分越接近 1,用户就越有可能对该人感兴趣。
我在算法的第一部分遇到问题:在 R(occurrences(person)) 中命名一个人出现的计数。我有以下代码:
def candidates(user)
@following = user.following # the persons the user follows or S
@follower = [] #defining an empty array to put L in
@following.each do |follow|
@follower = @follower + follow.followers #populating the array
end
@followees = [] #defining an array to put T in
@follower.each do |ff|
@followees = @followees + ff.following #populating the array
end
@followees = @followees - @following #getting rid of the persons that the user is already following so T - S which gives us R
@rezultat = []
@sugested = @followees & @followees #removing the duplicates
@sugested.each do |gg| #for each user that he might want to follow
nr = 0
@followees.each do |ff|
if (ff.email == gg.email) then nr = nr + 1 #find out how many times a user makes an appearance in the reunion of the intervals
end
end
if(gg.following.count != 0) then
score = ( nr/@followees.count() ) * ( gg.followers.count / gg.following.count) #calculating score without taking into consideration retweets yet
else score = 0
end
end
end
end
现在我必须得到分数。我面临的问题是计算 R 中同一对象的出现次数。该对象是具有以下字段的用户模型对象:
- 1。编号
- 2。姓名
- 3。电邮
我是怎么想到数它们的,但我不确定它是否有效(而且我不喜欢它)。每次我偶然发现该电子邮件时(因为电子邮件是唯一的),用我想计算出现次数的当前人的电子邮件解析整个数组,并在数字上加 1。还有其他想法吗?
此外,我应该如何保持关系 Person-Score 以便在填充它之后对分数进行轻松排序,这样我就可以让 Person 对象显示它们 :D?。
感谢任何提示或代码!
最佳答案
只是多一点 ruby 的方式:
@following = user.following
@follower = @following.collect { |following| following.followers }
@followees = @follower.collect { |follower| follower.following }
@followees = @followees - @following
@score = {}
@followees.uniq.map { |suggested| @score[suggested] = @followees.count(suggested)}
@score.select! { |user,count| count>0 && user.following.count>0 }
@score.each do |sguser,count|
@score[sguser] =
(count/sguser.followees.count) *
(sguser.followers.count / sguser.following.count)
end
结果你会得到哈希@score { suggested_user: score_value } ,你可以根据需要对其进行排序。
如果您的数据足够大,您可以将更多数据移至 SQL 领域(JOIN、GROUP),从而减少数组的大小和数量。或者甚至可以在数据库中直接执行此操作,而根本不会为您的应用程序获取任何值(据我所知,这是可能的)。
关于ruby-on-rails - 用于在 Ruby 中实现类似 twitter 的 Web 应用程序的推荐系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602311/