前言
完善了一个与管理员有关的接口以及用户画像的接口。
管理员接口:用户统计信息的获取
现在只有获取用户性别的数据,之后还可以添加别的,用label区分
Controller层:
/**
* 获取所有用户统计信息
* @param label
* @return
*/
@RequestMapping(value = "/statistics/{label}", method = RequestMethod.GET, produces = "application/json")
public Response getStatistics(
@PathVariable String label) {
//获取用户的性别统计信息
if("sex".equals(label))
{
List<SexCount> sexCounts = new ArrayList<>();
sexCounts = userService.selectSexCount();
if(sexCounts.size() == 0)
{
throw new BaseException(ErrorInfoEnum.STATISTIC_NOT_FIND);
}
return Response.success("获取统计信息成功", sexCounts);
}
return Response.error(532,"获取统计信息失败");
}
Service层:
public List<SexCount> selectSexCount()
{
return userDao.selectSexCount();
}
Dao层:
/**
*获取性别的统计信息
* @return
*/
@Select("select sex , count(sex) as count " +
"from user group by sex")
List<SexCount> selectSexCount();
用户画像接口——获取给用户推荐的标签
推荐系统根据推荐标签来推荐对应标签的文章,所以在用户画像中可以显示一下给用户推荐的标签信息。
/**
* 获取推荐的标签信息
* @param userId
* @return
* @throws IOException
* @throws TasteException
*/
public List<Tag> getRecommendedTag(int userId) throws IOException, TasteException {
return userPortraitService.selectRecommendedTagList(userId);
}
/**
* 推荐的标签
*
* @param userId
* @return
* @throws IOException
* @throws TasteException
*/
public List<Tag> selectRecommendedTagList(int userId) throws IOException, TasteException {
List<Integer> tagIdList = new ArrayList<>();
tagIdList = getRecommendedTagIdListByAls(userId);
if (tagIdList.size() == 0) {
tagIdList = getRecommendedTagIdListByCF(userId);
}
List<Tag> tagList = new ArrayList<>();
for (Integer tagId : tagIdList) {
Tag tag = tagDao.getTagById(tagId);
tagList.add(tag);
}
return tagList;
}
/**
* 使用Als推荐标签
*
* @param userId
* @return
*/
public List<Integer> getRecommendedTagIdListByAls(int userId) {
//推荐的标签
String tagString = alsRecommended.getRecommendTag(userId);
//将标签分割为数组
String temp[] = tagString.split("#");
List<Integer> tagIdList = new ArrayList<>();
for (int i = 0; i < temp.length; i++) {
if (!temp[i].equals("1839")) {
tagIdList.add(Integer.valueOf(temp[i]));
}
}
return tagIdList;
}
/**
* 使用CF 推荐标签
*
* @param userId
* @return
*/
public List<Integer> getRecommendedTagIdListByCF(int userId) throws IOException, TasteException {
//基于用户的协同过滤 推荐得到的标签id
List<RecommendedItem> userBasedList = cfRecommended.userBasedRecommendedWithTag(userId, 10);
//基于标签id的协同过滤 推荐得到的标签id
List<RecommendedItem> itemBaseList = cfRecommended.itemBaseRecommendedWithTag(userId, 10);
Map<Integer, Double> score = new HashMap<>();
for (RecommendedItem recommendedItem : userBasedList) {
int itemId = (int) recommendedItem.getItemID();
if (!score.containsKey(itemId)) {
score.put(itemId, 0.0);
}
score.put(itemId, score.get(itemId) + recommendedItem.getValue());
}
for (RecommendedItem recommendedItem : itemBaseList) {
int itemId = (int) recommendedItem.getItemID();
if (!score.containsKey(itemId)) {
score.put(itemId, 0.0);
}
score.put(itemId, score.get(itemId) + recommendedItem.getValue());
}
List<Map.Entry<Integer, Double>> list = new ArrayList<>(score.entrySet());
list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
int index = 0;
List<Integer> tagIdList = new ArrayList<>();
for (Map.Entry<Integer, Double> t : list) {
if (t.getKey() != 1839) {
tagIdList.add(t.getKey());
if (++index > 10) {
break;
}
}
}
return tagIdList;
}
最后展示出来发现,推荐的标签基本都是用户阅读文章的时出现最多的标签,偶尔会出现新的标签,看来推荐的效果还可以。