Python推薦演算法指令碼與Springboot的java介面封裝(非常重要)
阿新 • • 發佈:2022-01-12
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介面的呼叫與測試