diff --git a/src/main/java/com/sztzjy/marketing/controller/stu/StuPythonController.java b/src/main/java/com/sztzjy/marketing/controller/stu/StuPythonController.java index ce9079c..7fd9df3 100644 --- a/src/main/java/com/sztzjy/marketing/controller/stu/StuPythonController.java +++ b/src/main/java/com/sztzjy/marketing/controller/stu/StuPythonController.java @@ -1,9 +1,11 @@ package com.sztzjy.marketing.controller.stu; import cn.hutool.core.util.IdUtil; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.sztzjy.marketing.annotation.AnonymousAccess; import com.sztzjy.marketing.entity.dto.SentimentAnalyDTO; +import com.sztzjy.marketing.qianfan.util.Json; import com.sztzjy.marketing.util.ResultEntity; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -14,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.*; +import java.util.List; +import java.util.Map; @RestController @@ -25,7 +29,7 @@ public class StuPythonController { @ApiOperation("情感分析") @AnonymousAccess public ResultEntity validatePythonCode(@RequestBody JSONObject text) { - System.out.println(text); + // System.out.println(text); String testText = text.getString("text"); String code = "from sentiment import Sentiment\n" + "\n" + @@ -44,11 +48,11 @@ public class StuPythonController { // 替换代码中的 test_text 为实际的 testText String updatedCode = code.replace("'有什么我真的很无助,我真的不知道说你什么好'", "'" + testText + "'"); - System.out.println(updatedCode); + //System.out.println(updatedCode); try { String s = IdUtil.simpleUUID(); String tempPythonFile = "/usr/local/tianzeProject/digitalMarketing/cnsenti/cnsenti/" + s + ".py"; - System.out.println("文件是多少多少:"+tempPythonFile); + File file1 = new File(tempPythonFile); // 确保父目录存在 @@ -79,7 +83,7 @@ public class StuPythonController { try (PrintWriter out = new PrintWriter(file1)) { out.println(updatedCode); } - System.out.println("文件路径为1111111111:"+tempPythonFile); + // 确认 Docker 命令 String[] command = {"docker", "exec", "pyexe", "python", tempPythonFile}; @@ -106,21 +110,28 @@ public class StuPythonController { // 等待进程执行完成 int exitCode = process.waitFor(); if (exitCode == 0) { - JSONObject jsonObject = new JSONObject(Boolean.parseBoolean(output.toString())); - - // 访问JSON对象的属性 - String pos = jsonObject.getString("pos"); - String neg = jsonObject.getString("neg"); + String outputStr = output.toString().trim(); + // 使用正则表达式替换 np.float64(...) 为其内部的值 + if (outputStr.contains("np.float64")){ + outputStr = outputStr.replaceAll("np\\.float64\\(([0-9\\.]+)\\)", "$1"); + }else if (outputStr.contains("np.int64")){ + outputStr = outputStr.replaceAll("np\\.int64\\(([0-9\\.]+)\\)", "$1"); + } - SentimentAnalyDTO sentimentAnalyDTO=new SentimentAnalyDTO(); - sentimentAnalyDTO.setPos(pos); - sentimentAnalyDTO.setNeg(neg); + Map parse = JSONObject.parseObject(outputStr, Map.class); + Object posStr = parse.get("pos"); + Object negStr = parse.get("neg"); + SentimentAnalyDTO sentimentAnalyDTO = new SentimentAnalyDTO(); + sentimentAnalyDTO.setPos(posStr.toString()); + sentimentAnalyDTO.setNeg(negStr.toString()); - System.out.println("Python code output:\n" + output.toString()); + // System.out.println("Python code output:\n" + output.toString()); return new ResultEntity(HttpStatus.OK, sentimentAnalyDTO); + + } else { - System.err.println("Error executing Python code:\n" + errors.toString()); + // System.err.println("Error executing Python code:\n" + errors.toString()); return new ResultEntity(HttpStatus.INTERNAL_SERVER_ERROR, errors.toString()); } } catch (IOException | InterruptedException e) { @@ -128,4 +139,6 @@ public class StuPythonController { return new ResultEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } + + }