Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/quizdata/_format_gift.py
blob: 6078cab01054bbeb8a9aaab9205982119d079261 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'''
    Parsing and support functions for the Moodle GIFT format.
'''
from pyparsing import (Word, Literal, Optional, Group, OneOrMore, 
                       ParseException, Combine, restOfLine,
                       nums, StringEnd, ZeroOrMore, oneOf, 
                       originalTextFor, CharsNotIn, NotAny,
                       ParserElement, printables, White,
                       FollowedBy, delimitedList)
from quizdata.question import (MultipleChoiceQuestion, 
                               MissingWordQuestion, 
                               TrueFalseQuestion, 
                               ShortAnswerQuestion, 
                               NumericalQuestion, 
                               MatchingQuestion)

__all__ = [ 'parse', 'question', 'questions' ]

ParserElement.enablePackrat()
ParserElement.setDefaultWhitespaceChars(" \t")

comment = Literal("//") + restOfLine
NL     = Literal("\n").suppress()

def kill_whitespace(s, l, t):
    #print t.asList()[1:-1]
    return [ ' '.join(t.asList()[1:-1]).strip() ]

def printables_except(chars):
    l = list(printables)
    for c in chars:
        l.remove(c)
    return ''.join(l)

def restricted_text(allowed_chars):
    return originalTextFor(
            Optional(NL) + 
            OneOrMore((Word(allowed_chars)
                + Optional(NL)))
            ).setParseAction( kill_whitespace )

text = restricted_text(printables)
qtext = restricted_text(printables_except("{"))
ans_text = restricted_text(printables_except("=~#}->"))
title_text = restricted_text(printables_except(":"))
format_text = restricted_text(printables_except("]"))

number = Combine(Word(nums) + Optional("." + Optional(Word(nums))))
dcolon = Literal("::").suppress()
qtitle = dcolon + title_text + Optional(NL) + dcolon + Optional(NL)

qformat = Literal("[").suppress() + format_text + Literal("]").suppress() + Optional(NL)

correct_ans = (Literal("=") + ans_text)("correct_text")
wrong_ans = Literal("~") + ans_text("wrong_text")
ans_explain = Literal("#") + ans_text("explain_text")
ans_tf = oneOf("T F TRUE FALSE")("tf_text")
ans_match = Literal("=") + ans_text("left_match") + Literal("->") + ans_text("right_match")

ans_numeric = Group(Optional(Literal("="))
                + (number("number") + Optional( Literal(":") + number("range")) 
                  ^number("beg_range") + Literal("..") + number("end_range"))
                + Optional( ans_explain )("explain")) + Optional(NL)

answer = Group(( ( correct_ans ^ wrong_ans ^ ans_tf )("ans_text") + Optional(ans_explain)("explain")) 
               ^ ans_match("matching_answer") ) + Optional(NL)

answer_list = Group((Literal("{").suppress() 
                  + Optional(NL) 
                  + OneOrMore(answer)
                  + Literal("}").suppress())
              ^
               (Literal("{#").suppress() 
                  + Optional(NL)
                  + OneOrMore(ans_numeric)("numeric_ans") 
                  + Literal("}").suppress())) 

question = ZeroOrMore(NL) + Group( 
                  Optional(qformat)("format") + 
                  Optional(qtitle)("title") + 
                  qtext("text") + 
                  answer_list("answers") + 
                  Optional(text)("text_additional") )

question.ignore(comment)

questions = delimitedList(question, delim=OneOrMore(NL))("questions") + ZeroOrMore(NL) + StringEnd()
questions.ignore(comment)

def _search_file(parser, stream):
    return parser.searchString(stream.read())

def parse(stream, params):
    parsed = _search_file(question, stream)
    ret = []
    for q in parsed:
        ques = _question_maker(q[0])
        if ques:
            ret.append(ques)
    
    return ret

def _question_maker(q):
    answers = q['answers']
    try:
        #print "ANSWER KEYS: ", [ answer.keys() for answer in answers if hasattr(answer, 'keys') ]
        if 'text_additional' in q: #XXX This really a different question type?
            return _missing_word_question_maker(q) 
        elif any('tf_text' in answer.keys() for answer in answers if hasattr(answer, 'keys')):
            return _true_false_question_maker(q)
        elif any('numeric_ans' in answer.keys() for answer in answers if hasattr(answer, 'keys')):
            print "XXX: SKIPPING NUMERICAL QUESTION!"
        elif all("wrong_text" not in answer.keys() for answer in answers if hasattr(answer, 'keys')):
            return _short_answer_question_maker(q)
        elif all("left_match" in answer.keys() for answer in answers if hasattr(answer, 'keys')):
            _matching_question_maker(q)
        elif len(answers) > 1:
            return _multi_choice_question_maker(q)
        else:
            print ("XXX: Couldn't identify question type!")
            print q
            return None
    except Exception:
        import traceback
        print "Error converting parsed format into question object!"
        print zip(q.keys(), (str(v) for v in q.values()))
        print q
        traceback.print_exc()
        return None

def _missing_word_question_maker(q):
    answers = _make_answers(q['answers'])
    correct = _get_correct_answer(q['answers'])
    params = {
            'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode,
            'tail_text': str(q['text_additional'][0]),
            'text' : str(q['text'][0]),
            'answers' : answers,
            'correct' : correct
    }
    if 'title' in q:
        params['title'] = q['title'][0]
    return MissingWordQuestion(**params)
    
def _short_answer_question_maker(q):
    answers = _make_answers(q['answers'])
    correct = _get_correct_answer(q['answers'])
    params = {
            'text': str(q['text'][0]),
            'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode,
            'correct' : correct
            }
    if 'title' in q:
        params['title'] = q['title'][0]
    return ShortAnswerQuestion(**params)

def _matching_question_maker(q):
    answers = _make_answers(q['answers'])
    params = {
            'text': str(q['text'][0]),
            'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode,
            'answers' : answers,
            }
    if 'title' in q:
        params['title'] = str(q['title'][0])
    return MatchingQuestion(**params)

def _true_false_question_maker(q):
    params = {
            'text': str(q['text'][0]),
            'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode,
            'correct' : q['answers'][0][0]
            }
    if 'title' in q:
        params['title'] = q['title'][0]
    return TrueFalseQuestion(**params)

def _multi_choice_question_maker(q):
    answers = _make_answers(q['answers'])
    correct = _get_correct_answer(q['answers'])
    params = {
            'text': str(q['text'][0]),
            'markup_type': MARKUP_TYPES[q['format']] if 'format' in q else unicode,
            'answers': answers,
            'correct': correct,
            }
    if 'title' in q:
        params['title'] = q['title'][0]
    return MultipleChoiceQuestion(**params)

class AnswerList(list):
    pass

def _make_answers(a):
    #if all('left_match' in a for answer in a):
    #    return _make_matching_answers(a)
    ret = AnswerList()
    for i in a:
        ret.append(str(i[1]))
        if i[0] == '=':
            ret.correct = i[1]
    return ret

def _make_matching_answers(a):
    ret = AnswerList()
    for i in a[0]:
        ret.append((str(i['left_match']), str(i['right_match'])))
    return ret

def _get_correct_answer(answers):
    #XXX: needs help...
    for a in answers:
        if a[0] == '=':
            return a[1]
    else:
        raise Exception("Couldn't find correct answer!?")