|
@@ -1,27 +1,27 @@
|
|
|
-package com.yaoxiang.diagnosis.service;
|
|
|
+package com.yaoxiang.diagnosis.issue;
|
|
|
|
|
|
import com.yaoxiang.diagnosis.config.Constants;
|
|
|
import com.yaoxiang.diagnosis.entity.Issue;
|
|
|
import com.yaoxiang.diagnosis.entity.IssueOption;
|
|
|
import com.yaoxiang.diagnosis.file.FileService;
|
|
|
import com.yaoxiang.diagnosis.word.WordUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+@Slf4j
|
|
|
public abstract class IssueParseAdapter implements IssueParseHandler {
|
|
|
|
|
|
public static final String QUESTION_PATTERN = "#Q(\\d+)#";
|
|
|
public static final String ANSWER_PATTERN = "#Ans#";
|
|
|
public static final String OPTION_PATTERN = "^#[A-Z]#(.*)";
|
|
|
public static final String OPTION_REPLACE_PATTERN = "#[A-Z]#";
|
|
|
- public static final String MIND_OPTION = "#M(\\d+)#";
|
|
|
+ public static final String LITE_QUESTION_PATTERN = "#q(\\d+)#";
|
|
|
public static final String SECTION_PATTERN = "#Section (\\d+)#";
|
|
|
public static final String TAG_PATTERN = "#Tag#";
|
|
|
public static final String PICTURE_PATTERN = "#(Small|small|Middle|middle|Large|large)#";
|
|
@@ -30,14 +30,72 @@ public abstract class IssueParseAdapter implements IssueParseHandler {
|
|
|
public static final String SUFFIX_PATTERN = "#";
|
|
|
|
|
|
public static final List<String> TOPIC1 = Arrays.asList(QUESTION_PATTERN, ANSWER_PATTERN, TAG_PATTERN);
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(IssueParseAdapter.class);
|
|
|
+ public static final List<String> TOPIC2 = Arrays.asList(QUESTION_PATTERN, ANSWER_PATTERN);
|
|
|
+ public static final List<String> TOPIC3 = Arrays.asList(LITE_QUESTION_PATTERN, ANSWER_PATTERN);
|
|
|
protected final Pattern issuePattern = Pattern.compile("\\d+");
|
|
|
- @Resource
|
|
|
- private FileService fileService;
|
|
|
|
|
|
- abstract void parseQuestion(Issue issue, String code, List<XWPFParagraph> ps);
|
|
|
+ private final FileService fileService;
|
|
|
|
|
|
- public void parseAnswer(Issue issue, List<XWPFParagraph> list) {
|
|
|
+ protected IssueParseAdapter(FileService fileService) {
|
|
|
+ this.fileService = fileService;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void parseQuestion(BaseIssue issue, String code, List<XWPFParagraph> ps) {
|
|
|
+ List<IssueOption> options = new ArrayList<>();
|
|
|
+ int i = 0;
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
+ boolean hasFormula = false;
|
|
|
+ String index = "";
|
|
|
+ Map<String, List<XWPFParagraph>> indexMap = new LinkedHashMap<>();
|
|
|
+ boolean isQuestion = true;
|
|
|
+ for (XWPFParagraph p : ps) {
|
|
|
+
|
|
|
+ String text = p.getText();
|
|
|
+ if (WordUtil.notContent(p, text)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ text = text.replaceAll(PICTURE_PATTERN, "");
|
|
|
+ if (text.matches(OPTION_PATTERN)) {
|
|
|
+ isQuestion = false;
|
|
|
+ index = text;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isQuestion) {
|
|
|
+ content.append("<p>").append(text).append("</p>");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<XWPFParagraph> list = indexMap.getOrDefault(index, new ArrayList<>());
|
|
|
+ list.add(p);
|
|
|
+ indexMap.put(index, list);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Map.Entry<String, List<XWPFParagraph>> entry : indexMap.entrySet()) {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (XWPFParagraph p : entry.getValue()) {
|
|
|
+ builder.append("<p>").append(p.getText()).append("</p>");
|
|
|
+ }
|
|
|
+ String text = builder.toString();
|
|
|
+ IssueOption option = initOption(i++, issue.getSection(), text);
|
|
|
+ String optionNoContent = String.format("parseQuestion,题号为%s的题目选项 %s 未检测到内容,题目内容为“%s”,请检查选项结构", code, text, content);
|
|
|
+ Assert.hasText(option.getContent(), optionNoContent);
|
|
|
+ options.add(option);
|
|
|
+ }
|
|
|
+
|
|
|
+ String noContent = String.format("parseQuestion,题号为 %s 的题目未检测到题干,题目内容为 “%s” ,请检查", code, content);
|
|
|
+ String noOptions = String.format("parseQuestion,题号为 %s 的题目未检测到选项,题目内容为 “%s” ,请检查", code, content);
|
|
|
+ String lostOptions = String.format("parseQuestion,题号为 %s 的题目检测到选项数量为 %s ,题目内容为 “%s” ,请检查选项换行情况", code, options.size(), content);
|
|
|
+ Assert.hasText(content.toString(), noContent);
|
|
|
+ Assert.notEmpty(options, noOptions);
|
|
|
+
|
|
|
+ Assert.isTrue(options.size() >= Constants.OPTION_NUMS, lostOptions);
|
|
|
+ issue.setContent(content.toString());
|
|
|
+ issue.setOptions(options);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void parseAnswer(BaseIssue issue, List<XWPFParagraph> list) {
|
|
|
for (XWPFParagraph p : list) {
|
|
|
String text = p.getText();
|
|
|
if (StringUtils.isBlank(text)) {
|
|
@@ -79,11 +137,11 @@ public abstract class IssueParseAdapter implements IssueParseHandler {
|
|
|
if (text.matches(QUESTION_PATTERN)) {
|
|
|
tag = text;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (tag.isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
- logger.info("initQuestions,current tag is {},current text is {}", tag, text);
|
|
|
+ log.info("initQuestions,current tag is {},current text is {}", tag, text);
|
|
|
List<XWPFParagraph> list = result.getOrDefault(tag, new ArrayList<>());
|
|
|
list.add(paragraph);
|
|
|
result.put(tag, list);
|
|
@@ -117,11 +175,11 @@ public abstract class IssueParseAdapter implements IssueParseHandler {
|
|
|
|
|
|
tag = QUESTION_PATTERN;
|
|
|
}
|
|
|
- logger.info("initGroup, current Tag is {},current Text is {}", tag, text);
|
|
|
+ log.info("initGroup, current Tag is {},current Text is {}", tag, text);
|
|
|
List<XWPFParagraph> ps = map.get(tag);
|
|
|
|
|
|
if (ps == null) {
|
|
|
- logger.error("unknown tag {}", tag);
|
|
|
+ log.error("unknown tag {}", tag);
|
|
|
continue;
|
|
|
}
|
|
|
ps.add(p);
|
|
@@ -150,16 +208,6 @@ public abstract class IssueParseAdapter implements IssueParseHandler {
|
|
|
return option;
|
|
|
}
|
|
|
|
|
|
- public Map<String, String> getMapTopic() {
|
|
|
- Map<String, String> topic = new HashMap<>();
|
|
|
-
|
|
|
- topic.put(Constants.TOPIC_QUESTION, QUESTION_PATTERN);
|
|
|
- topic.put(Constants.TOPIC_ANSWER, ANSWER_PATTERN);
|
|
|
- topic.put(Constants.TOPIC_TAG, TAG_PATTERN);
|
|
|
-
|
|
|
- return topic;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public List<Issue> parse(List<XWPFParagraph> list, Long subjectId, String grade, Long chapterId) {
|
|
|
Map<String, List<XWPFParagraph>> map = split(list);
|