|
from flask import Flask, request, render_template |
|
import pandas as pd |
|
import nltk |
|
from nltk.tokenize import word_tokenize |
|
from nltk.corpus import stopwords |
|
from nltk.stem import WordNetLemmatizer |
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
nltk.download('punkt') |
|
nltk.download('stopwords') |
|
nltk.download('wordnet') |
|
|
|
|
|
lemmatizer = WordNetLemmatizer() |
|
stop_words = set(stopwords.words('english')) |
|
|
|
|
|
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") |
|
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", aggregation_strategy="simple") |
|
|
|
|
|
def preprocess_text(text): |
|
|
|
tokens = word_tokenize(text) |
|
|
|
cleaned_tokens = [lemmatizer.lemmatize(token.lower()) for token in tokens if token.isalpha() and token.lower() not in stop_words] |
|
return ' '.join(cleaned_tokens) |
|
|
|
@app.route('/') |
|
def home(): |
|
return render_template('index.html') |
|
|
|
@app.route('/analyze', methods=['POST']) |
|
def analyze(): |
|
if request.method == 'POST': |
|
comments = request.form['comments'] |
|
cleaned_comments = preprocess_text(comments) |
|
|
|
|
|
sentiment_result = sentiment_pipeline(cleaned_comments)[0] |
|
|
|
|
|
entities_result = ner_pipeline(cleaned_comments) |
|
|
|
|
|
result = { |
|
'original_comment': comments, |
|
'cleaned_comment': cleaned_comments, |
|
'sentiment': sentiment_result, |
|
'entities': entities_result |
|
} |
|
|
|
return render_template('result.html', result=result) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |