-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSenti.py
More file actions
73 lines (58 loc) · 1.96 KB
/
Copy pathSenti.py
File metadata and controls
73 lines (58 loc) · 1.96 KB
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 ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from textblob import TextBlob
#ANALYSING SENTIMENT AND RATING THEM FROM -1 TO +1
def analyse_sentimentwt(df , term):
authenticator = IAMAuthenticator('api-key')
language_translator = LanguageTranslatorV3(
version='date',
authenticator=authenticator
)
language_translator.set_service_url('url')
df[term].replace(['[deleted]','[removed]'],[1.1,1.1],inplace=True)
f_list=df[term].to_list()
for l in f_list:
l=l.encode('unicode-escape').decode('utf-8')
pol=list()
for l in f_list:
print(l,"\n")
if (l!=1.1):
language = language_translator.identify(l).get_result()
n=language['languages']
m=n[0]['language']
if(m=='hi'):
translation = language_translator.translate(text=l,model_id='hi-en').get_result()
p=translation['translations']
k=p[0]['translation']
l=k
analysis=TextBlob(l).sentiment
t=analysis.polarity
pol.append(t)
else:
pol.append(1.1)
df['sentiment']=pol
return df
def analyse_sentiment(df , term):
f_list=df[term].to_list()
pol=list()
for l in f_list:
analysis=TextBlob(l).sentiment
t=analysis.polarity
pol.append(t)
df['sentiment']=pol
pol2=list()
for l in pol:
if(l>0):
pol2.append("Positive")
elif(l<0):
pol2.append("Negative")
else:
pol2.append("Neutral")
df['roundoff']=pol2
df=df[df.sentiment != 0]
return df
def pretty_txt(input_value):
input_value = input_value.replace(" ", "")
input_value = input_value.replace("-", "")
input_value = input_value.replace("[^a-zA-Z#]", " ")
return (input_value)