import pandas as pd
import nltk
import re
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
from sklearn.metrics import accuracy_score, precision_score, f1_score, confusion_matrix, classification_report
File_CSV ="F:/Per Semester/Semester 7/NLP_Materi/Tugas/Tugas 5_162112233066/GTA_Review Data Sets.csv"
CSV = pd.read_csv(File_CSV)
print("Data Training : ", CSV.head())
nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
Preprocessing
def preprocess_text(text):
if isinstance(text, str):
text = re.sub(r"[^a-zA-Z\s]", "", text)
text = text.lower()
words = [word for word in text.split() if word not in stop_words]
return " ".join(words)
return "No Value"
Preprocess_CSV = CSV['processed_text'] = CSV['Review'].apply(preprocess_text)
print("Train Result : \n" , Preprocess_CSV.head(100))
long_string = ','.join(list(CSV['processed_text'].values))
wordcloud = WordCloud(background_color="white", max_words=100, contour_width=3, contour_color='steelblue')
wordcloud.generate(long_string)
wordcloud.to_image()
tfidf = TfidfVectorizer(max_features=1000)
X = tfidf.fit_transform(CSV['processed_text']).toarray()
y = CSV['Sentiment']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')
conf_matrix = confusion_matrix(y_test, y_pred)
print("Accuracy:", accuracy)
print("Precision:", precision)
print("F1 Score :", f1)
print("Result :\n", classification_report(y_test, y_pred))
plt.figure(figsize=(10, 7))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=['Irrelevant', 'Positive', 'Negative', 'Neutral'], yticklabels=['Irrelevant', 'Positive', 'Negative', 'Neutral'])
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()