自己写的简单的商品推荐算法,以地区为单位的推荐,不知道这个算法怎么样,希望得到比较好的建议...

 package cn.test.business;import java.io.File;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Calendar;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;import java.util.TreeMap;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.mahout.cf.taste.eval.RecommenderBuilder;import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;import org.apache.mahout.cf.taste.model.DataModel;import org.apache.mahout.cf.taste.recommender.RecommendedItem;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool; public class OldUserBus {  static String outMonth = Global.getConfig("outMonth");	static JedisPool pool = JedisUtil.getPool();	static Jedis jedis = pool.getResource();	static float itemScore = 0.03f; // 如果算法没有推荐分数,需要计算的分数	static float priceLat = 0.001f; // 计算的价格	static float priceMid = 259; // 地区房价价格临界点	static float dayLat = 0.001f; // 天数越大,越不推荐	static float smallAreaCodeScore = 0.011f; // 该地区值多少分,地区基本分数	static float roomLat = 0.9f; // 非最佳房间数相	static float sitLat = 0.9f; // 非最佳厅数	static int roomNum = 3; // 最佳推荐房间数	static int sitNum = 1; // 最佳推荐厅间数	/*	 *teseJuint对算法结果提取出来,进行二次运算,添加各种纬度进行结果运算	 * 	 * itemLoglikelihood	 */	public static int adLatite(long exitId,			LinkedHashMap<Integer, Float> tmpMap, int smallAreaCode) {		String udid = UserBus.catRedis2udid(Integer.valueOf(exitId + ""));		int Id = 0;		double price = 0.0;		String roomType = "";		String createTime = "";		long dayNum = 0;		double score = 0.0;		String date = DateUtils.getDate("yyyy/MM/dd HH:mm:s");		try {			// 根据uid,找到用户之前没有看到房源,进行一个排分,再按照一个时间排序			String sql = "SELECT  DISTINCT h.`Id`, h.`Price` ,h.`roomType` ,h.`createTime`  FROM  hw_h_click_log  h  "					+ "WHERE h.`smallAreaCode`="					+ smallAreaCode					+ "   AND h.`udid` !='"					+ udid					+ "' ORDER BY h.`createTime` DESC ,h.`Price`  LIMIT 10 ";			ResultSet res = SqlUtils.result(sql);			TreeMap<Double, Double> tmp = new TreeMap<Double, Double>();			while (res.next()) {				Id = res.getInt(1);				price = res.getDouble(2);				roomType = res.getString(3);				createTime = res.getDate(4) + "";				String[] spilt = roomType.replace("厅", "").split("室");				// System.out.println(spilt[0] +"--"+spilt[1]);				int roomInt = Integer.valueOf(spilt[0]);				int sitInt = Integer.valueOf(spilt[1]);				Float itemLat = tmpMap.get(res.getInt(1));				if (null == itemLat || StringUtils.isBlank(itemLat + "")) {					itemLat = itemScore;				}				// System.out.println("-result ::" +java.lang.Math.abs(result));				dayNum = DateUtils.pastDays(DateUtils.parseDate(createTime));// 计算多少天数				double priceScore = price - priceMid;				double dayScore = dayNum * dayLat; // 发布天数乘以 天的纬度数				score = itemLat + smallAreaCodeScore - (priceScore * priceLat)						- dayScore;// 得到推荐分数				System.out.println("--itemLat:::" + itemLat + "--dayScore:::"						+ dayNum * dayLat + "--score:::" + score						+ "--priceScore:::" + priceScore * priceLat);				tmp.put(score, (double) Id);			}			Iterator<Double> iterator_2 = tmp.keySet().iterator();			int sort = 0;			/*			 * DateUtils.getDate("yyyy/MM/dd HH:mm:s")			 */			while (iterator_2.hasNext()) {				sort++;				Object key = iterator_2.next();				System.out.println(sort + "::tmp.get(key) is :" + tmp.get(key)						+ "--- " + key);				sql = "INSERT  INTO `hw_h_old_recom` (`udid`,`Id`,`smallAreaCode`,`sort`,`score`, `createTime` ) VALUES "						+ "('"						+ udid						+ "',"						+ tmp.get(key)						+ ","						+ smallAreaCode						+ " , "						+ sort						+ " , '"						+ key						+ "' , '"						+ DateUtils.getDate("yyyy/MM/dd HH:mm:s") + "')";				// boolean bb=SqlUtils.insert(sql);				// System.out.println("--insert---" +bb);			}		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return 0;	}	/*	 *teseJuint调用本地算法进行计算	 * 	 * itemLoglikelihood	 */	public static int userCityLog(int smallAreaCode) {		try {			String pvCsv = "datafile//" + smallAreaCode + "pv.csv";			String Csv = "datafile//" + smallAreaCode + ".csv";			DataModel dataModel = RecommendFactory.buildDataModelNoPref(pvCsv);			RecommenderBuilder userCity = RecommenderEvaluator					.userCityBlock(dataModel);			LongPrimitiveIterator iter = dataModel.getUserIDs();			while (iter.hasNext()) {				long uid = iter.nextLong();				System.out.print("userCityBlock    =>");				RecommenderFilterOutdateResult.filterOutdate(uid,						userCity, dataModel, Csv);				List<RecommendedItem> recommendations = RecommenderFilterOutdateResult						.filterOutdate(uid, userCity, dataModel, Csv);				boolean skip = false;				if (!skip || recommendations.size() > 0) {					System.out.printf("uid:%s,", uid);					for (RecommendedItem recommendation : recommendations) {						System.out.printf("(%s,%f)",								recommendation.getItemID(),								recommendation.getValue());					}					System.out.println();				}			}		} catch (Exception e) {			e.printStackTrace();		}		return 0;	}	/*	 *teseJuint调用本地算法进行计算	 * 	 * itemLoglikelihood	 */	public static int useitemLog(int smallAreaCode) {		LinkedHashMap<Integer, LinkedHashMap<Integer, Float>> map = new LinkedHashMap<Integer, LinkedHashMap<Integer, Float>>();		try {			areaImport(smallAreaCode);			String pvCsv = "datafile//" + smallAreaCode + "pv.csv";			String Csv = "datafile//" + smallAreaCode + ".csv";			DataModel dataModel = RecommendFactory.buildDataModelNoPref(pvCsv);			RecommenderBuilder itemLog = RecommenderEvaluator.itemLoglikelihood(dataModel);			LongPrimitiveIterator iter = dataModel.getUserIDs();			while (iter.hasNext()) {				long uid = iter.nextLong();				System.out.print("itemLoglikelihood=>");				List<RecommendedItem> recommendations = RecommenderFilterOutdateResult						.filterOutdate(uid, itemLog, dataModel, Csv);				boolean skip = false;				if (!skip || recommendations.size() > 0) {					System.out.printf("uid:%s,", uid);					LinkedHashMap<Integer, Float> tmpMap = new LinkedHashMap<Integer, Float>();					for (RecommendedItem recommendation : recommendations) {						System.out.printf("(%s,%f)",recommendation.getItemID(),	recommendation.getValue());						tmpMap.put((int) recommendation.getItemID(),recommendation.getValue());					}					map.put((int) uid, tmpMap);					adLatite(uid, tmpMap, smallAreaCode);// 添加纬度进行运算					System.out.println();				}			}		} catch (Exception e) {			e.printStackTrace();		}		return 0;	}	/*	 *teseJuint根据itemLoglikelihood算法需要,从 我们的mysql日志表里再导出csv格式的数据	 * 	 * 这两个csv文件,做算法匹配 pvCsv 用户id+,+id Csv 房源id+房源时间+房源价格	 * 	 * 为了减轻压力,都是分地区而导,分地区而计算PV总数	 */	public static int areaImport(int smallAreaCode) {		try {			String sql = "SELECT   h.`udid`, h.`Id`   FROM  hw_h_click_log  h  WHERE h.`smallAreaCode`="+ smallAreaCode;			ResultSet res = SqlUtils.result(sql);			String result = "";			String Ids = "";			HashSet<Integer> set = new HashSet<Integer>();			while (res.next()) {				// System.out.println(res.getString(1) +"," +res.getInt(2));				int changeId = UserBus.catUdidInsert(res.getString(1));				result += changeId + "," + res.getInt(2) + "\n";				set.add(res.getInt(2));			}			boolean bb = FileUtil.writeTxtFile(result, new File("datafile//" + smallAreaCode + "pv.csv"));			result = "";			Iterator<Integer> iterator = set.iterator();			while (iterator.hasNext()) {				Ids += iterator.next() + ",";			}			sql = "SELECT   DISTINCT h.`Id` ,h.`createTime` ,h.`Price`  FROM  hw_h_click_log  h  WHERE h.`Id` IN ("					+ Ids.substring(0, Ids.length() - 1) + ")";			res = SqlUtils.result(sql);			while (res.next()) {				// System.out.println(res.getString(1) +","+res.getDate(2)+","				// +res.getDouble(3));				result += res.getString(1) + "," + res.getDate(2) + ","						+ res.getDouble(3) + "\n";			}			boolean bb = FileUtil.writeTxtFile(result, new File(					"datafile//" + smallAreaCode + ".csv"));			System.out.println(bb + "----" + bb);		} catch (Exception e) {			e.printStackTrace();		}		return 0;	}	/*	 *teseJuint删除所有过期的推荐房源	 */	public static int dropArea(String delDate) {		try {			String sql = "DELETE FROM hw_h_new_recom  WHERE hw_h_new_recom.`createTime` < '"					+ delDate + "'";			ResultSet res = SqlUtils.result(sql);		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();			return SysStatus.TTE_FAILED;		}		return SysStatus.TTE_SUCCESSED;	}	/*	 *teseJuint遍历所有的地区 ,特别针对老用户的	 */	public static int sortAreaOld() {		int smallAreaCode = 0;		try {			String sql = "SELECT DISTINCT h.`smallAreaCode`  FROM  hw_h_click_log  h";			ResultSet res = SqlUtils.result(sql);			while (res.next()) {				smallAreaCode = res.getInt(1);				if (0 != res.getInt(1)&& StringUtils.isNotBlank(smallAreaCode + "")) {					useitemLog(smallAreaCode);				}			}		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();			return SysStatus.TTE_FAILED;		}		return SysStatus.TTE_SUCCESSED;	}	/*	 * 入查:地区id 出查:地区的平均房价	 * 	 * 这个地方有待改进	 */	public static double areaAvgPrice(int smallAreaCode) {		Connection conn = null;		Statement stmt = null;		ResultSet res = null;		double price = 0.0;		int i = 0;		try {			conn = MqjdbcUtils.getConnection();			stmt = conn.createStatement();			res = stmt.executeQuery("SELECT   DISTINCT h.`Id` ,h.`Price`  FROM hw_h_click_log  h  WHERE h.`smallAreaCode` ="							+ smallAreaCode);			while (res.next()) {				// System.out.println(res.getInt(1) +"--" +res.getDouble(2));				price += res.getDouble(2);				i++;			}			System.out.println(price / i + "执行运行结�?::" + smallAreaCode);			return price / i;		} catch (SQLException e) {			// TODO Auto-generated catch block			e.printStackTrace();			return price / i;		}	}	public static void main(String[] args) {		sortAreaOld();		// areaImport(11891);		// useitemLog(11891);		// userCityLog(11891);		// adLatite(exitId, tmpMap, smallAreaCode);	}

 

转载于:https://my.oschina.net/sin58/blog/653996

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值