-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2Q1.py
177 lines (153 loc) · 3.83 KB
/
A2Q1.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
# build vocab
# train: calculate the value of (phi_k| y) *5
# done in python 2
# find max p(y|x) for all 5 classes //ignore words that were not part of training set
import json
import string
import math
import time
# from nltk.tokenize import sent_tokenize, word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
l=[0,0,0,0,0]
vocab={}
n=[0,0,0,0,0]
start=time.time()
with open('train.json', 'r') as f:
for line in f:
tweet = json.loads(line)
s=(int)(tweet['stars'])
s=s-1
# t=word_tokenize(tweet['text'])#counts fullstops and stuff as well
# t=tweet['text'].split()
t=CountVectorizer().build_analyzer()(tweet['text'])
l[s]=l[s]+len(t)
n[s]=n[s]+1
for w in t:
if w in vocab:
vocab[w][s]=vocab[w][s]+1
else:
wl=[1,1,1,1,1]
wl[s]=wl[s]+1
vocab[w]=wl
unchangvocab={}
unchangvocab=vocab
print(vocab['perfect'])
v=len(vocab)
print(len(vocab))
print(l)
ld=[0,0,0,0,0]
size=0
for i in [0,1,2,3,4]:
l[i]+=v
ld[i]=math.log(l[i])
print(i)
size+=n[i]
for i in [0,1,2,3,4]:
print(i)
print(n[i]/size)
n[i]=math.log(n[i]/size)
print(l)
print(n)
print(vocab['perfect'])
logvocab={}
for w,x in vocab.items():
for i in [0,1,2,3,4]:
# print(x[i])
# print(w)
x[i]=math.log(x[i])-ld[i]
logvocab[w]=x
correct=0
total=0
# n=[-1.8963699309639557, -2.508278510342461, -2.20936629854321, -1.5147894521793739, -0.8235872882885679]
print(n)
p=[0,0,0,0,0]
# sleep(100)
with open('test.json', 'r') as f:
for line in f:
tweet = json.loads(line)
s=(int)(tweet['stars'])
# t=tweet['text'].split()
t=CountVectorizer().build_analyzer()(tweet['text'])
for i in [0,1,2,3,4]:
p[i]=n[i]
# print(n)
for w in t:
if w in logvocab:
x=logvocab[w]
for i in [0,1,2,3,4]:
p[i]+=x[i]
prediction=p.index(max(p))+1
# print(p)
if s==prediction:
correct+=1
total+=1
# if prediction!=5:
# print(prediction)
print("test")
print(correct)
print(total)
print(correct/total*100)
end=time.time()
# print(end-start)
correct=0
total=0
# n=[-1.8963699309639557, -2.508278510342461, -2.20936629854321, -1.5147894521793739, -0.8235872882885679]
print(n)
# sleep(100)
with open('train.json', 'r') as f:
for line in f:
tweet = json.loads(line)
s=(int)(tweet['stars'])
# t=tweet['text'].split()
t=CountVectorizer().build_analyzer()(tweet['text'])
for i in [0,1,2,3,4]:
p[i]=n[i]
# print(n)
for w in t:
if w in logvocab:
x=logvocab[w]
for i in [0,1,2,3,4]:
p[i]+=x[i]
prediction=p.index(max(p))+1
# print(p)
if s==prediction:
correct+=1
total+=1
# if prediction!=5:
# print(prediction)
print("train")
print(correct)
print(total)
print(correct/total*100)
end=time.time()
# [712, 657, 1836, 8104, 18215]
# 186775
# [10843518, 5824327, 7304887, 12777019, 19583362]
# 0
# 1
# 2
# 3
# 4
# 0
# 0.15011255029240642
# 1
# 0.08140826216365785
# 2
# 0.10977018800759808
# 3
# 0.21985446985446985
# 4
# 0.43885452968186783
# [11030293, 6011102, 7491662, 12963794, 19770137]
# [-1.8963699309639557, -2.508278510342461, -2.20936629854321, -1.5147894521793739, -0.8235872882885679]
# [712, 657, 1836, 8104, 18215]
# [-1.8963699309639557, -2.508278510342461, -2.20936629854321, -1.5147894521793739, -0.8235872882885679]
# test
# 80278
# 133718
# 60.03529816479457
# [-1.8963699309639557, -2.508278510342461, -2.20936629854321, -1.5147894521793739, -0.8235872882885679]
# train
# 337262
# 534872
# 63.05471215543158