Skip to content

Commit 5ec8537

Browse files
authored
add script
1 parent c0b68a7 commit 5ec8537

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

Sentiment Analysis/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Sentiment Analysis using Support Vector Machine (SVM)
2+
3+
This Python script performs sentiment analysis on text data using a Support Vector Machine (SVM) classifier. It reads data from a CSV file, preprocesses the text, and trains an SVM model to classify the sentiment of each text into positive or negative.
4+
5+
## Requirements
6+
7+
- Python 3.x
8+
- scikit-learn
9+
- numpy
10+
- pandas
11+
12+
Install the required libraries using the following command:
13+
`pip install scikit-learn numpy pandas`
14+
15+
## Usage
16+
17+
1. Prepare your data: Create a CSV file (`data.csv`) with two columns: 'text' containing the text data (sentences, reviews, etc.), and 'label' containing the corresponding sentiment labels (e.g., positive or negative).
18+
19+
2. Run the script: Execute the Python script `Sentiment_Analysis.py` to perform sentiment analysis on the data.
20+
21+
22+
## Output
23+
24+
The script will print the accuracy and classification report of the SVM model on the test set.
25+
26+
## Author(s)
27+
28+
Srujana
29+
30+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pandas as pd
2+
import numpy as np
3+
from sklearn.feature_extraction.text import TfidfVectorizer
4+
from sklearn.model_selection import train_test_split
5+
from sklearn.svm import SVC
6+
from sklearn.metrics import accuracy_score, classification_report
7+
8+
9+
def sentiment_analysis():
10+
"""
11+
Perform sentiment analysis using an SVM classifier.
12+
13+
The function reads the data from a CSV file, preprocesses it, and trains an SVM classifier
14+
for sentiment analysis on the 'text' column with the 'label' column as the target.
15+
16+
Prints the accuracy and classification report on the test data.
17+
"""
18+
# Load data from a CSV file (replace 'data.csv' with your data file)
19+
data = pd.read_csv('data.csv')
20+
21+
# Preprocess data (remove any special characters, convert to lowercase, etc.)
22+
data['text'] = data['text'].apply(preprocess_text)
23+
24+
# Split the data into features (X) and labels (y)
25+
X = data['text']
26+
y = data['label']
27+
28+
# Convert text data to numerical features using TF-IDF
29+
vectorizer = TfidfVectorizer()
30+
X = vectorizer.fit_transform(X)
31+
32+
# Split the data into training and testing sets
33+
X_train, X_test, y_train, y_test = train_test_split(
34+
X, y, test_size=0.2, random_state=42)
35+
36+
# Train an SVM classifier
37+
svm_classifier = SVC(kernel='linear')
38+
svm_classifier.fit(X_train, y_train)
39+
40+
# Make predictions on the test set
41+
y_pred = svm_classifier.predict(X_test)
42+
43+
# Calculate and print accuracy and classification report
44+
accuracy = accuracy_score(y_test, y_pred)
45+
print("Accuracy:", accuracy)
46+
print("Classification Report:")
47+
print(classification_report(y_test, y_pred, zero_division=1))
48+
49+
50+
def preprocess_text(text):
51+
# Replace special characters with spaces
52+
text = text.replace('\n', ' ')
53+
text = text.replace('\t', ' ')
54+
text = text.replace('-', ' ')
55+
56+
# Convert to lowercase
57+
text = text.lower()
58+
59+
return text
60+
61+
62+
if __name__ == '__main__':
63+
sentiment_analysis()

Sentiment Analysis/data.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
text,label
2+
"I love this product!",positive
3+
"This is amazing!",positive
4+
"Terrible experience. Would not recommend.",negative
5+
"Not bad, but not great either.",neutral
6+
"The best purchase I've made!",positive
7+
"I regret buying this.",negative
8+
"This is fantastic!",positive
9+
"Not the best, but okay.",neutral
10+
"Great value for money.",positive
11+
"Awful quality. Do not buy.",negative

0 commit comments

Comments
 (0)