Team Ai
Apppublic

AdityaM23/Sentiment_Classification_G12

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py69 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3import pandas as pd4import matplotlib.pyplot as plt5from sklearn.feature_extraction.text import CountVectorizer6from sklearn.model_selection import train_test_split7from sklearn.metrics import accuracy_score,classification_report, ConfusionMatrixDisplay8import re9import string10from sklearn.tree import DecisionTreeClassifier11from sklearn.feature_extraction.text import TfidfVectorizer12 13df = pd.read_csv('train.csv')14 15df.dropna(inplace=True)16 17df['sentiment'] = df['sentiment'].astype('category').cat.codes18 19df = df[['selected_text','sentiment']]20 21X_train = df['selected_text']22y_train = df['sentiment']23 24vectorization = TfidfVectorizer()25XV_train = vectorization.fit_transform(X_train)26 27dt = DecisionTreeClassifier()28dt.fit(XV_train, y_train)29def output_lable(n):30    if n == 0:31        return "The Text Sentiment is Negative"32    elif n == 1:33        return "The Text Sentiment is Neutral"34    elif n == 2:35        return "The Text Sentiment is Positive"36 37def wp(text):38    text = text.lower()39    text = re.sub('\[.*?\]', '', text)40    text = re.sub("\\W"," ",text)41    text = re.sub('https?://\S+|www\.\S+', '', text)42    text = re.sub('<.*?>+', '', text)43    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)44    text = re.sub('\n', '', text)45    text = re.sub('\w*\d\w*', '', text)46    return text47        48def manual_testing(news):49    testing_news = {"text":[news]}50    new_def_test = pd.DataFrame(testing_news)51    new_def_test["text"] = new_def_test["text"].apply(wp)52    new_x_test = new_def_test["text"]53    new_xv_test = vectorization.transform(new_x_test)54    # pred_lr = lr.predict(new_xv_test)55    pred_dt = dt.predict(new_xv_test)56    # pred_rfc = rfc.predict(new_xv_test)57 58    return output_lable(pred_dt[0])59 60st.title("Sentiment Analysis")61user_input = st.text_area("Enter your text here:")62if st.button("Analyze"):63    if user_input:64        sentiment = manual_testing(user_input)65        sentiment_label = {0: "Negative", 1: "Neutral", 2: "Positive"}66        # st.json("Predicted Sentiment:", sentiment_label[sentiment])67        st.write("Predicted Sentiment:", sentiment_label[sentiment])68    else:69        st.warning("Please enter some text.")