Python推荐算法脚本与Springboot的java接口封装(非常重要)

1、推荐算法的Python脚本文件(.py文件):

ModelPredictAllLinux.py的Python脚本文件
import sys
import jieba
import joblib
import operator

#按照个数多少排序算法
def SortList(merge_predict_id_list):
set_a = list(set(merge_predict_id_list)) # 去重得到一个集合
count_set_a = {} # 存放元素和出现次数的字典,key为元素,value为出现次数
for item in set_a:
count_set_a[item] = merge_predict_id_list.count(item)
sorted_list_a = sorted(count_set_a.items(), key=operator.itemgetter(1))
result_id_list = [] # 存放最后的结果
for item in sorted_list_a[::-1]: # 按value值从大到小排序
result_id_list.append(item[0])
return result_id_list
#预测主方法
def ModelPredict(newQuestionText):
# 部署服务器存放机器学习模型的位置
ModelSavePath = "/home/biiuser/ImmediateAction/ModelSave/"
#对新工单的内容细节进行分词处理
newQuestionJieba = jieba.cut(newQuestionText)
newQuestionList = list(newQuestionJieba)
newQuestionJiebaString = ' '.join(newQuestionList)
tfChar = joblib.load("/home/biiuser/ImmediateAction/ModelSave/TFChar.pkl")
textData = tfChar.transform([newQuestionJiebaString])
newQuestionVec = textData.toarray()

# 正常加载各个模型模型
TargetNameList=["IS_RETURN","ORG_CODE","RETURN_REASON_CODE","RETURN_COMPANY_CODE","RETURN_REASON_CODE","CATEGORY_ID"]
TargetNameList = ["IS_RETURN"]
for i in range(len(TargetNameList)):
TargetName=TargetNameList[i]
log_OVO = joblib.load(ModelSavePath + TargetName + "_LogOVO_Model.pkl")
svm = joblib.load(ModelSavePath + TargetName + "_SVM_Model.pkl")
rf = joblib.load(ModelSavePath + TargetName + "_Bagging_Model.pkl")
xgboost = joblib.load(ModelSavePath + TargetName + "_Boosting_Model.pkl")

# 对预测结果进行合并去重处理
if TargetName == "IS_RETURN":
merge_predict_id_list = [xgboost.predict(newQuestionVec)[0]]
else:
merge_predict_id_list = [xgboost.predict(newQuestionVec)[0], rf.predict(newQuestionVec)[0],
svm.predict(newQuestionVec)[0],
log_OVO.predict(newQuestionVec)[0]]
result_id_list=SortList(merge_predict_id_list)
# 返回打印的结果
print(result_id_list)

if __name__ == '__main__':
ModelPredict(sys.argv[1]) #使用sys.argv实现java输入参数到Python脚本所需输入参数的传参
  # for i in range(1, len(sys.argv)):
  # TargetName = sys.argv[i]
  # ModelTrain(TargetName)

2、springboot后端项目的java接口封装
package com.test.demo.controller;

import com.test.demo.annotation.AutoLog;
import com.test.demo.entity.CSVUtils;
import com.test.demo.entity.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/WorkOrderRecommendate")
public class WorkOrderRecommendateController {

@AutoLog(value = "工单特定目标推荐算法调用接口(本地测试接口)")
@ApiOperation(value = "工单特定目标推荐算法调用接口", notes = "工单党支部推荐算法调用接口")
@GetMapping(value ="/ModelPredict")
public Result<ArrayList<String>> ModelPredict(@RequestParam(name = "detail") String detail,
@RequestParam(name = "targetName") String targetName) {
Result<ArrayList<String>> result= new Result<ArrayList<String>>();
try {
long startTime=System.currentTimeMillis(); //获取开始时间
String[] args1=new String[]{"python","./src/main/resources/ModelPredictLinux.py",detail,targetName};
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
String line;
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
Pattern pattern= Pattern.compile("'(.*?)'");
Matcher m = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
while (m.find()) {
list.add(m.group().trim().replace("'","")+"");
}
result.setResult(list);
}
in.close();
pr.waitFor();
System.out.println("end");
long endTime=System.currentTimeMillis(); //获取结束时间
result.setMessage("推荐算法的运行时间为:"+(endTime-startTime)/1000+"s");
} catch (Exception e) {
System.out.printf("接口调用失败");
result.setMessage(e.getMessage());
}
return result;
}

