forked from desh2608/crnn-relation-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_ddi.py
executable file
·221 lines (164 loc) · 6.39 KB
/
svm_ddi.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
from sklearn.feature_extraction import DictVectorizer
from sklearn import datasets
from sklearn import svm
from sklearn import cross_validation
from sklearn.cross_validation import KFold
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from nltk.tokenize import WordPunctTokenizer
from sklearn.metrics import classification_report
import nltk
import numpy as np
import re
import pickle
from geniatagger import GeniaTagger
tagger = GeniaTagger('/home/desh/geniatagger-3.0.2/geniatagger')
tokenizer = WordPunctTokenizer()
def preProcess(sent):
sent = re.sub(r"(dg)+","num",sent.lower())
sent = tokenizer.tokenize(sent)
sent = ' '.join(sent)
sent_list,_,_,_,_ = zip(*tagger.parse(sent))
sent = ' '.join(sent_list)
return sent
def find_sub_list(sl,l):
sll=len(sl)
for ind in (i for i,e in enumerate(l) if e==sl[0]):
if l[ind:ind+sll]==sl:
return ind,ind+sll-1
def dataRead(fname):
print "Input File Reading"
fp = open(fname, 'r')
samples = fp.read().strip().split('\n\n')
sent_lengths = [] #1-d array
sent_contents = [] #2-d array [[w1,w2,....] ...]
sent_lables = [] #1-d array
entity1_list = [] #2-d array [[e1,e1_s,e1_e,e1_t] [e1,e1_s,e1_e,e1_t]...]
entity2_list = [] #2-d array [[e1,e1_s,e1_e,e1_t] [e1,e1_s,e1_e,e1_t]...]
for sample in samples:
sent, ent1, relation = sample.strip().split('\n')
if len(sent.split()) > 100:
continue
sent_lables.append(relation)
sent_contents.append(sent)
e1 = sent.split().index('DRUGA')
e2 = sent.split().index('DRUGB')
entity1_list.append(e1)
entity2_list.append(e2)
return sent_contents, entity1_list, entity2_list, sent_lables
def makeFeatures(sent_list, entity1_list, entity2_list):
print 'Making Features'
word_list = []
d1_list = []
d2_list = []
type_list = []
pos_list = []
chunk_list = []
for sent, ent1, ent2 in zip(sent_list, entity1_list, entity2_list):
sent = preProcess(sent)
sent_list1, _, pos_list1, chunk_list1, _ = zip(*tagger.parse(sent))
# distance1 feature
d1 = []
for i in range(len(sent_list1)):
d1.append(abs(i-ent1))
#distance2 feature
d2 = []
for i in range(len(sent_list1)):
d2.append(abs(i-ent2))
word_list.append(sent_list1)
pos_list.append(pos_list1)
chunk_list.append(chunk_list1)
d1_list.append(d1)
d2_list.append(d2)
# type_list.append(t)
return word_list, pos_list, chunk_list, d1_list, d2_list, type_list
def makeVector(sent_list, entity1_list, entity2_list, pos_list, chunk_list):
features = []
#['her amiodarone', 10, 11, 'treatment']
for sent, ent1, ent2, pos, chunk in zip(sent_list, entity1_list, entity2_list, pos_list, chunk_list):
sent = sent.split()
sentf = {}
#---------------------Context Features------------------------------------------
#CF1 : any word between relation arguments
for k,i in enumerate(range(ent1+1, ent2)):
sentf["CF1_"+str(k)] = sent[i]
#CF2 : any pos between relation arguments
for k,i in enumerate(range(ent1+1, ent2)):
sentf["CF2_"+str(k)] = pos[i]
#CF3 : any bigram between relation arguments
sent_bigrams = list(nltk.bigrams(sent[ent1+1:ent2]))
for k,bigram in enumerate(sent_bigrams):
sentf['CF3_'+str(k)] = '-'.join(bigram)
#CF4 : word preciding first argument
if ent1 == 0:
sentf['CF4'] = '<S>'
else:
sentf['CF4'] = sent[ent1 - 1]
#CF5 : word prediding second arguments
if ent2 == 0:
sentf['CF5'] = '<S>'
else:
sentf['CF5'] = sent[ent2 - 1]
#CF6 : any three words succeeding the first arguments
if ent1 <= len(sent) - 3 :
sent_trigrams = list(nltk.trigrams(sent[ent1+1:]))
for k,trigram in enumerate(sent_trigrams):
sentf['CF6_'+str(k)] = '-'.join(trigram)
else:
sentf['CF6_0'] = '<E>'
#CF7 : any three succeeding the second arguments
if ent2 <= len(sent) - 3 :
sent_trigrams = list(nltk.trigrams(sent[ent2+1:]))
for k,trigram in enumerate(sent_trigrams):
sentf['CF7_'+str(k)] = '-'.join(trigram)
else:
sentf['CF7_0'] = '<E>'
#CF8 : sequence of chunk type between relation argumemts
sentf['CF8'] = '-'.join(chunk[ent1+1:ent2])
#CF9 : string of words between relation arguments
sentf['CF9'] = '-'.join(sent[ent1+1:ent2])
#CF13 : Distance between two arguments
sentf['CF13'] = abs(sent.index('DRUGA') - sent.index('DRUGB'))
#CF14 : Presence of puncuation sign between arguments
if sent[ent1 : ent2] == [','] or ['and'] or ['or'] or ['/'] :
sentf['CF14'] = True
else:
sentf['CF14'] = False
features.append(sentf)
return features
ftrain = './ddi/train_data.txt'
tr_sent_contents, tr_entity1_list, tr_entity2_list, tr_sent_lables = dataRead(ftrain)
ftest = './ddi/test_data.txt'
te_sent_contents, te_entity1_list, te_entity2_list, te_sent_lables = dataRead(ftest)
# label_dict = {'other':0, 'TrWP': 1, 'TeCP': 2, 'TrCP': 3, 'TrNAP': 4, 'TrAP': 5, 'PIP': 6, 'TrIP': 7, 'TeRP': 8}
label_dict = {'false':0, 'mechanism': 1, 'effect': 2, 'advise': 3, 'int': 4}
#print X_train
Y_train = [label_dict[i] for i in tr_sent_lables]
Y_train = np.array(Y_train)
Y_test = [label_dict[i] for i in te_sent_lables]
Y_test = np.array(Y_test)
tr_sent_list, tr_pos_list, tr_chunk_list, tr_d1_list, tr_d2_list, tr_type_list = makeFeatures(tr_sent_contents, tr_entity1_list, tr_entity2_list)
tr_features = makeVector(tr_sent_contents, tr_entity1_list, tr_entity2_list, tr_pos_list, tr_chunk_list)
te_sent_list, te_pos_list, te_chunk_list, te_d1_list, te_d2_list, te_type_list = makeFeatures(te_sent_contents, te_entity1_list, te_entity2_list)
te_features = makeVector(te_sent_contents, te_entity1_list, te_entity2_list, te_pos_list, te_chunk_list)
print "len X_train before", len(tr_features)
print "len X_test before", len(te_features)
#features = tr_features + te_features
vec = DictVectorizer()
train = vec.fit_transform(tr_features)#.toarray()
features_name = vec.get_feature_names()
print "number of features", len(features_name)
# label_dict = {'other':0, 'TrWP': 1, 'TeCP': 2, 'TrCP': 3, 'TrNAP': 4, 'TrAP': 5, 'PIP': 6, 'TrIP': 7, 'TeRP': 8}
label_dict = {'false':0, 'mechanism': 1, 'effect': 2, 'advise': 3, 'int': 4}
X_train = train
X_test = vec.transform(te_features)
print "X_train after",X_train.shape
print "X_test after",X_test.shape
acc_list = []
clf = svm.SVC(kernel='linear', C=0.1).fit(X_train, Y_train)
a = clf.score(X_test, Y_test)
print "accuracy", a
y_true = Y_test
y_pred = clf.predict(X_test)
print(classification_report(y_true, y_pred,[1,2,3,4],digits=4))