-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_utils.py
191 lines (169 loc) · 5.91 KB
/
data_utils.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
import pickle
from gensim import corpora, models, similarities
from scipy.stats.stats import pearsonr as pr
import datetime
from random import shuffle
import math
'''
A file of self contained methods, used in preprocessing
'''
def pearsonsR(masterList):
# input masterlist[featurelist, featurelist, featurelist, featurelist ...]
# where featurelist has the label at last spot
# want pr(x,y) to output (1.0, 0.0) or (-1.0, 0.0). (0, 1) is very uncorrelated.
featlen = len(masterList[0]) - 1
y = [instance[-1] for instance in masterList]
countNot1 = 0
count1 = 0
for yval in y:
if yval != -1:
countNot1+=1
else:
count1+=1
print("Not -1: %d" %countNot1)
print("-1: %d" %count1)
exes = [[instance[j] for instance in masterList] for j in range(featlen)]
idx = 0
for x in exes:
firstVal = x[0]
out = True
for xval in x:
out = out and (xval == firstVal)
if out:
print('problem index ', idx)
idx += 1
vals = [pr(x,y) for x in exes]
return vals
def makeCollocated(corp,stops):
newCorp = list()
curDoc = list()
for word in corp:
if type(word) != str:
print(word, ' is not a string (data_utils.makeCollocated)')
elif word == "$|$":
newCorp.append(curDoc)
curDoc = list()
elif word in stops:
pass
else:
curDoc.append(word)
return newCorp
def applyFilters(bigrammer,filterList):
for f in filterList:
f(bigrammer)
return bigrammer
def collocRecursively(corp,constructor,threshhold,addUnrelated,addBigram,measureFunc,filters=None):
bgFinder = constructor(corp)
if filters:
bgFinder = applyFilters(bgFinder,filters)
bgScores = {bg:score for bg,score in bgFinder.score_ngrams(measureFunc)}
print(sorted(list(bgScores.items()),key=lambda tup: tup[1])[-6:])
idx = 0
N = len(corp)
newCorp = list()
flag = False
while idx < N-1:
bg = (corp[idx],corp[idx+1])
if bgScores.get((bg[0],bg[1]),0) > threshhold:
addBigram(newCorp,bg)
idx += 2
flag = True
else:
addUnrelated(newCorp,bg[0])
idx += 1
if idx == N-1:
addUnrelated(newCorp,corp[idx])
if flag:
return collocRecursively(newCorp, constructor, threshhold, addUnrelated, addBigram, filters)
return newCorp
def collocateAndLDA(allWords, stopFile):
with open(stopFile,"rU") as sf:
stops = {line.lower().strip() for line in sf.readlines()}
'''
constructor = lambda c: BigramCollocationFinder.from_words(c)
threshhold = 6000
addUnrelated = lambda c, x: c.append(x)
addBigram = lambda c, tup: c.append(tup[0]+"_"+tup[1])
measureFunc = BigramAssocMeasures().likelihood_ratio
filters = [lambda bg: bg.apply_word_filter(lambda t: (len(t) <2 and not t.isalnum()) or (t in set(punct) | {"$|$"})),\
lambda bg: bg.apply_ngram_filter(lambda w1,w2: (w1 in stops) and (w2 in stops))]
# return toLdaModel(makeCollocated(collocRecursively(\
# allWords,constructor,threshhold,addUnrelated,addBigram,measureFunc,filters),interpFunc),70)
'''
return toLdaModel(makeCollocated(allWords,stops),70)
def timeToDate(time):
weekend = 0
s= datetime.datetime.fromtimestamp(time)
dayOfTheWeek=s.strftime("%a")
if dayOfTheWeek=='Sat' or dayOfTheWeek=='Sun':
weekend = 1
hour24=s.strftime("%H")
bucket = math.floor(int(hour24)/6)
return (weekend,bucket)
def makeAllocationDict(trainFiles,testFiles,devFiles,annoteFiles):
allocationDict = dict() #userid to (int,int): first int for train,test,devtest, or dev, second for label
for annoteFile in annoteFiles:
with open(annoteFile) as f:
f.readline()
if "expert" in annoteFile:
allocationDict.update({line[0]:(1,int(line[1])) for line in [l.strip().split(",") for l in f]})
else:
allUsrs = f.readlines()
shuffle(allUsrs)
t = int(len(allUsrs) * 0.8)
allocationDict.update({line[0]:(0,int(line[1])) for line in [l.strip().split(",") for l in allUsrs[0:t]]})
allocationDict.update({line[0]:(2,int(line[1])) for line in [l.strip().split(",") for l in allUsrs[t::2]]})
allocationDict.update({line[0]:(3,int(line[1])) for line in [l.strip().split(",") for l in allUsrs[t+1::2]]})
for trainFile in trainFiles:
default = - ("controls" in trainFile)
with open(trainFile) as tfile:
allocationDict.update({line.strip():allocationDict.get(line.strip(),(0,default)) for line in tfile})
for testFile in testFiles:
default = - ("controls" in testFile)
val = - default # 1 (testSet) if controls, 0 (training) if otherwise
with open(testFile) as tfile:
allocationDict.update({line.strip():allocationDict.get(line.strip(),(val,default)) for line in tfile})
for devFile in devFiles:
default = - ("controls" in devFile)
with open(devFile) as dfile:
total = dfile.readlines()
allocationDict.update({line.strip():(allocationDict.get(line.strip(),(2,default))) for line in total[1::2]})
allocationDict.update({line.strip():(allocationDict.get(line.strip(),(2,default))) for line in total[0::2]})
with open("allocationDict.p","wb") as f:
print("Pickling allocator")
pickle.dump(allocationDict,f)
return allocationDict
def toLdaModel(docLists, num_topics):
dictionary = corpora.dictionary.Dictionary(docLists)
corpus = [dictionary.doc2bow(docList) for docList in docLists]
model = models.ldamulticore.LdaMulticore(corpus, num_topics,workers=7)
with open("ldaModel.p","wb") as f:
pickle.dump(model,f)
docVecs = list()
for doc in corpus:
docVec = [0] * num_topics
sparse = model.get_document_topics(doc)
for top,val in sparse:
docVec[top] = val
docVecs.append(docVec)
return docVecs, num_topics
def stitchTogether(numFs):
'''
:param postFs:
:param textFs:
:param timeFs:
:return: unpickles everything and returns union of each
'''
allPosts = list()
allText = list()
suicideTimes = dict()
for idx in range(numFs):
with open("allText_%d.p" %idx, "rb") as f:
allText += pickle.load(f)
with open("allPosts_%d.p" %idx, "rb") as f:
allPosts += pickle.load(f)
with open("suicideTimes_%d.p" %idx, "rb") as f:
dc = pickle.load(f)
for usr,lst in dc.items():
suicideTimes[usr] = suicideTimes.get(usr,list()) + lst
return allPosts,allText,suicideTimes