NameError:未定义名称“情感”

时间:2019-10-21 14:11:29

标签: python flask

当我尝试获取变量'sentiment'时,我遇到此错误(NameError:名称'sentiment'未定义),即使我将其作为全局变量也不起作用,如果有人可以帮助我,请< / p>

@app.route('/sentiments_analysis', methods=['GET', 'POST'])
def sentiments_analysis():
    if request.method == 'POST' and 'largtext' in request:
        global  entities 
        global sentiment
        text = request.form.get['largtext']
        client = language.Client()
        document = client.document_from_text(text)
        sent_analysis = document.analyze_sentiment()
        dir(sent_analysis)
        sent_analysis = document.analyze_entities()
        dir(ent_analysis)
        sentiment = sent_analysis.sentiment
        entities = sent_analysis.entities

    return render_template('sentiments.html',sentiment=sentiment,entities=entities)

1 个答案:

答案 0 :(得分:1)

我通过以下方法解决了这个问题:

@app.route('/sentiments_analysis', methods=['GET', 'POST'])
def sentiments_analysis():
    entities = ''
    sentiment = ''
    if request.method == 'POST' and 'largtext' in request:
        text = request.form.get['largtext']
        client = language.Client()
        document = client.document_from_text(text)
        sent_analysis = document.analyze_sentiment()
        dir(sent_analysis)
        sent_analysis = document.analyze_entities()
        dir(ent_analysis)
        sentiment = sent_analysis.sentiment
        entities = sent_analysis.entities

    return render_template('sentiments.html',sentiment=sentiment,entities=entities)