-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTraining_Fine_Classification.py
191 lines (183 loc) · 5.44 KB
/
Training_Fine_Classification.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
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##
#annotator=Annotator()
#st = NERTagger(read_property('StanfordNerClassifier'),read_property('StanfordNerJarPath'))
#f=open(read_property('coarse_classification_path'),"r")
#fi=open(read_property('FineOutputfilesPath')+'fine_classification.txt',"w")
#t_class=[]
#for line in f:
# label=line.split()[0]
# file_w=read_property('FineInputFiles')+label+"_training.txt"
# fa=open(file_w,"r")
# train_class=[]
# for each_line in fa:
# #print each_line
# l=each_line.split()[1].split(":")[1]
# #print l
# train_class.append(l)
# #print train_class
# fa.close()
# #print "The label is",label
# file_word=read_property('FineOutputfilesPath')+label+"_training_word.txt"
# file_POS=read_property('FineOutputfilesPath')+label+"_training_POS.txt"
# file_NER=read_property('FineOutputfilesPath')+label+"_training_NER.txt"
# file_Chunk=read_property('FineOutputfilesPath')+label+"_training_Chunk.txt"
# ################################################## TRAINING##############################################
#
# ######################### BAG OF WORDS ################################
# print "Training"
# f1=open(file_word,"r")
# corpus=[]
# for lines in f1:
# l=lines.split()
# words=""
# for w in l:
# words=words+w+" "
# corpus.append(words)
# vectorizer_words= CountVectorizer(min_df=1,ngram_range=(1, 2))
# X_words = vectorizer_words.fit_transform(corpus)
# f1.close()
# print "word feature extraction done"
#
#
# ######################### BAG OF WORDS OF POS ################################
# f2=open(file_POS,"r")
# corpus=[]
# for lines in f2:
# l=lines.split()
# words=""
# for w in l:
# words=words+w+" "
# corpus.append(words)
# vectorizer_POS= CountVectorizer(min_df=1,ngram_range=(1, 2))
# X_POS = vectorizer_POS.fit_transform(corpus)
# f2.close()
# print "POS feature extraction done"
#
#
# ######################### BAG OF WORDS OF NER ################################
# f3=open(file_NER,"r")
# corpus=[]
# for lines in f3:
# l=lines.split()
# words=""
# for w in l:
# words=words+w+" "
# corpus.append(words)
# vectorizer_NER= CountVectorizer(min_df=1,ngram_range=(1, 2))
# X_NER = vectorizer_NER.fit_transform(corpus)
# f3.close()
# print "NER feature extraction done"
#
#
# ######################### BAG OF WORDS OF Chunks ################################
# f4=open(file_Chunk,"r")
# corpus=[]
# for lines in f4:
# l=lines.split()
# words=""
# for w in l:
# words=words+w+" "
# corpus.append(words)
# vectorizer_Chunk= CountVectorizer(min_df=1,ngram_range=(1, 2))
# X_Chunk = vectorizer_Chunk.fit_transform(corpus)
# f4.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 ##############################################
#
# ######################### BAG OF WORDS ################################
# print "Testing"
# corpus=[]
# words=""
# l=line.split()
# for w in l:
# words=words+w+" "
# corpus.append(words)
# X_words = vectorizer_words.transform(corpus)
# print "word feature extraction for test done "
#
# ######################### BAG OF WORDS OF POS ################################
# corpus=[]
# text = nltk.word_tokenize(line)
# pos_seq=nltk.pos_tag(text)
# pos_tags=""
# for pos in pos_seq:
# pos_tags=pos_tags+pos[1]+" "
# corpus.append(pos_tags)
# X_POS = vectorizer_POS.transform(corpus)
# print "POS feature extraction for test done"
#
# ######################### BAG OF WORDS OF NER ################################
# corpus=[]
# ner=st.tag(line.split())
# ner_tag=""
# for n in ner:
# ner_tag=ner_tag+n[1]+" "
# corpus.append(ner_tag)
# X_NER= vectorizer_NER.transform(corpus)
# print "NER feature extraction for test done"
#
#
# ######################### BAG OF WORDS OF Chunks ################################
# corpus=[]
# chunks=annotator.getAnnotations(line)['chunk']
# chunk=""
# for elem in chunks:
# chunk=chunk+elem[1]+" "
# corpus.append(chunk)
# X_Chunk= vectorizer_Chunk.transform(corpus)
# print "Chunk feature extraction for test done done"
#
#
# 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)
# print test_class
# fi.write(label + ":")
# fi.write(test_class[0]+" ")
# fi.write(line.split(":")[1])
# t_class.append(label+":"+test_class[0])
#
#fi.close()
#f.close()
###################################### Accuracy Calculation ################################################
test_class_gold=[]
f=open(read_property('testfilepath'),'r')
for lines in f:
test_class_gold.append(lines.split()[0])
print t_class
print test_class_gold
print len(t_class)
print len(test_class_gold)
hits=0.00
for i in range(0,len(t_class)):
if t_class[i]==test_class_gold[i]:
print t_class[i]
hits=hits+1
print "Number of hits = ",hits
print "The accuracy is ",((hits/len(t_class))*100.0)," %"