-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (39 loc) · 1.41 KB
/
app.py
File metadata and controls
51 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import (
Flask,
render_template,
request,
redirect,
url_for,
flash,
get_flashed_messages,
)
from datetime import datetime
from naivebayes import classify_comment, train_classifier
import csv, os
import pandas as pd
from collections import Counter
app = Flask(__name__)
app.secret_key = "new-refresh-clear-all-ting-22"
CSV_PATH = r"C:\Users\Jonathan Philips\Coding\nltk-text-tone-processing\resources\responses.csv"
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
comment = request.form["comment"]
label = classify_comment(comment)
with open(CSV_PATH, "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(
[comment, label, datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
)
train_classifier()
flash(label)
return redirect(url_for("index"))
messages = get_flashed_messages()
label = messages[0] if messages else ""
df = pd.read_csv(CSV_PATH, names=["text", "sentiment", "timestamp"], skiprows=1)
valid_sentiments = {"positive", "negative", "neutral", "mixed"}
df = df[df["sentiment"].str.lower().isin(valid_sentiments)]
sentiment_counts = Counter(df["sentiment"])
return render_template("index.html", label=label, sentiment_counts=sentiment_counts)
if __name__ == "__main__":
app.run(debug=True)