获取错误“Local variable referenced before assignment”- Python

lkaoscv7  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(161)

下面的代码给我错误“UnboundLocalError:局部变量'summary'在赋值前引用”

@app.route('/analyse',methods=['GET'])
def analyse():

rawtext = request.args.get('raw_text')

blob = TextBlob(rawtext)

received_text2 = blob

blob_sentiment  =   str(TextBlob(rawtext).sentiment.polarity)
blob_subjectivity = str(TextBlob(rawtext).sentiment.subjectivity)

number_of_tokens = len(list(blob.words))
# Extracting Main Points

nouns = list()

for word, tag in blob.tags:
    if tag == 'NN':
        nouns.append(word.lemmatize())
        len_of_words = len(nouns)
        rand_words = random.sample(nouns,len(nouns))
        final_word = list()
        for item in rand_words:
            word = Word(item).pluralize()
            final_word.append(word)
            summary = final_word



resp = make_response(jsonify({'error':'false', 'Nouns':summary, 'sentiment':blob_sentiment, 'subjectivity': blob_subjectivity}))
resp.status_code = 200
resp.mimetype = 'application/json'
return resp

app.run(host ='0.0.0.0',port=99)

ie3xauqp

ie3xauqp1#

乍一看,似乎有两件事正在发生:

  • 要么没有tag == 'NN',这意味着summary永远不会被创建。
  • 或者rand_words是空的,这意味着永远不会循环。

在这两种情况下,问题看起来都没有到达你声明summary的部分,我的意见是调试代码的那一段并检测失败的地方。

相关问题