-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_thread_feats.py
308 lines (248 loc) · 10.9 KB
/
extract_thread_feats.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
# coding=utf-8
# python extract_thread_feats.py ../dataconversion/Viva_forum/samples/106long20threads 106long20threads.threadfeats.out
# python extract_thread_feats.py ../dataconversion/Viva_forum/samples/kankerthreads kankerthreads.threadfeats.out
# python extract_thread_feats.py ../dataconversion/GIST_FB/threads GIST_FB.threadfeats.out
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
import math
import fileinput
from collections import defaultdict
import xml.etree.ElementTree as ET
#from sklearn import datasets
#from sklearn.feature_extraction import DictVectorizer
#from sklearn.feature_extraction.text import CountVectorizer
#from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np
#import matplotlib
# Force matplotlib to not use any Xwindows backend.
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
#from sklearn.datasets import make_blobs
#from sklearn.cluster import KMeans
#from sklearn.cluster import AgglomerativeClustering
#from sklearn.cluster import SpectralClustering
#import numpy
#from scipy.sparse import coo_matrix, hstack
from scipy.linalg import norm
rootdir = sys.argv[1]
outfile = sys.argv[2]
#postcounts = dict()
#postcountfilename = "../dataconversion/postcountperthread.reddit.txt"
#if re.match(".*Viva.*",rootdir):
# postcountfilename = "../dataconversion/postcountperthread.viva.txt"
#sys.stderr.write("Read postcountperthread file\n")
#with open(postcountfilename,"r") as postcountfile:
# for line in postcountfile:
# parts = line.rstrip().split("\t")
# filename = parts[0]
# postcount = parts[1]
# postcounts[filename] = postcount
#print filename, postcount
#postcountfile.close()
#ngram_vectorizer = CountVectorizer(analyzer='char_wb', ngram_range=(4, 4), min_df=1)
def tokenize(t):
text = t.lower()
text = re.sub("\n"," ",text)
text = re.sub('[^a-zèéeêëûüùôöòóœøîïíàáâäæãåA-Z0-9- \']', "", text)
wrds = text.split()
return wrds
def fast_cosine_sim(a, b):
if len(b) < len(a):
a, b = b, a
up = 0
for key, a_value in a.iteritems():
b_value = b.get(key, 0)
up += a_value * b_value
if up == 0:
return 0
return up / norm(a.values()) / norm(b.values())
title_column = list()
threadid_column = list()
bodyoffirstpost_column = list()
postcount_column = list()
postcountcutoff_column = list()
category_column = list()
length_of_title_column = list()
length_of_opening_post_column = list()
avg_postlength_column = list()
questioncount_column = list()
avgcossimwiththread_column = list()
avgcossimwithprevious_column = list()
sys.stderr.write("Read files in "+rootdir+"\n")
for f in os.listdir(rootdir):
if f.endswith("xml"):
tree = ET.parse(rootdir+"/"+f)
root = tree.getroot()
print f
for thread in root:
threadid = thread.get('id')
category = thread.find('category').text
title = thread.find('title').text
if title is None:
title = ""
termvectorforthread = dict() # key is term, value is termcount for full thread
termvectors = defaultdict(dict) # key is postid, value is dict with term -> termcount for post
cosinesimilaritiesthread = list()
cosinesimilaritiesprevious = list()
postcount_for_postid = dict()
postid_for_postcount = dict()
#sys.stderr.write(f+"\t"+title+"\n")
threadid_column.append(threadid)
title_column.append(title)
length_of_title_column.append(len(title))
category_column.append(category)
sum_postlength = 0
noofposts = 0
for posts in thread.findall('posts'):
firstpost = posts.findall('post')[0]
bodyoffirstpost = firstpost.find('body').text
bodyoffirstpost_column.append(bodyoffirstpost)
if bodyoffirstpost is None:
length_of_opening_post_column.append(0)
questioncount_column.append(0)
else:
length_of_opening_post_column.append(len(bodyoffirstpost))
questioncount_column.append(bodyoffirstpost.count('?'))
postcount = 0
for post in posts.findall('post'):
postid = post.get('id')
postcount_for_postid[postid] = postcount
postid_for_postcount[postcount] = postid
noofposts += 1
if postcount > 51:
continue
bodyofpost = post.find('body').text
#if threadid == "160543202978_10152800850187979":
# print threadid, postid, bodyofpost
if bodyofpost is None:
bodyofpost = ""
words = tokenize(bodyofpost)
if bodyofpost is not None:
sum_postlength += len(bodyofpost)
for word in words:
#print word, nrofsyllables(word)
if word in termvectorforthread: # dictionary over all posts
termvectorforthread[word] += 1
else:
termvectorforthread[word] = 1
if word in termvectors[postid].keys(): # dictionary per post
termvectors[postid][word] += 1
else:
termvectors[postid][word] = 1
postcount += 1
noofposts_cutoff = noofposts
if noofposts_cutoff > 50:
noofposts_cutoff = 50
postcount_column.append(noofposts)
postcountcutoff_column.append(noofposts_cutoff)
avg_postlength_column.append(sum_postlength/noofposts)
#ngram_vectorizer.fit_transform(['jumpy fox'])
#ngram_vectorizer.fit_transform([title])
#print ngram_vectorizer.get_feature_names()
for postid in termvectors.keys():
termvectorforpost = termvectors[postid]
for word in termvectorforthread:
if word not in termvectorforpost:
termvectorforpost[word] = 0
termvectors[postid] = termvectorforpost
cossimthread = fast_cosine_sim(termvectors[postid], termvectorforthread)
cossimprevious = 0
#print postid
if postcount_for_postid[postid] > 1:
cossimprevious = fast_cosine_sim(termvectors[postid], termvectors[postid_for_postcount[postcount_for_postid[postid]-1]])
cosinesimilaritiesthread.append(cossimthread)
cosinesimilaritiesprevious.append(cossimprevious)
avgcossimwiththread = np.average(cosinesimilaritiesthread)
avgcossimwiththread_column.append(avgcossimwiththread)
avgcossimwithprevious = np.average(cosinesimilaritiesprevious)
avgcossimwithprevious_column.append(avgcossimwithprevious)
#sys.stderr.write("Compute matrices...\n")
#X = ngram_vectorizer.fit_transform(title_column)
#X = ngram_vectorizer.fit_transform(bodyoffirstpost_column)
#print ngram_vectorizer.fit(title_column).vocabulary_
#matrix = X.toarray()
nonsparse_dimensions = (postcount_column,postcountcutoff_column,length_of_title_column,length_of_opening_post_column,avg_postlength_column,questioncount_column,avgcossimwiththread_column,avgcossimwithprevious_column,category_column)
noofitems = len(title_column)
sys.stderr.write("No of items: "+str(noofitems)+"\n")
#noofdimensions_sparse = len(ngram_vectorizer.get_feature_names())
noofdimensions_nonsparse = len(nonsparse_dimensions)
#sys.stderr.write("No of dimensions (sparse): "+str(noofdimensions_sparse)+"\n")
sys.stderr.write("No of dimensions (nonsparse): "+str(noofdimensions_nonsparse)+"\n")
nonsparsematrix = [[0 for x in range(noofdimensions_nonsparse)] for y in range(noofitems)]
i=0
for title in title_column:
instance_array=[]
for column in nonsparse_dimensions:
instance_array.append(column[i])
nonsparsematrix[i] = instance_array
i += 1
#print threadid_column
#print nonsparsematrix
threadfeatsfile = open(outfile,'w')
threadfeatsfile.write("threadid\tpostcount\tpostcount_cutoff\tlength_of_title\tlength_of_opening_post\tavg_postlength\tquestion_count\tavg_cossim_with_thread\tavg_cossim_with_previous\tcategory\n")
ci =0
for instance_array in nonsparsematrix:
threadfeatsfile.write(threadid_column[ci])
for value in instance_array:
threadfeatsfile.write("\t"+str(value))
threadfeatsfile.write("\n")
ci += 1
threadfeatsfile.close()
#combimatrix = hstack([X,nonsparsematrix]).toarray()
#http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.sparse.coo_matrix.html
#X._shape = (noofitems,noofdimensions+1)
#i=0
#j=noofdimensions-1
#for value in postcount_column:
# sys.stderr.write("i:"+str(i)+" j:"+str(j)+"\n")
# X[i,j] = value
# i += 1
#print X.toarray()
#sys.stderr.write("Cluster threads...\n")
#for title in title_column:
# sys.stderr.write(title+"\n")
#nclusters_values = (2,4,8)
#nclusters_values = [4]
#for nclusters in nclusters_values:
#kmeans = KMeans(n_clusters=nclusters)
#spectral = SpectralClustering(n_clusters=nclusters)
#agglom = AgglomerativeClustering(n_clusters=nclusters)
#print kmeans.fit(X)
#kmeans_predictions = kmeans.fit_predict(X)
#kmeans_predictions_nonsparse = kmeans.fit_predict(nonsparsematrix)
#kmeans_predictions_combi = kmeans.fit_predict(combimatrix)
#spectral_predictions = spectral.fit_predict(X)
#print "n_clusters:", nclusters, "\tinertia:", kmeans.inertia_
#print "n_clusters:", nclusters, "\tn_leaves:", agglom.n_leaves_
#predictions = kmeans_predictions
#predictions = kmeans_predictions_nonsparse
#predictions = kmeans_predictions_combi
#predictions = spectral_predictions
#sys.stderr.write("No of predictions:"+str(len(predictions))+"\n")
#clusters = dict()
#j=0
#for title in title_column:
#sys.stderr.write(title+"\t")
#sys.stderr.write(str(predictions[j])+"\n")
#cluster_id = predictions[j]
#cluster = list()
#if clusters.has_key(cluster_id):
# cluster = clusters[cluster_id]
# cluster.append(title)
# clusters[cluster_id] = cluster
# j += 1
#print "\n\n>", nclusters, "clusters\n\n"
#for cluster_id in clusters:
# cluster = clusters[cluster_id]
# clustersize = len(cluster)
# print "\n--------\nCLUSTER", cluster_id, "(", clustersize,"threads)\n--------\n"
# i=0
# for title in cluster:
# featvalues = ""
# for column in nonsparse_dimensions:
# featvalues += ","+str(column[i])
# print "\t-", title,"(",featvalues,")"
# i += 1