-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.py
73 lines (50 loc) · 1.78 KB
/
Model.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
from textblob import TextBlob
import pandas as pd
import re
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
def inputs():
flag = True
lines2 = []
while flag:
val = input("Enter the text: ")
lines2.append(val)
ans = input("Do u want to enter another tweet y/n? ")
flag = ans == 'y'
df = pd.DataFrame({'Tweets' : lines2})
df['Tweets'] = df['Tweets'].apply(cleanText)
df['Polarity'] = df['Tweets'].apply(getPolarity)
df['Analysis'] = df['Polarity'].apply(getAnalysis)
sortedDf = printHistory(df, True)
return (str(sortedDf))
def input(text):
lines2 = []
lines2.append(text)
df = pd.DataFrame({'Tweets' : lines2})
df['Tweets'] = df['Tweets'].apply(cleanText)
df['Polarity'] = df['Tweets'].apply(getPolarity)
df['Analysis'] = df['Polarity'].apply(getAnalysis)
sortedDf = printHistory(df, True)
return (str(sortedDf))
#Clean the text
def cleanText(text):
text = re.sub(r'@[A-za-z0-9]+' , '', text) #Remove mentions
text = re.sub(r'RT[\s]+', '', text) #remove retweets
text = re.sub(r'https?:\/\/\S+', '', text) #remove hyperlinks
return text
#Create a function to get polarity
def getPolarity(text):
return TextBlob(text).sentiment.polarity
#Create a fn to compute the negative, neutral and positive analysis
def getAnalysis(score):
if score < - 0.2:
return 'Negative'
elif score > 0.2:
return 'Positive'
else:
return 'Neutral'
def printHistory(df, asc = True, num = 5):
sortedDF = df.sort_values(by = ['Polarity'], axis=0, ascending = asc)
sortedDF = sortedDF.reset_index(drop=True)
sortedDF = sortedDF[['Tweets', 'Analysis']].iloc[:num]
return sortedDF