package com.qxueyou.scc.exercise.service.impl.parser;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import com.qxueyou.scc.exercise.service.impl.Node;
|
import com.qxueyou.scc.exercise.service.impl.ParseResult;
|
import com.qxueyou.scc.exercise.service.impl.Parser;
|
import com.qxueyou.scc.exercise.service.impl.node.Item;
|
import com.qxueyou.scc.exercise.service.impl.node.Option;
|
|
|
|
public class OptionParser extends Parser {
|
|
/**
|
* 习题选项编号模式[带括号]
|
*/
|
//Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[、..]");
|
|
/**
|
* 习题选项 word自带的格式的 a.a、a) 这三种较为普遍,目前03可以解析成功,07解析失败*[\t]
|
*/
|
Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFGHIJKLMNOabcdefghijklmno]\\)?[..、]");
|
|
/**
|
* 习题选项编号模式
|
*/
|
Pattern optNo = Pattern.compile("[ABCDEFGHIJKLMNOabcdefghijklmno]");
|
|
|
@Override
|
public ParseResult parse(Node node, String str) {
|
return parse((Option)node,str);
|
}
|
|
/**
|
* 解析 Item
|
* @param item
|
* @param str
|
* @return
|
*/
|
private ParseResult parse(Option option, String str) {
|
|
//习题选项
|
Matcher optNoWithBracketsMatch = optNoWithBrackets.matcher(str);
|
|
//尝试调用上级解析器继续解析
|
if(!matchBegin(optNoWithBracketsMatch)){
|
return new ParseResult(false,ParseResult.STEP_PRE,null);
|
}
|
|
Matcher optNoMatch = null;
|
|
int start = 0;
|
Option preOpt = null;
|
|
optNoWithBracketsMatch.reset();
|
while(optNoWithBracketsMatch.find()){
|
|
String noWithBrackets = optNoWithBracketsMatch.group();
|
optNoMatch = optNo.matcher(noWithBrackets);
|
optNoMatch.find();
|
String no = optNoMatch.group();
|
|
Option opt = getOpt(no.toUpperCase(),(Item)option.getParent());
|
|
if(start!=optNoWithBracketsMatch.start() && preOpt!=null){
|
preOpt.setContent(
|
beautyContent(
|
str.substring(start, optNoWithBracketsMatch.start())
|
)
|
);
|
}
|
preOpt = opt;
|
start = optNoWithBracketsMatch.end();
|
|
// 在最开始匹配成功即跳出循环
|
//break ;
|
}
|
|
preOpt.setContent(beautyContent(str.substring(start, str.length())));
|
|
return new ParseResult(true,ParseResult.STEP_CUR,null);
|
|
}
|
|
/**
|
* 去除文本中多余字符
|
* @param content
|
* @return
|
*/
|
private String beautyContent(String str){
|
String prefix = ".。~、、..)";
|
String content = str;
|
while(content.length()>0 && prefix.contains(content.substring(0, 1))){
|
content = content.substring(1);
|
}
|
return content;
|
}
|
|
/**
|
* 获取题目选项
|
* @param no
|
* @param item
|
* @return
|
*/
|
private Option getOpt(String no,Item item){
|
Option opt = item.getOption(no);
|
if(opt==null){
|
|
opt = item.getOption("");
|
|
if(opt==null){
|
opt = new Option();
|
item.addNode(opt);
|
opt.setNo(no);
|
}
|
}
|
return opt;
|
}
|
}
|