-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsentiment_analyzer.py
95 lines (79 loc) · 2.15 KB
/
sentiment_analyzer.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
from sklearn.naive_bayes import GaussianNB
from sklearn.feature_extraction.text import CountVectorizer
train = [
("i am happy","pos"),
("i love this music","pos"),
("this is my best friend","pos"),
("this is an awesome day","pos"),
("i am sad","neg"),
("this is an amazing movie","pos"),
("i hate this movie","neg"),
("i am tired of this stuff","neg"),
("i cant deal with this","neg"),
]
train_features = []
train_labels = []
for data in train:
train_features.append(data[0])
train_labels.append(data[1])
print(train_features)
print(train_labels)
"""
['i am happy', 'i love this music', 'this is my best friend', 'this is an awesome day', 'i am sad', 'this is an amazing movie', 'i hate this movie', 'i am tired of this stuff', 'i cant deal with this']
['pos', 'pos', 'pos', 'pos', 'neg', 'pos', 'neg', 'neg', 'neg']
"""
k = ["balu love mougli","balu love bageera"]
count_vect = CountVectorizer()
sample = count_vect.fit_transform(k)
print(sample.toarray())
"""
[[0 1 1 1]
[1 1 1 0]]
"""
k = ["balu love pickachu"]
k = count_vect.transform(k)
print(k.toarray())
"""
[[0 1 1 0]]
"""
x_train = count_vect.fit_transform(train_features)
print(x_train.toarray())
"""
[[1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0]
[0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0]
[0 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0]
[0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0]
[0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1]]
"""
y_train = []
for data in train_labels:
if data == "pos":
y_train.append(0)
elif data == "neg":
y_train.append(1)
print(y_train)
"""
[0, 0, 0, 0, 1, 0, 1, 1, 1]
"""
classifier = GaussianNB()
classifier.fit(x_train.toarray(),y_train)
k = count_vect.transform(["i am crazy"])
output = classifier.predict(k.toarray())
#print(output)
"""
[0]
"""
"""
[1]
"""
if output[0] == 0:
print("positve")
else:
print("negative")
"""
negative
"""