Skip to content

Commit

Permalink
Python-textblob: Add Flask app with sentiment analysis and form handling
Browse files Browse the repository at this point in the history
  • Loading branch information
charakamihiranga committed Sep 18, 2024
1 parent 66621a5 commit 8d97975
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 25 deletions.
File renamed without changes.
21 changes: 21 additions & 0 deletions Python-TextBlob with Flask/.idea/Python-TextBlob with Flask.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Binary file not shown.
30 changes: 30 additions & 0 deletions Python-TextBlob with Flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Flask, request, render_template, jsonify
from textblob import TextBlob


app = Flask(__name__)


@app.route('/')
def home():
return render_template('index.html')

@app.route('/analyzereview', methods=['POST'])
def analyzereview():
data = request.get_json()
review = data.get('review')
blob = TextBlob(review)
sentiment = blob.sentiment.polarity

if sentiment > 0:
result = "Positive Review✅🎉"
elif sentiment < 0:
result = "Negative Review🫤"
elif sentiment == 0:
result = "Neutral Review👍"

return jsonify({'sentiment': result})


if __name__ == '__main__':
app.run()
16 changes: 16 additions & 0 deletions Python-TextBlob with Flask/static/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
document.getElementById('review-form').addEventListener('submit', async function(event) {
event.preventDefault();

const reviewText = document.getElementById('review').value;

const response = await fetch('/analyzereview', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ review: reviewText })
});

const data = await response.json();
document.getElementById('result').textContent = 'Sentiment: ' + data.sentiment;
});
75 changes: 75 additions & 0 deletions Python-TextBlob with Flask/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Review Sentiment Analysis</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.review-form {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
width: 400px;
}

.review-form h2 {
text-align: center;
}

.review-form textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
resize: vertical;
margin-bottom: 15px;
}

.review-form button {
width: 100%;
padding: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

.review-form button:hover {
background-color: #218838;
}

.result {
text-align: center;
font-size: 18px;
margin-top: 15px;
}
</style>
</head>
<body>

<div class="review-form">
<h2>Review Sentiment Analysis</h2>
<form id="review-form">
<textarea name="review" id="review" rows="5" placeholder="" required></textarea><br>
<button type="submit">Submit Review</button>
</form>
<div class="result" id="result">
</div>
</div>

<script src="/static/js/script.js"></script>

</body>
</html>
10 changes: 0 additions & 10 deletions Python-TextBlob/.idea/Python-TextBlob.iml

This file was deleted.

12 changes: 0 additions & 12 deletions Python-TextBlob/main.py

This file was deleted.

0 comments on commit 8d97975

Please sign in to comment.