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

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

ModelPredictAllLinux.py的Python脚本文件
import sysimport jiebaimport joblibimport 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> ModelPredict(@RequestParam(name = "detail") String detail,                                                  @RequestParam(name = "targetName") String targetName) {        Result> result= new Result>();        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 list = new ArrayList();                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> ModelPredictAll(@RequestParam(name = "detail") String detail) {        Result> result= new Result>();        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 listAll = new ArrayList();            while ((line = in.readLine()) != null) {                System.out.println("输出的结果是:"+line);                Pattern pattern= Pattern.compile("'(.*?)'");                Matcher m = pattern.matcher(line);                ArrayList list = new ArrayList();                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> ModelPredictLinux(@RequestParam(name = "detail") String detail,                                                       @RequestParam(name = "targetName") String targetName) {        Result> result= new Result>();        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 list = new ArrayList();                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> ModelPredictAllLinux(@RequestParam(name = "detail") String detail) {        Result> result= new Result>();        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 listAll = new ArrayList();            while ((line = in.readLine()) != null) {                System.out.println("输出的结果是:"+line);                Pattern pattern= Pattern.compile("'(.*?)'");                Matcher m = pattern.matcher(line);                ArrayList list = new ArrayList();                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> modelTrain() {        Result> result= new Result>();        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 list = new ArrayList();            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> modelTrainLinux() {        Result> result= new Result>();        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 list = new ArrayList();            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接口的调用与测试

Original: https://www.cnblogs.com/Yanjy-OnlyOne/p/15793127.html
Author: The-Chosen-One
Title: Python推荐算法脚本与Springboot的java接口封装(非常重要)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6488/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总