@AutoLog(value = "工单综合推荐算法调用接口(本地测试接口)")
@ApiOperation(value = "工单综合推荐算法调用接口", notes = "工单综合推荐算法调用接口")
@GetMapping(value ="/ModelPredictAll")
public Result<ArrayList<ArrayList>> ModelPredictAll(@RequestParam(name = "detail") String detail) {
Result<ArrayList<ArrayList>> result= new Result<ArrayList<ArrayList>>();
try {
long startTime=System.currentTimeMillis(); //获取开始时间
String[] args1=new String[]{"python","./src/main/resources/ModelPredictAllLinux.py",detail};
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
String line;
ArrayList<ArrayList> listAll = new ArrayList<ArrayList>();
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
Pattern pattern= Pattern.compile("'(.*?)'");
Matcher m = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
while (m.find()) {
list.add(m.group().trim().replace("'","")+"");
}
listAll.add(list);
result.setResult(listAll);
}
in.close();
pr.waitFor();
System.out.println("end");
long endTime=System.currentTimeMillis(); //获取结束时间
result.setMessage("推荐算法的运行时间为:"+(endTime-startTime)/1000+"s");
} catch (Exception e) {
System.out.printf("接口调用失败");
result.setMessage(e.getMessage());
}
return result;
}

@AutoLog(value = "工单特定目标推荐算法调用接口(服务器部署)")
@ApiOperation(value = "工单特定目标推荐算法调用接口", notes = "工单特定目标推荐算法调用接口")
@GetMapping(value ="/ModelPredictLinux")
public Result<ArrayList<String>> ModelPredictLinux(@RequestParam(name = "detail") String detail,
@RequestParam(name = "targetName") String targetName) {
Result<ArrayList<String>> result= new Result<ArrayList<String>>();
try {
long startTime=System.currentTimeMillis(); //获取开始时间
String[] args1=new String[]{"python3","/home/biiuser/ImmediateAction/PythonModel/ModelPredictLinux.py",detail,targetName};
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
Pattern pattern= Pattern.compile("'(.*?)'");
Matcher m = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
while (m.find()) {
list.add(m.group().trim().replace("'","")+"");
}
result.setResult(list);
}
in.close();
pr.waitFor();
System.out.println("end");
long endTime=System.currentTimeMillis(); //获取结束时间
result.setMessage("推荐算法的运行时间为:"+(endTime-startTime)/1000+"s");
} catch (Exception e) {
System.out.printf("接口调用失败");
result.setMessage(e.getMessage());
}
return result;
}

@AutoLog(value = "工单综合推荐调用接口(服务器部署)")
@ApiOperation(value = "工单综合推荐接口", notes = "工单综合推荐接口")
@GetMapping(value ="/ModelPredictAllLinux")
public Result<ArrayList<ArrayList>> ModelPredictAllLinux(@RequestParam(name = "detail") String detail) {
Result<ArrayList<ArrayList>> result= new Result<ArrayList<ArrayList>>();
try {
long startTime=System.currentTimeMillis(); //获取开始时间
String[] args1=new String[]{"python3","/home/biiuser/ImmediateAction/PythonModel/ModelPredictAllLinux.py",detail}; #部署服务器上Python脚本的存放文件夹,需注意部署服务器上必须安装Python和推荐算法的运行环境
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"UTF-8"));
String line;
ArrayList<ArrayList> listAll = new ArrayList<ArrayList>();
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
Pattern pattern= Pattern.compile("'(.*?)'");
Matcher m = pattern.matcher(line);
ArrayList<String> list = new ArrayList<String>();
while (m.find()) {
list.add(m.group().trim().replace("'","")+""); //将Python脚本print输出的字符串列表进行逐个选取,实现Python脚本输出结果到java接口输出结果的传参
}
listAll.add(list);
result.setResult(listAll);
}
in.close();
pr.waitFor();
System.out.println("end");
long endTime=System.currentTimeMillis(); //获取结束时间
result.setMessage("推荐算法的运行时间为:"+(endTime-startTime)/1000+"s");
} catch (Exception e) {
System.out.printf("接口调用失败");
result.setMessage(e.getMessage());
}
return result;
}

