|
@@ -253,6 +253,7 @@ public class PaperService {
|
|
|
return addWithQuestion(paper);
|
|
|
// return Result.ok(paper);
|
|
|
}
|
|
|
+
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Result uploadPaperTest(PaperVo paperVo, Long templateId, String url, byte[] data) {
|
|
|
Paper paper = init(paperVo, url);
|
|
@@ -283,62 +284,6 @@ public class PaperService {
|
|
|
return Result.ok(paper);
|
|
|
}
|
|
|
|
|
|
- private Map<String, List<XWPFParagraph>> initSections(Paper paper, String sectionPattern, String questionPattern, String picturePattern, List<XWPFParagraph> paragraphs) {
|
|
|
- //保证顺序
|
|
|
- Map<String, List<XWPFParagraph>> sections = new LinkedHashMap<>();
|
|
|
- String tag = "";
|
|
|
- boolean betweenSectionAndQuestion = false;
|
|
|
- logger.info("sectionPattern is {},questionPattern is {}",sectionPattern,questionPattern);
|
|
|
- StringBuilder material = new StringBuilder();
|
|
|
- int section = 0;
|
|
|
- List<Section> sectionList = new ArrayList<>();
|
|
|
- logger.info("initSections start");
|
|
|
- boolean hasFormula = false;
|
|
|
- for (XWPFParagraph paragraph : paragraphs) {
|
|
|
- String text = paragraph.getText();
|
|
|
- //此处应判断下是否有图片、文字或者公式
|
|
|
- if (WordUtil.notContent(paragraph, text)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- //下一个section
|
|
|
- if (text.matches(sectionPattern)) {
|
|
|
- logger.info("match section: text is {} sectionPattern is {}", text, sectionPattern);
|
|
|
- Matcher matcher = Pattern.compile("\\d+").matcher(text);
|
|
|
- //获取到section编号
|
|
|
- if (matcher.find()) {
|
|
|
- section = Integer.parseInt(matcher.group());
|
|
|
- }
|
|
|
- //1 2 3
|
|
|
- tag = String.valueOf(section);
|
|
|
- betweenSectionAndQuestion = true;
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (betweenSectionAndQuestion && text.matches(questionPattern)) {
|
|
|
- Section s = initSection(section, material.toString());
|
|
|
- sectionList.add(s);
|
|
|
- //清空背景材料
|
|
|
- material = new StringBuilder();
|
|
|
- betweenSectionAndQuestion = false;
|
|
|
- }
|
|
|
- //Session后面可能有背景材料,背景材料里面可能有图片和公式,这个放后面来处理
|
|
|
- if (betweenSectionAndQuestion) {
|
|
|
- //解析背景材料的图片和公式
|
|
|
- wordService.parsePicture(paragraph, picturePattern);
|
|
|
- hasFormula = wordService.parseFormula(paragraph);
|
|
|
- material.append("<p>").append(paragraph.getText()).append("</p>");
|
|
|
- }
|
|
|
- //此处可能是第一段
|
|
|
- if (StringUtils.isNotBlank(tag)) {
|
|
|
- List<XWPFParagraph> list = sections.getOrDefault(tag, new ArrayList<>());
|
|
|
- list.add(paragraph);
|
|
|
- sections.put(tag, list);
|
|
|
- }
|
|
|
- }
|
|
|
- paper.setSections(sectionList);
|
|
|
- logger.info("initSections end,sections num is {}", sectionList.size());
|
|
|
- return sections;
|
|
|
- }
|
|
|
-
|
|
|
private void parsePaper(Paper paper, PaperTemplate template, XWPFDocument document) {
|
|
|
String tagPattern = template.getSection();
|
|
|
String questionPattern = template.getQuestion();
|
|
@@ -346,10 +291,9 @@ public class PaperService {
|
|
|
List<XWPFParagraph> paragraphs = document.getParagraphs();
|
|
|
Assert.notEmpty(paragraphs, "未检测到段落,请检查上传的文档");
|
|
|
String name = paragraphs.get(0).getText();
|
|
|
- //TODO check name of paper
|
|
|
parsePaperName(paper, name, template);
|
|
|
//获取模块,根据模块,给段落分组
|
|
|
- Map<String, List<XWPFParagraph>> sections = initSections(paper, tagPattern, questionPattern, picturePattern, paragraphs);
|
|
|
+ Map<String, List<XWPFParagraph>> sections = initSections(paper, template, paragraphs);
|
|
|
Assert.notEmpty(sections, "未检测到模块,请检查上传的文档");
|
|
|
// List<String> topics = paperTemplateService.getTopic(template);
|
|
|
Map<String, String> topic = paperTemplateService.getMapTopic(template);
|
|
@@ -368,9 +312,9 @@ public class PaperService {
|
|
|
// Section current = ss.stream().filter(section -> s.contains(section.getNumber().toString())).findFirst().orElse(null);
|
|
|
Section current = sectionMap.get(s);
|
|
|
//根据题目标签,给题目分组
|
|
|
- Map<String, List<XWPFParagraph>> questions = initQuestions(current, questionPattern, ps);
|
|
|
+ Map<String, List<XWPFParagraph>> questions = initQuestions(current, template, ps);
|
|
|
current.setStartNumber(currentNumber);
|
|
|
- List<Question> list = parseQuestions(questions, current, template, topic, abilities);
|
|
|
+ List<Question> list = parseQuestions(questions, current, template, topic, paper.getUseAbility(),abilities);
|
|
|
current.setNums(list.size());
|
|
|
currentNumber = currentNumber + list.size();
|
|
|
qs.put(s, list);
|
|
@@ -400,11 +344,68 @@ public class PaperService {
|
|
|
sb.append(g).append("年级").append(first ? "上学期" : "下学期");
|
|
|
}
|
|
|
//取最后一个字为卷,一般为A、B卷
|
|
|
- sb.append(name.substring(name.length() -1)).append("卷");
|
|
|
+ sb.append(name.substring(name.length() - 1)).append("卷");
|
|
|
paper.setName(sb.toString());
|
|
|
}
|
|
|
|
|
|
- private List<Question> parseQuestions(Map<String, List<XWPFParagraph>> questions, Section section, PaperTemplate template, Map<String, String> topic, List<String> abilities) {
|
|
|
+ private Map<String, List<XWPFParagraph>> initSections(Paper paper, PaperTemplate template, List<XWPFParagraph> paragraphs) {
|
|
|
+ //保证顺序
|
|
|
+ Map<String, List<XWPFParagraph>> sections = new LinkedHashMap<>();
|
|
|
+ String tag = "";
|
|
|
+ boolean betweenSectionAndQuestion = false;
|
|
|
+ logger.info("sectionPattern is {},questionPattern is {}", template.getSection(), template.getQuestion());
|
|
|
+ StringBuilder material = new StringBuilder();
|
|
|
+ int section = 0;
|
|
|
+ List<Section> sectionList = new ArrayList<>();
|
|
|
+ logger.info("initSections start");
|
|
|
+ boolean hasFormula = false;
|
|
|
+ for (XWPFParagraph paragraph : paragraphs) {
|
|
|
+ String text = paragraph.getText();
|
|
|
+ //此处应判断下是否有图片、文字或者公式
|
|
|
+ if (WordUtil.notContent(paragraph, text)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //下一个section
|
|
|
+ if (text.matches(template.getSection())) {
|
|
|
+ logger.info("match section: text is {} sectionPattern is {}", text, template.getSection());
|
|
|
+ Matcher matcher = Pattern.compile("\\d+").matcher(text);
|
|
|
+ //获取到section编号
|
|
|
+ if (matcher.find()) {
|
|
|
+ section = Integer.parseInt(matcher.group());
|
|
|
+ }
|
|
|
+ //1 2 3
|
|
|
+ tag = String.valueOf(section);
|
|
|
+ betweenSectionAndQuestion = true;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (betweenSectionAndQuestion && text.matches(template.getQuestion())) {
|
|
|
+ Section s = initSection(section, material.toString());
|
|
|
+ sectionList.add(s);
|
|
|
+ //清空背景材料
|
|
|
+ material = new StringBuilder();
|
|
|
+ betweenSectionAndQuestion = false;
|
|
|
+ }
|
|
|
+ //Session后面可能有背景材料,背景材料里面可能有图片和公式,这个放后面来处理
|
|
|
+ if (betweenSectionAndQuestion) {
|
|
|
+ //解析背景材料的图片和公式
|
|
|
+ wordService.parseSubScript(paragraph);
|
|
|
+ wordService.parsePicture(paragraph, template.getPicturePattern());
|
|
|
+ hasFormula = wordService.parseFormula(paragraph);
|
|
|
+ material.append("<p>").append(paragraph.getText()).append("</p>");
|
|
|
+ }
|
|
|
+ //此处可能是第一段
|
|
|
+ if (StringUtils.isNotBlank(tag)) {
|
|
|
+ List<XWPFParagraph> list = sections.getOrDefault(tag, new ArrayList<>());
|
|
|
+ list.add(paragraph);
|
|
|
+ sections.put(tag, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ paper.setSections(sectionList);
|
|
|
+ logger.info("initSections end,sections num is {}", sectionList.size());
|
|
|
+ return sections;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Question> parseQuestions(Map<String, List<XWPFParagraph>> questions, Section section, PaperTemplate template, Map<String, String> topic, Boolean useAbility,List<String> abilities) {
|
|
|
List<Question> list = new ArrayList<>();
|
|
|
//问题合集
|
|
|
Pattern pattern = Pattern.compile("\\d+");
|
|
@@ -412,7 +413,7 @@ public class PaperService {
|
|
|
Matcher matcher = pattern.matcher(k);
|
|
|
if (k.matches(template.getQuestion()) && matcher.find()) {
|
|
|
String code = matcher.group();
|
|
|
- Question question = questionService.initQuestion(section.getNumber(), section.getStartNumber(), Integer.valueOf(code));
|
|
|
+ Question question = questionService.initQuestion(section.getNumber(), section.getStartNumber(), Integer.valueOf(code),useAbility);
|
|
|
wordService.parseQuestion(question, v, topic, template, abilities);
|
|
|
list.add(question);
|
|
|
}
|
|
@@ -420,7 +421,7 @@ public class PaperService {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- private Map<String, List<XWPFParagraph>> initQuestions(Section section, String questionPattern, List<XWPFParagraph> ps) {
|
|
|
+ private Map<String, List<XWPFParagraph>> initQuestions(Section section, PaperTemplate paperTemplate, List<XWPFParagraph> ps) {
|
|
|
Map<String, List<XWPFParagraph>> questions = new ConcurrentHashMap<>();
|
|
|
String tag = "";
|
|
|
for (XWPFParagraph paragraph : ps) {
|
|
@@ -428,9 +429,17 @@ public class PaperService {
|
|
|
if (WordUtil.notContent(paragraph, text)) {
|
|
|
continue;
|
|
|
}
|
|
|
+ //识别上下标
|
|
|
+ wordService.parseSubScript(paragraph);
|
|
|
+ //识别图片
|
|
|
+ wordService.parsePicture(paragraph, paperTemplate.getPicturePattern());
|
|
|
+ //识别公式
|
|
|
+ wordService.parseFormula(paragraph);
|
|
|
+ //重新获取text
|
|
|
+ text = paragraph.getText();
|
|
|
//下一个question
|
|
|
// questionPattern ^#[A-Z]#(.*)
|
|
|
- if (text.matches(questionPattern)) {
|
|
|
+ if (text.matches(paperTemplate.getQuestion())) {
|
|
|
tag = text;
|
|
|
}
|
|
|
logger.info("initQuestions,current tag is {},current text is {}", tag, text);
|