-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtactus2table.py
executable file
·313 lines (278 loc) · 10.9 KB
/
tactus2table.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python3
"""
tactus2table.py: convert xml files from tactus to csv tables
usage: tactus2table.py file1 [file2 ...]
note: writes output to ../output/emails.csv
20180412 erikt(at)xs4all.nl
"""
import csv
import gzip
import nltk
import operator
import re
import sys
import xml.etree.ElementTree as ET
from operator import itemgetter
COMMAND = sys.argv.pop(0)
USAGE = "usage: "+COMMAND+" file1 [file2 ...]"
INTAKEQUESTIONNAIRE = "./Intake/Questionnaire"
INTAKETITLEALTERNATIVE = "Vragenlijst"
INTAKE = "Intake"
QUESTIONNAIRE = "./Treatment/TreatmentSteps/TreatmentStep/Questionnaire"
QUESTIONNAIRETITLES = { INTAKE:True,"Lijst tussenmeting":True,"Lijst nameting":True,"Lijst 3 maanden":True,"Lijst half jaar":True }
QUESTIONS = "./Content/question"
ANSWERS = "./answer"
MESSAGES = "./Messages/Message"
AGE = "leeftijd"
CLIENT = "CLIENT"
COUNSELOR = "COUNSELOR"
SENDER = "Sender"
RECIPIENT = "Recipients"
QUESTION = "question"
DATE = "DateSent"
BODY = "Body"
SUBJECT = "Subject"
MAILDATEID = 3
MAILBODYID = 5
OUTPUTDIR = "/home/erikt/projects/e-mental-health/usb/output"
EMAILFILE = OUTPUTDIR+"/emails.csv"
EMAILHEADING = ["id","sender","receipient","date","subject","text"]
EMPTYTOKEN = "EMPTY"
MULTIWORDTOKEN = "MULTIWORD"
MINVALUE = 5
REMOVEDTOKEN = "REMOVED"
SEPARATOR= "-"
ID = "ID"
NOID = "NOID"
exceptions = {"medi0":True,"medi00":True,"drugs0":True}
valueCounts = {}
def cleanupText(text):
text = re.sub(r"\s+"," ",text)
text = re.sub(r"^ ","",text)
text = re.sub(r" $","",text)
return(text)
def makeId(fileName):
thisId = re.sub(r".*/","",fileName)
thisId = re.sub(r"\.xml.*$","",thisId)
return(thisId)
def anonymizeCounselor(name):
if name != CLIENT: return(COUNSELOR)
else: return(name)
def tokenize(text):
return(" ".join(nltk.word_tokenize(text,language='dutch')))
def getEmailData(root,thisId):
clientMails = []
counselorMails = []
for message in root.findall(MESSAGES):
body = ""
date = ""
recipient = ""
sender = ""
subject = ""
for child in message:
if child.tag == SENDER:
sender = anonymizeCounselor(cleanupText(child.text))
elif child.tag == RECIPIENT:
recipient = anonymizeCounselor(cleanupText(child.text))
elif child.tag == DATE: date = cleanupText(child.text)
elif child.tag == SUBJECT: subject = tokenize(cleanupText(child.text))
elif child.tag == BODY: body = cleanupText(child.text)
if sender == CLIENT: clientMails.append([thisId,sender,recipient,date,subject,body])
else: counselorMails.append([thisId,sender,recipient,date,subject,body])
clientMails = cleanupMails(clientMails,counselorMails)
counselorMails = cleanupMails(counselorMails,clientMails)
allMails = clientMails
allMails.extend(counselorMails)
return(sorted(allMails,key=lambda subList:subList[MAILDATEID]))
# the sentence chunks produced by nltk are quite coarse and
# leave too much of the quoted text in the emails
def sentenceSplitNltk(text): return(nltk.sent_tokenize(text,language='dutch'))
def sentenceSplitLocal(text):
tokens = text.split()
sentence = []
sentences = []
for token in tokens:
sentence.append(token)
if not re.search(r"[a-zA-Z0-9'\"]",token):
if len(sentence) == 1 and len(sentences) > 0:
sentences[-1] += " "+" ".join(sentence)
else: sentences.append(" ".join(sentence))
sentence = []
if len(sentence) > 0: sentences.append(" ".join(sentence))
return(sentences)
def sentenceSplit(text):
return(sentenceSplitNltk(text))
def cleanupMails(clientMails, counselorMails):
clientSentenceDates = {}
counselorSentenceDates = {}
for i in range(0,len(clientMails)):
date = clientMails[i][MAILDATEID]
body = clientMails[i][MAILBODYID]
sentences = sentenceSplit(body)
for s in sentences:
if (s in clientSentenceDates and date < clientSentenceDates[s]) or \
not s in clientSentenceDates:
clientSentenceDates[s] = date
for i in range(0,len(counselorMails)):
date = counselorMails[i][MAILDATEID]
body = counselorMails[i][MAILBODYID]
sentences = sentenceSplit(body)
for s in sentences:
if s in clientSentenceDates and date < clientSentenceDates[s]:
counselorSentenceDates[s] = date
del(clientSentenceDates[s])
elif s in counselorSentenceDates and date < counselorSentenceDates[s]:
counselorSentenceDates[s] = date
elif not s in clientSentenceDates and not s in counselorSentenceDates:
counselorSentenceDates[s] = date
for i in range(0,len(clientMails)):
date = clientMails[i][MAILDATEID]
body = clientMails[i][MAILBODYID]
sentences = sentenceSplit(body)
body = ""
for s in sentences:
if s in clientSentenceDates and clientSentenceDates[s] == date:
if body != "": body += " "
body += s
clientMails[i][MAILBODYID] = body
return(clientMails)
def store(array,outFileName):
with open(outFileName,"w",encoding="utf8") as csvfile:
csvwriter = csv.writer(csvfile,delimiter=',',quotechar='"')
csvwriter.writerow(EMAILHEADING)
for row in array: csvwriter.writerow(row)
csvfile.close()
def removeInitialKeywords(value):
return(re.sub("^.*:\s*","",value))
def removeDoubleQuotes(value):
return(cleanupText(re.sub(r"\"","",value)))
def removeCommas(value):
return(cleanupText(re.sub(r","," ",value)))
def isEmptyString(value):
return(re.search(r"^\s*$",value))
def getFirstWordIfNumber(value):
return(re.search(r"(\d+)\s",value))
def isMultiword(value):
return(re.search(r"\s",value))
def isMailAddress(value):
return(re.search(r"@",value))
def isPhoneNumber(value):
return(re.search(r"\d\d\d\d\d",value))
def count(value,questionaireTitle):
global valueCounts
if not questionaireTitle in valueCounts:
valueCounts[questionaireTitle] = {}
if value in valueCounts[questionaireTitle]:
valueCounts[questionaireTitle][value] += 1
else:
valueCounts[questionaireTitle][value] = 1
return()
def summarizeCellValueFirst(value,questionaireTitle):
value = removeCommas(removeDoubleQuotes(removeInitialKeywords(value))).lower()
if isEmptyString(value): return(EMPTYTOKEN)
count(value,questionaireTitle)
return(value)
def summarizeCellValueLast(value):
firstWordIfNumber = getFirstWordIfNumber(value)
if firstWordIfNumber != None: return(firstWordIfNumber[1])
elif isMultiword(value): return(MULTIWORDTOKEN)
elif isMailAddress(value) or isPhoneNumber(value): return(REMOVEDTOKEN)
else: return(value)
def getQuestionnaires(root,thisId):
global exceptions
qs = []
number = "0"
for questionnaires in INTAKEQUESTIONNAIRE,QUESTIONNAIRE:
for questionnaire in root.findall(questionnaires):
title = cleanupText(questionnaire.findall("./Title")[0].text)
if title == INTAKETITLEALTERNATIVE: title = INTAKE
if title in QUESTIONNAIRETITLES:
q = {"0-title":title,"0-id":thisId}
for question in questionnaire.findall(QUESTIONS):
numbers = question.findall("./questionNumber")
if numbers != None and len(numbers) > 0:
number = cleanupText(numbers[0].text)
else:
number = str(int(number)+1)
for answer in question.findall(ANSWERS):
if ID in answer.attrib.keys(): key = answer.attrib[ID]
else: key = NOID
answerTexts = answer.findall("./answerText")
if len(answerTexts) > 0: value = cleanupText(answerTexts[0].text)
else: value = ""
summary = summarizeCellValueFirst(value,title)
qKey = number+SEPARATOR+key
if qKey in q:
print("clash for",thisId,title,shortkey,qKey,":",q[qKey],"<>",summary)
q[qKey] = summary
qs.append(q)
return(qs)
def getTitles(questionnaires):
titles = {}
for q in questionnaires: titles[q["0-title"]] = True
return(titles)
def getColumns(questionnaires,title):
columns = {}
for questionnaire in questionnaires:
if questionnaire["0-title"] == title:
for field in questionnaire.keys():
columns[field] = True
return(columns)
def countsFilter(value,questionaireTitle):
global valueCounts
if questionaireTitle in valueCounts and \
value in valueCounts[questionaireTitle] and \
valueCounts[questionaireTitle][value] >= MINVALUE: return(value)
else: return(summarizeCellValueLast(value))
def keyCombine(number,string):
return(str(number)+SEPARATOR+string)
def keySplit(key):
keyParts = key.split(SEPARATOR)
return(int(keyParts[0]),keyParts[1])
def sortKeys(keys):
splitKeys = [keySplit(k) for k in keys]
sortedKeys = sorted(splitKeys,key=itemgetter(0,1))
return([keyCombine(k[0],k[1]) for k in sortedKeys])
def storeDictTitles(questionnaires):
titles = getTitles(questionnaires)
for title in titles.keys():
columns = getColumns(questionnaires,title)
outFileName = OUTPUTDIR+"/"+title+".csv"
with open(outFileName,"w",encoding="utf8") as csvfile:
csvwriter = csv.writer(csvfile,delimiter=',',quotechar='"')
heading = []
for columnName in sortKeys(columns.keys()):
heading.append(columnName)
csvwriter.writerow(heading)
for questionnaire in questionnaires:
if questionnaire["0-title"] == title:
row = []
for columnName in sortKeys(columns.keys()):
try: row.append(countsFilter(questionnaire[columnName],title))
except: row.append(EMPTYTOKEN)
csvwriter.writerow(row)
csvfile.close()
return()
def readRootFromFile(inFileName):
if re.search(r"\.gz$",inFileName):
inFile = gzip.open(inFileName,"rb")
inFileContent = inFile.read()
inFile.close()
return(ET.fromstring(inFileContent))
else:
tree = ET.parse(inFileName)
root = tree.getroot()
return(root)
def main(argv):
emails = []
questionnaires = []
for inFileName in sys.argv:
root = readRootFromFile(inFileName)
thisId = makeId(inFileName)
emails.extend(getEmailData(root,thisId))
questionnaires.extend(getQuestionnaires(root,thisId))
# if len(emails) > 0: store(emails,EMAILFILE)
storeDictTitles(questionnaires)
return(0)
if __name__ == "__main__":
sys.exit(main(sys.argv))