添加相似和推荐功能

This commit is contained in:
2023-06-14 19:10:31 +08:00
parent c1438af1e4
commit 2019e15b7a
13 changed files with 715 additions and 91 deletions

View File

@@ -6,10 +6,7 @@ import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.wx.application.adapter.dto.qo.GorseQ;
import com.wx.application.base.BaseController;
@@ -29,82 +26,100 @@ import lombok.extern.slf4j.Slf4j;
@RequestMapping("/gorse")
@RestController("gorseController")
public class GorseController extends BaseController {
@Autowired
GorseService gorseService;
@Autowired
RiskUserService riskUserService;
@Autowired
EntrysService entrysService;
@Autowired
NebulaOperateService nebulaOperateService;
@Autowired
ImportGraphService importGraphService;
/**
* 推荐根据userId
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/recommend_by_userid")
@Autowired
GorseService gorseService;
@Autowired
RiskUserService riskUserService;
@Autowired
EntrysService entrysService;
@Autowired
NebulaOperateService nebulaOperateService;
@Autowired
ImportGraphService importGraphService;
/**
* 推荐根据userId
*
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/recommend_by_userid")
public ResponseData recommendByUserId(@RequestBody GorseQ gQo) throws Exception {
List<String> res = gorseService.getRecommend(gQo.getUserId());
if(res == null || res.size() == 0) {
return success();
}
Map mQ = new HashMap<>();
mQ.put("INS_fid", StringUtils.join(res, ","));
return success(entrysService.queryList(mQ));
}
/**
* 根据列表推荐
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/popular_by_category")
List<String> res = gorseService.getRecommend(gQo.getUserId());
if (res == null || res.size() == 0) {
return success();
}
Map mQ = new HashMap<>();
mQ.put("INS_fid", StringUtils.join(res, ","));
return success(entrysService.queryList(mQ));
}
/**
* 根据列表推荐
*
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/popular_by_category")
public ResponseData popularByCategory(@RequestBody GorseQ gQo) throws Exception {
List<Item> res = gorseService.getPopular(gQo.getCategory());
return success(res);
}
@PostMapping(value = "/ipt")
List<Item> res = gorseService.getPopular(gQo.getCategory());
return success(res);
}
@PostMapping(value = "/ipt")
public ResponseData ipt() throws Exception {
importGraphService.iptUser();
importGraphService.iptentrys();
return success();
}
/**
* 推荐根据知识图谱
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/recommend_by_userid_with_graph")
importGraphService.iptUser();
importGraphService.iptentrys();
return success();
}
/**
* 推荐根据知识图谱
*
* @param gQo
* @return
* @throws Exception
*/
@PostMapping(value = "/recommend_by_userid_with_graph")
public ResponseData recommendByUserIdWithGraph(@RequestBody GorseQ gQo) throws Exception {
NebulaModel nebulaModel = nebulaOperateService.findOnePathById("", gQo.getUserId());
List<NebulaNode> nodes = nebulaModel.getNodes();
if(nodes != null && nodes.size() > 0) {
nodes.get(0).getVid();
}
List<String> res = gorseService.getRecommend(gQo.getUserId());
if(res == null || res.size() == 0) {
return success();
}
Map mQ = new HashMap<>();
mQ.put("INS_fid", StringUtils.join(res, ","));
return success(entrysService.queryList(mQ));
}
NebulaModel nebulaModel = nebulaOperateService.findOnePathById("", gQo.getUserId());
List<NebulaNode> nodes = nebulaModel.getNodes();
if (nodes != null && nodes.size() > 0) {
nodes.get(0).getVid();
}
List<String> res = gorseService.getRecommend(gQo.getUserId());
if (res == null || res.size() == 0) {
return success();
}
Map mQ = new HashMap<>();
mQ.put("INS_fid", StringUtils.join(res, ","));
return success(entrysService.queryList(mQ));
}
@PostMapping(value = "/get_similar_user")
public ResponseData getSimilarUser(@RequestBody GorseQ gQo) throws Exception {
return success(gorseService.getSimilarUser(gQo.getUserId()));
}
@PostMapping(value = "/get_recommend_by_user")
public ResponseData getRecommendByUser(@RequestBody GorseQ gQo) throws Exception {
return success(gorseService.getRecommendByUser(gQo.getUserId(), gQo.getRecommendation(), gQo.getCategory(), gQo.getN()));
}
@PostMapping(value = "/get_similar_item")
public ResponseData getSimilarItem(@RequestBody GorseQ gQo) throws Exception {
return success(gorseService.getSimilarItem(gQo.getItemId(), gQo.getCategory(), gQo.getN()));
}
}

