欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

以[SpringBoot房产平台]为例算法精进之路系列1

程序员文章站 2024-01-15 22:26:16
...

算法说明
目前来说面试岗位和做项目都需要一定的算法功底,我这里给大家演示基于文本的推荐算法的实现过程,帮助大家迅速掌握算法的开发思路。

1.分析采用算法的缘由没什么要在系统中加入算法?
系统原有功能如下
以[SpringBoot房产平台]为例算法精进之路系列1

需求分析结束以后产品汪给我提了以下需求
①根据用户点击量推荐最热房源,这个借助数据库房源表的点击量即可进行推荐,具体实现代码如下:

    public List<House> getHotHouse(Integer size) {
        House query = new House();
        List<Long> list = getHot();
        list = list.subList(0, Math.min(list.size(), size));
        if (list.isEmpty()) {
            return Lists.newArrayList();
        }
        query.setIds(list);
        final List<Long> order = list;
        List<House> houses = houseService.queryAndSetImg(query, PageParams.build(size, 1));
        Ordering<House> houseSort = Ordering.natural().onResultOf(hs -> {
            return order.indexOf(hs.getId());
        });
        return houseSort.sortedCopy(houses);
    }
    <select id="selectPageHouses" resultType="house">
        select
        <include refid="houseField"/>
        from house a
        <include refid="houseCondition"></include>
        <choose>
            <when test="house.sort != null and house.sort == 'price_asc'">
                order by a.price asc
            </when>
            <when test="house.sort != null and house.sort == 'price_desc'">
                order by a.price desc
            </when>
            <otherwise>
                order by a.create_time desc
            </otherwise>
        </choose>
        <if test="pageParams.offset !=null and pageParams.limit != null">
            limit #{pageParams.offset},#{pageParams.limit}
        </if>
        <if test="pageParams.offset == null and pageParams.limit != null">
            limit #{pageParams.limit}
        </if>
    </select>

这一块实现最后落地就是一条简单的sql,算法我认为难点没啥,下一块才是最难的,基于文本推荐

②增加了一个“我的偏爱设置”,这样用户随时都可以选择自己比较关注的房间类型。给用户推荐的算法是:满足越多的用户偏好,则推荐的排名越靠前,按照偏好的命中来进行排序 。

我的实现步骤如下
每个用户的偏好都是独立的,用户偏好设置如下图所示:
以[SpringBoot房产平台]为例算法精进之路系列1
推荐功能
因为推荐功能是要基于用户的喜好的,所以在登录后才能显示,未登录是无法看到这一板块的,只要用户登录,就可以看到推荐的模块显示在用户的主页上,用户推荐的原理上面已经解释了,具体的效果如下图所示:
以[SpringBoot房产平台]为例算法精进之路系列1
推荐房源默认显示四个,根据偏好-关键词匹配的多少来排序的,比如碧桂园这个楼盘,包含我们的全部3个偏好,所以它显示在第一个推荐,如果关键词的匹配数是一样的,则可能出现随机的排列。

实现的具体代码是基于文本推荐的,我这里的话展示一下,方便大家学习

    public List<House> getTuijians(User user) {
        House query = new House();
        query.setSort("create_time");
        List<House> houses = houseService.queryAndSetImg(query, new PageParams(30, 1));
        List<House> sortHouses = new ArrayList<>();
        Map<House,Integer> recommendIndex = new HashMap<>();
        Map<House,Integer> sortedIndex = new HashMap<>();
        for(House h : houses){
            if(point(user.getTags(),h.getProperties())!=0)
                recommendIndex.put(h,point(user.getTags(),h.getProperties()));
        }
        //排序
        List<Map.Entry<House, Integer>> list = new ArrayList(recommendIndex.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<House, Integer>>()
        {
            @Override
            public int compare(Map.Entry<House, Integer> o1, Map.Entry<House, Integer> o2)
            {
                //按照value值,重小到大排序
//                return o1.getValue() - o2.getValue();

                //按照value值,从大到小排序
                return o2.getValue() - o1.getValue();

                //按照value值,用compareTo()方法默认是从小到大排序
                //return o1.getValue().compareTo(o2.getValue());
            }
        });
        Iterator<Map.Entry<House, Integer>> iter = list.iterator();
        Map.Entry<House, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            //sortedIndex.put(tmpEntry.getKey(), tmpEntry.getValue());
            sortHouses.add(tmpEntry.getKey());
            if(sortHouses.size()>=4)
                break;
        }

        return sortHouses;
        //map转list
        //return new ArrayList<House>(sortedIndex.keySet());
        //return houses;
    }

这样看来算法实现的思路总结就是:明确需求算法,找现成算法实现的代码,搬砖改造成本系统需要的算法,然后CV进行改改就行了,是不是很简单?哈哈哈哈,下面介绍一下系统的所采用的的技术。

以[SpringBoot房产平台]为例算法精进之路系列1
2.加入算法后系统的效果演示
自我感觉有了算法的灵魂 系统的创新点倍增 甲方爸爸也很满意明我也提升了薪资 哈哈哈
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1
以[SpringBoot房产平台]为例算法精进之路系列1

3.总结

看来采用了算法系统确实能设计的比较完美 验证了 程序=数据结构+算法 这个原理,基于这个原理我们能在软件开发的路上越走越远,能够开发出更加健壮的系统,加油,安排,兄弟们!

房产平台 房地产 springboot mybatis ssm