簡體   English   中英

我正在嘗試為我的“情緒分析”項目制作前端,但不知道如何讓我的“預測功能”在前端工作

[英]I'm trying to make Front-end for my “Sentiment Analysis” project but can't figure out how to make my “prediction_function” work at the Front-end

我正在制作一個可用於任何語言的情緒分析項目。 這是它的工作原理:在代碼的最后部分“result”將一個句子翻譯成英語。 然后predict_function(result.text)將英文文本分類為正面、負面或中性。

如果我單獨運行它,代碼工作正常。 現在我正在嘗試制作前端,唯一的問題是我無法弄清楚如何將prediction_function與它聯系起來。 翻譯功能在那里工作,但唯一剩下的就是在前端對翻譯的文本進行分類。 我是新手,我確實做了很多改變,但無法讓它發揮作用。

這是我的全部代碼:(我想不需要查看整個代碼,因為我覺得問題出在結尾部分,在@app.route('/', methods=['POST']) 行之后)

from flask import Flask, request, render_template
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import nltk
import pandas as pd
import numpy as np
import seaborn as sns
import regex as re
import math

import googletrans

from googletrans import Translator
from nltk.tokenize import word_tokenize

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('form.html')

df = pd.read_csv('C:/Users/path/file.csv')
df = df.rename(columns = {'clean_text':'Comment'})
df.head()

df.describe()

cat = []
for val in df['category'].values:
  if val not in cat:
    cat.append(val)
print(cat)

index_arr = []
for index, val in df.iterrows():
    if val['category'] not in [-1.0, 0.0, 1.0]:
        index_arr.append(index)
print(index_arr)
df.drop(index_arr, axis = 0, inplace = True)

sns.countplot(x='category',data=df)

def clean_comments(comment):
    comment = re.sub(r'\$\w*', '', str(comment))
    comment = re.sub(r'^RT[\s]+', '', str(comment))
    comment = re.sub(r'https?:\/\/.*[\r\n]*', '', str(comment))
    comment = re.sub(r'#', '', str(comment))
    comment = re.sub(r"@[^\s]+[\s]?",'',comment)
    comment = re.sub('[^ a-zA-Z0-9]', '', comment)
    comment = re.sub('[0-9]', '', comment)
    return comment

df['Comment'] = df['Comment'].apply(clean_comments)
df.head()

nltk.download('stopwords')
from nltk.corpus import stopwords

stop_words = stopwords.words('english')

def removing_stopwords(words):
  cleaned_tokens = []
  for val in words.split(' '):
    val = val.lower()
    if val not in stop_words and val != '':
      cleaned_tokens.append(val)
  return(cleaned_tokens)

df['Comment'] = df['Comment'].apply(removing_stopwords) 
df.head()

from nltk.stem.porter import PorterStemmer

def stem_comments(words):
  ps = PorterStemmer()
  stemmed_review = []
  for review in  words:
    stemmed_review.append(ps.stem(review))
  return stemmed_review

df['Comment'] = df['Comment'].apply(stem_comments)   
df.head()

temp = df.iloc[:,0].values
X = [' '.join(ele) for ele in temp]
X = np.array(X)
Y = df.iloc[:,1].values

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features=5000)
X = vectorizer.fit_transform(X).toarray()
print(X.shape)

print(Y[:5])
print(Y.shape)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.01)

print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

del X
del Y
del temp
del df

from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB()
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)

from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
print("Accuracy = ", accuracy_score(y_pred, y_test))

import seaborn as sn
from matplotlib.figure import Figure
df_cm = pd.DataFrame(cm, index = [0,1,2],columns = [0,1,2])
f = Figure(figsize = (20,10))
sn.heatmap(df_cm, annot=True)

def predict_function(sentence):
    sentence = clean_comments(sentence)
    sentence = removing_stopwords(sentence)
    sentence = stem_comments(sentence)
    
    X = [' '.join([str(elem) for elem in sentence])]
    X = np.array(X)
    X = vectorizer.transform(X).toarray()
    
    result = classifier.predict(X)

    if result == -1.0:
        print("Negative")
    elif result == 0.0:
        print("Neutral")
    else:
        print("Positive")

@app.route('/', methods=['POST'])
def my_form_post():
    text1 = request.form['text1'].lower()

    translator = Translator(service_urls=['translate.googleapis.com'])
    result = translator.translate(text1, dest='en')
    senti=predict_function(result.text)

    return render_template('form.html', final=result.text, last=senti, text1=text1)



if __name__ == "__main__":
    app.run(debug=True, host="127.0.0.1", port=5002, threaded=True)

前端的 HTML 代碼:

<body>
    <h1>Welcome To Sentiment Analyzer</h1>
    <form method="POST">
        <textarea name="text1" placeholder="Say Something: ...." rows="10" cols="109"></textarea><br><br>

        <input class="example_a" type="submit">
    </form>
    {% if final %}
    <div> 
        <h2>The Sentiment of</h2> '{{ text1 }}' <h2>is {{ final }} </h2> <h2>is {{ last }} </h2>
        {% else %}
        <p></p>
        {% endif %}
    </div>
</body>

在您的 predict_function 函數中,您不會返回任何值,只是打印它是否為正值。 嘗試用 return 語句替換最后的那些打印語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM