今天毕设的任务是获取用户行为,根据TF-IDF算法计算标签权重,并写入数据库;
代码:
Action.java
package entity;
public class Action {
private int user_id;//用户id
private int tag_id;//标签id
private String tag_name;//标签名称
private String tag_type;//标签类型
private String action_name;//行为名称
private int action_count;//行为次数
private String action_time;//行为时间
private double weight;//全权重
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getTag_id() {
return tag_id;
}
public void setTag_id(int tag_id) {
this.tag_id = tag_id;
}
public String getTag_name() {
return tag_name;
}
public void setTag_name(String tag_name) {
this.tag_name = tag_name;
}
public String getTag_type() {
return tag_type;
}
public void setTag_type(String tag_type) {
this.tag_type = tag_type;
}
public String getAction_name() {
return action_name;
}
public void setAction_name(String action_name) {
this.action_name = action_name;
}
public int getAction_count() {
return action_count;
}
public void setAction_count(int action_count) {
this.action_count = action_count;
}
public String getAction_time() {
return action_time;
}
public void setAction_time(String action_time) {
this.action_time = action_time;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
Tag.java
package entity;
public class Tag {
private int tag_id;//标签id
private String tag_name;//标签名称
private String tag_type;//标签类型
private int count;//标签被标记次数
public int getTag_id() {
return tag_id;
}
public void setTag_id(int tag_id) {
this.tag_id = tag_id;
}
public String getTag_name() {
return tag_name;
}
public void setTag_name(String tag_name) {
this.tag_name = tag_name;
}
public String getTag_type() {
return tag_type;
}
public void setTag_type(String tag_type) {
this.tag_type = tag_type;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Shoes.java
package entity;
public class Shoes {
private int id;//商品id
private String shoes_name;//商品名称
private float price;//商品价格
private String image;//图片链接
private String deal;//销量
private String shop_name;//店铺名称
private String address;//店铺地址
private String url;//商品链接
private String images;//详情图片链接
private String details;//商品介绍
private String source;//商品来源网址
private String history_price;//商品历史价格
private float low_price;//商品最低价格
private String low_date;//最低价格日期
private String analyse;//价格走势分析
private String trend;//价格走势
private String tag;//商品标签
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShoes_name() {
return shoes_name;
}
public void setShoes_name(String shoes_name) {
this.shoes_name = shoes_name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDeal() {
return deal;
}
public void setDeal(String deal) {
this.deal = deal;
}
public String getShop_name() {
return shop_name;
}
public void setShop_name(String shop_name) {
this.shop_name = shop_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getHistory_price() {
return history_price;
}
public void setHistory_price(String history_price) {
this.history_price = history_price;
}
public float getLow_price() {
return low_price;
}
public void setLow_price(float low_price) {
this.low_price = low_price;
}
public String getLow_date() {
return low_date;
}
public void setLow_date(String low_date) {
this.low_date = low_date;
}
public String getAnalyse() {
return analyse;
}
public void setAnalyse(String analyse) {
this.analyse = analyse;
}
public String getTrend() {
return trend;
}
public void setTrend(String trend) {
this.trend = trend;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
}
ActionDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import util.DBUtil;
import dao.TagDao;
public class ActionDao {
/*
*计算TF-IDF值
*/
/**
*calculate the tag frequency
* @throws SQLException
*/
public static float tf(int shoes_id, String tag) throws SQLException {
int tagFrequency = 0;
DecimalFormat df=new DecimalFormat("0.00");//java保留两位小数
ShoesDao shoesDao = new ShoesDao();
String[] shoes_tag = shoesDao.getShoesTagByShoesId(shoes_id);
for (String str : shoes_tag) {
if (str.equalsIgnoreCase(tag)) {
tagFrequency++;
}
}
System.out.println(tagFrequency);
System.out.println(shoes_tag.length);
return Float.parseFloat(df.format((float)tagFrequency/shoes_tag.length));
}
/**
*calculate the inverse document frequency
*/
public static float idf(String tag)
{
int shoes_count = 100000;
TagDao tagDao = new TagDao();
int count = tagDao.getCountByTagName(tag);
return (float) Math.log(((float)shoes_count/count));
}
/**
* calculate tf-idf
* @throws SQLException
*/
public double tfIdf(int shoes_id, String tag) throws SQLException {
return tf(shoes_id, tag) * idf(tag);
}
/*
* TF-IDF结束
*/
//添加用户行为时不传入商品id,可以计算TF=1 ,计算IDF,比如注册,搜索
public void addUserAaction_noShoes(int user_id,List<String> tag,String action_name) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//获取当前年月日
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
String action_time = year + "-" + month + "-" + day;
System.out.println(action_time);
//设置计算用户行为权重
float action_weight = 0;
if(action_name == "点击")
action_weight = (float) 0.6;
else if(action_name == "搜索")
action_weight = (float) 0.8;
else if(action_name == "收藏")
action_weight = (float) 1;
else if(action_name == "取消收藏")
action_weight = (float) -1;
else if(action_name == "不感兴趣")
action_weight = (float) -0.8;
else
action_weight = (float) 0.65;
System.out.println(action_weight);
for(int i = 0;i<tag.size();i++) {//将tag数组循环保存到user_action中
//先在user_action表中查询该条行为是否存在
String sql = "select * from user_action where user_id=? and tag_name=? and action_name=? and action_time=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, user_id);
preparedStatement.setString(2,tag.get(i));
preparedStatement.setString(3, action_name);
preparedStatement.setString(4, action_time);
resultSet = preparedStatement.executeQuery();
System.out.println("查询是否存在该条行为:"+resultSet);
System.out.println(resultSet.next());
if (resultSet.next()) {//该条行为若存在,将count+1
int count = resultSet.getInt("id")+1;
System.out.println("该条行为存在,count为"+count);
float weight = (float) (action_weight * 0.5 * idf(tag.get(i)) * count);
System.out.println(weight);
sql = "update user_action set action_count=?, weight = ? where user_id=? and tag_name=? and action_name=? and action_time=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, count);
preparedStatement.setFloat(2, weight);
preparedStatement.setInt(3,user_id);
preparedStatement.setString(4, tag.get(i));
preparedStatement.setString(5, action_name);
preparedStatement.setString(6, action_time);
preparedStatement.executeUpdate();
}else {//该条行为不存在,将该条行为插入到数据库中
System.out.println("该条行为不存在");
sql ="select * from tag where tag_name=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, tag.get(i));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {//查询这个标签是不是在标签库中,如果在将行为插入到user_action中
int tag_id = resultSet.getInt("tag_id");
String tag_type = resultSet.getString("tag_type");
float weight = (float) (action_weight * 0.5 * idf(tag.get(i)) * 1);
sql = "insert into user_action(user_id,tag_id,tag_name,tag_type,action_name,action_count,action_time,weight) values (?,?,?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, user_id);
preparedStatement.setInt(2,tag_id);
preparedStatement.setString(3, tag.get(i));
preparedStatement.setString(4, tag_type);
preparedStatement.setString(5, action_name);
preparedStatement.setInt(6, 1);
preparedStatement.setString(7, action_time);
preparedStatement.setFloat(8, weight);
preparedStatement.executeUpdate();
}
}
}
}
//添加用户行为时传入商品id,可以计算TF-IDF,比如点击,收藏,取消收藏,不感兴趣
public void addUserAction_withshoes(int user_id,List<String> tag,int shoes_id,String action_name ) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//获取当前年月日
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
String action_time = year + "-" + month + "-" + day;
System.out.println(action_time);
//设置计算用户行为权重
float action_weight = 0;
if(action_name == "点击")
action_weight = (float) 0.6;
else if(action_name == "搜索")
action_weight = (float) 0.8;
else if(action_name == "收藏")
action_weight = (float) 1;
else if(action_name == "取消收藏")
action_weight = (float) -1;
else if(action_name == "不感兴趣")
action_weight = (float) -0.8;
else
action_weight = (float) 0.65;
System.out.println(action_weight);
for(int i = 0;i<tag.size();i++) {//将tag数组循环保存到user_action中
//先在user_action表中查询该条行为是否存在
String sql = "select * from user_action where user_id=? and tag_name=? and action_name=? and action_time=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, user_id);
preparedStatement.setString(2,tag.get(i));
preparedStatement.setString(3, action_name);
preparedStatement.setString(4, action_time);
resultSet = preparedStatement.executeQuery();
System.out.println("查询是否存在该条行为:"+resultSet);
System.out.println(resultSet.next());
if (resultSet.next()) {//该条行为若存在,将count+1
int count = resultSet.getInt("id")+1;
System.out.println("该条行为存在,count为"+count);
float weight = (float) (action_weight * 0.5 * tfIdf(shoes_id,tag.get(i)) * count);
System.out.println(weight);
sql = "update user_action set action_count=?, weight = ? where user_id=? and tag_name=? and action_name=? and action_time=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, count);
preparedStatement.setFloat(2, weight);
preparedStatement.setInt(3,user_id);
preparedStatement.setString(4, tag.get(i));
preparedStatement.setString(5, action_name);
preparedStatement.setString(6, action_time);
preparedStatement.executeUpdate();
}else {//该条行为不存在,将该条行为插入到数据库中
System.out.println("该条行为不存在");
sql ="select * from tag where tag_name=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, tag.get(i));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {//查询这个标签是不是在标签库中,如果在将行为插入到user_action中
int tag_id = resultSet.getInt("tag_id");
String tag_type = resultSet.getString("tag_type");
float weight = (float) (action_weight * 0.5 * idf(tag.get(i)) * 1);
sql = "insert into user_action(user_id,tag_id,tag_name,tag_type,action_name,action_count,action_time,weight) values (?,?,?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, user_id);
preparedStatement.setInt(2,tag_id);
preparedStatement.setString(3, tag.get(i));
preparedStatement.setString(4, tag_type);
preparedStatement.setString(5, action_name);
preparedStatement.setInt(6, 1);
preparedStatement.setString(7, action_time);
preparedStatement.setFloat(8, weight);
preparedStatement.executeUpdate();
}
}
}
}
}
TagDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.DBUtil;
public class TagDao {
public int getCountByTagName(String tag_name) {
int count = 0;
Connection connection = DBUtil.getConnection();
String sql ="select count from tag where tag_name = ?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,tag_name);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {//此标签存在
count = resultSet.getInt("count");
System.out.println(count);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return count;
}
}
ShoesDao.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.DBUtil;
public class ShoesDao {
public String[] getShoesTagByShoesId(int shoes_id) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String[] tag =null;
String sql = "select tag from shoes where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, shoes_id);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
tag = resultSet.getString("tag").split( "\\|");
}
for(int i = 0;i<tag.length;i++) {
System.out.println(tag[i]);
}
return tag;
}
}
来源:https://www.cnblogs.com/qilin20/p/12291697.html