diff options
author | Greg S <enimihil@gmail.com> | 2009-05-01 18:38:16 (GMT) |
---|---|---|
committer | Greg S <enimihil@gmail.com> | 2009-05-01 18:38:16 (GMT) |
commit | c4459d838d97e5dff3b46b2da4bf05223fc95845 (patch) | |
tree | feb8913468ddcce7cb8dbd7a7e91fcb1a187437c | |
parent | 70c921743862d622a1ae049b6d3c225a1ef736f5 (diff) |
First run through to make Question objects from parser.
Indicates a need for a more sophisticated parser.
-rw-r--r-- | quizdata/__init__.py | 1 | ||||
-rw-r--r-- | quizdata/_format_gift.py | 112 |
2 files changed, 109 insertions, 4 deletions
diff --git a/quizdata/__init__.py b/quizdata/__init__.py index 2bddc6e..52c3bf4 100644 --- a/quizdata/__init__.py +++ b/quizdata/__init__.py @@ -7,5 +7,4 @@ import re from peak.rules import when, abstract, around -from ._question import Question from ._urlproc import open diff --git a/quizdata/_format_gift.py b/quizdata/_format_gift.py index f84da79..dad97de 100644 --- a/quizdata/_format_gift.py +++ b/quizdata/_format_gift.py @@ -7,6 +7,12 @@ from pyparsing import (Word, Literal, Optional, Group, OneOrMore, originalTextFor, CharsNotIn, NotAny, ParserElement, printables, White, FollowedBy, delimitedList) +from quizdata.question import (MultipleChoiceQuestion, + MissingWordQuestion, + TrueFalseQuestion, + ShortAnswerQuestion, + NumericalQuestion, + MatchingQuestion) __all__ = [ 'parse', 'question', 'questions' ] @@ -88,10 +94,110 @@ def parse(stream, params): parsed = _search_file(question, stream) ret = [] for q in parsed: - ret.append(_question_maker(q)) + ret.append(_question_maker(q[0])) return ret def _question_maker(q): - #XXX: NotImplemented - return q + try: + if 'text_additional' in q: + _missing_word_question_maker(q) + elif q['answers'][0][0] in ['T', 'F', 'TRUE', 'FALSE']: + _true_false_question_maker(q) + elif "->" in list(q['answers'][0]): + _matching_question_maker(q) + elif ':' in list(q['answers'][0]): + print "XXX: SKIPPING NUMERICAL QUESTION!" + elif len(q['answers']) == 1 and q['answers'][0][0] == '=': + _short_answer_question_maker(q) + elif len(q['answers']) > 1: + _multi_choice_question_maker(q) + else: + print ("XXX: Couldn't identify question type!") + print q + return None + except Exception: + print "Error converting parsed format into question object!" + print q + return None + +def _missing_word_question_maker(q): + answers = _make_answers(q['answers']) + correct = answers.correct + 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 = answers.correct + 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 = answers.correct + 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 len(a[0]) > 2: + 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() + print a + for i in a: + print i + ret.append((str(i[1]), str(i[3]))) + return ret |