parent
9fed807881
commit
01678fc7da
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 240 KiB |
@ -0,0 +1,66 @@
|
||||
from django.db import models
|
||||
|
||||
class Movie(models.Model):
|
||||
title = models.CharField(max_length=200)
|
||||
director = models.CharField(max_length=100)
|
||||
actors = models.CharField(max_length=500)
|
||||
genre = models.CharField(max_length=100)
|
||||
# 添加其他特征字段
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
from nltk.tokenize import word_tokenize
|
||||
from nltk.corpus import stopwords
|
||||
|
||||
def extract_features(movie):
|
||||
# 从电影对象中提取特征信息
|
||||
title = movie.title
|
||||
director = movie.director
|
||||
actors = movie.actors
|
||||
genre = movie.genre
|
||||
|
||||
# 在这里,您可以执行文本处理,如分词、去停用词等
|
||||
tokens = word_tokenize(title.lower() + " " + director.lower() + " " + actors.lower() + " " + genre.lower())
|
||||
features = [word for word in tokens if word not in stopwords.words('english')]
|
||||
|
||||
return features
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import math
|
||||
|
||||
def cosine_similarity(features1, features2):
|
||||
# 计算余弦相似度
|
||||
dot_product = sum(features1.count(word) * features2.count(word) for word in set(features1) & set(features2)
|
||||
magnitude1 = math.sqrt(sum(features1.count(word) ** 2 for word in features1))
|
||||
magnitude2 = math.sqrt(sum(features2.count(word) ** 2 for word in features2))
|
||||
|
||||
if magnitude1 == 0 or magnitude2 == 0:
|
||||
return 0 # 避免零除错误
|
||||
|
||||
return dot_product / (magnitude1 * magnitude2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def content_based_recommendation(user, item, num_recommendations=5):
|
||||
item_features = extract_features(item)
|
||||
|
||||
# 获取所有其他物品
|
||||
all_items = Movie.objects.exclude(id=item.id)
|
||||
|
||||
# 计算每个物品与目标物品的相似度
|
||||
similarities = [(other_item, cosine_similarity(item_features, extract_features(other_item))) for other_item in all_items]
|
||||
|
||||
# 根据相似度降序排序
|
||||
similarities.sort(key=lambda x: x[1], reverse=True)
|
||||
|
||||
# 选择相似度最高的物品作为推荐
|
||||
recommended_items = [item for item, similarity in similarities[:num_recommendations]]
|
||||
|
||||
return recommended_items
|
Loading…
Reference in new issue