-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis (1).py
More file actions
276 lines (193 loc) · 7.5 KB
/
sentiment_analysis (1).py
File metadata and controls
276 lines (193 loc) · 7.5 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
"""Sentiment Analysis.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1BcGnaVR6A3UQjUEBEKrn5ay0tlOAYRqC
"""
pip install pandas numpy nltk transformers scikit-learn matplotlib bertopic
import pandas as pd
# Load the dataset (replace 'dataset.csv' with your file path)
data = pd.read_csv('/content/sample_dataset.csv')
# Display the first few rows
print(data.head())
# Check if the dataset has null values
print(data.isnull().sum())
# Drop rows with missing values
data.dropna(inplace=True)
import re
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('punkt')
# Define preprocessing function
def preprocess_text(text):
# Remove URLs, mentions, hashtags, and special characters
text = re.sub(r"http\S+|@\S+|#\S+", "", text)
text = re.sub(r"[^a-zA-Z\s]", "", text)
text = text.lower()
# Remove stopwords
words = text.split()
stop_words = set(stopwords.words('english'))
words = [word for word in words if word not in stop_words]
return " ".join(words)
# Apply preprocessing to the dataset
data['Cleaned_Text'] = data['text_column'].apply(preprocess_text) # Replace 'text_column' with the actual column name containing text
print(data.head())
from transformers import pipeline
# Load the sentiment analysis model
sentiment_pipeline = pipeline("sentiment-analysis")
# Predict sentiment for each cleaned text
data['Sentiment'] = data['Cleaned_Text'].apply(lambda x: sentiment_pipeline(x)[0]['label'])
print(data[['Cleaned_Text', 'Sentiment']].head())
import matplotlib
from bertopic import BERTopic
# Fit BERTopic to the cleaned text
topic_model = BERTopic()
topics, _ = topic_model.fit_transform(data['Cleaned_Text'])
# Add topics to the dataset
data['Topic'] = topics
# Display topic information
print(topic_model.get_topic_info())
# Save the dataset with predictions and topics
data.to_csv('/content/sentiment_dashboard.csv', index=False)
print("Results saved to sentiment_dashboard.csv")
import os
import pandas as pd
from transformers import pipeline
# Define the folder path
folder_path = r"C:\Users\anilr\OneDrive\Desktop\sentiment results"
file_name = "sentiment_results.csv"
# Check if the folder exists; create if not
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Define file path
file_path = os.path.join(folder_path, file_name)
# Example data for sentiment analysis (replace with your own dataset)
example_data = [
"I love this product! It's amazing.",
"This is the worst experience I've ever had.",
"Completely neutral about this, it's okay.",
"Highly recommend this to everyone!",
"Terrible service, I won't come back.",
]
# Load sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# Analyze sentiments
sentiments = []
for text in example_data:
result = sentiment_pipeline(text)[0]
sentiments.append({"Text": text, "Sentiment": result['label'], "Confidence": result['score']})
# Create a DataFrame
df = pd.DataFrame(sentiments)
# Save DataFrame to CSV
df.to_csv(file_path, index=False)
print(f"Sentiment results saved to {file_path}")
pip install streamlit
from google.colab import files
uploaded = files.upload()
# After uploading, load the dataset using pandas
import pandas as pd
# Assuming the file is named 'dataset.csv', replace it with the actual filename
data = pd.read_csv('/content/sample_dataset.csv')
# Display the first few rows
print(data.head())
from google.colab import drive
drive.mount('/content/drive')
# Assuming the dataset is in the folder "MyDrive" and is named "dataset.csv"
data = pd.read_csv('/content/sample_dataset.csv')
# Display the first few rows
print(data.head())
import csv
# Assume 'data_to_export' is a list of dictionaries
# Check if the dataset has null values
print(data.isnull().sum())
# Optionally, check for the shape of the dataset
print("Shape of the dataset:", data.shape)
# Drop rows with missing values
data.dropna(inplace=True)
# Verify if the missing values are removed
print(data.isnull().sum())
# Upload dataset (from local machine)
from google.colab import files
uploaded = files.upload()
# Load the dataset
import pandas as pd
data = pd.read_csv('/content/sample_dataset.csv') # Replace with your actual filename
# Display the first few rows of the dataset
print(data.head())
# Check if the dataset has null values
print(data.isnull().sum())
# Drop rows with missing values
data.dropna(inplace=True)
# Verify if the missing values are removed
print(data.isnull().sum())
from google.colab import drive
drive.mount('/content/drive')
# Load the dataset from your Google Drive
data = pd.read_csv('/content/sample_dataset.csv') # Replace with your actual path
# Display the first few rows
print(data.head())
# Check for null values
print(data.isnull().sum())
# Drop rows with missing values
data.dropna(inplace=True)
# Verify if the missing values are removed
print(data.isnull().sum())
import re
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('punkt')
# Define preprocessing function
def preprocess_text(text):
# Remove URLs, mentions, hashtags, and special characters
text = re.sub(r"http\S+|@\S+|#\S+", "", text)
text = re.sub(r"[^a-zA-Z\s]", "", text)
text = text.lower()
# Remove stopwords
words = text.split()
stop_words = set(stopwords.words('english'))
words = [word for word in words if word not in stop_words]
return " ".join(words)
# Apply preprocessing to the dataset
data['Cleaned_Text'] = data['text_column'].apply(preprocess_text) # Replace 'text_column' with your actual column name
print(data.head())
from transformers import pipeline
# Load the sentiment analysis model
sentiment_pipeline = pipeline("sentiment-analysis")
# Predict sentiment for each cleaned text
data['Sentiment'] = data['Cleaned_Text'].apply(lambda x: sentiment_pipeline(x)[0]['label'])
print(data[['Cleaned_Text', 'Sentiment']].head())
import matplotlib.pyplot as plt
from transformers import pipeline
import pandas as pd
# Load the dataset
data = pd.read_csv('/content/sample_dataset.csv') # Replace with your file path
# Load the sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# Apply sentiment analysis to each row in the 'text_column'
data['Sentiment'] = data['text_column'].apply(lambda x: sentiment_pipeline(x)[0]['label'])
# Optionally, save the result with sentiment labels back to CSV
data.to_csv('/content/sentiment_analysis_results.csv', index=False)
# Display the data with predicted sentiment
print(data[['text_column', 'Sentiment']].head())
import matplotlib as plt
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", revision="714eb0f")
from transformers import pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", revision="714eb0f")
from transformers import pipeline
# Load the sentiment analysis model
sentiment_pipeline = pipeline("sentiment-analysis")
# Function to get sentiment for user input
def get_sentiment(input_text):
result = sentiment_pipeline(input_text)
sentiment = result[0]['label']
confidence = result[0]['score']
return sentiment, confidence
# Get user input
user_input = input("Enter a sentence for sentiment analysis: ")
# Get the sentiment prediction
sentiment, confidence = get_sentiment(user_input)
# Output the result
print(f"Sentiment: {sentiment}")
print(f"Confidence score: {confidence:.4f}")