算法說明
目前來說面試崗位和做項目都需要一定的算法功底,我這裏給大家演示基於文本的推薦算法的實現過程,幫助大家迅速掌握算法的開發思路。
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,算法我認爲難點沒啥,下一塊纔是最難的,基於文本推薦
②增加了一個“我的偏愛設置”,這樣用戶隨時都可以選擇自己比較關注的房間類型。給用戶推薦的算法是:滿足越多的用戶偏好,則推薦的排名越靠前,按照偏好的命中來進行排序 。
我的實現步驟如下
每個用戶的偏好都是獨立的,用戶偏好設置如下圖所示:
推薦功能
因爲推薦功能是要基於用戶的喜好的,所以在登錄後才能顯示,未登錄是無法看到這一板塊的,只要用戶登錄,就可以看到推薦的模塊顯示在用戶的主頁上,用戶推薦的原理上面已經解釋了,具體的效果如下圖所示:
推薦房源默認顯示四個,根據偏好-關鍵詞匹配的多少來排序的,比如碧桂園這個樓盤,包含我們的全部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進行改改就行了,是不是很簡單?哈哈哈哈,下面介紹一下系統的所採用的的技術。
2.加入算法後系統的效果演示
自我感覺有了算法的靈魂 系統的創新點倍增 甲方爸爸也很滿意明我也提升了薪資 哈哈哈
3.總結
看來採用了算法系統確實能設計的比較完美 驗證了 程序=數據結構+算法 這個原理,基於這個原理我們能在軟件開發的路上越走越遠,能夠開發出更加健壯的系統,加油,安排,兄弟們!
房產平臺 房地產 springboot mybatis ssm