-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc2.py
81 lines (69 loc) · 2.12 KB
/
wc2.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
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
from wordcloud import WordCloud
tweetFile = open("tweets.json", "r")
tweetData = json.load(tweetFile)
tweetFile.close()
long_text = []
for tweet in tweetData:
tweet_text = tweet["text"]
long_text.append(tweet_text)
long = " ".join(long_text)
list_words = long.split()
positive = []
negative = []
neutral = []
for word in list_words:
word = word.lower().rstrip().lstrip()
if "http" in word:
continue
if word[0] == "#" or word[0] == "@":
word = word [1: ]
if len(word) < 4:
continue
if word[-1] in {",", "?", ".", ":", "/", "!", '"', "'"}:
word = word[: -1]
word_polarity = TextBlob(word).polarity
if word_polarity < -0.25:
negative.append(word)
if word_polarity > 0.25:
positive.append(word)
else:
neutral.append(word)
positive_count = {}
positive_count2 = {}
for word in positive:
positive_count[word] = positive_count.get(word, 0) + 1
for word, count in positive_count.items():
if count < 2:
continue
else:
positive_count2[word] = count
word_cloud = WordCloud().generate_from_frequencies(positive_count2)
plt.imshow(word_cloud, interpolation = 'bilinear')
plt.show()
neutral_count = {}
neutral_count2 = {}
for word in neutral:
neutral_count[word] = neutral_count.get(word, 0) + 1
for word, count in neutral_count.items():
if count < 2:
continue
else:
neutral_count2[word] = count
word_cloud = WordCloud().generate_from_frequencies(neutral_count2)
plt.imshow(word_cloud, interpolation = 'bilinear')
plt.show()
negative_count = {}
negative_count2 = {}
for word in negative:
negative_count[word] = negative_count.get(word, 0) + 1
for word, count in negative_count.items():
if count < 2:
continue
else:
negative_count2[word] = count
word_cloud = WordCloud().generate_from_frequencies(negative_count2)
plt.imshow(word_cloud, interpolation = 'bilinear')
plt.show()