-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTraining_Coarse_Classification1.py
231 lines (171 loc) · 5.3 KB
/
Training_Coarse_Classification1.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
import re
import nltk
from sklearn.feature_extraction.text import CountVectorizer
from nltk.tag.stanford import NERTagger
from scipy.sparse import hstack
from sklearn.svm import LinearSVC
from practnlptools.tools import Annotator
from readproperties import read_property
##removing special characters from sentence##
def preprocess(raw_sentence):
sentence= re.sub(r'[$|.|!|"|(|)|,|;|`|\']',r'',raw_sentence)
return sentence
##making the file format ready to use##
def file_preprocess(filename):
corpus=[]
classes=[]
f=open(filename,'r')
lines=f.readlines()
for line in lines:
line=line.rstrip('\n')
if not (line=="\n"):
classes.append((line.split()[0]).split(":")[0])
for line in lines:
line=line.rstrip('\n')
line=preprocess(line)
sentence=""
words=line.split()
for i in range(0,len(words)):
if not(i==0):
sentence=sentence+(words[i])+" "
corpus.append(sentence)
f.close()
return corpus,classes
##Compute POS##
def compute_POS_Tags(corpus):
POS=[]
for sentence in corpus:
text = nltk.word_tokenize(sentence)
pos_seq=nltk.pos_tag(text)
pos_tags=""
for pos in pos_seq:
pos_tags=pos_tags+pos[1]+" "
POS.append(pos_tags)
return POS
##Compute NER##
def compute_NER(corpus):
NER=[]
#fi=open("NER_features_train.txt","w")
st = NERTagger(read_property('StanfordNerClassifier'),read_property('StanfordNerJarPath'))
for sentence in corpus:
ner=st.tag(sentence.split())
ner_tag=""
for n in ner:
ner_tag=ner_tag+n[1]+" "
NER.append(ner_tag)
return NER
##Compute Chunks##
def compute_Chunks(corpus):
Chunk_Tags=[]
annotator=Annotator()
for sentence in corpus:
chunks=annotator.getAnnotations(sentence)['chunk']
chunk=""
for elem in chunks:
chunk=chunk+elem[1]+" "
Chunk_Tags.append(chunk)
return Chunk_Tags
######################################TRAINING############################################
#######Train class labels#####
train_class=[]
f=open(read_property('trainingfilepath'),'r')
lines=f.readlines()
for line in lines:
line=line.rstrip('\n')
if not (line=="\n"):
train_class.append((line.split()[0]).split(":")[0])
###words in question###
print "Training"
f=open(read_property('word_features_train_coarse_path'),"r")
corpus=[]
for lines in f:
l=lines.split()
words=""
for w in l:
words=words+w+" "
corpus.append(words)
vectorizer_words= CountVectorizer(min_df=1)
X_words = vectorizer_words.fit_transform(corpus)
f.close()
print "word feature extraction done"
###POS tags in question###
f=open(read_property('POS_features_train_coarse_path'),"r")
corpus=[]
for lines in f:
l=lines.split()
words=""
for w in l:
words=words+w+" "
corpus.append(words)
vectorizer_POS= CountVectorizer(min_df=1)
X_POS = vectorizer_POS.fit_transform((corpus))
f.close()
print "POS feature extraction done"
###NER tags in question###
f=open(read_property('NER_features_train_coarse_path'),"r")
corpus=[]
for lines in f:
l=lines.split()
words=""
for w in l:
words=words+w+" "
corpus.append(words)
vectorizer_NER= CountVectorizer(min_df=1)
X_NER = vectorizer_NER.fit_transform((corpus))
print "Vectorize"
f.close()
print "NER feature extraction done"
###Chunk tags in question###
f=open(read_property('Chunk_features_train_path'),"r")
corpus=[]
for lines in f:
l=lines.split()
words=""
for w in l:
words=words+w+" "
corpus.append(words)
vectorizer_Chunk= CountVectorizer(min_df=1)
X_Chunk = vectorizer_Chunk.fit_transform((corpus))
f.close()
print "Chunk feature extraction done"
X=hstack((X_words,X_POS))
X_train=hstack((X,X_NER))
X_train=hstack((X_train,X_Chunk))
######################################TESTING############################################
print "In Testing"
filename_test=read_property('testfilepath')
corpus_test,test_class_gold=file_preprocess(filename_test)
###words in question###
X_words = vectorizer_words.transform(corpus_test)
print "Word features extracted"
###POS tags in question###
X_POS = vectorizer_POS.transform(compute_POS_Tags(corpus_test))
print "POS features extracted"
###NER tags in question###
X_NER = vectorizer_NER.transform(compute_NER(corpus_test))
print "NER features extracted"
###Chunk tags in question###
X_Chunk = vectorizer_Chunk.transform(compute_Chunks(corpus_test))
print "Chunk features extracted"
X=hstack((X_words,X_POS))
X_test=hstack((X,X_NER))
X_test=hstack((X_test,X_Chunk))
###################Applying the LinearSVC Classifier#########################
print "Applying SVC"
self = LinearSVC(loss='l2', dual=False, tol=1e-3)
self = LinearSVC.fit(self, X_train, train_class)
test_class = LinearSVC.predict(self, X_test)
#####Calculating success rate#####
hits=0.00
fi=open(read_property('coarse_classification_path'),"w")
for i in range(0,len(test_class)):
print test_class[i]," : ",corpus_test[i],"\n"
str_l=test_class[i]," : ",corpus_test[i],"\n"
fi.write(test_class[i]+" : ")
fi.write(corpus_test[i]+"\n")
fi.close()
for i in range(0,len(test_class)):
if test_class[i]==test_class_gold[i]:
hits=hits+1
print "Number of hits = ",hits
print "The accuracy is ",((hits/len(test_class))*100.0)," %"