コミットを比較

...

3 コミット

作成者 SHA1 メッセージ 日付
みてるぞ 067066910a Merge branch 'main' into #92 2026-01-07 03:24:44 +09:00
みてるぞ a56db7f293 Merge remote-tracking branch 'origin/main' into #92 2025-12-30 13:08:04 +09:00
みてるぞ 138550bf02 #92 2025-08-02 01:51:33 +09:00
2個のファイルの変更32行の追加0行の削除
+4
ファイルの表示
@@ -0,0 +1,4 @@
class TagSimilarity < ApplicationRecord
belongs_to :tag, class_name: 'Tag', foreign_key: 'tag_id'
belongs_to :target_tag, class_name: 'Tag', foreign_key: 'target_tag_id'
end
+28
ファイルの表示
@@ -0,0 +1,28 @@
namespace :tag_similarity do
desc '関聯タグ・テーブル作成'
task calc: :environment do
dot = -> a, b { (a.keys & b.keys).sum { |k| a[k] * b[k] } }
norm = -> v { Math.sqrt(v.values.sum { |e| e * e }) }
cos = -> a, b do
na = norm.(a)
nb = norm.(b)
if na.zero? || nb.zero?
0.0
else
dot.(a, b) / na / nb
end
end
tags = Tag.includes(:posts).to_a
tags.each_with_index do |tag, i|
existence_of_posts = tag.posts.index_with(1)
((i + 1)...tags.size).each do |j|
target_tag = tags[j]
existence_of_target_posts = target_tag.posts.index_with(1)
TagSimilarity.find_or_initialize_by(tag:, target_tag:).tap { |ts|
ts.cos = cos.(existence_of_posts, existence_of_target_posts)
}.save!
end
end
end
end