派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.qxueyou.scc.exercise.service.impl.parser;
 
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.lang3.StringUtils;
 
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.ItemType;
 
public class ItemTypeParser extends Parser {
 
    /**
     * 习题类型关键字
     */
    private static Map<String,String> typeMap = new HashMap<String,String>();
    
    static{
        typeMap.put("单选", ItemType.TYPE_SINGLE);
        typeMap.put("单项选择", ItemType.TYPE_SINGLE);
        typeMap.put("多选", ItemType.TYPE_MULTI);
        typeMap.put("多项选择", ItemType.TYPE_MULTI);
        typeMap.put("判断", ItemType.TYPE_TRUE_OR_FALSE);
    }
 
    /**
     * 习题开始模式匹配
     */
    Pattern itemPattern = Pattern.compile("\\d+");
 
    @Override
    public ParseResult parse(Node node, String str) {
        return parse((ItemType) node, str);
    }
 
    /**
     * 解析 ItemType
     * 
     * @param doc
     * @param str
     * @return
     */
    private ParseResult parse(ItemType itemType, String str) {
        
        // 习题
        Matcher matcher = itemPattern.matcher(str);
 
        if (matchBegin(matcher) && StringUtils.isNotEmpty(itemType.getType())) {
            Item item = new Item();
            itemType.addNode(item);
            return new ParseResult(false, ParseResult.STEP_NEXT, item);
        }
        
        // 题目类型
        for (String keyword : typeMap.keySet()) {
            if (str.contains(keyword)) {
                if(itemType.getType().equals(typeMap.get(keyword))){
                    return new ParseResult(true, ParseResult.STEP_CUR, null);
                }else{
                    return new ParseResult(false, ParseResult.STEP_PRE, null);
                }
            }
        }
 
        //20150907修改:本来返回true,没有解析到一个类型,抛到上层
        return new ParseResult(false, ParseResult.STEP_PRE, null);
 
    }
 
}