-
Notifications
You must be signed in to change notification settings - Fork 0
/
getQuestions.py
64 lines (58 loc) · 2.55 KB
/
getQuestions.py
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
#!/usr/bin/env python
# getQuestions.py: extract questions from questionaires in tactus file
# usage: getQuestions.py file
# 20191022 erikt(at)xs4all.nl
import gzip
import re
import sys
import xml.etree.ElementTree as ET
INTAKEQUESTIONNAIRE = "./Intake/Questionnaire"
QUESTIONNAIRE = "./Treatment/TreatmentSteps/TreatmentStep/Questionnaire"
QUESTIONNAIRETITLES = { "Intake":True,"Lijst tussenmeting":True,"Lijst nameting":True,"Lijst 3 maanden":True,"Lijst half jaar":True, "Vragenlijst":True }
QUESTIONS = "./Content/question"
TITLE = "title"
ANSWER = "answer"
QUESTIONNUMBER = "questionNumber"
def cleanupText(text):
text = re.sub(r"\s+"," ",text)
text = re.sub(r"^ ","",text)
text = re.sub(r" $","",text)
return(text)
def main(argv):
inFileName = argv.pop(1)
inFile = gzip.open(inFileName,"rb")
inFileContent = inFile.read()
inFile.close()
root = ET.fromstring(inFileContent)
for questionnaires in INTAKEQUESTIONNAIRE,QUESTIONNAIRE:
for questionnaire in root.findall(questionnaires):
title = cleanupText(questionnaire.findall("./Title")[0].text)
print("###",title)
if title in QUESTIONNAIRETITLES:
questionNumber = "0"
for question in questionnaire.findall(QUESTIONS):
questionTitle = ""
numbers = question.findall("./questionNumber")
if numbers != None and len(numbers) > 0:
questionNumber = cleanupText(numbers[0].text)
else:
questionNumber = str(int(questionNumber)+1)
for elementQ in question:
if elementQ.tag == TITLE:
questionTitle = cleanupText(elementQ.text)
if elementQ.tag == QUESTIONNUMBER:
questionNumber = cleanupText(elementQ.text)
if elementQ.tag == ANSWER:
ID = elementQ.attrib["ID"]
answerTitle = ""
for elementA in elementQ:
if elementA.tag == TITLE:
answerTitle = cleanupText(elementA.text)
if answerTitle == "":
print(questionNumber,ID,questionTitle)
else:
print(questionNumber,ID,answerTitle)
questionTitle = answerTitle
if __name__ == "__main__":
sys.exit(main(sys.argv))
exit(0)