情感分析
parent
30d4ce417a
commit
1ddd667fee
@ -0,0 +1,119 @@
|
||||
package com.sztzjy.marketing.controller.stu;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.sztzjy.marketing.annotation.AnonymousAccess;
|
||||
import com.sztzjy.marketing.util.ResultEntity;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
|
||||
@RestController
|
||||
@Api(tags = "python接口")
|
||||
@RequestMapping("/api/python")
|
||||
public class StuPythonController {
|
||||
|
||||
@PostMapping("/sentimentAnaly")
|
||||
@ApiOperation("情感分析")
|
||||
@AnonymousAccess
|
||||
public ResultEntity validatePythonCode(@RequestBody JSONObject text) {
|
||||
System.out.println(text);
|
||||
String testText = text.getString("text");
|
||||
String code = "from sentiment import Sentiment\n" +
|
||||
"\n" +
|
||||
"def calculate_sentiment(text: str) -> dict:\n" +
|
||||
" \"\"\"\n" +
|
||||
" Wrapper function to calculate sentiment of a text using SentimentAnalysis class.\n" +
|
||||
" \"\"\"\n" +
|
||||
" senti = Sentiment()\n" +
|
||||
" return senti.sentiment_calculate(text)\n" +
|
||||
"\n" +
|
||||
"\n" +
|
||||
"test_text = '有什么我真的很无助,我真的不知道说你什么好'\n" +
|
||||
"result = calculate_sentiment(test_text)\n" +
|
||||
"print(result)";
|
||||
|
||||
// 替换代码中的 test_text 为实际的 testText
|
||||
String updatedCode = code.replace("'有什么我真的很无助,我真的不知道说你什么好'", "'" + testText + "'");
|
||||
|
||||
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);
|
||||
|
||||
// 确保父目录存在
|
||||
File parentDir = file1.getParentFile();
|
||||
if (!parentDir.exists()) {
|
||||
System.out.println("Parent directory does not exist. Creating it.");
|
||||
if (!parentDir.mkdirs()) {
|
||||
System.out.println("Failed to create directories.");
|
||||
return new ResultEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建文件并写入内容
|
||||
if (!file1.exists()) {
|
||||
try {
|
||||
boolean fileCreated = file1.createNewFile();
|
||||
if (fileCreated) {
|
||||
System.out.println("File created successfully: " + tempPythonFile);
|
||||
} else {
|
||||
System.out.println("File already exists: " + tempPythonFile);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new ResultEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
try (PrintWriter out = new PrintWriter(file1)) {
|
||||
out.println(updatedCode);
|
||||
}
|
||||
System.out.println("文件路径为1111111111:"+tempPythonFile);
|
||||
|
||||
// 确认 Docker 命令
|
||||
String[] command = {"docker", "exec", "pyexe", "python", tempPythonFile};
|
||||
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
// 获取进程的输入流
|
||||
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
||||
|
||||
// 读取 Python 代码的输出
|
||||
StringBuilder output = new StringBuilder();
|
||||
String line;
|
||||
while ((line = inputStream.readLine()) != null) {
|
||||
output.append(line).append("\n");
|
||||
}
|
||||
|
||||
// 读取 Python 代码的错误信息
|
||||
StringBuilder errors = new StringBuilder();
|
||||
while ((line = errorStream.readLine()) != null) {
|
||||
errors.append(line).append("\n");
|
||||
}
|
||||
|
||||
// 等待进程执行完成
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode == 0) {
|
||||
System.out.println("Python code output:\n" + output.toString());
|
||||
return new ResultEntity(HttpStatus.OK, output.toString());
|
||||
} else {
|
||||
System.err.println("Error executing Python code:\n" + errors.toString());
|
||||
return new ResultEntity(HttpStatus.INTERNAL_SERVER_ERROR, errors.toString());
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return new ResultEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue