-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileparser.py
282 lines (228 loc) · 8.11 KB
/
fileparser.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
import sys
import re
import itertools
import functools
import json
import requests
import pymongo
from pymongo import MongoClient
import pprint
pp = pprint.PrettyPrinter(indent=2)
client = MongoClient()
database = client['rocconf']
collection = database['participation']
class Item(object):
xmin = ""
xmax = ""
intervals = []
class Interval(object):
index = 0
xmin = 0
xmax = 0
text = ""
def make_item(size,xmin,xmax):
item = Item()
item.xmin = xmin
item.xmax = xmax
item.intervals = [size]
return item
def make_interval(index,xmin,xmax,text,name):
interval = Interval()
interval.index = index
interval.xmin = xmin
interval.xmax = xmax
interval.text = text
interval.name = name
return interval
def add_interval(item, interval):
item.intervals[interval.index] = interval
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
def extractMin(inputfile):
match = re.search(r'\s+xmin = (\d+\.?\d*)', inputfile)
if(match): return match.group(1)
def extractMax(inputfile):
match = re.search(r'\s+xmax = (\d+\.?\d*)', inputfile)
if(match): return match.group(1)
def isSound(str):
return re.search(r'sounding',str)
def readFile(file):
inputfile = open(file)
# size = sum(1 for line in inputfile)
count = 0
size = 0
items = [] # a list of item object
intervals = []
sounds = []
for line in inputfile:
if count<3: next(inputfile)
elif count==3: size = int(filter(str.isdigit, line))
else:
# print line
match = re.search(r'\s+item \[(\d+)\]', line)
match_interval = re.search(r'\s+intervals \[(\d+)\]', line)
if(match_interval):
xmin = extractMin(next(inputfile))
xmax = extractMax(next(inputfile))
text = re.search(r'\s+text = (.+)',next(inputfile))
name = file.split('.')[0]
interval = make_interval(int(match_interval.group(1)),xmin,xmax,text.group(1),name)
intervals.insert(int(match_interval.group(1)),interval)
count+=1
for interval in intervals:
if(isSound(interval.text)):
sounds.append(interval)
# print len(sounds)
return sounds
def overlap(a,b):
# Filtering for duration of the speech
if((float(a.xmax) - float(a.xmin)) < 0.6):
return False
if((float(b.xmax) - float(b.xmin)) < 0.6):
return False
# Is a contained within b?
if (float(a.xmin) <= float(b.xmax) and float(a.xmin) >= float(b.xmin) and float(a.xmax) >= float(b.xmin) and float(a.xmax) <= float(b.xmax)):
return True
# Is b contained within a?
elif(float(b.xmin) <= float(a.xmax) and float(b.xmin) >= float(a.xmin) and float(b.xmax) >= float(a.xmin) and float(b.xmax) <= float(a.xmax)):
return True
else:
# These two are not contained in the other
return False
def sameSegment(max,min):
return max == min
def compare(item1, item2):
if float(item1.xmin)<float(item2.xmin): return -1
elif float(item1.xmin)>float(item2.xmin): return 1
else: return 0
def length(a):
return float(a.xmax)-float(a.xmin)
def adds(sounds):
total = 0
for s in sounds:
total+=length(s)
return total
print 'Argument', str(sys.argv)
files = []
dict = {}
counter = 0
overlapcount = 0
users = [] #readFile(sys.argv[1])[0].name
for arg in sys.argv[1:]:
users.append(readFile(arg)[0].name)
# dict['interrupted'] = 0 #being interrupted
# dict['interrupting'] = 0 #interrupting others
dict['interruption'] = {} #interruption duh
dict['turntaking'] = {} #turn taking duh
dict['participation'] = {} #speaking percentage duh
# get the session key for this data
first = sys.argv[1].split('/')
second = first[1].split('_')
dict['session_key'] = second[1]
for user in users:
dict['interruption'][user] = {}
dict['interruption'][user]['interrupting'] = 0
dict['interruption'][user]['interrupted'] = 0
for count in range(1,len(sys.argv)):
for count2 in range(count+1,len(sys.argv)):
#merge intervals
files.append(readFile(sys.argv[count]))
files.append(readFile(sys.argv[count2]))
#initiate turntakingkeys
dict['turntaking'][readFile(sys.argv[count])[0].name+'-'+readFile(sys.argv[count2])[0].name] = 0
dict['turntaking'][readFile(sys.argv[count2])[0].name+'-'+readFile(sys.argv[count])[0].name] = 0
dict['turntaking'][readFile(sys.argv[count])[0].name+'-'+readFile(sys.argv[count])[0].name] = None
dict['turntaking'][readFile(sys.argv[count2])[0].name+'-'+readFile(sys.argv[count2])[0].name] = None
#total sounding duration
dict['participation'][readFile(sys.argv[count])[0].name] = adds(readFile(sys.argv[count]))
dict['participation'][readFile(sys.argv[count2])[0].name] = adds(readFile(sys.argv[count2]))
#checking for overlap by comparing intervals in each audio
for a in readFile(sys.argv[count]):
for b in readFile(sys.argv[count2]):
if(float(a.xmin)>float(b.xmax)): continue #uncomparable
elif(float(b.xmin)>float(a.xmax)): continue #uncomparable
#interruption
#iterating client
# for user in users
for user in users:
if(overlap(a,b)):
# print a.name,a.xmin, a.xmax, b.name,b.xmin, b.xmax
if(a.xmin>b.xmin and user==a.name):
dict['interruption'][user]['interrupting'] += 1
elif(b.xmin>=a.xmin and user==a.name):
dict['interruption'][user]['interrupted'] += 1
else:
dict['interruption'][user]['interrupting'] += 1
overlapcount+=1
arr3 = []
flag = []
for afile in files:
for a in afile:
arr3.append(a)
arr3.sort(cmp=compare)
# # initial flagged interval
flag.append(arr3[0])
# print 'turn', flag[0].name, flag[0].xmin, flag[0].xmax, '*'
for a in arr3:
prevname = flag[0].name
if(float(a.xmax) <= float(flag[0].xmax) and float(a.xmin)>=float(flag[0].xmin)):
continue
else:
flag[0] = a
# print 'turn', flag[0].name, flag[0].xmin, flag[0].xmax
if(dict['turntaking'][prevname+'-'+flag[0].name]!=None):
dict['turntaking'][prevname+'-'+flag[0].name] +=1
for key in sorted(dict['turntaking'],key=len):
if(dict['turntaking'][key]!=None):
print key
else:
dict['turntaking'].pop(key, dict['turntaking'][key])
total = 0
for key in sorted(dict['participation'],key=len):
total+=dict['participation'][key]
dict['participation']['total'] = total
'''
pp.pprint(dict)
with open('result.json','w') as outfile:
json.dump(dict, outfile, indent=4,sort_keys=True, separators=(',',':'), ensure_ascii=False)
'''
# Replacing keys here for more readable formatting
final_dict = {}
final_dict['session_key'] = dict['session_key']
p = re.compile("Data/fixed_" + dict['session_key'] + "_")
interrupt_raw = dict['interruption']
interrupt_fixed = {}
for k,v in interrupt_raw.iteritems():
result = p.sub("",k)
final = result.split('_')
interrupt_fixed[final[0]] = v
final_dict['interruption'] = interrupt_fixed
participation_raw = dict['participation']
participation_fixed = {}
for k,v in participation_raw.iteritems():
if(key != "total"):
result = p.sub("",k)
final = result.split('_')
participation_fixed[final[0]] = v
else:
participation_fixed[k] = v
final_dict['participation'] = participation_fixed
turntaking_raw = dict['turntaking']
turntaking_fixed = {}
for k,v in turntaking_raw.iteritems():
result = p.sub("",k)
inter = result.split('-')
first = inter[0].split('_')
second = inter[1].split('_')
turntaking_fixed[first[0] + '-' + second[0]] = v
final_dict['turntaking'] = turntaking_fixed
#pp.pprint(final_dict)
pp.pprint(collection.insert_one(final_dict).inserted_id)
#does not work if there is no url duh
# url = './display'
# headers = {'content-type': 'application/json'}
# r = requests.post(url,data=j,headers=headers)