134 lines
3.3 KiB
Python
134 lines
3.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..database import get_db
|
|
from ..models import Komentar
|
|
from ..preprocessing import preprocess_text
|
|
from ..ml_model import predict_sentiment, get_tfidf
|
|
|
|
router = APIRouter(
|
|
prefix="/prediction",
|
|
tags=["Klasifikasi Sentimen"]
|
|
)
|
|
|
|
|
|
@router.post("/all")
|
|
def predict_all_data(
|
|
db: Session = Depends(get_db)
|
|
):
|
|
data = db.query(Komentar).all()
|
|
|
|
if not data:
|
|
return {
|
|
"message": "Tidak ada data komentar",
|
|
"jumlah_data": 0,
|
|
"positif": 0,
|
|
"negatif": 0
|
|
}
|
|
|
|
jumlah_positif = 0
|
|
jumlah_negatif = 0
|
|
|
|
for item in data:
|
|
|
|
# =========================
|
|
# PREPROCESSING
|
|
# =========================
|
|
hasil = preprocess_text(item.komentar)
|
|
|
|
item.case_folding = hasil["case_folding"]
|
|
item.normalisasi = hasil["normalisasi"]
|
|
item.cleaning = hasil["cleaning"]
|
|
item.tokenization = hasil["tokenization"]
|
|
item.stopword_removal = hasil["stopword_removal"]
|
|
item.stemming = hasil["stemming"]
|
|
item.text_preprocessed = hasil["text_preprocessed"]
|
|
|
|
# =========================
|
|
# TF-IDF
|
|
# =========================
|
|
hasil_tfidf = get_tfidf(
|
|
item.text_preprocessed
|
|
)
|
|
|
|
item.tfidf = str(hasil_tfidf)
|
|
|
|
# =========================
|
|
# NAÏVE BAYES
|
|
# =========================
|
|
hasil_prediksi = predict_sentiment(
|
|
item.text_preprocessed
|
|
)
|
|
|
|
item.sentiment = hasil_prediksi["sentiment"]
|
|
item.probability = hasil_prediksi["probability"]
|
|
|
|
# =========================
|
|
# HITUNG JUMLAH SENTIMEN
|
|
# =========================
|
|
if item.sentiment == "Positif":
|
|
jumlah_positif += 1
|
|
else:
|
|
jumlah_negatif += 1
|
|
|
|
db.commit()
|
|
|
|
return {
|
|
"message": "Prediksi seluruh data berhasil",
|
|
"jumlah_data": len(data),
|
|
"positif": jumlah_positif,
|
|
"negatif": jumlah_negatif
|
|
}
|
|
|
|
|
|
@router.post("/{id}")
|
|
def predict_data(
|
|
id: int,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
data = (
|
|
db.query(Komentar)
|
|
.filter(Komentar.id == id)
|
|
.first()
|
|
)
|
|
|
|
if not data:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="Data komentar tidak ditemukan"
|
|
)
|
|
|
|
hasil = preprocess_text(data.komentar)
|
|
|
|
data.case_folding = hasil["case_folding"]
|
|
data.normalisasi = hasil["normalisasi"]
|
|
data.cleaning = hasil["cleaning"]
|
|
data.tokenization = hasil["tokenization"]
|
|
data.stopword_removal = hasil["stopword_removal"]
|
|
data.stemming = hasil["stemming"]
|
|
data.text_preprocessed = hasil["text_preprocessed"]
|
|
|
|
hasil_tfidf = get_tfidf(
|
|
data.text_preprocessed
|
|
)
|
|
|
|
data.tfidf = str(hasil_tfidf)
|
|
|
|
hasil_prediksi = predict_sentiment(
|
|
data.text_preprocessed
|
|
)
|
|
|
|
data.sentiment = hasil_prediksi["sentiment"]
|
|
data.probability = hasil_prediksi["probability"]
|
|
|
|
db.commit()
|
|
db.refresh(data)
|
|
|
|
return {
|
|
"id": data.id,
|
|
"komentar": data.komentar,
|
|
"text_preprocessed": data.text_preprocessed,
|
|
"tfidf": hasil_tfidf,
|
|
"sentiment": data.sentiment,
|
|
"probability": data.probability
|
|
} |