誠意租房網blog2

 (一)數據庫設計部分:java

建立數據庫hourse:算法

1:關注表數據庫

CREATE TABLE `guanzhu` (api

  `id` int(11) NOT NULL AUTO_INCREMENT,數據庫設計

  `uid` int(11) DEFAULT NULL,ui

  `hid` int(11) DEFAULT NULL,spa

  `time` datetime DEFAULT NULL,設計

  PRIMARY KEY (`id`)圖片

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;文檔

2:歷史表

CREATE TABLE `history` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `uid` int(11) DEFAULT NULL,

  `uname` varchar(65) DEFAULT NULL,

  `createtime` datetime DEFAULT NULL,

  `hid` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=133 DEFAULT CHARSET=utf8;

3:房屋信息表

CREATE TABLE `hourse` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `uname` varchar(255) DEFAULT NULL COMMENT '房東名字',

  `uid` int(11) DEFAULT NULL,

  `image` varchar(255) DEFAULT NULL COMMENT '主視圖',

  `image1` varchar(255) DEFAULT NULL,

  `image2` varchar(255) DEFAULT NULL,

  `area` int(11) DEFAULT NULL COMMENT '面積',

  `address` varchar(255) DEFAULT NULL COMMENT '地址',

  `rent_type` int(11) DEFAULT NULL COMMENT '0 日租 1月租',

  `day_price` double DEFAULT NULL COMMENT '日租金',

  `month_price` double DEFAULT NULL COMMENT '月租金',

  `hourse_type` int(66) unsigned DEFAULT NULL COMMENT '1一室一廳2兩室一廳3三室一廳',

  `state` int(11) DEFAULT '0' COMMENT '0可申請 1已申請 2已審覈 3已入組 4已退房',

  `fabu_state` int(11) DEFAULT '0' COMMENT '0 未審覈 本身房屋審覈狀況',

  `name` varchar(64) DEFAULT NULL COMMENT '名字',

  `xiaoqv` varchar(64) DEFAULT NULL COMMENT '歸屬小區',

  `introduce` varchar(4000) DEFAULT NULL COMMENT '房子介紹',

  `xqintrodece` varchar(4000) DEFAULT NULL COMMENT '小區介紹',

  `createtime` datetime DEFAULT NULL COMMENT '發佈時間',

  `tel` varchar(64) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

4:訂單房屋表

CREATE TABLE `hourse_order` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `hid` int(11) DEFAULT NULL COMMENT '房子ID',

  `in_time` datetime DEFAULT NULL COMMENT '入住時間',

  `pay_type` int(11) DEFAULT NULL COMMENT '支付方式',

  `out_time` datetime DEFAULT NULL COMMENT '退房時間',

  `price` double DEFAULT NULL COMMENT '總價',

  `state` int(11) DEFAULT NULL COMMENT '申請狀態',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

5:留言實體表

CREATE TABLE `message` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `content` varchar(2000) DEFAULT NULL COMMENT '留言內容',

  `uid` int(11) DEFAULT NULL COMMENT '留言人',

  `hid` int(11) DEFAULT NULL COMMENT '被留言房屋',

  `rank` int(11) DEFAULT NULL COMMENT '評分',

  `time` datetime DEFAULT NULL COMMENT '評論時間',

  `uname` varchar(64) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

6:CREATE TABLE `my_hource` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `hid` int(11) DEFAULT NULL,

  `time` datetime DEFAULT NULL,

  `uid` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

7:申請表

CREATE TABLE `shenqing` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `uid` int(11) DEFAULT NULL COMMENT '申請人id',

  `hid` int(11) DEFAULT NULL COMMENT '房子ID',

  `time` datetime DEFAULT NULL COMMENT '申請時間',

  `uname` varchar(64) DEFAULT NULL COMMENT '房東姓名',

  `state` int(11) DEFAULT NULL COMMENT '申請狀態',

  `intime` varchar(64) DEFAULT NULL COMMENT '入住時間',

  `renttype` int(11) DEFAULT NULL COMMENT '租住類型',

  `price` double DEFAULT NULL COMMENT '價格',

  `hname` varchar(64) DEFAULT NULL COMMENT '房子姓名',

  `huid` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

8:用戶表

CREATE TABLE `user` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `username` varchar(255) DEFAULT NULL,

  `password` varchar(255) DEFAULT NULL,

  `realname` varchar(255) DEFAULT NULL,

  `role` varchar(32) DEFAULT 's' COMMENT 'admin guest',

  `image` varchar(64) DEFAULT NULL COMMENT 'u圖片',

  `address` varchar(255) DEFAULT NULL COMMENT '收貨地址',

  `phone` varchar(64) DEFAULT NULL,

  `idnumber` varchar(65) DEFAULT NULL COMMENT '身份證號',

  `tel` varchar(64) DEFAULT NULL,

  `sex` int(11) DEFAULT '0',

  `pay` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

(二)典型代碼展現部分:(代碼不知道貼什麼,因此詳細設計與系統實現仍是見文檔吧)

import java.util.*;

import java.util.Map.Entry;

/**

 * 基於用戶的協同過濾推薦算法實現

 * @author Administrator

 */

public class UserCF {

       /**

        *

        * @param userList 用戶喜愛集合

        * @param recommendUser 須要的用戶

        * @return

        */

       public List<Integer> recoment(List<RecommentUser> userList,Integer recommendUser){

              List<Sorf> sorfList = new ArrayList<>();

              int[][] sparseMatrix = new int[userList.size()][userList.size()];//創建用戶稀疏矩陣,用於用戶類似度計算【類似度矩陣】

              Map<Integer, Integer> userItemLength = new HashMap<>();//存儲每個用戶對應的不一樣物品總數  eg: A 3

              Map<Integer, Set<Integer>> itemUserCollection = new HashMap<>();//創建物品到用戶的倒排表 eg: a A B

              Set<Integer> items = new HashSet<>();//輔助存儲物品集合

              Map<Integer, Integer> userID = new HashMap<>();//輔助存儲每個用戶的用戶ID映射

              Map<Integer, Integer> idUser = new HashMap<>();//輔助存儲每個ID對應的用戶映射

              System.out.println("Input user--items maping infermation:<eg:A a b d>");

 

              for(int i = 0; i < userList.size() ; i++){//依次處理N個用戶 輸入數據  以空格間隔

                     RecommentUser user = userList.get(i);

                     int uid = userList.get(i).getId();

                     int size = userList.get(i).getLikeList().size();

                     userItemLength.put(uid, size);//eg: A 3

                     userID.put(uid, i);//用戶ID與稀疏矩陣創建對應關係

                     idUser.put(i, uid);

                     //創建物品--用戶倒排表

                     for(int j = 0; j < size; j ++){

                            if(items.contains(user.getLikeList().get(j))){//若是已經包含對應的物品--用戶映射,直接添加對應的用戶

                                   itemUserCollection.get(user.getLikeList().get(j)).add(uid);

                            }else{//不然建立對應物品--用戶集合映射

                                   items.add(user.getLikeList().get(j));

                                   itemUserCollection.put(user.getLikeList().get(j), new HashSet<Integer>());//建立物品--用戶倒排關係

                                   itemUserCollection.get(user.getLikeList().get(j)).add(uid);

                            }

                     }

              }

 

 

              System.out.println(itemUserCollection.toString());

              //計算類似度矩陣【稀疏】

              Set<Entry<Integer, Set<Integer>>> entrySet = itemUserCollection.entrySet();

              Iterator<Entry<Integer, Set<Integer>>> iterator = entrySet.iterator();

              while(iterator.hasNext()){

                     Set<Integer> commonUsers = iterator.next().getValue();

                     for (Integer user_u : commonUsers) {

                            for (Integer user_v : commonUsers) {

                                   if(user_u.equals(user_v)){

                                          continue;

                                   }

                                   sparseMatrix[userID.get(user_u)][userID.get(user_v)] += 1;//計算用戶u與用戶v都有正反饋的物品總數

                            }

                     }

              }

 

              for (int i = 0; i < userList.size(); i++) {

                     System.out.println("用戶" + userList.get(i).getId() + ":" + userList.get(i).getLikeList().toString());

              }

              //計算用戶之間的類似度【餘弦類似性】

              int recommendUserId = userID.get(recommendUser);

              for (int j = 0;j < sparseMatrix.length; j++) {

                     if(j != recommendUserId){

                            System.out.println(idUser.get(recommendUserId)+"--"+idUser.get(j)+"類似度:"+sparseMatrix[recommendUserId][j]/Math.sqrt(userItemLength.get(idUser.get(recommendUserId))*userItemLength.get(idUser.get(j))));

                     }

              }

 

              //計算指定用戶recommendUser的物品推薦度

              for(Integer item: items){//遍歷每一件物品

                     Set<Integer> users = itemUserCollection.get(item);//獲得購買當前物品的全部用戶集合

                     if(!users.contains(recommendUser)){//若是被推薦用戶沒有購買當前物品

                            // 則進行推薦度計算

                            double itemRecommendDegree = 0.0;

                            for(Integer user: users){

                                   itemRecommendDegree += sparseMatrix[userID.get(recommendUser)][userID.get(user)]/Math.sqrt(userItemLength.get(recommendUser)*userItemLength.get(user));//推薦度計算

                            }

                            System.out.println("商品"+item+" 對用戶 "+recommendUser +"'推薦度:"+itemRecommendDegree);

                            Sorf sorf = new Sorf(item,itemRecommendDegree);

                            sorfList.add(sorf);

                     }

              }

              Collections.sort(sorfList);

              int size = 0;

              if(sorfList.size() <5){

                     size = sorfList.size();

              }else {

                     size = 4;

              }

              List<Integer> ids = new ArrayList<>();

              for (int i = 0; i < size; i++) {

                     ids.add(sorfList.get(i).getKey());

              }

              return ids;

       }

}

相關文章
相關標籤/搜索