|
|
|
import streamlit as st |
|
import joblib |
|
import re |
|
import string |
|
import nltk |
|
from nltk.corpus import stopwords |
|
from nltk.tokenize import word_tokenize |
|
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory |
|
|
|
|
|
nltk.download("stopwords") |
|
nltk.download("punkt") |
|
nltk.download("punkt_tab") |
|
|
|
|
|
model = joblib.load("random_forest_model.pkl") |
|
vectorizer = joblib.load("tfidf_vectorizer.pkl") |
|
|
|
|
|
factory = StemmerFactory() |
|
stemmer = factory.create_stemmer() |
|
|
|
|
|
|
|
def delete_unused_char(text): |
|
text = re.sub(r"@[A-Za-z0-9]+", "", text) |
|
text = re.sub(r"#[A-Za-z0-9]+", "", text) |
|
text = re.sub(r"RT[\s]", "", text) |
|
text = re.sub(r"http\S+", "", text) |
|
text = re.sub(r"[0-9]+", "", text) |
|
text = re.sub(r"[^\w\s]", "", text) |
|
text = text.replace("\n", " ") |
|
text = text.translate( |
|
str.maketrans("", "", string.punctuation) |
|
) |
|
text = text.strip(" ") |
|
return text |
|
|
|
|
|
|
|
def cleaned_text(text): |
|
delete_unused_char(text) |
|
|
|
text = text.lower() |
|
|
|
text = text.translate(str.maketrans("", "", string.punctuation)) |
|
|
|
text = re.sub(r"\d+", "", text) |
|
|
|
words = word_tokenize(text) |
|
|
|
stop_words = set(stopwords.words("indonesian")) |
|
words = [word for word in words if word not in stop_words] |
|
|
|
words = [stemmer.stem(word) for word in words] |
|
return " ".join(words) |
|
|
|
|
|
|
|
def predict_sentiment(text): |
|
text = cleaned_text(text) |
|
X = vectorizer.transform([text]) |
|
prediction = model.predict(X)[0] |
|
return prediction |
|
|
|
|
|
|
|
st.title("Analisis Sentimen Review BRI Mobile π³") |
|
st.write("Masukkan review dan dapatkan prediksi sentimen (Positif, Negatif, Netral)") |
|
|
|
|
|
user_input = st.text_area("Masukkan review di sini:") |
|
|
|
if st.button("Prediksi Sentimen"): |
|
if user_input.strip() == "": |
|
st.warning("Silakan masukkan teks terlebih dahulu!") |
|
else: |
|
sentiment = predict_sentiment(user_input) |
|
st.success(f"Prediksi Sentimen: **{sentiment}**") |
|
|
|
|
|
st.write("Dibuat dengan π oleh Muhammad Farkhan Adhitama") |
|
|