滑动窗口对话情感分析代码逻辑错误排查与修正请求
对话情感累积评估的代码问题排查与修正
原代码核心问题
- 逻辑偏离需求:原代码使用固定大小的滑动窗口逻辑,但需求是累积式逐步评估——从单句开始,每新增一条语句,同时评估该单句和所有历史语句+新语句的组合情感,而非固定窗口的滑动。
- 循环范围错误:原循环基于
len(conversation) - window_size + 1,只遍历了固定窗口的次数,没有覆盖所有语句的单句和累积评估。 - 窗口拼接逻辑混乱:随意修改窗口的前后内容,导致每次评估的语句组既不是单句,也不是累积的完整对话片段,完全不符合需求。
修正后的实现代码
import openai # 对话语句列表 conversation = [ "Hi, how are you?", "I'm not doing very well, thanks for asking. How about you?", "It is the best of times and the worst of times.", "I'm not sure what to make of that.", "Do you have any plans for the weekend?", "Not yet, I'm still deciding.", "How about you?", "I'm planning to go hiking on Saturday." ] # 情感映射(用于后续数值计算) sentiment_mapping = {"Positive": 1, "Neutral": 0, "Negative": -1} sentiment_results = [] # 存储所有评估结果,格式:(语句内容, 单句情感, 累积组合情感, 单句数值, 累积数值) for idx in range(len(conversation)): # 1. 获取当前语句 current_utterance = conversation[idx] # 2. 获取从第一条到当前语句的累积组合 cumulative_conversation = conversation[:idx+1] # 评估当前单句的情感 single_response = openai.Completion.create( engine="text-davinci-003", prompt=f"classify the sentiment of this text as Positive, Negative, or Neutral: {current_utterance}\nResult:", temperature=0, max_tokens=1, n=1, stop=None, frequency_penalty=0, presence_penalty=0 ) single_sentiment = single_response.choices[0].text.strip() single_score = sentiment_mapping.get(single_sentiment, 0) # 评估累积组合的情感 cumulative_text = " ".join(cumulative_conversation) cumulative_response = openai.Completion.create( engine="text-davinci-003", prompt=f"classify the sentiment of this text as Positive, Negative, or Neutral: {cumulative_text}\nResult:", temperature=0, max_tokens=1, n=1, stop=None, frequency_penalty=0, presence_penalty=0 ) cumulative_sentiment = cumulative_response.choices[0].text.strip() cumulative_score = sentiment_mapping.get(cumulative_sentiment, 0) # 保存结果 sentiment_results.append({ "utterance": current_utterance, "single_sentiment": single_sentiment, "cumulative_sentiment": cumulative_sentiment, "single_score": single_score, "cumulative_score": cumulative_score }) # 打印调试信息 print(f"===== 第{idx+1}条语句 =====") print(f"单句内容: {current_utterance}") print(f"单句情感: {single_sentiment}") print(f"累积组合内容: {cumulative_text}") print(f"累积组合情感: {cumulative_sentiment}\n") # 计算对话最终情感均值 total_cumulative_scores = [res["cumulative_score"] for res in sentiment_results] final_average_score = sum(total_cumulative_scores) / len(total_cumulative_scores) final_sentiment = next(k for k, v in sentiment_mapping.items() if abs(v - final_average_score) <= 0.5) print("===== 对话最终情感评估 =====") print(f"累积情感均值: {final_average_score:.2f}") print(f"最终情感分类: {final_sentiment}")
修正后逻辑说明
- 遍历对话中的每一条语句,索引从0到最后一条
- 对每条语句单独调用情感评估API,得到单句情感结果
- 同时将从第一条到当前语句的所有内容拼接,调用API评估累积组合的情感
- 将情感分类映射为数值(Positive=1, Neutral=0, Negative=-1),方便后续计算均值
- 最后通过累积情感数值的均值,得到对话的最终情感分类
预期调试输出示例
===== 第1条语句 ===== 单句内容: Hi, how are you? 单句情感: Neutral 累积组合内容: Hi, how are you? 累积组合情感: Neutral ===== 第2条语句 ===== 单句内容: I'm not doing very well, thanks for asking. How about you? 单句情感: Negative 累积组合内容: Hi, how are you? I'm not doing very well, thanks for asking. How about you? 累积组合情感: Neutral ===== 第3条语句 ===== 单句内容: It is the best of times and the worst of times. 单句情感: Neutral 累积组合内容: Hi, how are you? I'm not doing very well, thanks for asking. How about you? It is the best of times and the worst of times. 累积组合情感: Neutral ...(后续语句输出类似) ===== 对话最终情感评估 ===== 累积情感均值: 0.12 最终情感分类: Neutral
内容的提问来源于stack exchange,提问作者Ja4H3ad
相关产品推荐
相关产品推荐

