|
@@ -1,11 +1,15 @@
|
|
package cn.com.qmth.am.service.impl;
|
|
package cn.com.qmth.am.service.impl;
|
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.math.RoundingMode;
|
|
|
|
+import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -19,6 +23,7 @@ import com.qmth.boot.core.retrofit.exception.RetrofitResponseError;
|
|
|
|
|
|
import cn.com.qmth.am.bean.ds.ChatReq;
|
|
import cn.com.qmth.am.bean.ds.ChatReq;
|
|
import cn.com.qmth.am.bean.ds.ChatResult;
|
|
import cn.com.qmth.am.bean.ds.ChatResult;
|
|
|
|
+import cn.com.qmth.am.bean.ds.DsChoice;
|
|
import cn.com.qmth.am.bean.ds.MarkingReq;
|
|
import cn.com.qmth.am.bean.ds.MarkingReq;
|
|
import cn.com.qmth.am.bean.ds.OcrMessage;
|
|
import cn.com.qmth.am.bean.ds.OcrMessage;
|
|
import cn.com.qmth.am.bean.ds.OcrReq;
|
|
import cn.com.qmth.am.bean.ds.OcrReq;
|
|
@@ -69,19 +74,63 @@ public class DsMarkingServiceImpl implements DsMarkingService {
|
|
return text;
|
|
return text;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // @Override
|
|
|
|
+ // public AutoScoreResult autoScore(AutoScoreRequest request) {
|
|
|
|
+ // String question = FreeMarkerUtil.getDsMarkingReq(request);
|
|
|
|
+ // MarkingReq dreq = new MarkingReq(sysProperty.getMarkingModel());
|
|
|
|
+ // dreq.addMsg(ChatRole.user, question);
|
|
|
|
+ // String res = marking(dreq);
|
|
|
|
+ // ChatResult result = JSONObject.parseObject(res, ChatResult.class);
|
|
|
|
+ // String text = result.getChoices().stream().filter(choice ->
|
|
|
|
+ // choice.getMessage().getRole() == ChatRole.assistant)
|
|
|
|
+ // .map(choice -> choice.getMessage().getContent()).findFirst().orElse("");
|
|
|
|
+ // try {
|
|
|
|
+ // AutoScoreResult scoreResult = new AutoScoreResult();
|
|
|
|
+ // String scoreStr = fomatStr(text);
|
|
|
|
+ // scoreResult.setTotalScore(Double.valueOf(scoreStr));
|
|
|
|
+ // return scoreResult;
|
|
|
|
+ // } catch (Exception e) {
|
|
|
|
+ // log.error(e.getMessage() + " | " + res);
|
|
|
|
+ // return null;
|
|
|
|
+ // }
|
|
|
|
+ // }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public AutoScoreResult autoScore(AutoScoreRequest request) {
|
|
public AutoScoreResult autoScore(AutoScoreRequest request) {
|
|
String question = FreeMarkerUtil.getDsMarkingReq(request);
|
|
String question = FreeMarkerUtil.getDsMarkingReq(request);
|
|
MarkingReq dreq = new MarkingReq(sysProperty.getMarkingModel());
|
|
MarkingReq dreq = new MarkingReq(sysProperty.getMarkingModel());
|
|
dreq.addMsg(ChatRole.user, question);
|
|
dreq.addMsg(ChatRole.user, question);
|
|
String res = marking(dreq);
|
|
String res = marking(dreq);
|
|
- ChatResult result = JSONObject.parseObject(res, ChatResult.class);
|
|
|
|
- String text = result.getChoices().stream().filter(choice -> choice.getMessage().getRole() == ChatRole.assistant)
|
|
|
|
- .map(choice -> choice.getMessage().getContent()).findFirst().orElse("");
|
|
|
|
|
|
+ DsChoice result = JSONObject.parseObject(res, DsChoice.class);
|
|
try {
|
|
try {
|
|
|
|
+ String text = result.getMessage().getContent();
|
|
AutoScoreResult scoreResult = new AutoScoreResult();
|
|
AutoScoreResult scoreResult = new AutoScoreResult();
|
|
- String scoreStr = fomatStr(text);
|
|
|
|
- scoreResult.setTotalScore(Double.valueOf(scoreStr));
|
|
|
|
|
|
+ // 依据总分与步骤分计算最大精度
|
|
|
|
+ int scale = Math.max(getDecimalPlaces(request.getIntervalScore()),
|
|
|
|
+ getDecimalPlaces(request.getTotalScore()));
|
|
|
|
+ int stepCount = request.getStandardAnswer().size();
|
|
|
|
+ String scoreStr = null;
|
|
|
|
+ if (stepCount > 1) {
|
|
|
|
+ scoreStr = fomatStrByRex(text);
|
|
|
|
+ } else {
|
|
|
|
+ scoreStr = fomatStr(text);
|
|
|
|
+ }
|
|
|
|
+ String[] scores = StringUtils.split(scoreStr, ",");
|
|
|
|
+ double[] scoreArray = new double[stepCount];
|
|
|
|
+ for (int i = 0; i < stepCount; i++) {
|
|
|
|
+ // 根据得分率与步骤总分计算实际得分,按最大精度保留小数位数
|
|
|
|
+ double score = BigDecimal
|
|
|
|
+ .valueOf(Math.min(Integer.parseInt(scores[i].trim()), 100)
|
|
|
|
+ * request.getStandardAnswer().get(i).getScore())
|
|
|
|
+ .divide(BigDecimal.valueOf(100), scale, RoundingMode.HALF_UP).doubleValue();
|
|
|
|
+ scoreArray[i] = score;
|
|
|
|
+ }
|
|
|
|
+ scoreResult.setStepScore(scoreArray);
|
|
|
|
+ scoreResult.setTotalScore(Arrays.stream(scoreArray).mapToObj(BigDecimal::new)
|
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
|
|
|
|
+ if ((scoreResult.getTotalScore() + "").length() > 4) {
|
|
|
|
+ log.error(" | " + res);
|
|
|
|
+ }
|
|
return scoreResult;
|
|
return scoreResult;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage() + " | " + res);
|
|
log.error(e.getMessage() + " | " + res);
|
|
@@ -89,6 +138,21 @@ public class DsMarkingServiceImpl implements DsMarkingService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private String fomatStrByRex(String scoreStr) {
|
|
|
|
+ int tag = scoreStr.lastIndexOf("</think>");
|
|
|
|
+ if (tag != -1) {
|
|
|
|
+ scoreStr = scoreStr.substring(tag).trim();
|
|
|
|
+ }
|
|
|
|
+ String ret = scoreStr.replaceAll(",", ",").replaceAll("。", "").replaceAll("[0-9]\\.", "");
|
|
|
|
+ Pattern pattern = Pattern.compile("(\\d{1,3}\\s*,\\s*)+\\d{1,3}");
|
|
|
|
+ Matcher matcher = pattern.matcher(ret);
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
+ return matcher.group();
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("返回格式错误");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private String fomatStr(String scoreStr) {
|
|
private String fomatStr(String scoreStr) {
|
|
scoreStr = scoreStr.substring(scoreStr.lastIndexOf("\n") + 1).trim();
|
|
scoreStr = scoreStr.substring(scoreStr.lastIndexOf("\n") + 1).trim();
|
|
String ret = scoreStr.replaceAll(",", ",").replaceAll("。", "").replaceAll(":", ":");
|
|
String ret = scoreStr.replaceAll(",", ",").replaceAll("。", "").replaceAll(":", ":");
|
|
@@ -111,6 +175,9 @@ public class DsMarkingServiceImpl implements DsMarkingService {
|
|
// throw new RuntimeException("返回格式错误");
|
|
// throw new RuntimeException("返回格式错误");
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
+ private int getDecimalPlaces(double value) {
|
|
|
|
+ return Math.max(0, BigDecimal.valueOf(value).stripTrailingZeros().scale());
|
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
String scoreStr = "</think>。\\n\\n\\n70,70,60\\n\\n评分结果2个3,29,110 \\n\\n考生的回答完全覆盖了所有的关键内容,逻辑清晰,术语使用准确";
|
|
String scoreStr = "</think>。\\n\\n\\n70,70,60\\n\\n评分结果2个3,29,110 \\n\\n考生的回答完全覆盖了所有的关键内容,逻辑清晰,术语使用准确";
|