| | |
| | | public class ItemParser extends Parser { |
| | | |
| | | /** |
| | | * 习题开始模式匹配 |
| | | * 习题开始模式匹配 |
| | | */ |
| | | Pattern patternItem = Pattern.compile("\\d+"); |
| | | |
| | | private Pattern analysis = Pattern.compile("解析"); |
| | | private Pattern analysis = Pattern.compile("解析"); |
| | | |
| | | /** |
| | | * 习题选项编号模式[带括号] |
| | | * 习题选项编号模式[带括号] |
| | | */ |
| | | //Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[、..]"); |
| | | //Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[、..]"); |
| | | |
| | | /** |
| | | * 习题选项 word自带的格式的 a.a、a) 这三种较为普遍,目前03可以解析成功,07解析失败*[\t] |
| | | * 习题选项 word自带的格式的 a.a、a) 这三种较为普遍,目前03可以解析成功,07解析失败*[\t] |
| | | */ |
| | | Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[..、]"); |
| | | Pattern optNoWithBrackets = Pattern.compile("\\(?[ABCDEFabcdef]\\)?[..、]"); |
| | | |
| | | ItemAnswerParser answerParser = new ItemAnswerParser(); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 解析 Item |
| | | * 解析 Item |
| | | * @param item |
| | | * @param str |
| | | * @return |
| | | */ |
| | | private ParseResult parse(Item item, String str) { |
| | | |
| | | //习题 |
| | | //习题 |
| | | Matcher itemNoMatch = patternItem.matcher(str); |
| | | Matcher optNoWithBracketsMatch = optNoWithBrackets.matcher(str); |
| | | Matcher analysisMatch = analysis.matcher(str); |
| | | |
| | | //尝试调用上级解析器继续解析 |
| | | //尝试调用上级解析器继续解析 |
| | | if(!matchBegin(itemNoMatch) && !matchBegin(optNoWithBracketsMatch) && !matchBegin(analysisMatch)){ |
| | | return new ParseResult(false,ParseResult.STEP_PRE,null); |
| | | } |
| | | |
| | | |
| | | //解析 no,title |
| | | //解析 no,title |
| | | if(matchBegin(itemNoMatch)){ |
| | | itemNoMatch.find(); |
| | | String no = itemNoMatch.group(); |
| | |
| | | item.setNo(no); |
| | | item.setTitle(beautyTitle(title)); |
| | | |
| | | //解析参考答案 |
| | | //解析参考答案 |
| | | answerParser.parse(item, str); |
| | | |
| | | return new ParseResult(true,ParseResult.STEP_CUR,null); |
| | | } |
| | | } |
| | | |
| | | //习题选项 |
| | | //习题选项 |
| | | if(matchBegin(optNoWithBracketsMatch)){ |
| | | Option opt = new Option(); |
| | | item.addNode(opt); |
| | | return new ParseResult(false,ParseResult.STEP_NEXT,opt); |
| | | } |
| | | |
| | | //解析 习题分析 |
| | | //解析 习题分析 |
| | | if(matchBegin(analysisMatch)){ |
| | | analysisParser.parse(item,str); |
| | | //20150907修改:返回item |
| | | //20150907修改:返回item |
| | | return new ParseResult(true,ParseResult.STEP_CUR,item); |
| | | } |
| | | |
| | | //20150907修改:本来返回true,没有解析到一个类型,抛到上层 |
| | | //20150907修改:本来返回true,没有解析到一个类型,抛到上层 |
| | | return new ParseResult(false,ParseResult.STEP_PRE,null); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 去除习题标题多余字符 |
| | | * 去除习题标题多余字符 |
| | | * @param title |
| | | * @return |
| | | */ |
| | | private String beautyTitle(String title){ |
| | | String prefix = ".。~、."; |
| | | String prefix = ".。~、."; |
| | | while(title.length()>0 && prefix.contains(title.substring(0, 1))){ |
| | | title = title.substring(1); |
| | | } |
| | |
| | | |
| | | public static void main(String[] args){ |
| | | ItemParser parser = new ItemParser(); |
| | | parser.parse(null, "59、在培训效果评估的指标中,技能转换指标通常通过()来判断。"); |
| | | parser.parse(null, "59、在培训效果评估的指标中,技能转换指标通常通过()来判断。"); |
| | | } |
| | | |
| | | } |