Spaces:
Build error
Build error
Commit
·
ea86464
1
Parent(s):
9a62e79
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nltk
|
2 |
+
import re
|
3 |
+
import pandas as pd
|
4 |
+
from nltk.corpus import stopwords
|
5 |
+
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
6 |
+
|
7 |
+
# Load the required NLTK corpora
|
8 |
+
nltk.download("vader_lexicon")
|
9 |
+
nltk.download("stopwords")
|
10 |
+
|
11 |
+
# Initialize the SentimentIntensityAnalyzer
|
12 |
+
sia = SentimentIntensityAnalyzer()
|
13 |
+
|
14 |
+
# Define a function to clean the text
|
15 |
+
def clean_text(text):
|
16 |
+
text = re.sub("[^a-zA-Z]", " ", text)
|
17 |
+
text = text.lower()
|
18 |
+
text = text.split()
|
19 |
+
text = [word for word in text if word not in set(stopwords.words("english"))]
|
20 |
+
text = " ".join(text)
|
21 |
+
return text
|
22 |
+
|
23 |
+
# Define a function to calculate the sentiment score
|
24 |
+
def sentiment_score(text):
|
25 |
+
score = sia.polarity_scores(text)
|
26 |
+
return score["compound"]
|
27 |
+
|
28 |
+
# Read the data into a pandas DataFrame
|
29 |
+
df = pd.read_csv("content.csv")
|
30 |
+
|
31 |
+
# Clean the text and calculate the sentiment score
|
32 |
+
df["Clean_Text"] = df["Content"].apply(lambda x: clean_text(x))
|
33 |
+
df["Sentiment_Score"] = df["Clean_Text"].apply(lambda x: sentiment_score(x))
|
34 |
+
|
35 |
+
# Classify the content quality based on the sentiment score
|
36 |
+
df["Content_Quality"] = df["Sentiment_Score"].apply(lambda x: "Good" if x >= 0.5 else "Bad")
|
37 |
+
|
38 |
+
# Print the final result
|
39 |
+
print(df)
|
40 |
+
|
41 |
+
from flask import Flask, request, render_template
|
42 |
+
|
43 |
+
app = Flask(__name__)
|
44 |
+
|
45 |
+
@app.route("/", methods=["GET", "POST"])
|
46 |
+
def index():
|
47 |
+
if request.method == "POST":
|
48 |
+
content = request.form["content"]
|
49 |
+
clean_text = clean_text(content)
|
50 |
+
sentiment_score = sentiment_score(clean_text)
|
51 |
+
content_quality = "Good" if sentiment_score >= 0.5 else "Bad"
|
52 |
+
return render_template("index.html", content_quality=content_quality)
|
53 |
+
return render_template("index.html")
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
app.run(host="0.0.0.0",port=7860,debug=True)
|