Form Inputan 


Hasil


 1. Install Library yang Dibutuhkan

pip install Flask Flask-MySQLdb

Install dulu modul TextBlob: 

pip install textblob 

 

2. Buat Database MySQL 

CREATE DATABASE sentiment_db;

USE sentiment_db;

CREATE TABLE sentiment_log (

    id INT AUTO_INCREMENT PRIMARY KEY,

    text TEXT NOT NULL,

    sentiment VARCHAR(20),

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);


3. app.py dengan MySQL 

from flask import Flask, render_template, request

from flask_mysqldb import MySQL

from textblob import TextBlob


app = Flask(__name__)


# Konfigurasi MySQL

app.config['MYSQL_HOST'] = 'localhost'

app.config['MYSQL_USER'] = 'root'         # Ganti sesuai user MySQL kamu

app.config['MYSQL_PASSWORD'] = ''         # Ganti jika ada password

app.config['MYSQL_DB'] = 'sentiment_db'


mysql = MySQL(app)


@app.route('/')

def index():

    return render_template('index.html')


@app.route('/analyze', methods=['GET', 'POST'])

def analyze():

    result = None

    if request.method == 'POST':

        text = request.form['text']

        blob = TextBlob(text)

        polarity = blob.sentiment.polarity

        if polarity > 0:

            result = 'Positif'

        elif polarity < 0:

            result = 'Negatif'

        else:

            result = 'Netral'


        # Simpan ke database

        cur = mysql.connection.cursor()

        cur.execute("INSERT INTO sentiment_log (text, sentiment) VALUES (%s, %s)", (text, result))

        mysql.connection.commit()

        cur.close()

    return render_template('analyze.html', result=result)


4. Tambahan: Menampilkan About dan Riwayat Analisis

@app.route('/about')

def about():

    return render_template('about.html')

if __name__ == '__main__':

    app.run(debug=True)

@app.route('/history')

def history():

    cur = mysql.connection.cursor()

    cur.execute("SELECT * FROM sentiment_log ORDER BY created_at DESC")

    logs = cur.fetchall()

    cur.close()

    return render_template('history.html', logs=logs)


5. templates/base.html (Template Utama) 

<!DOCTYPE html>

<html>

<head>

    <title>Sentiment App</title>

</head>

<body>

    <h1>Sentiment Analysis</h1>

    <nav>

        <a href="/">Home</a> |

        <a href="/analyze">Analisis</a> |

                 <a href="/hsitory">Riwayat</a> | 

        <a href="/about">Tentang</a>

    </nav>

    <hr>

    {% block content %}{% endblock %}

</body>

</html>

6. templates/index.html

 {% extends 'base.html' %}

{% block content %}

<p>Selamat datang di Aplikasi Analisis Sentimen!</p>

{% endblock %}

 

7. templates/analyze.html 

{% extends 'base.html' %}

{% block content %}

<form method="post">

    <textarea name="text" rows="5" cols="40" placeholder="Masukkan teks..."></textarea><br>

    <input type="submit" value="Analisa">

</form>

{% if result %}

    <h3>Hasil Analisis: {{ result }}</h3>

{% endif %}

{% endblock %}


8. templates/about.html 

{% extends 'base.html' %}

{% block content %}

<p>Aplikasi ini menggunakan TextBlob untuk analisis sentimen bahasa Inggris.</p>

{% endblock %}