-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccuracy.py
More file actions
140 lines (94 loc) · 2.78 KB
/
Copy pathaccuracy.py
File metadata and controls
140 lines (94 loc) · 2.78 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
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import with_statement
from __future__ import unicode_literals
# Import the libraries created for this task
from RulesClassifier import RulesClassifier
from LexiconClassifier import LexiconClassifier
from ml1 import MachineLearningClassifier
from PreProcess import pre_process
from TwitterHybridClassifier import TwitterHybridClassifier
from nltk import ConfusionMatrix
from pprint import pprint
import tokenizer
import nltk
import csv
# Import other libraries used
import pickle
import codecs
import os
import sys
f=codecs.open('first_edit.tsv','r+',encoding='utf8')
lines=f.readlines()
message=[]
sentiment=[]
tweets=[]
for line in lines:
x=line.split('\t')
a=tokenizer.tokenize(x[3])
b=nltk.pos_tag(a)
message.append(b)
sentiment.append(x[1])
for i in range(len(message)):
d=(message[i],sentiment[i])
tweets.append(d)
f=codecs.open('twitter-dev-gold-A.tsv','r+',encoding='utf8')
lines=f.readlines()
message1=[]
sentiment1=[]
tweets1=[]
for line in lines:
x=line.split('\t')
a=tokenizer.tokenize(x[5])
b=nltk.pos_tag(a)
message1.append(b)
sentiment1.append(x[4])
for i in range(len(message1)):
d1=(message1[i],sentiment1[i])
tweets1.append(d1)
tweet=tweets+tweets1
trainset=tweet
def confusion_matrix(gold,guess):
correct = 0
total = len(gold)
for i in range(len(gold)):
if guess[i] == gold[i]:
correct += 1
accuracy = float(correct) / float(total)
print('Accuracy: {:.2%}'.format(accuracy))
# Confusion Matrix
cm = ConfusionMatrix(gold, guess)
print(cm)
f=codecs.open('input.txt','r+',encoding='utf8')
lines=f.readlines()
f1=codecs.open('output.txt','r+',encoding='utf8')
lines1=f1.readlines()
Myobject=TwitterHybridClassifier(trainset)
#count = {'RB':0, 'LB':0, 'ML':0 }
observed = list()
answer = list()
for line in lines:
x=line.split('\t')
prediction=Myobject.classify(x[5])
if (len(prediction)==1):
result=prediction[0][0]
elif (len(prediction)==2):
result=prediction[1][0]
else:
result=prediction[2][0]
#count[method] += 1
observed.append(result)
#print(prediction)
for line in lines1:
line=line.strip()
line = line.replace('\\\\', '').replace('\\"', '"').replace("\\'", "'").replace('\\u2019', '\'').replace('\\u002c', ',')
x=line.split('\t')
SENTIMENT=x[4]
answer.append(SENTIMENT)
confusion_matrix(answer,observed)
'''print ('Statistics - Number of instances processed by each method')
print ('Rule Based: ',count['RB'])
print ('Lexicon Based: ',count['LB'])
print ('Machine Learning: ',count['ML'])'''