|
@@ -25,6 +25,8 @@ import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
@@ -64,7 +66,7 @@ public class WordService {
|
|
|
logger.info("parsing question,pid={},section={},number={}", q.getPid(), q.getSection(), q.getNumber());
|
|
|
Map<String, List<XWPFParagraph>> group = initGroup(ps, topic, template);
|
|
|
|
|
|
- parseQuestion(q, group.get(topic.get(Constants.TOPIC_QUESTION)), template.getOptionPattern());
|
|
|
+ parseQuestion(q, group.get(topic.get(Constants.TOPIC_QUESTION)), template.getOptionPattern(), template.getPicturePattern());
|
|
|
parseAnswer(q, group.get(topic.get(Constants.TOPIC_ANSWER)));
|
|
|
parseTag(q, group.get(topic.get(Constants.TOPIC_TAG)));
|
|
|
parseAbility(q, group.get(topic.get(Constants.TOPIC_ABILITY)), abilities);
|
|
@@ -75,7 +77,7 @@ public class WordService {
|
|
|
private void parseQuestion(Question q, Map<String, List<XWPFParagraph>> group) {
|
|
|
logger.info("parsing question,pid={},section={},number={}", q.getPid(), q.getSection(), q.getNumber());
|
|
|
String[] a = {"A1", "A2", "A3", "A4", "A5"};
|
|
|
- parseQuestion(q, group.get(Constants.TOPIC_QUESTION), "^[A-Z]\\.(.*)");
|
|
|
+ parseQuestion(q, group.get(Constants.TOPIC_QUESTION), "^[A-Z]\\.(.*)", "^#(Small|Middle|Large)#");
|
|
|
parseAnswer(q, group.get(Constants.TOPIC_ANSWER));
|
|
|
parseTag(q, group.get(Constants.TOPIC_TAG));
|
|
|
parseAbility(q, group.get(Constants.TOPIC_ABILITY), Arrays.asList(a));
|
|
@@ -84,29 +86,31 @@ public class WordService {
|
|
|
logger.info("parse question finish,pid={},section={},number={}", q.getPid(), q.getSection(), q.getNumber());
|
|
|
}
|
|
|
|
|
|
- private void parseQuestion(Question question, List<XWPFParagraph> ps, String optionPattern) {
|
|
|
+ private void parseQuestion(Question question, List<XWPFParagraph> ps, String optionPattern, String picturePattern) {
|
|
|
List<QuestionOption> options = new ArrayList<>();
|
|
|
int i = 0;
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
boolean hasFormula = false;
|
|
|
for (XWPFParagraph p : ps) {
|
|
|
- parsePicture(p);
|
|
|
+ parsePicture(p, picturePattern);
|
|
|
|
|
|
hasFormula = parseFormula(p);
|
|
|
String text = p.getText();
|
|
|
+
|
|
|
+ text = text.replaceAll(picturePattern, "");
|
|
|
if (text.matches(optionPattern)) {
|
|
|
|
|
|
QuestionOption option = initOption(i++, text);
|
|
|
- String optionNoContent = String.format("题号为%s的题目选项 %s 未检测到内容,题目内容为“%s”,请检查", question.getNumber(), text.charAt(0), content);
|
|
|
+ String optionNoContent = String.format("parseQuestion,题号为%s的题目选项 %s 未检测到内容,题目内容为“%s”,请检查", question.getNumber(), text.charAt(0), content);
|
|
|
Assert.hasText(option.getContent(), optionNoContent);
|
|
|
options.add(option);
|
|
|
} else {
|
|
|
content.append("<p>").append(text).append("</p>");
|
|
|
}
|
|
|
}
|
|
|
- String noContent = String.format("题号为 %s 的题目未检测到题干,题目内容为 “%s” ,请检查", question.getNumber(), content);
|
|
|
- String noOptions = String.format("题号为 %s 的题目未检测到选项,题目内容为 “%s” ,请检查", question.getNumber(), content);
|
|
|
- String lostOptions = String.format("题号为 %s 的题目检测到选项数量为 %s ,题目内容为 “%s” ,请检查", question.getNumber(), options.size(), content);
|
|
|
+ String noContent = String.format("parseQuestion,题号为 %s 的题目未检测到题干,题目内容为 “%s” ,请检查", question.getNumber(), content);
|
|
|
+ String noOptions = String.format("parseQuestion,题号为 %s 的题目未检测到选项,题目内容为 “%s” ,请检查", question.getNumber(), content);
|
|
|
+ String lostOptions = String.format("parseQuestion,题号为 %s 的题目检测到选项数量为 %s ,题目内容为 “%s” ,请检查", question.getNumber(), options.size(), content);
|
|
|
Assert.hasText(content.toString(), noContent);
|
|
|
Assert.notEmpty(options, noOptions);
|
|
|
|
|
@@ -145,11 +149,12 @@ public class WordService {
|
|
|
Map<String, Double> json = new HashMap<>();
|
|
|
|
|
|
|
|
|
- json.put("A0", 1d);
|
|
|
- AbilityScore a0 = new AbilityScore("A0", "", 2L, question.getPid(), 0L, 0d);
|
|
|
- list.add(a0);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
for (XWPFParagraph p : ps) {
|
|
|
String text = p.getText();
|
|
|
+ logger.info("parseAbility,section {} ,question number={},ability {} ", question.getSection(), question.getNumber(), text);
|
|
|
String[] ss = text.split("=");
|
|
|
String abilityCode = ss[0];
|
|
|
double score = Double.parseDouble(ss[1]);
|
|
@@ -219,7 +224,13 @@ public class WordService {
|
|
|
if (WordUtil.notContent(p, text)) {
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ if (text.matches(template.getOptionPattern()) || text.matches(template.getPicturePattern())) {
|
|
|
+ List<XWPFParagraph> ps = map.get(template.getQuestion());
|
|
|
+ ps.add(p);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
if (text.startsWith(template.getPrefix()) && text.endsWith(template.getSuffix())) {
|
|
|
tag = text;
|
|
|
continue;
|
|
@@ -229,7 +240,7 @@ public class WordService {
|
|
|
|
|
|
tag = template.getQuestion();
|
|
|
}
|
|
|
- logger.info("init Group, current Tag is {},current Text is {}", tag, text);
|
|
|
+ logger.info("initGroup, current Tag is {},current Text is {}", tag, text);
|
|
|
List<XWPFParagraph> ps = map.get(tag);
|
|
|
|
|
|
if (ps == null) {
|
|
@@ -244,8 +255,8 @@ public class WordService {
|
|
|
private QuestionOption initOption(int index, String content) {
|
|
|
QuestionOption option = new QuestionOption();
|
|
|
option.setOindex(index);
|
|
|
-
|
|
|
- option.setContent(content.substring(2));
|
|
|
+
|
|
|
+ option.setContent(content.substring(3));
|
|
|
option.setCreatetime(new Date());
|
|
|
option.setCorrect(false);
|
|
|
return option;
|
|
@@ -255,9 +266,16 @@ public class WordService {
|
|
|
return text.matches("^[A-Z]、(.*)");
|
|
|
}
|
|
|
|
|
|
- public void parsePicture(XWPFParagraph p) {
|
|
|
+ public void parsePicture(XWPFParagraph p, String picturePattern) {
|
|
|
String text = p.getText();
|
|
|
- logger.info("before parse run: " + text);
|
|
|
+ Matcher matcher = Pattern.compile(picturePattern).matcher(text);
|
|
|
+ if (!matcher.find()) {
|
|
|
+ logger.info("parsePicture,before parse run ,no picture: {}", text);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String size = matcher.group().replaceAll("#", "");
|
|
|
+
|
|
|
+ logger.info("parsePicture,before parse run: " + text);
|
|
|
List<XWPFRun> runs = p.getRuns();
|
|
|
int i = 0;
|
|
|
Map<Integer, String> insert = new HashMap<>();
|
|
@@ -265,31 +283,18 @@ public class WordService {
|
|
|
List<XWPFPicture> pictures = run.getEmbeddedPictures();
|
|
|
logger.debug("run pos " + (i++) + " embed picture size " + pictures.size());
|
|
|
|
|
|
- String img = "<img src=\"%s\" style=\"max-width: %spx\" />";
|
|
|
+
|
|
|
+ String img = "<p style=\"text-align:center\"><img src=\"%s\" style=\"max-width: %spx;center\" /></p>";
|
|
|
if (CommonUtil.notEmpty(pictures)) {
|
|
|
+
|
|
|
XWPFPicture pp = pictures.get(0);
|
|
|
String pName = pp.getPictureData().getFileName();
|
|
|
|
|
|
String suffix = pName.substring(pName.lastIndexOf("."));
|
|
|
String saveName = CommonUtil.randomUUID() + suffix;
|
|
|
byte[] pData = pp.getPictureData().getData();
|
|
|
-
|
|
|
- int width = Constants.DEFAULT_WIDTH, height = Constants.DEFAULT_HEIGHT;
|
|
|
- try {
|
|
|
- BufferedImage image = ImageIO.read(new ByteArrayInputStream(pData));
|
|
|
- width = Math.min(image.getWidth(), Constants.MAX_WIDTH);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- logger.info("picture des is {},width is {},height is {}", pName, width, height);
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("can't get width and height from picture, picture des is {},error message is {}", pName, e.getMessage());
|
|
|
- }
|
|
|
+ int width = getWidth(pData);
|
|
|
+ width = setWidth(size, width);
|
|
|
|
|
|
|
|
|
|
|
@@ -298,18 +303,54 @@ public class WordService {
|
|
|
|
|
|
|
|
|
insert.put(i, String.format(img, url, width));
|
|
|
- logger.debug("picture url is {}, des is {} ", url, pp.getDescription());
|
|
|
+ logger.info("picture url is {}, des is {} ", url, pp.getDescription());
|
|
|
}
|
|
|
}
|
|
|
insert.forEach((k, v) -> {
|
|
|
XWPFRun r = p.insertNewRun(k);
|
|
|
r.setText(v);
|
|
|
});
|
|
|
- logger.debug("after parse run: " + p.getText());
|
|
|
+
|
|
|
+ logger.info("after parse run: " + p.getText());
|
|
|
+ }
|
|
|
+
|
|
|
+ private int setWidth(String size, int width) {
|
|
|
+ switch (size) {
|
|
|
+ case Constants.PICTURE_SIZE_SMALL:
|
|
|
+ width = setBound(width, 0, Constants.PICTURE_SMALL_SIZE);
|
|
|
+ break;
|
|
|
+ case Constants.PICTURE_SIZE_MIDDLE:
|
|
|
+ width = setBound(width, 0, Constants.PICTURE_MIDDLE_SIZE);
|
|
|
+ break;
|
|
|
+ case Constants.PICTURE_SIZE_LARGE:
|
|
|
+ width = setBound(width, 0, Constants.PICTURE_LARGE_SIZE);
|
|
|
+ }
|
|
|
+ return width;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getWidth(byte[] pData) {
|
|
|
+
|
|
|
+ int width = Constants.DEFAULT_WIDTH, height = Constants.DEFAULT_HEIGHT;
|
|
|
+ try {
|
|
|
+ BufferedImage image = ImageIO.read(new ByteArrayInputStream(pData));
|
|
|
+ width = Math.min(image.getWidth(), Constants.MAX_WIDTH);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ logger.info("picture width is {},height is {}", width, height);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("can't get width and height from picture, picture error message is {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return width;
|
|
|
}
|
|
|
|
|
|
private int setBound(int bound, int min, int max) {
|
|
|
- return bound < min ? min : (bound > max ? max : bound);
|
|
|
+ return bound < min ? min : (Math.min(bound, max));
|
|
|
}
|
|
|
|
|
|
public boolean parseFormula(XWPFParagraph p) {
|