View File

@@ -8,6 +8,12 @@ import lombok.EqualsAndHashCode;
public class GorseQ {
private String userId;
private String itemId;
private String category;
private Integer n;
private String recommendation;
}

View File

@@ -34,4 +34,9 @@ public class RiskUserController extends BaseController {
return success(userService.queryPage(entrysQ));
}
@PostMapping(value = "/query_unique")
public ResponseData queryUnique(@RequestBody Map entrysQ) {
return success(userService.queryUnique(entrysQ));
}
}

View File

@@ -10,6 +10,7 @@ import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@Service
@@ -23,7 +24,7 @@ public class GorseService {
//Gorse client = new Gorse("", "");
//System.out.println(client.getRecommend("1265177464"));
}
public RowAffected insertUser(User user) throws IOException {
return this.request("POST", this.endpoint + "/api/user", user, RowAffected.class);
@@ -60,13 +61,40 @@ public class GorseService {
public List<String> getRecommend(String userId) throws IOException {
return Arrays.asList(this.request("GET", this.endpoint + "/api/recommend/" + userId, null, String[].class));
}
public List<Item> getPopular(String category) throws IOException {
return Arrays.asList(this.request("GET", this.endpoint + "/api/dashboard/popular/" + category, null, Item[].class));
}
/**
* 相似(获取相似用户)
*/
public List<User> getSimilarUser(String userId) throws IOException {
return Arrays.asList(this.request("GET", this.endpoint + "/api/dashboard/user/" + userId + "/neighbors", null, User[].class));
}
/**
* 洞悉(根据用户获取推荐)
*/
public List<Item> getRecommendByUser(String userId, String recommendation, String category, Integer n) throws IOException {
return Arrays.asList(this.request("GET", this.endpoint + "/api/dashboard/recommend/" + userId
+ "/" + recommendation + "/" + category + "?n=" + ((n == null || n <= 0) ? 10 : n), null, Item[].class));
}
/**
* 相似物品根据物品ID和分类获取相似物品
*/
public List<Item> getSimilarItem(String itemId, String category, Integer n) throws IOException {
String url = this.endpoint + "/api/dashboard/item/" + itemId + "/neighbors";
if (StringUtils.isNotBlank(category)) {
// 当默认查询10条
url += "/" + category + "?n=" + ((n == null || n <= 0) ? 10 : n);
}
System.out.println(url);
return Arrays.asList(this.request("GET", url, null, Item[].class));
}
private <Request, Response> Response request(String method, String url, Request request, Class<Response> responseClass) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

View File

@@ -14,12 +14,12 @@ public class Item {
private String timestamp;
private String comment;
private Integer score;
private Float score;
public Item() {
}
public Item(String itemId, Boolean isHidden, List<String> labels, List<String> categories, String timestamp, String comment,Integer score) {
public Item(String itemId, Boolean isHidden, List<String> labels, List<String> categories, String timestamp, String comment,Float score) {
this.itemId = itemId;
this.isHidden = isHidden;
this.labels = labels;
@@ -60,7 +60,7 @@ public class Item {
}
@JsonProperty("Score")
public Integer getScore() {
public Float getScore() {
return score;
}

View File

@@ -12,6 +12,8 @@ public class User {
private String userId;
private List<String> labels;
private Float score;
public User() {
}
@@ -30,6 +32,11 @@ public class User {
return labels;
}
@JsonProperty("Score")
public Float getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;