|
|
@@ -1,5 +1,6 @@
|
|
|
package cn.reghao.tnb.content.app.exam.service;
|
|
|
|
|
|
+import cn.reghao.jutil.jdk.db.Page;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
import cn.reghao.jutil.jdk.text.TextFile;
|
|
|
import cn.reghao.jutil.tool.id.SnowFlake;
|
|
|
@@ -7,15 +8,14 @@ import cn.reghao.tnb.content.app.ContentApplication;
|
|
|
import cn.reghao.tnb.content.app.exam.db.mapper.*;
|
|
|
import cn.reghao.tnb.content.app.exam.model.constant.QuestionType;
|
|
|
import cn.reghao.tnb.content.app.exam.model.dto.PaperAddDto;
|
|
|
-import cn.reghao.tnb.content.app.exam.model.po.Paper;
|
|
|
-import cn.reghao.tnb.content.app.exam.model.po.PaperQuestion;
|
|
|
-import cn.reghao.tnb.content.app.exam.model.po.Question;
|
|
|
-import cn.reghao.tnb.content.app.exam.model.po.QuestionOption;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.po.*;
|
|
|
+import cn.reghao.tnb.content.app.exam.model.query.QuestionQuery;
|
|
|
import com.google.gson.JsonElement;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import lombok.Getter;
|
|
|
import lombok.NoArgsConstructor;
|
|
|
import lombok.Setter;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
@@ -238,18 +238,245 @@ public class ExamTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void paperTest() {
|
|
|
- int paperId = 3;
|
|
|
- Paper paper = paperMapper.findByPaperId(paperId);
|
|
|
- List<PaperQuestion> paperQuestionList = paperQuestionMapper.findByPaperId(paperId);
|
|
|
- paperQuestionList.forEach(paperQuestion -> {
|
|
|
- long questionId = paperQuestion.getQuestionId();
|
|
|
- List<Question> questionList = questionMapper.findByQuestionIds(List.of(questionId));
|
|
|
- Question question = questionList.get(0);
|
|
|
- int questionType = question.getType();
|
|
|
- });
|
|
|
+ public void gaokaoQuestionTest() {
|
|
|
+ String baseDir = "/home/reghao/Downloads/gaokao/objective";
|
|
|
+ File dir = new File(baseDir);
|
|
|
+ for (File file : dir.listFiles()) {
|
|
|
+ parseQuestion(file.getAbsolutePath());
|
|
|
+ }
|
|
|
+
|
|
|
+ baseDir = "/home/reghao/Downloads/gaokao/subjective";
|
|
|
+ dir = new File(baseDir);
|
|
|
+ for (File file : dir.listFiles()) {
|
|
|
+ parseQuestion1(file.getAbsolutePath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void parseQuestion(String filePath) {
|
|
|
+ int subjectId = 1;
|
|
|
+ if (filePath.contains("Math")) {
|
|
|
+ subjectId = 2;
|
|
|
+ } else if (filePath.contains("English")) {
|
|
|
+ subjectId = 3;
|
|
|
+ } else if (filePath.contains("Physics")) {
|
|
|
+ subjectId = 4;
|
|
|
+ } else if (filePath.contains("Chemistry")) {
|
|
|
+ subjectId = 5;
|
|
|
+ } else if (filePath.contains("Biology")) {
|
|
|
+ subjectId = 6;
|
|
|
+ } else if (filePath.contains("Political")) {
|
|
|
+ subjectId = 7;
|
|
|
+ } else if (filePath.contains("History")) {
|
|
|
+ subjectId = 8;
|
|
|
+ } else if (filePath.contains("Geography")) {
|
|
|
+ subjectId = 9;
|
|
|
+ }
|
|
|
+
|
|
|
+ JsonElement jsonElement3 = JsonConverter.jsonFileToObject(new File(filePath), JsonElement.class);
|
|
|
+ JsonObject jsonObject = jsonElement3.getAsJsonObject();
|
|
|
+ for (JsonElement jsonElement0 : jsonObject.get("example").getAsJsonArray()) {
|
|
|
+ JsonObject jsonObject0 = jsonElement0.getAsJsonObject();
|
|
|
+ try {
|
|
|
+ int year = jsonObject0.get("year").getAsInt();
|
|
|
+ String category = jsonObject0.get("category").getAsString();
|
|
|
+ String question = jsonObject0.get("question").getAsString();
|
|
|
+ String questionContent0 = getHtml1(question);
|
|
|
+
|
|
|
+ String[] questionArr = question.split("\\n");
|
|
|
+ int len = questionArr.length;
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < len-4; i++) {
|
|
|
+ sb.append(questionArr[i]).append(System.lineSeparator());
|
|
|
+ }
|
|
|
+
|
|
|
+ String questionContent = sb.toString();
|
|
|
+ if (questionContent.isBlank()) {
|
|
|
+ System.out.printf("%s -> 单选题格式不正确\n", filePath);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ questionContent = getHtml1(questionContent);
|
|
|
+
|
|
|
+ List<String> questionOptions = new ArrayList<>();
|
|
|
+ for (int i = len-4; i < questionArr.length; i++) {
|
|
|
+ String questionOption = getHtml1(questionArr[i]);
|
|
|
+ questionOptions.add(questionOption);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> answers = new ArrayList<>();
|
|
|
+ for (JsonElement answerElement : jsonObject0.get("answer").getAsJsonArray()) {
|
|
|
+ answers.add(answerElement.getAsString());
|
|
|
+ }
|
|
|
+ int answerNum = 0;
|
|
|
+ if (answers.size() == 1) {
|
|
|
+ String answer = jsonObject0.get("answer").getAsString();
|
|
|
+ if (answer.equalsIgnoreCase("A")) {
|
|
|
+ answerNum = 1;
|
|
|
+ } else if (answer.equalsIgnoreCase("B")) {
|
|
|
+ answerNum = 2;
|
|
|
+ } else if (answer.equalsIgnoreCase("C")) {
|
|
|
+ answerNum = 3;
|
|
|
+ } else if (answer.equalsIgnoreCase("D")) {
|
|
|
+ answerNum = 4;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.printf("%s -> 单选题答案超过1个\n", filePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ String analysis = jsonObject0.get("analysis").getAsString();
|
|
|
+ analysis = getHtml1(analysis);
|
|
|
+ int score = jsonObject0.get("score").getAsInt();
|
|
|
+
|
|
|
+ String extra = String.format("%s 分 | %s | %s", score, year, category);
|
|
|
+ int questionType = QuestionType.QUESTION1.getCode();
|
|
|
+ long questionId = idGenerator.nextId();
|
|
|
+
|
|
|
+ //Question question0 = new Question(questionId, questionContent, questionType, extra);
|
|
|
+ Question question0 = new Question(questionId, questionContent0, questionType, extra);
|
|
|
+ question0.setAnalysis(analysis);
|
|
|
+ question0.setSubjectId(subjectId);
|
|
|
+ List<QuestionOption> questionOptions0 = new ArrayList<>();
|
|
|
+ for (int i = 0; i < questionOptions.size(); i++) {
|
|
|
+ String content0 = questionOptions.get(i);
|
|
|
+ int pos = i + 1;
|
|
|
+ boolean correct = pos == answerNum;
|
|
|
+ QuestionOption questionOption0 = new QuestionOption(questionId, content0, pos, correct);
|
|
|
+ questionOptions0.add(questionOption0);
|
|
|
+ }
|
|
|
+ question0.setQuestionOptions(questionOptions0);
|
|
|
+
|
|
|
+ questionMapper.save(question0);
|
|
|
+ questionOptionMapper.saveAll(questionOptions0);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof ArrayIndexOutOfBoundsException) {
|
|
|
+ System.out.printf("%s -> ArrayIndexOutOfBoundsException\n", filePath);
|
|
|
+ } else {
|
|
|
+ System.out.println(filePath);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void parseQuestion1(String filePath) {
|
|
|
+ int subjectId = 1;
|
|
|
+ if (filePath.contains("Math")) {
|
|
|
+ subjectId = 2;
|
|
|
+ } else if (filePath.contains("English")) {
|
|
|
+ subjectId = 3;
|
|
|
+ } else if (filePath.contains("Physics")) {
|
|
|
+ subjectId = 4;
|
|
|
+ } else if (filePath.contains("Chemistry")) {
|
|
|
+ subjectId = 5;
|
|
|
+ } else if (filePath.contains("Biology")) {
|
|
|
+ subjectId = 6;
|
|
|
+ } else if (filePath.contains("Political")) {
|
|
|
+ subjectId = 7;
|
|
|
+ } else if (filePath.contains("History")) {
|
|
|
+ subjectId = 8;
|
|
|
+ } else if (filePath.contains("Geography")) {
|
|
|
+ subjectId = 9;
|
|
|
+ }
|
|
|
+
|
|
|
+ JsonElement jsonElement3 = JsonConverter.jsonFileToObject(new File(filePath), JsonElement.class);
|
|
|
+ JsonObject jsonObject = jsonElement3.getAsJsonObject();
|
|
|
+ for (JsonElement jsonElement0 : jsonObject.get("example").getAsJsonArray()) {
|
|
|
+ JsonObject jsonObject0 = jsonElement0.getAsJsonObject();
|
|
|
+ try {
|
|
|
+ int year = jsonObject0.get("year").getAsInt();
|
|
|
+ String category = jsonObject0.get("category").getAsString();
|
|
|
+ String question = jsonObject0.get("question").getAsString();
|
|
|
+ String answer = jsonObject0.get("answer").getAsString();
|
|
|
+ String analysis = jsonObject0.get("analysis").getAsString();
|
|
|
+ int score = jsonObject0.get("score").getAsInt();
|
|
|
+
|
|
|
+ String extra = String.format("%s 分 | %s | %s", score, year, category);
|
|
|
+ int questionType = QuestionType.QUESTION6.getCode();
|
|
|
+ long questionId = idGenerator.nextId();
|
|
|
+
|
|
|
+ String questionContent0 = getHtml1(question);
|
|
|
+ Question question0 = new Question(questionId, questionContent0, questionType, extra);
|
|
|
+ question0.setAnalysis(analysis);
|
|
|
+ question0.setSubjectId(subjectId);
|
|
|
+ questionMapper.save(question0);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof ArrayIndexOutOfBoundsException) {
|
|
|
+ System.out.printf("%s -> ArrayIndexOutOfBoundsException\n", filePath);
|
|
|
+ } else {
|
|
|
+ System.out.println(filePath);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getHtml1(String content) {
|
|
|
+ String tag1 = "<span class=\"math-tex\">\\(";
|
|
|
+ String tag2 = "\\)</span>";
|
|
|
+ int count = 1;
|
|
|
+ while (content.contains("$")) {
|
|
|
+ if (count%2 != 0) {
|
|
|
+ content = StringUtils.replaceOnce(content, "$", tag1);
|
|
|
+ //question1 = question1.replaceFirst("\\$", tag1);
|
|
|
+ } else {
|
|
|
+ content = StringUtils.replaceOnce(content, "$", tag2);
|
|
|
+ //question1 = question1.replaceFirst("\\$", tag2);
|
|
|
+ }
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ content = content.replace("\n", "<br>");
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ PaperService paperService;
|
|
|
+ @Test
|
|
|
+ public void createPaperTest() {
|
|
|
+ int questionType = QuestionType.QUESTION1.getCode();
|
|
|
+ QuestionQuery questionQuery = new QuestionQuery.Builder()
|
|
|
+ .pageNumber(1)
|
|
|
+ .pageSize(50)
|
|
|
+ .type(questionType)
|
|
|
+ .build();
|
|
|
+ Page page = questionQuery.getPage();
|
|
|
+ List<Question> questions = questionMapper.findQuestionByPage(page, questionQuery);
|
|
|
+ List<PaperQuestion> paperQuestions = new ArrayList<>();
|
|
|
+ for (int i = 0; i < questions.size(); i++) {
|
|
|
+ Question question = questions.get(i);
|
|
|
+ int pos = i + 1;
|
|
|
+ int score = 1;
|
|
|
+ paperQuestions.add(new PaperQuestion(pos, question, score));
|
|
|
+ }
|
|
|
+
|
|
|
+ questionType = QuestionType.QUESTION6.getCode();
|
|
|
+ questionQuery = new QuestionQuery.Builder()
|
|
|
+ .pageNumber(1)
|
|
|
+ .pageSize(50)
|
|
|
+ .type(questionType)
|
|
|
+ .build();
|
|
|
+ page = questionQuery.getPage();
|
|
|
+ questions = questionMapper.findQuestionByPage(page, questionQuery);
|
|
|
+ paperQuestions = new ArrayList<>();
|
|
|
+ for (int i = 0; i < questions.size(); i++) {
|
|
|
+ Question question = questions.get(i);
|
|
|
+ int pos = i + 1;
|
|
|
+ int score = 1;
|
|
|
+ paperQuestions.add(new PaperQuestion(pos, question, score));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int totalScore = paperQuestions.stream().mapToInt(PaperQuestion::getScore).sum();
|
|
|
+ Double passScoreDouble = totalScore * 0.6;
|
|
|
+ int passScore = passScoreDouble.intValue();
|
|
|
|
|
|
PaperAddDto paperAddDto = new PaperAddDto();
|
|
|
+ paperAddDto.setExamName("Exam2025");
|
|
|
+ paperAddDto.setTotalScore(totalScore);
|
|
|
+ paperAddDto.setPassScore(passScore);
|
|
|
+ paperAddDto.setExamDuration(90);
|
|
|
+ paperAddDto.setStartTime(new Date());
|
|
|
+ paperAddDto.setEndTime(new Date());
|
|
|
+ paperAddDto.setQuestions(paperQuestions);
|
|
|
+ paperService.createExamPaper(paperAddDto);
|
|
|
}
|
|
|
|
|
|
private void getPaper(PaperAddDto paperAddDto) {
|
|
|
@@ -261,4 +488,28 @@ public class ExamTest {
|
|
|
return question;
|
|
|
}).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPaperAnswerTest() {
|
|
|
+ int paperId = 1;
|
|
|
+ int resultId = 1;
|
|
|
+ List<PaperAnswer> paperAnswerList = paperAnswerMapper.findByResultId(resultId);
|
|
|
+
|
|
|
+ Map<Long, List<Object>> map = new HashMap<>();
|
|
|
+ paperAnswerList.forEach(paperAnswer -> {
|
|
|
+ long questionId = paperAnswer.getQuestionId();
|
|
|
+ int type = paperAnswer.getQuestionType();
|
|
|
+ List<String> answers = List.of(paperAnswer.getAnswer());
|
|
|
+ List<Object> objectList;
|
|
|
+ if (type == QuestionType.QUESTION1.getCode() || type == QuestionType.QUESTION4.getCode()) {
|
|
|
+ objectList = answers.stream()
|
|
|
+ .map(val -> val.isBlank() ? -1 : Integer.parseInt(val))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ } else {
|
|
|
+ objectList = Arrays.asList(answers);
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put(questionId, objectList);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|