我正在制作一个可以用于任何语言的情感分析项目。它的工作原理如下:在代码的最后部分,“result”将一个句子翻译成英语。然后predict_function(result.text)将英语文本分类为积极、消极或中性。
如果我单独运行代码,它运行得很好。现在我在尝试制作前端,唯一的问题是我无法弄清楚如何将prediction_function与之连接。翻译功能在那里工作得很好,但唯一剩下的就是在前端对翻译后的文本进行分类。我对此很新手,我确实做了很多更改,但始终无法让它工作。
这是我的完整代码:(我认为没有必要查看整个代码,因为我觉得问题出在最后部分,在@app.route(‘/’, methods=[‘POST’])行之后)
from flask import Flask, request, render_templatefrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityimport nltkimport pandas as pdimport numpy as npimport seaborn as snsimport regex as reimport mathimport googletransfrom googletrans import Translatorfrom nltk.tokenize import word_tokenizeapp = 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 commentdf['Comment'] = df['Comment'].apply(clean_comments)df.head()nltk.download('stopwords')from nltk.corpus import stopwordsstop_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 PorterStemmerdef stem_comments(words): ps = PorterStemmer() stemmed_review = [] for review in words: stemmed_review.append(ps.stem(review)) return stemmed_reviewdf['Comment'] = df['Comment'].apply(stem_comments) df.head()temp = df.iloc[:,0].valuesX = [' '.join(ele) for ele in temp]X = np.array(X)Y = df.iloc[:,1].valuesfrom sklearn.feature_extraction.text import TfidfVectorizervectorizer = 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_splitX_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 Xdel Ydel tempdel dffrom sklearn.naive_bayes import MultinomialNBclassifier = MultinomialNB()classifier.fit(X_train, y_train)y_pred = classifier.predict(X_test)from sklearn.metrics import confusion_matrix, accuracy_scorecm = confusion_matrix(y_test, y_pred)print(cm)print("Accuracy = ", accuracy_score(y_pred, y_test))import seaborn as snfrom matplotlib.figure import Figuredf_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>欢迎使用情感分析器</h1> <form method="POST"> <textarea name="text1" placeholder="说点什么:...." rows="10" cols="109"></textarea><br><br> <input class="example_a" type="submit"> </form> {% if final %} <div> <h2>的感情</h2> '{{ text1 }}' <h2>是 {{ final }} </h2> <h2>是 {{ last }} </h2> {% else %} <p></p> {% endif %} </div></body>
回答:
在你的predict_function函数中,你没有返回任何值,只是打印它是否是积极的。尝试用返回语句替换末尾的那些打印语句。