Skip to content

Commit 36e9f91

Browse files
Merge pull request #2602 from srujana-16/sentiment_analysis
Sentiment analysis script added - GSSOC'23
2 parents d9bb317 + 360117f commit 36e9f91

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

SCRIPTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@
119119
| 109\. | Domain Name Availability Checker | This script is a Python tool that allows you to check the availability of domain names using the GoDaddy API. | [Take Me](./Domain_Name_Availability/) | [Sabhi Sharma](https//github.com/sabhisharma-ise)
120120
| 110\. | Automatic Spelling Checker and Corrector | This Script is used to detect spelling errors in a text and correct them if the user wishes to do so. | [Take Me](./Automatic_Spelling_Checker_Corrector/) | [Sabhi Sharma](https//github.com/sabhisharma-ise)
121121
| 111\. | File Searcher | The File Search script is a Python tool that allows you to search for files with a specific extension in a directory. It recursively searches through all subdirectories of the specified directory and returns a list of files that match the provided file extension. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/File\Search) | [Srujana Vanka](https://github.com/srujana-16)
122+
| 112\. | Sentiment Analysis | 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. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/Sentiment\Analysis) | [Srujana Vanka](https://github.com/srujana-16)
122123
| 112\. | Data Scraping | A Python script that retrieves data from various platforms such as social media, weather services, and financial data providers using APIs. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/DataScraping) | [Shraddha Singh](https://github.com/shraddha761)
123124
| 112\. | Automated Data Reporting | A Python script that automates the process of generating data reports from CSV files. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/AutomatedDataReporting) | [Shraddha Singh](https://github.com/shraddha761)
124125
| 112\. | Ludo Game | This python script will create a ludo game for the user to interact with it and play the game and enjoy the game. | [Take Me](./Ludo_Game) | [Avdhesh Varshney](https://github.com/Avdhesh-Varshney)
125126
| 112\. | Web Server Log Analysis Script | A Python script to parse and analyze web server logs to extract useful information such as visitor statistics, popular pages, and potential security threats. | [Take Me](https://github.com/avinashkranjan/Amazing-Python-Scripts/tree/master/WebServer) | [Shraddha Singh](https://github.com/shraddha761)
126127
| 112\. | Open Websites By Speaking | This python script can allow user to open any website just by speaking. | [Take Me](./Websites_Automation) | [Avdhesh Varshney](https://github.com/Avdhesh-Varshney)
127128
| 112\. | Advisor App | This python script allows user to give so many advices and motivation quote lines. | [Take Me](./Advisor_App) | [Avdhesh Varshney](https://github.com/Avdhesh-Varshney)
128129
| 113\. | Fake News Detection | The Fake News Detection is Python based ML script which allows you to check if your news article is Real or Fake. | [Take me](https://github.com/Parul1606/Amazing-Python-Scripts/tree/testing/Fake-News-Detection) | [Parul Pandey](https://github.com/Parul1606)
129-

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)