Download this file

451 lines (369 with data), 21.1 kB

  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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#AUTHOR: RAHUL VERMA and SPIRO RAZIS
import sys
import re
import pprint
import numpy
from sklearn import svm
from sklearn import linear_model
import time
from random import shuffle
start_time = time.time()
numpy.set_printoptions(threshold=numpy.nan)
def parseTextViaPMCID(textFile, pmcidFeatureList, uniqueWordsDictionary,lim):
if textFile.startswith("beneficial"):
#print("beneficial")
fileType = "beneficial".encode('utf-8')
elif textFile.startswith("harmful"):
#print("harmful")
fileType = "harmful".encode('utf-8')
else:
#print("invalid file name")
sys.exit(2)
limit = 0
entryCount = 0
disease = ""
causeOrTreatment = ""
relation = ""
newEntry = False
with open(textFile, "r") as openedTextFile:
for line in openedTextFile:
if limit < lim:
if line.startswith("pmcid : "): #it's the idNumber
entryCount += 1
newEntry = True
elif line.startswith("sentence: "): #it's a sentence
pass
elif line.startswith("entities: "): #it's the two in a relationship
disease = line[11:line.index(",")].lower().encode('utf-8')
causeOrTreatment = line[(line.index(",")+2):-2].lower().encode('utf-8')
#add disease and cause/treatment to dictionary of unique words/phrases
if disease not in uniqueWordsDictionary:
uniqueWordsDictionary[disease] = {}
if causeOrTreatment not in uniqueWordsDictionary:
uniqueWordsDictionary[causeOrTreatment] = {}
elif line.startswith("offsets : "): #the position of the entities
pass
elif line.startswith("relation: "): #the actual relationship
relation = line[10:-1].lower().encode('utf-8')
else:
if line.startswith("\n") and (newEntry == True):
pmcidFeatureList.append([disease, causeOrTreatment, relation, fileType])
disease = ""
causeOrTreatment = ""
relation = ""
newEntry = False
limit += 1
else:
print("invalid line: %s" %(line))
sys.exit(2)
else: break
return (pmcidFeatureList, entryCount, uniqueWordsDictionary)
def printFeatureWithCellValue(numpyRow, featureRow):
for index, feature in enumerate(featureRow):
print("%s: %d" %(feature, numpyRow[index]))
print("harmfulOrBeneficial: %d" %(numpyRow[-1]))
return
def printFeaturesWithValuesEqualOne(numpyRow, featureRow):
for index, feature in enumerate(featureRow):
if numpyRow[index] == 1:
print("%s: %d" %(feature, numpyRow[index]))
print("harmfulOrBeneficial: %d" %(numpyRow[-1]))
return
def parseEntitiesIntoUnigrams(beneficialFile, harmfulFile, beneficialLimit, harmfulLimit):
beneficialEntry = 0
harmfulEntry = 0
entitiesTrainingDictionary= {}
entityUnigramList = []
beneficialFullEntitiesList = []
harmfulFullEntitiesList = []
sentenceUnigramList = []
beneficialSplitSentences = []
harmfulSplitSentences = []
entityUnigrams = {}
harmfulUnigrams = {}
beneficialUnigrams = {}
sentenceFeatureUnigrams = {}
testArrayForWritingEntries = numpy.empty(shape = (1, 1), dtype = "S128")
#WORKING ON THE UNIGRAMS OF THE TRAINING BENEFICIAL ENTITIES HERE
with open(beneficialFile, "r") as openedBeneficialFile:
for line in openedBeneficialFile:
if beneficialEntry < beneficialLimit:
if line.startswith("entities: "):
#individual entities
disease = line[11:line.index(",")].lower().encode('utf-8')
causeOrTreatment = line[(line.index(",")+2):-2].lower().encode('utf-8')
if disease not in entitiesTrainingDictionary:
entitiesTrainingDictionary[disease] = {}
if causeOrTreatment not in entitiesTrainingDictionary:
entitiesTrainingDictionary[causeOrTreatment] = {}
#unigrams composing the entities
entityUnigramList = re.split("-|, |\. |\/| ", line[11:-2].lower())
for entry in entityUnigramList:
if (entry != "") and (entry not in entityUnigrams):
entityUnigrams[entry] = {}
beneficialEntry += 1
else: break
#WORKING ON THE TRAINING HARMFUL ENTITIES HERE
with open(harmfulFile, "r") as openedHarmfulFile:
for line in openedHarmfulFile:
if harmfulEntry < harmfulLimit:
if line.startswith("entities: "):
#individual entities
disease = line[11:line.index(",")].lower().encode('utf-8')
causeOrTreatment = line[(line.index(",")+2):-2].lower().encode('utf-8')
if disease not in entitiesTrainingDictionary:
entitiesTrainingDictionary[disease] = {}
if causeOrTreatment not in entitiesTrainingDictionary:
entitiesTrainingDictionary[causeOrTreatment] = {}
entityUnigramList = re.split("-|, |\. |\/| ", line[11:-2].lower())
for entry in entityUnigramList:
if (entry != "") and (entry not in entityUnigrams):
entityUnigrams[entry] = {}
harmfulEntry += 1
else: break
beneficialEntry = 0
mostRecentPMCID = ""
with open(beneficialFile, "r") as openedBeneficialFile:
for line in openedBeneficialFile:
if line.startswith("pmcid : "): #it's the pmcid line
mostRecentPMCID = line[11:-1]
elif line.startswith("sentence: "):
sentenceUnigramList = re.split("\—|\-|\, |\.|\/|\(|\)|\'|\"|\[|\]|\ |\“|\”|\,|\d|\<|\>|\:|\$|\%|\*|\′", line[10:-2].lower())
beneficialSplitSentences.append(sentenceUnigramList)
if beneficialEntry < beneficialLimit:
for word in sentenceUnigramList:
if (word != "") and (word not in entityUnigrams):
if word not in sentenceFeatureUnigrams:
try:
testArrayForWritingEntries[0,0] = word
sentenceFeatureUnigrams[word] = {}
sentenceFeatureUnigrams[word]["beneficial"] = {}
sentenceFeatureUnigrams[word]["beneficial"]["pmcid"] = {}
sentenceFeatureUnigrams[word]["beneficial"]["pmcid"][mostRecentPMCID] = {}
sentenceFeatureUnigrams[word]["beneficial"]["count"] = 0
sentenceFeatureUnigrams[word]["harmful"] = {}
sentenceFeatureUnigrams[word]["harmful"]["pmcid"] = {}
sentenceFeatureUnigrams[word]["harmful"]["count"] = 0
except UnicodeEncodeError: pass
else: #it is in the feature unigrams already, so add the
if mostRecentPMCID not in sentenceFeatureUnigrams[word]["beneficial"]["pmcid"]: #and the same pmcid isn't already there
sentenceFeatureUnigrams[word]["beneficial"]["pmcid"][mostRecentPMCID] = {}
beneficialEntry += 1
elif line.startswith("entities: "):
#individual entities
disease = line[11:line.index(",")].lower().encode('utf-8')
causeOrTreatment = line[(line.index(",")+2):-2].lower().encode('utf-8')
beneficialFullEntitiesList.append([disease, causeOrTreatment])
else: pass
harmfulEntry = 0
mostRecentPMCID = ""
with open(harmfulFile, "r") as openedHarmfulFile:
for line in openedHarmfulFile:
if line.startswith("pmcid : "): #it's the pmcid line
mostRecentPMCID = line[11:-1]
elif line.startswith("sentence: "):
sentenceUnigramList = re.split("\—|\-|\, |\.|\/|\(|\)|\'|\"|\[|\]|\ |\“|\”|\,|\d|\<|\>|\:|\$|\%|\*|\′", line[10:-2].lower())
harmfulSplitSentences.append(sentenceUnigramList)
if harmfulEntry < harmfulLimit:
for word in sentenceUnigramList:
if (word != "") and (word not in entityUnigrams):
if word not in sentenceFeatureUnigrams:
try:
testArrayForWritingEntries[0,0] = word
sentenceFeatureUnigrams[word] = {}
sentenceFeatureUnigrams[word]["beneficial"] = {}
sentenceFeatureUnigrams[word]["beneficial"]["pmcid"] = {}
sentenceFeatureUnigrams[word]["beneficial"]["count"] = 0
sentenceFeatureUnigrams[word]["harmful"] = {}
sentenceFeatureUnigrams[word]["harmful"]["pmcid"] = {}
sentenceFeatureUnigrams[word]["harmful"]["pmcid"][mostRecentPMCID] = {}
sentenceFeatureUnigrams[word]["harmful"]["count"] = 0
except UnicodeEncodeError: pass
else:
if mostRecentPMCID not in sentenceFeatureUnigrams[word]["harmful"]["pmcid"]: #and the same pmcid isn't already there
sentenceFeatureUnigrams[word]["harmful"]["pmcid"][mostRecentPMCID] = {}
harmfulEntry += 1
elif line.startswith("entities: "):
disease = line[11:line.index(",")].lower().encode('utf-8')
causeOrTreatment = line[(line.index(",")+2):-2].lower().encode('utf-8')
harmfulFullEntitiesList.append([disease, causeOrTreatment])
else: pass
for word in sentenceFeatureUnigrams:
for benefitHarmfulOrEntity in sentenceFeatureUnigrams[word]:
#start counting!
for pmcid in sentenceFeatureUnigrams[word][benefitHarmfulOrEntity]["pmcid"]:
sentenceFeatureUnigrams[word][benefitHarmfulOrEntity]["count"] += 1
if (sentenceFeatureUnigrams[word]["beneficial"]["count"] > 1) or (sentenceFeatureUnigrams[word]["harmful"]["count"] > 1):
if sentenceFeatureUnigrams[word]["beneficial"]["count"] > (2*sentenceFeatureUnigrams[word]["harmful"]["count"]):
beneficialUnigrams[word] = {}
elif sentenceFeatureUnigrams[word]["harmful"]["count"] > (2*sentenceFeatureUnigrams[word]["beneficial"]["count"]):
harmfulUnigrams[word] = {}
else: pass #the words can't be categorized one way or the other
return (entitiesTrainingDictionary,
beneficialUnigrams, harmfulUnigrams,
beneficialEntry, harmfulEntry,
beneficialSplitSentences, harmfulSplitSentences,
beneficialFullEntitiesList, harmfulFullEntitiesList)
def main(argv):
#Python3 training.py beneficial.txt harmful.txt
if len(argv) != 3:
print("invalid number of arguments")
sys.exit(2)
#two separate lists because don't know how many entries in each, so dividing one list will be difficult
(entitiesTrainingDictionary, beneficialUnigrams, harmfulUnigrams,
beneficialCount, harmfulCount, pmcidBeneficialSentences, pmcidHarmfulSentences,
beneficialFullEntitiesList, harmfulFullEntitiesList) = parseEntitiesIntoUnigrams(argv[1], argv[2], 10356, 9797)
benprec = 10356/beneficialCount
harmprec = 9797/harmfulCount
numFeatures = len(entitiesTrainingDictionary) + len(beneficialUnigrams) + len(harmfulUnigrams) + 1 #plus 1 for harmful or beneficial
uniqueFeaturesArray = numpy.empty(shape = (1, numFeatures), dtype="S128")
#place the dictionary words into the array
for index, feature in enumerate(entitiesTrainingDictionary):
uniqueFeaturesArray[0, index] = feature
finalColumn = len(entitiesTrainingDictionary)
for index, feature in enumerate(beneficialUnigrams):
currentColumn = index + finalColumn
uniqueFeaturesArray[0, currentColumn] = feature
finalColumn += len(beneficialUnigrams)
for index, feature in enumerate(harmfulUnigrams):
currentColumn = index + finalColumn
uniqueFeaturesArray[0, currentColumn] = feature
uniqueFeaturesArray[0][:-1].sort()
beneficial80Percent = int(beneficialCount * benprec)-1
beneficial20Percent = int(beneficialCount - beneficial80Percent)
harmful80Percent = int(harmfulCount * harmprec)-1
harmful20Percent = int(harmfulCount - harmful80Percent)
trainArray = numpy.empty(shape=((beneficial80Percent + harmful80Percent), numFeatures), dtype=numpy.int8) #Default is numpy.float64
testArray = numpy.empty(shape=((beneficial20Percent + harmful20Percent), numFeatures), dtype=numpy.int8)
#training data
for entry in range(0, beneficial80Percent):
#for each entry, find the index of the given feature
for word in pmcidBeneficialSentences[entry]:
#get the index of the given feature
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], word.encode("utf-8"))
if uniqueFeaturesArray[0][featureColumn] == word.encode("utf-8"):
trainArray[entry, featureColumn] = 1
for entity in beneficialFullEntitiesList[entry]:
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], entity)
if uniqueFeaturesArray[0][featureColumn] == entity:
trainArray[entry, featureColumn] = 1
trainArray[entry, -1] = 1
for entry in range(0, harmful80Percent):
trainingEntry = entry + beneficial80Percent
for word in pmcidHarmfulSentences[entry]:
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], word.encode("utf-8"))
if uniqueFeaturesArray[0][featureColumn] == word.encode("utf-8"):
trainArray[trainingEntry, featureColumn] = 1
for entity in harmfulFullEntitiesList[entry]:
#get the index of the given feature
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], entity)
if uniqueFeaturesArray[0][featureColumn] == entity:
trainArray[trainingEntry, featureColumn] = 1
#test data
for entry in range(0, beneficial20Percent):
dataEntry = entry + beneficial80Percent #finding next beneficial entry, starting from 60% until 80%
for word in pmcidBeneficialSentences[dataEntry]:
#get the index of the given feature
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], word.encode("utf-8"))
if uniqueFeaturesArray[0][featureColumn] == word.encode("utf-8"):
testArray[entry, featureColumn] = 1
for entity in beneficialFullEntitiesList[dataEntry]:
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], entity)
if uniqueFeaturesArray[0][featureColumn] == entity:
testArray[entry, featureColumn] = 1
testArray[entry, -1] = 1
for entry in range(0, harmful20Percent):
dataEntry = entry + harmful80Percent # finding the next harmful entry starting from 60% until 80%
testEntry = entry + beneficial20Percent #because the prior data entered ended with beneficial20Percent
for word in pmcidHarmfulSentences[dataEntry]:
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], feature.encode("utf-8"))
if uniqueFeaturesArray[0][featureColumn] == word.encode("utf-8"):
testArray[testEntry, featureColumn] = 1
for entity in harmfulFullEntitiesList[dataEntry]:
featureColumn = numpy.searchsorted(uniqueFeaturesArray[0][:-1], entity)
if uniqueFeaturesArray[0][featureColumn] == entity:
testArray[testEntry, featureColumn] = 1
###################################################CLASSIFICATION SECTION################################################################
#Here we set up our list for support vectors and our list for classes.
#We will setup lists to hold our support vectors our classes.
supportVectorsL = []
classesListL = []
for row in trainArray:
y1 = row[len(row)-1]
supportVectorsL.append(row[:-1])
classesListL.append(y1)
#Here we initialize our Linear classifier
supportVectors = numpy.asarray(supportVectorsL)
classesList = numpy.asarray(classesListL)
#Here we try out the linear regresion stuff
classifier = linear_model.LogisticRegression()
classifier.fit(supportVectors,classesList)
############Test our sets through our logisitc model##################
print("--------------------LOGISTIC------------------------")
logistic(classifier,testArray,"TEST")
print("--------------------SVM------------------------")
#Here we set up the svm
classifier = svm.SVC()
classifier.fit(supportVectors,classesList)
classifier.kernel="linear"
############Test our sets through our SVM model##################
SVC(classifier,testArray,"TEST")
sys.exit(0)
def SVC(classifier, testArray,t):
testpredictionarray = []
for row in testArray:
predictionvector = row[:-1]
if 1 in predictionvector:
predictionvector = [predictionvector]
prediction = classifier.predict(predictionvector)
pre = int(prediction[0])
else:
pre = -1
testpredictionarray.append(pre)
totalAccuray(testArray,testpredictionarray,t)
featAccuracy(testArray,testpredictionarray,t,1)
featAccuracy(testArray,testpredictionarray,t,2)
def logistic(classifier, testArray,t):
testpredictionarray = []
for row in testArray:
predictionvector = row[:-1]
if 1 in predictionvector:
predictionvector = [predictionvector]
prediction = classifier.predict(predictionvector)
pre = int(prediction[0])
else:
pre = -1
testpredictionarray.append(pre)
totalAccuray(testArray,testpredictionarray,t)
featAccuracy(testArray,testpredictionarray,t,1)
featAccuracy(testArray,testpredictionarray,t,2)
def totalAccuray(testArray,testpredictionarray,t):
testcounter = 0
#here we test for accuracy in the test set results.
for x in range(0,len(testArray)):
t1= testArray[x][len(testArray[x])-1]
t1 = int(t1)
if t1 == testpredictionarray[x]:
testcounter = testcounter + 1
accuracy= testcounter/len(testArray)
print(t+" set accuracy = " + str(accuracy))
def featAccuracy(testArray,testpredictionarray,t,y):
actual = 0
testcounter = 0
for x in range(0,len(testArray)):
l = list(testArray[x])
c = l.count(1)
if c == y:
actual+=1
t1= testArray[x][len(testArray[x])-1]
t1 = int(t1)
if t1 == testpredictionarray[x]:
testcounter = testcounter + 1
try:
accuracy= testcounter/actual
except ZeroDivisionError:
print(t+" set accuracy for only "+str(y)+" feature vectors = UNDEFINED")
return
print(t+" set accuracy for only "+str(y)+" feature vectors = " + str(accuracy))
main(sys.argv)
#