package com.iplatform.rectext.api;
|
|
import com.iplatform.reccommon.ResponseValue;
|
import com.iplatform.reccommon.config.TextSimilarProperties;
|
import com.iplatform.rectext.pojo.SearchRequest;
|
import com.iplatform.rectext.pojo.TextSimilar;
|
import com.iplatform.rectext.pojo.UploadText;
|
import com.walker.infrastructure.utils.JsonUtils;
|
import com.walker.infrastructure.utils.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.ResponseEntity;
|
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.client.RestTemplate;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@RestController
|
@RequestMapping("/api/text")
|
public class TextResultApi {
|
|
private final transient Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
private RestTemplate restTemplate = null;
|
|
private TextSimilarProperties textSimilarProperties = null;
|
|
@Autowired
|
public TextResultApi(RestTemplate restTemplate, TextSimilarProperties textSimilarProperties){
|
this.restTemplate = restTemplate;
|
this.textSimilarProperties = textSimilarProperties;
|
}
|
|
/**
|
* 业务上传课程文本分析数据。<p></p>
|
* 1.请求参数格式:<br>
|
* <pre>
|
* {
|
* "type":"kecheng", // 文本分类,业务设置标识字符串
|
* "title":["10","20","30"], // title为课程ID
|
* "text":["课程描述1","课程描述2","课程描述3"] // text为课程描述等一个字符串
|
* }
|
* </pre>
|
* 2.响应格式:<br>
|
* <pre>
|
* {"state":true, "code":1, "msg":"success"} // 1 成功,其他失败
|
* </pre>
|
* @param json
|
* @return
|
* @author 时克英
|
* @date 2022-11-03
|
*/
|
@RequestMapping("/upload")
|
public ResponseValue uploadText(@RequestBody String json){
|
if(StringUtils.isEmpty(json)){
|
return ResponseValue.error("上传的课程分析数据为空");
|
}
|
String url = this.textSimilarProperties.getAiService();
|
logger.info("upload text url = " + url);
|
try {
|
UploadText uploadText = JsonUtils.jsonStringToObject(json, UploadText.class);
|
String error = this.postUploadText(url, uploadText);
|
if(error != null){
|
return ResponseValue.error(error);
|
}
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
return ResponseValue.success();
|
}
|
|
/**
|
* 给定一句话(或标签文本内容)检索出相似的课程列表,最多40个。<br>
|
* 需要业务根据评分排序使用,可设置一个阈值,分数高的排前面。<p></p>
|
* 1.请求参数格式:<br>
|
* <pre>
|
* {"query":"他是一个会计师", "type":"kecheng"}
|
* </pre>
|
*
|
* 2.响应格式:<br>
|
* <pre>
|
* {
|
* "state": true,
|
* "code": 1,
|
* "msg": "success",
|
* "data": [
|
* {
|
* "dis": 0.3613262176513672,
|
* "id": "9",
|
* "text": "会计培训_会计全科班报名学费,零会计基础知识,欲全面学习会计专业技能,达到一般企业的熟练会计水平",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.3019663691520691,
|
* "id": "2",
|
* "text": "初级会计师考试包含哪几门呢?初级会计师考试一直是考试两门,分别为《初级会计实务》和《经济法基础》。参加初级资格考试的人员,在一个考试年度内通过全部科目的考试,才可获得初级资格证书。",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.29971787333488464,
|
* "id": "8",
|
* "text": "湖州市淘宝美工培训 淘宝电商培训报名热线,2022年湖州市春华职业培训学校开设电脑系列课程:办公、平面、美工、设计、制版、淘宝等;会计课程:基础会计,初中级会计考证,会计实务,会计全科班等,成人在职专科本科学历进修,淘宝美工爱好者、淘宝店铺经营者、平面设计爱好者。掌握网店主页美工设计、海报、焦点图、宝贝详情页的美工设计,图片空间,网站代码,实现视觉营销",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.23417186737060547,
|
* "id": "7",
|
* "text": "2022年台州玉环县成人教育学历进修报名,学习形式有自考,函授与远程教育,学制短,文凭电子注册,国家教育部承认,高升本设置专业:会计,专升本设置专业:会计,机电一体化,工商管理",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.22891874611377716,
|
* "id": "10",
|
* "text": "需要掌握财务知识的企业管理人员也可学习该课程。参加考试成绩合格颁发ATEP全国会计实务资格初级证书及会计从业资格证书",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.11250658333301544,
|
* "id": "11",
|
* "text": "海宁市成人函授大学会计专科、本科学历招生,东北农业大学网络教育学院创建于2000年7月,是全国首批三十一所远程教育试点学校之一,也是全国第一所进行现代远程教育试点的高等农业院校",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.10873384773731232,
|
* "id": "13",
|
* "text": "宁波杭州湾淘宝培训 电商运营推广班报名学费,春华淘宝开店培训班招收零基础学员、淘宝开店创业者、全职妈妈、打工仔、淘宝新手卖家,对有志于网络销售,淘宝开店等的社会各界人士,对网络零售和电子商务有更深入的理解,学习网店的建设;对网店的流程各方面都比较了解,能够独立运营网店,淘宝会员注册、支付宝实名认证、网上购物流程、淘宝店铺注册",
|
* "type": "kecheng"
|
* },
|
* {
|
* "dis": 0.07954521477222443,
|
* "id": "3",
|
* "text": "一、二级消防师考试最新教,2022年版教材明确为一、二级注册消防工程师共用,同时按照考试大纲及考查范围对一级注册消防工程师和二级注册消防工程师学习",
|
* "type": "kecheng"
|
* }
|
* ],
|
* "total": 8
|
* }
|
* </pre>
|
* @param json
|
* @return
|
*/
|
@RequestMapping("/search")
|
public ResponseValue searchTextList(@RequestBody String json){
|
if(StringUtils.isEmpty(json)){
|
return ResponseValue.error("缺少查询条件:{'query':'demo'}");
|
}
|
String url = this.textSimilarProperties.getAiService();
|
logger.info("upload text url = " + url);
|
try {
|
SearchRequest request = JsonUtils.jsonStringToObject(json, SearchRequest.class);
|
List<TextSimilar> list = this.getSearchResultList(url, request);
|
if(list == null){
|
return ResponseValue.error("推荐系统未返回任何相似文本集合");
|
}
|
return ResponseValue.success(list);
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
private List<TextSimilar> getSearchResultList(String url, SearchRequest request){
|
ResponseEntity<String> entity = restTemplate.postForEntity(url + "/ai/text/search", request, String.class);
|
if(entity == null){
|
return null;
|
}
|
String jsonData = entity.getBody();
|
try {
|
List<TextSimilar> list = JsonUtils.jsonStringToObject(jsonData, List.class);
|
if(StringUtils.isEmptyList(list)){
|
logger.error("\"python返回文本相似集合结果为空!\"");
|
return null;
|
}
|
return list;
|
|
} catch (Exception e) {
|
logger.error("解析json结果错误:" + jsonData, e);
|
return null;
|
}
|
}
|
|
private String postUploadText(String url, UploadText request){
|
ResponseEntity<String> entity = restTemplate.postForEntity(url + "/ai/text/upload", request, String.class);
|
if(entity == null){
|
return null;
|
}
|
String jsonData = entity.getBody();
|
try {
|
Map<String, Object> map = JsonUtils.jsonStringToObject(jsonData, Map.class);
|
if(map == null || !map.containsKey("code")){
|
logger.error("python返回结果为空,或者没有code标志,无法判断调用加载视频成功!");
|
return "python返回结果为空,或者没有code标志,无法判断调用加载视频成功!";
|
}
|
String code = map.get("code").toString();
|
if(code.equals("0")){
|
logger.info("python upload_text_to_server() 处理成功! ");
|
return null;
|
}
|
// logger.warn("python notify_gather_once() 加载视频处理失败, batchId = " + batchId);
|
return map.get("msg") == null? "错误": map.get("msg").toString();
|
|
} catch (Exception e) {
|
logger.error("解析json结果错误:" + jsonData, e);
|
return e.getMessage();
|
}
|
}
|
}
|