@AutoLog(value = "推荐算法模型训练接口(本地测试)")
@ApiOperation(value = "推荐算法模型训练接口", notes = "推荐算法模型训练接口")
@GetMapping(value ="/ModelTrain")
public Result<ArrayList<String>> modelTrain() {
Result<ArrayList<String>> result= new Result<ArrayList<String>>();
try {
Calendar c_begin = Calendar.getInstance();//可以对每个时间域单独修改
int year = c_begin.get(Calendar.YEAR);
int month = c_begin.get(Calendar.MONTH);
int date = c_begin.get(Calendar.DATE);
int hour = c_begin.get(Calendar.HOUR_OF_DAY);
int minute = c_begin.get(Calendar.MINUTE);
int second = c_begin.get(Calendar.SECOND);
System.out.println("开始时间为:"+year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

System.out.println("startTraining...");
long startTime=System.currentTimeMillis(); //获取模型开始训练的时间
String[] args1=new String[]{"python","./src/main/resources/ModelTrainLinux.py"};
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
String line;
ArrayList<String> list = new ArrayList<String>();
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
list.add(line);
result.setResult(list);
}
in.close();
pr.waitFor();
System.out.println("endTraining...");
long endTime=System.currentTimeMillis(); //获取结束时间
Calendar c_end = Calendar.getInstance();//可以对每个时间域单独修改
int end_year = c_end.get(Calendar.YEAR);
int end_month = c_end.get(Calendar.MONTH);
int end_date = c_end.get(Calendar.DATE);
int end_hour = c_end.get(Calendar.HOUR_OF_DAY);
int end_minute = c_end.get(Calendar.MINUTE);
int end_second = c_end.get(Calendar.SECOND);
System.out.println("结束时间为:"+end_year + "/" + end_month + "/" + end_date + " " +end_hour + ":" +end_minute + ":" + end_second);
result.setMessage("推荐算法训练的时间为:"+(endTime-startTime)/60000+"min");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

@AutoLog(value = "推荐算法模型训练接口(服务器部署)")
@ApiOperation(value = "推荐算法模型训练接口", notes = "推荐算法模型训练接口")
@GetMapping(value ="/ModelTrainLinux")
public Result<ArrayList<String>> modelTrainLinux() {
Result<ArrayList<String>> result= new Result<ArrayList<String>>();
try {
Calendar c_begin = Calendar.getInstance();//可以对每个时间域单独修改
int year = c_begin.get(Calendar.YEAR);
int month = c_begin.get(Calendar.MONTH);
int date = c_begin.get(Calendar.DATE);
int hour = c_begin.get(Calendar.HOUR_OF_DAY);
int minute = c_begin.get(Calendar.MINUTE);
int second = c_begin.get(Calendar.SECOND);
System.out.println("开始时间为:"+year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
System.out.println("startTraining...");
long startTime=System.currentTimeMillis();
//获取模型开始训练的时间
String[] args1=new String[]{"python3","/home/biiuser/ImmediateAction/PythonModel/ModelTrainLinux.py"};
Process pr=Runtime.getRuntime().exec(args1);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
String line;
ArrayList<String> list = new ArrayList<String>();
while ((line = in.readLine()) != null) {
System.out.println("输出的结果是:"+line);
list.add(line);
result.setResult(list);
}
in.close();
pr.waitFor();
System.out.println("endTraining...");
long endTime=System.currentTimeMillis(); //获取结束时间
Calendar c_end = Calendar.getInstance();//可以对每个时间域单独修改
int end_year = c_end.get(Calendar.YEAR);
int end_month = c_end.get(Calendar.MONTH);
int end_date = c_end.get(Calendar.DATE);
int end_hour = c_end.get(Calendar.HOUR_OF_DAY);
int end_minute = c_end.get(Calendar.MINUTE);
int end_second = c_end.get(Calendar.SECOND);
System.out.println("结束时间为:"+end_year + "/" + end_month + "/" + end_date + " " +end_hour + ":" +end_minute + ":" + end_second);
result.setMessage("推荐算法训练的时间为:"+(endTime-startTime)/60000+"min");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

@AutoLog(value = "导出CSV文件类")
@ApiOperation(value = "导出CSV文件类", notes = "导出SCV类")
@GetMapping(value ="/testCSV")
public void TestCSV(){
System.err.printf("接口调用成功!");
CSVUtils.writeCSV();
return ;
}
}
3、实现java接口的调用与测试

 

posted @ 2022-01-12 16:18  The-Chosen-One  阅读(2716)  评论(1编辑  收藏  举报