-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentiment_Analysis.py
175 lines (85 loc) · 2.91 KB
/
Sentiment_Analysis.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'''
# 1.Install and Import Dependencies
# In[26]:
get_ipython().system('pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117')
# In[27]:
get_ipython().system('pip install transformers requests beautifulsoup4 pandas numpy')
# In[28]:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import requests
from bs4 import BeautifulSoup
import re
# 2.Instantiate Model
# In[29]:
tokenizer=AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
model= AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
# 3.Encode and Calculate Sentiment
# In[38]:
tokens=tokenizer.encode('This is amazing, I loved it', return_tensors='pt')
# In[39]:
tokens
# In[40]:
tokenizer.decode(tokens[0])
# In[41]:
result=model(tokens)
# In[42]:
result
# In[43]:
int(torch.argmax(result.logits))+1
# 4.Collect Reviews
# In[44]:
r=requests.get('https://www.yelp.com/biz/mejico-sydney-2')
soup=BeautifulSoup(r.text, 'html.parser')
regex=re.compile('.*comment.*')
results=soup.find_all('p',{'class':regex})
reviews=[result.text for result in results]
# In[47]:
reviews[0]
# 5.Load Reviews into Dataframe and Score
# In[48]:
import pandas as pd
import numpy as np
# In[50]:
df=pd.DataFrame(np.array(reviews),columns=['review'])
# In[52]:
df['review'].iloc[0]
# In[53]:
def sentiment_score(review):
tokens=tokenizer.encode(review, return_tensors='pt')
result=model(tokens)
return int(torch.argmax(result.logits))+1
# In[56]:
sentiment_score(df['review'].iloc[4])
# In[57]:
df['sentiment']=df['review'].apply(lambda x: sentiment_score(x[:512]))
# In[59]:
df
# In[ ]:'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import requests
from bs4 import BeautifulSoup
import re
def sentiment_analyser(link):
tokenizer=AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
model= AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
#tokens=tokenizer.encode('This is amazing, I loved it', return_tensors='pt')
#tokenizer.decode(tokens[0])
#result=model(tokens)
#r=requests.get('https://www.yelp.com/biz/mejico-sydney-2')
r=requests.get(link)
soup=BeautifulSoup(r.text, 'html.parser')
regex=re.compile('.*comment.*')
results=soup.find_all('p',{'class':regex})
reviews=[result.text for result in results]
df=pd.DataFrame(np.array(reviews),columns=['review'])
def sentiment_score(review):
tokens=tokenizer.encode(review, return_tensors='pt')
result=model(tokens)
return int(torch.argmax(result.logits))+1
df['sentiment']=df['review'].apply(lambda x: sentiment_score(x[:512]))
return df