-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovk2liwc.py
137 lines (126 loc) · 4.71 KB
/
ovk2liwc.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
#!/usr/bin/env python3
"""
ovk2liwc.py: convert ovk text (in csv format) to liwc feature counts
usage: ovk2liwc.py < file.csv
20190211 erikt(at)xs4all.nl
"""
import csv
import sys
import importlib
t2l = importlib.import_module("tactus2liwc-en")
LIWCFILE = "Dutch_LIWC2015_Dictionary.dic"
CLIENT = "CLIENT"
COUNSELOR = "COUNSELOR"
CLIENTIDIN = "client-id"
CLIENTIDOUT = "CLIENTID"
SENDERIN = "sender"
SENDEROUT = "Sender"
NBROFTOKENS = "NBROFTOKENS"
NBROFMATCHES = "NBROFMATCHES"
NBROFSENTS = "NBROFSENTS"
RECIPIENT = "recipient"
DATEIN = "date"
DATEOUT = "DATE"
SUBJECT = "subject"
BODY = "text"
TIMEFRAME = "TIMEFRAME"
CLIENTIDID = 0
SENDERID = 1
SUBJECTID = 4
BODYID = 5
SETSIZE = 3
METADATA = [CLIENTIDOUT,TIMEFRAME,DATEOUT,NBROFSENTS,SENDEROUT,NBROFTOKENS,NBROFMATCHES]
KEYPREFIX = "COUNS_"
def readEmailData():
emails = []
csvreader = csv.DictReader(sys.stdin)
for row in csvreader:
row = dict(row)
emails.append([row[CLIENTIDIN],row[SENDERIN],row[RECIPIENT],row[DATEIN],row[SUBJECT],row[BODY]])
return(emails)
def countMails(emails,sender):
return(len([email for email in emails if email[SENDERID] == sender]))
def addToSet(sender,set,element,nbrOfUsed):
if element[SENDERID] == sender:
if len(set) == 0:
set = element
nbrOfUsed = 1
else:
set[SUBJECTID] += " "+ element[SUBJECTID]
set[BODYID] += " " + element[BODYID]
nbrOfUsed += 1
return(set,nbrOfUsed)
def combineData(emails,sender,start,end,setSize,getLast=False):
set = []
nbrOfUsed = 0
if not getLast:
i = start
while nbrOfUsed < setSize and i <= end:
set,nbrOfUsed = addToSet(sender,set,emails[i],nbrOfUsed)
i += 1
else:
i = end
while nbrOfUsed < setSize and i >= start:
set,nbrOfUsed = addToSet(sender,set,emails[i],nbrOfUsed)
i -= 1
return(set)
def combineMails(emailsIn):
currentId = ""
currentStart = -1
emailsOut = []
for e in range(0,len(emailsIn)):
if currentId == "":
currentId = emailsIn[e][CLIENTIDID]
currentStart = e
if currentId != emailsIn[e][CLIENTIDID] or e == len(emailsIn)-1:
firstMails = combineData(emailsIn,CLIENT,currentStart,e-1,SETSIZE)
if countMails(emailsIn[currentStart:e-1],CLIENT) >= 2*SETSIZE:
lastMails = combineData(emailsIn,CLIENT,currentStart,e-1,SETSIZE,getLast=True)
emailsOut.extend([firstMails, lastMails])
elif countMails(emailsIn[currentStart:e-1],CLIENT) > 0:
emailsOut.extend([firstMails])
firstMails = combineData(emailsIn,COUNSELOR,currentStart,e-1,SETSIZE)
if countMails(emailsIn[currentStart:e-1],COUNSELOR) >= 2*SETSIZE:
lastMails = combineData(emailsIn,COUNSELOR,currentStart,e-1,SETSIZE,getLast=True)
emailsOut.extend([firstMails, lastMails])
elif countMails(emailsIn[currentStart:e-1],COUNSELOR) > 0:
emailsOut.extend([firstMails])
currentStart = e
currentId = emailsIn[e][CLIENTIDID]
return(emailsOut)
def changeKeys(dictDataIn):
dictDataOut = {}
for key in dictDataIn:
newKey = KEYPREFIX+key
dictDataOut[newKey] = dictDataIn[key]
return(dictDataOut)
def printResults(features,results,clientids):
printData = {}
allFeatures = METADATA+list(features.values())
extraFeatures = [ KEYPREFIX+x for x in allFeatures ]
allFeatures.extend(extraFeatures)
csvwriter = csv.DictWriter(sys.stdout,fieldnames=allFeatures,restval=0)
csvwriter.writeheader()
lastClientId = ""
for r in range(0,len(results)):
results[r][CLIENTIDOUT] = clientids[r]
if clientids[r]+" "+results[r][SENDEROUT] != lastClientId: results[r][TIMEFRAME] = "T0"
else: results[r][TIMEFRAME] = "T1"
lastClientId = clientids[r]+" "+results[r][SENDEROUT]
printDataId = results[r][CLIENTIDOUT]+" "+results[r][TIMEFRAME]
if not printDataId in printData: printData[printDataId] = results[r]
else:
printDataItem = changeKeys(results[r])
for key in printDataItem: printData[printDataId][key] = printDataItem[key]
for key in sorted(printData.keys()):
csvwriter.writerow(printData[key])
def main(argv):
features,words,prefixes = t2l.readLiwcDict(t2l.LIWCDIR+LIWCFILE)
emails = readEmailData()
emailsCombined = combineMails(emails)
if len(emailsCombined) > 0:
results = t2l.emails2liwc(emailsCombined,features,words,prefixes)
clientids = [e[CLIENTIDID] for e in emailsCombined]
printResults(features,results,clientids)
if __name__ == "__main__":
sys.exit(main(sys.argv))