-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopicinfo2.py
279 lines (207 loc) · 7.74 KB
/
topicinfo2.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
# Isaac Julien
# Get labeled topic information
import csv
import pylab
import numpy as np
from datetime import datetime
from time import strptime
# Get labeled topic percentages for each turn, along with start and end times of each turn:
def read_data(filename, topics):
file = open(filename, 'r')
file.readline() # First line is field names.
SPEAKER_IDX = 2
TOPIC_IDX = 5
START_IDX = 0
STOP_IDX = 1
current_speaker = None
start_time = None
stop_time = None
num_topics = len(topics)
# Last feature is majority topic
features = np.zeros(num_topics)
turn_features = [] # features for each turn
turn_info = [] # (start time, end time) for each turn
contents = csv.reader(file, delimiter=',', quotechar='"')
speaker = None
for n, row in enumerate(contents):
speaker = row[SPEAKER_IDX]
if len(speaker) < 1:
continue
topic = row[TOPIC_IDX]
#if topic == '9':
# continue
if current_speaker is None:
current_speaker = speaker
time = row[START_IDX].split(".")[0] # Strip ms
start_time = datetime(*strptime(time, "%H:%M:%S")[0:6])
if speaker != current_speaker:
current_speaker = speaker
# Calculate feature percentages:
fsum = 0.0
for f in features:
fsum += f
for i in range(len(features)):
features[i] /= fsum
# TODO - THIS IS TEMPORARY:
for i in range(len(features)):
features[i] *= 1
# Save information
turn_features.append(features)
turn_info.append((start_time, stop_time, speaker))
# Reset start time:
time = row[START_IDX].split(".")[0] # Strip ms
start_time = datetime(*strptime(time, "%H:%M:%S")[0:6])
features = np.zeros(num_topics)
topic_index = topics.index(topic)
features[topic_index] += float(1)
time = row[STOP_IDX].split(".")[0] # Strip ms
stop_time = datetime(*strptime(time, "%H:%M:%S")[0:6])
topic_index = topics.index(topic)
features[topic_index] += float(1)
# Append final info:
turn_features.append(features)
turn_info.append((start_time, stop_time, speaker))
return turn_features, turn_info
# Topic information:
def topic_info(filename):
file = open(filename, 'r')
file.readline()
TOPIC_IDX = 5
topics = {}
contents = csv.reader(file, delimiter=',', quotechar='"')
for n, row in enumerate(contents):
topic = row[TOPIC_IDX]
if topic not in topics.keys():
topics[topic] = 1
else:
topics[topic] += 1
return topics.keys()
# Return list of (reactions, times):
def rxns(rfile):
file = open(rfile, 'r')
file.readline()
TIME_IDX = 2
user_data = []
contents = csv.reader(file, delimiter=',', quotechar='"')
num_skipped = 0
total = 0
for n, row in enumerate(contents):
time = row[TIME_IDX]
time = time.split(".")[0] # Strip ms
time = time.split(" ")[1] # Strip date
time = datetime(*strptime(time, "%H:%M:%S")[0:6])
REACTION = row[1]
QUESTIONS = row[4:13]
GENDER = 1 if row[14] == "male" else 0
PARTY = 0
if len(row[32]) > 0:
PARTY = 1 if row[32][-1] == 'y' else 2
MORE = row[26:29]
RELIGION = 0
rel = row[17].split(" ")[0]
if rel == "christian":
RELIGION = 1
elif rel == "jewish":
RELIGION = 2
elif rel == "none":
RELIGION = 3
elif rel == "muslim":
RELIGION = 4
features = [GENDER, PARTY, RELIGION] + QUESTIONS + MORE
skip = False
for feature in features:
if len(str(feature).strip()) < 1:
num_skipped += 1
skip = True
break
if skip:
continue
total += 1
user_data.append((features, REACTION, time))
print "Skipped " + str(num_skipped) + " data points with missing information."
print "Got " + str(total) + " useable data points."
return user_data
# Return features for turn associated with reaction time:
def get_topic_features(reaction_time, turn_info, turn_features):
for i, turn in enumerate(turn_info):
start_time = turn[0]
stop_time = turn[1]
if start_time < reaction_time < stop_time:
return turn_features[i]
return None
def get_lda_topic_features(reaction_time, turn_info, lda_features):
for i, turn in enumerate(turn_info):
start_time = turn[0]
stop_time = turn[1]
if start_time < reaction_time < stop_time:
return lda_features[i]
return None
def main():
# Number of points to use for classification:
num_points = 10000
rfile = 'resources/data/reactions_oct3_4project.csv'
user_data = rxns(rfile)
path = "resources/corpora/"
filename = path + "oct3_coded_transcript_sync.csv"
topics = topic_info(filename)
turn_features, turn_info = read_data(filename, topics)
# USER DATA: [(features, REACTION, time)]
# TURN INFO: [(start_time, stop_time, speaker)]
# TURN FEATURES: [features]
# Get LDA topic features:
lda_features = []
lda = open("lda_topics.txt", 'r')
for line in lda:
feature_vector = line.split()
for i in range(len(feature_vector)):
feature_vector[i] = feature_vector[i].split(":")[1]
lda_features.append(feature_vector)
features = []
labels = []
for data in user_data[:num_points]:
user_features = data[0]
REACTION = data[1].lower()
time = data[2]
# TODO - this is where you control whether you get LDA or Boydstun topic features:
# LDA:
#topic_features = get_lda_topic_features(time, turn_info, lda_features)
# Boydstun:
topic_features = get_topic_features(time, turn_info, turn_features)
if topic_features is None:
continue
all_features = user_features + topic_features.tolist()
reaction_map_all = {"obama:agree":0, "obama:disagree":1, "romney:agree":2, "romney:disagree":3,
"moderator:agree":4, "moderator:disagree":5, "obama:spin":6, "obama:dodge":7,
"romney:spin":8, "romney:dodge":9, "moderator:spin":10, "moderator:dodge":11}
reaction_map_negative_or_positive = {"obama:agree":1, "obama:disagree":0, "romney:agree":1, "romney:disagree":0,
"moderator:agree":1, "moderator:disagree":0, "obama:spin":2, "obama:dodge":2,
"romney:spin":2, "romney:dodge":2, "moderator:spin":2, "moderator:dodge":2}
# Write out label and features
label = str(reaction_map_all[REACTION])
features.append(all_features)
labels.append(label)
# Normalize features:
num_features = len(features[0])
maxes = np.zeros(num_features)
for j in range(num_features):
for i in range(len(features)):
if maxes[j] < float(features[i][j]):
maxes[j] = float(features[i][j])
for i in range(len(features)):
for j in range(num_features):
if maxes[j] > 0:
new_val = float(features[i][j]) / maxes[j]
features[i][j] = str(new_val)
# Write features and labels to file:
out = open("topicinfo.csv", 'w')
for i in range(len(features)):
feature_vector = features[i]
label = labels[i]
line = label + "\t"
for i in range(len(feature_vector)):
line += str(i) + ":" + str(feature_vector[i]) + "\t"
line += '\n'
out.write(line)
out.close()
if __name__ == "__main__":
main()