下面的代码给我错误“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)
1条答案
按热度按时间ie3xauqp1#
乍一看,似乎有两件事正在发生:
rand_words
是空的,这意味着永远不会循环。在这两种情况下,问题看起来都没有到达你声明summary的部分,我的意见是调试代码的那一段并检测失败的地方。