Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg S <enimihil@gmail.com>2009-04-01 18:12:59 (GMT)
committer Greg S <enimihil@gmail.com>2009-04-01 18:12:59 (GMT)
commitec60486cafe24e828f52a84de2d6e04b39d97c6a (patch)
treed047490f8ed59b666de332cd2d2bba61a7923db6
parent9287b97d1882e45e37b22a131e68a84c7136e965 (diff)
Basic GIFT format parins is working.
Questions are not yet constructed, nor does the parser support every feature of GIFT.
-rw-r--r--quizdata/_format_gift.py39
-rw-r--r--tests/examples.txt208
-rwxr-xr-xtests/test_gift_parse.py46
3 files changed, 293 insertions, 0 deletions
diff --git a/quizdata/_format_gift.py b/quizdata/_format_gift.py
index 527b2ab..b88e51c 100644
--- a/quizdata/_format_gift.py
+++ b/quizdata/_format_gift.py
@@ -1,6 +1,45 @@
'''
Parsing and support functions for the Moodle GIFT format.
'''
+from pyparsing import (Word, Literal, Optional, Group, OneOrMore,
+ ParseException, Combine, restOfLine, printables,
+ nums)
+
+_special_chars = set(":/=~#{}->")
+_filt_printables = ''.join([ p for p in printables if p not in _special_chars ])
+
+_text = OneOrMore(Word(_filt_printables))
+_number = Combine(Word(nums) + Optional("." + Optional(Word(nums))))
+
+_comment = Literal("//") + restOfLine
+_dcolon = Literal("::")
+_qtitle = _dcolon + _text + _dcolon
+
+_correct_ans = Literal("=") + _text
+_wrong_ans = Literal("~") + _text
+_ans_explain = Literal("#") + _text
+_ans_tf = Literal("TRUE") | Literal("FALSE") | Literal("T") | Literal("F")
+_ans_match = Literal("=") + _text + Literal("->") + _text
+
+_ans_numeric = (Optional(Literal("="))
+ + (_number + Optional( Literal(":") + _number)
+ |_number + Literal("..") + _number)
+ + Optional( _ans_explain ))
+
+_answer = ( (( _correct_ans | _wrong_ans ) + Optional(_ans_explain))
+ | _ans_tf | _ans_match )
+
+question = (Optional(_qtitle) + _text
+ + ("{" + OneOrMore(_answer) + "}" |
+ "{#" + OneOrMore(_ans_numeric) + "}"))
+question.ignore(_comment)
+
+questions = OneOrMore(question + Literal("\n\n"))
+questions.ignore(_comment)
+
+def _parse_only(text):
+ return questions.parseText(text)
def parse(text):
raise NotImplementedError()
+
diff --git a/tests/examples.txt b/tests/examples.txt
new file mode 100644
index 0000000..328952a
--- /dev/null
+++ b/tests/examples.txt
@@ -0,0 +1,208 @@
+// EXAMPLE QUESTIONS for the GIFT import filter
+// by Paul Tsuchido Shew, January 2004.
+// http://eutrainingsite.net/mod/quiz/format/gift/examples.txt
+
+//-----------------------------------------//
+// EXAMPLES FROM DESCRIPTION
+//-----------------------------------------//
+
+Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
+
+Grant is {~buried =entombed ~living} in Grant's tomb.
+
+Grant is buried in Grant's tomb.{FALSE}
+
+Who's buried in Grant's tomb?{=no one =nobody}
+
+When was Ulysses S. Grant born?{#1822:1}
+
+
+//-----------------------------------------//
+// EXAMPLES FROM DOCUMENTATION
+//-----------------------------------------//
+
+// ===Multiple Choice===
+
+Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
+
+Grant is {~buried =entombed ~living} in Grant's tomb.
+
+The American holiday of Thanksgiving is celebrated on the {
+ ~second
+ ~third
+ =fourth
+} Thursday of November.
+
+Japanese characters originally came from what country? {
+ ~India
+ =China
+ ~Korea
+ ~Egypt}
+
+// ===Short Answer===
+
+Who's buried in Grant's tomb?{=no one =nobody}
+
+Two plus two equals {=four =4}.
+
+// ===True-False===
+
+Grant is buried in Grant's tomb.{F}
+
+The sun rises in the east.{T}
+
+// ===Matching===
+
+Matching Question. {
+ =subquestion1 -> subanswer1
+ =subquestion2 -> subanswer2
+ =subquestion3 -> subanswer3
+ }
+
+Match the following countries with their corresponding capitals. {
+ =Canada -> Ottawa
+ =Italy -> Rome
+ =Japan -> Tokyo
+ =India -> New Delhi
+ }
+
+// ===Numerical===
+
+When was Ulysses S. Grant born? {#1822}
+
+What is the value of pi (to 3 decimal places)? {#3.1415:0.0005}.
+
+What is the value of pi (to 3 decimal places)? {#3.141..3.142}.
+
+When was Ulysses S. Grant born? {#
+ =1822:0
+ =%50%1822:2}
+
+// OPTIONS
+
+// ===Line Comments===
+
+// Subheading: Numerical questions below
+What's 2 plus 2? {#4}
+
+
+// ===Question Name===
+
+::Kanji Origins::Japanese characters originally
+came from what country? {=China}
+
+::Thanksgiving Date::The American holiday of Thanksgiving is
+celebrated on the {~second ~third =fourth} Thursday of November.
+
+// ===Feedback===
+
+What's the answer to this multiple-choice question?{
+~wrong answer#feedback comment on the wrong answer
+~another wrong answer#feedback comment on this wrong answer
+=right answer#Very good!}
+
+Who's buried in Grant's tomb?{
+=no one#excellent answer!
+=nobody#excellent answer!}
+
+// ===Specify text format===
+[markdown]Who's buried in **Grant's tomb**?{
+=no one#excellent answer!
+=nobody#excellent answer!}
+
+// ===Percentage Answer Weights===
+Grant is buried in Grant's tomb.{FALSE#No one is buried in Grant's tomb.}
+
+Difficult question.{~wrong answer ~%50%half credit answer =full credit answer}
+
+::Jesus' hometown::Jesus Christ was from {
+ ~Jerusalem#This was an important city, but the wrong answer.
+ ~%25%Bethlehem#He was born here, but not raised here.
+ ~%50%Galilee#You need to be more specific.
+ =Nazareth#Yes! That's right!}.
+
+::Jesus' hometown:: Jesus Christ was from {
+ =Nazareth#Yes! That's right!
+ =%75%Nazereth#Right, but misspelled.
+ =%25%Bethlehem#He was born here, but not raised here.}
+
+// ===Multiple Answers===
+
+What two people are entombed in Grant's tomb? {
+ ~No one
+ ~%50%Grant
+ ~%50%Grant's wife
+ ~Grant's father }
+
+What two people are entombed in Grant's tomb? {
+ ~%-50%No one
+ ~%50%Grant
+ ~%50%Grant's wife
+ ~%-50%Grant's father }
+
+// ===Special Characters===
+Which answer equals 5? {
+ ~ \= 2 + 2
+ = \= 2 + 3
+ ~ \= 2 + 4 }
+
+::GIFT Control Characters::
+Which of the following is NOT a control character for the GIFT import format? {
+ ~ \~ # \~ is a control character.
+ ~ \= # \= is a control character.
+ ~ \# # \# is a control character.
+ ~ \{ # \{ is a control character.
+ ~ \} # \} is a control character.
+ = \\ # Correct! \\ (backslash) is not a control character. BUT,
+ it is used to escape the control characters. So, to specify
+ a literal backslash, you must escape it with a backslash
+ (as shown in this example).
+}
+
+
+
+//-----------------------------------------//
+// EXAMPLES FROM gift/format.php
+//-----------------------------------------//
+
+Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
+
+Grant is {~buried =entombed ~living} in Grant's tomb.
+
+Grant is buried in Grant's tomb.{FALSE}
+
+Who's buried in Grant's tomb?{=no one =nobody}
+
+When was Ulysses S. Grant born?{#1822:5}
+
+Match the following countries with their corresponding
+capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
+
+//-----------------------------------------//
+// MORE COMPLICATED EXAMPLES
+//-----------------------------------------//
+
+::Grant's Tomb::Grant is {
+ ~buried#No one is buried there.
+ =entombed#Right answer!
+ ~living#We hope not!
+} in Grant's tomb.
+
+Difficult multiple choice question.{
+ ~wrong answer #comment on wrong answer
+ ~%50%half credit answer #comment on answer
+ =full credit answer #well done!}
+
+::Jesus' hometown (Short answer ex.):: Jesus Christ was from {
+ =Nazareth#Yes! That's right!
+ =%75%Nazereth#Right, but misspelled.
+ =%25%Bethlehem#He was born here, but not raised here.
+}.
+
+//this comment will be ignored by the filter
+::Numerical example::
+When was Ulysses S. Grant born? {#
+ =1822:0 #Correct! 100% credit
+ =%50%1822:2 #He was born in 1822.
+ You get 50% credit for being close.
+}
diff --git a/tests/test_gift_parse.py b/tests/test_gift_parse.py
new file mode 100755
index 0000000..f982384
--- /dev/null
+++ b/tests/test_gift_parse.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+import sys
+from os import path
+
+base_path = path.abspath(path.join(path.dirname(path.abspath(__file__)),'..'))
+sys.path.append(base_path)
+
+from quizdata import _format_gift
+
+#def test_example():
+# full_example = open(path.join(base_path, 'tests', "examples.txt")).read()
+#
+# print _format_gift.questions.parseString(full_example)
+
+def test_text():
+ print _format_gift._text.parseString(
+ "this is ? some ! text that should count as ' a single \"bit\" \
+ of text to the GIFT parser.")
+
+def test_simple_tf():
+ print _format_gift.question.parseString("4 is an even number{TRUE}\n")
+
+def test_simple_multi():
+ print _format_gift.question.parseString("""
+ What is the capital of France?{=Paris ~London ~Guam ~Tomato}
+
+ """)
+
+def test_title():
+ print _format_gift.question.parseString("""
+ ::Capital of France
+ ::What is the capital of France? {
+ =Paris
+ ~London
+ ~Guam
+ ~Tomato
+ }
+
+ """)
+
+if __name__=='__main__':
+ test_text()
+ test_simple_tf()
+ test_simple_multi()
+ test_title()