03_基于话题类型的推荐

这个相对来说比较简单,核心代码如下:
大体的过程如下:

①获取话题的类型特征,格式如:topicid,categoryid

②相似度计算,采用欧式距离

在这里插入图片描述
然后相似度取1/(1+d12),也就是说距离越近的,相似度越趋近于1,话题类型接近。等于1则说明是同一个类型

   try {
      val deletePath1 = new Path(path)
      val fs1 = FileSystem.get(URI.create(path), hadoopConf)
      if (fs1.exists(deletePath1)) fs1.delete(deletePath1, true)
      println("删除成功")
    } catch {
      case e: ArithmeticException => println(e)
    }
    //获取话题类型特征
    val topicrdd1: RDD[Topic] = sparkContext.textFile("hdfs://hdp01:9000/recs/category/category_15079").map {
      line =>
        val fields = line.split("\\[")
        val strings = fields(1).split("\\]")
        val strings1 = strings(0).split(",")
        Topic(strings1(0).toInt, strings1(1).toDouble)
    }

    val datardd: RDD[(Topic, Topic)] = topicrdd1.cartesian(topicrdd1)
    val topicResc = datardd.filter { case (a, b) => a.id != b.id }
      .map {
        case (a, b) =>
          val score = this.ojilideSim(a.CategoryId, b.CategoryId)
          (a.id, (b.id, score))
      }
      .filter(_._2._2 > 0.5)
      .sortByKey()
      .map {
        case (topicId, recs) =>
       //topicId.toString + "\001" + recs._1
          (topicId,recs._1)
      }

    val resultRdd= topicResc.groupByKey().map {
      case (topicId, recs) =>
        val recsarr: Array[Int] = recs.toArray
        val recsResult: Array[Int] = new Array[Int](30)
        for (i <- 0 until 29) {
          val n = Random.nextInt(recsarr.length)
          recsResult(i) = recsarr(n)
        }
        var string=""
        for(i<- 0 until recsResult.length-1){
          if(i==0){
            string=recsResult(0).toString
          }else{
            string=string+","+recsResult(i).toString
          }
        }
        topicId+"\001"+string
    }
    resultRdd.saveAsTextFile(path)
    sparkContext.stop()

  }
//欧式距离相似度计算
  def ojilideSim(category1: Double, category2: Double): Double = {
    val fenmu = math.sqrt(math.pow((category1 - category2), 2))
    1 / (1 + fenmu)
  }

③在hive中得到用户的推荐列表并且过滤掉用户已经相关联到的话题
用户与话题相关表:
在这里插入图片描述
话题类型相似表:
在这里插入图片描述
为了方便关联,在hive中进行行列转换将话题类型相似表转换成如下格式

create table user_recs_categroy_tmp as 
select topicid,simtopic from category_result lateral view explode(simtopicid) tmp as  simtopic;

在这里插入图片描述
然后运用Hive的不等值连接,关联用户相关话题,再过滤掉用户已经关联过的话题

select a.userinfoid,simtopicid from 
(select userinfoid,simtopicid from user_recs_categroy_tmp1)a
left join 
(select userinfoid,topicid from recs_userRalationTopic)b
on
a.simtopicid=b.topicid and a.userinfoid=b.userinfoid where b.topicid is null;

最后在上述表中随机抽取5个做为推荐结果

select userinfoid,concat_ws(',',collect_set(simtopicid)) simtopics 
from
(select a1.userinfoid,a1.simtopicid
from (select a1.userinfoid,a1.simtopicid,row_number()over(partition by a1.userinfoid order by rand()) rn
from user_recs_categroy a1) a1
where rn<=5)a2 
group by userinfoid;

在这里插入图片描述
④在这里,随机抽取一个看一下效果
用户162194关联的话题有:
在这里插入图片描述
在这里插入图片描述
可以发现,用户关联的话题中17349的类型标签为6
给其推荐的话题有18040,18289等
在这里插入图片描述
可见,召回率还是相当准确的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于Django的电影推荐系统和论坛可以帮助用户更好地发现喜欢的电影以及与其他用户交流和讨论。该系统可以根据用户的个人喜好、观影历史和评价等信息,为用户推荐符合其口味的电影。 在电影推荐方面,系统可以根据用户的浏览记录和评分,采用协同过滤或内容过滤算法来推荐相似类型或相似口味的电影。用户可以根据推荐结果进行电影评分和评论,系统会根据用户的反馈不断优化推荐的准确性。此外,用户还可以通过搜索功能浏览不同类型的电影,并按照不同条件进行筛选和排序。 在论坛方面,用户可以注册账号并登录到系统,然后参与不同的讨论话题。系统会提供不同的板块或分类,如影评、电影讨论、演员讨论等,以便用户可以根据自己的兴趣选择参与讨论的内容。此外,用户还可以发布自己的影评、观影心得或提供电影推荐给其他用户,促进用户之间的交流和分享。 为了确保系统的稳定性和安全性,我们可以使用Django提供的认证和权限系统来管理用户的访问和操作。系统管理员可以对用户的发布内容进行审核和管理,防止不当内容的出现。此外,我们还可以通过集成第三方登录服务(如Google或Facebook)来提供更多的注册和登录方式,增加系统的便利性和用户体验。 综上所述,基于Django的电影推荐系统和论坛可以为用户提供个性化的电影推荐和与其他用户的交流平台,帮助用户发现喜欢的电影并分享观影体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值