forked from FS-CSCI150-F21/FS-CSCI150-F21-Team4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetSentiment.py
93 lines (72 loc) · 3.28 KB
/
tweetSentiment.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
from twitterScrapeV1 import twitterMentionFunct, tweetFormatJson
import json
import re
import pickle
def tweetSentimentAnalyzer(userName, totalTweets):
scrapeSuccess = False
tweetData, scrapeSuccess = twitterMentionFunct(userName=userName, tweetAmount=totalTweets)
if scrapeSuccess is False:
return {} , scrapeSuccess
else:
scrapeSuccess = True
tweetFormatJson("tweetText.json",tweetData)
with open("vectorizer.pickle", "rb") as pickle_in:
processedVector = pickle.load(pickle_in)
with open("LogisticRegClass.pickle", "rb") as pickle_in:
logicRegClass = pickle.load(pickle_in)
with open('tweetText.json', encoding='utf-8') as infile:
tweetJson = json.load(infile)
testEntries = tweetJson['data']
testProcessed = []
for sentence in testEntries:
sentence = sentence['text']
# Remove all the special characters
processed_feature = re.sub(r'\W', ' ', sentence)
# remove all single characters
processed_feature= re.sub(r'\s+[a-zA-Z]\s+', ' ', processed_feature)
# Remove single characters from the start
processed_feature = re.sub(r'\^[a-zA-Z]\s+', ' ', processed_feature)
# Substituting multiple spaces with single space
processed_feature = re.sub(r'\s+', ' ', processed_feature, flags=re.I)
# Removing prefixed 'b'
processed_feature = re.sub(r'^b\s+', '', processed_feature)
# Converting to Lowercase
processed_feature = processed_feature.lower()
testProcessed.append(processed_feature)
processed_features = processedVector.transform(testProcessed).toarray()
prediction = logicRegClass.predict(processed_features)
print(processed_features.shape)
print(prediction)
predictionList= prediction.tolist()
possitiveTweetsTot = predictionList.count('4')
negativeTweetsTot = predictionList.count('0')
print(f"Number of Positive tweets: {possitiveTweetsTot}")
print(f"Number of Negative tweets: {negativeTweetsTot}")
return {"tweet_postive": possitiveTweetsTot, "tweet_negative": negativeTweetsTot}, scrapeSuccess
def textSentimentAnalyzer(text):
with open("vectorizer.pickle", "rb") as pickle_in:
processedVector = pickle.load(pickle_in)
with open("LogisticRegClass.pickle", "rb") as pickle_in:
logicRegClass = pickle.load(pickle_in)
sentence = text
testProcessed = []
# Remove all the special characters
processed_feature = re.sub(r'\W', ' ', sentence)
# remove all single characters
processed_feature= re.sub(r'\s+[a-zA-Z]\s+', ' ', processed_feature)
# Remove single characters from the start
processed_feature = re.sub(r'\^[a-zA-Z]\s+', ' ', processed_feature)
# Substituting multiple spaces with single space
processed_feature = re.sub(r'\s+', ' ', processed_feature, flags=re.I)
# Removing prefixed 'b'
processed_feature = re.sub(r'^b\s+', '', processed_feature)
# Converting to Lowercase
processed_feature = processed_feature.lower()
testProcessed.append(processed_feature)
processed_features = processedVector.transform(testProcessed).toarray()
prediction = logicRegClass.predict(processed_features)
if prediction[0] == '4':
isPositive = True
else:
isPositive = False
return isPositive