gpt4 book ai didi

python - 使用滑动窗口评估文本分组的情绪

转载 作者:行者123 更新时间:2023-12-02 22:45:52 26 4
gpt4 key购买 nike

我正在尝试创建一个滑动窗口来评估对话中话语分组的情绪,目标是:

  1. 评估 session 组中单个话语的情绪
  2. 根据上面第 1 项的陈述评估一组情绪,然后将新的话语字符串添加到预测器(对话中的下一个话语),以便预测器在新字符串的上下文中评估前一个字符串。请注意,此步骤中的单个附加语句也会收到一个情绪分数
  3. 通过向要评估的数据添加新的话语字符串来重复项目 1 和 2(其中新的第三个字符串话语得到评估,但也在前 2 个话语的上下文中进行评估 - 这样现在有三个话语要被评估除了个别新添加的字符串外,还进行了评估。
    例如:
Statement 1: Neutral
Statement 2: Positive
Statement 1+2: Neutral
Statement 3: Negative
Statement 1+2+3: Neutral
etc...

然后我可以非常简单地将分类转换为整数值,然后为最终的“对话”情感分类得出整个语句分组的平均值。

这是我的话语列表:

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."]

这是我的例程-

#Define the size of the sliding window
window_size = 5
sentiment_scores = []
for i in range(len(conversation) - window_size + 1):
# Get the window of utterances
window = conversation[i:i+window_size]
print("this is the conversation with window",conversation[i:i+window_size])

# Add one or two utterances from the previous window to the beginning of the current window
if i > 0:
window = conversation[i-1:i+window_size]


# Add one or two utterances from the next window to the end of the current window
if i < len(conversation) - window_size:
window = conversation[i:i+window_size+1]
print("This is the window when a conversation has been added", window)

# Join the utterances in the window into a single string
text = " ".join(window)
# print(text)

# Use the OpenAI completion class to evaluate sentiment for the window
response = openai.Completion.create(
engine="text-davinci-003",
prompt = f"classify the sentiment of this text as Positive, Negative, or Neutral: {text}\nResult:",
temperature=0,
max_tokens=1,
n=1,
stop=None,
frequency_penalty=0,
presence_penalty=0
)

# Extract the sentiment score from the response
sentiment = response.choices[0].text.strip()
print(sentiment)

# Add the sentiment score to the list
sentiment_scores.append(sentiment)
print(sentiment_scores)

不幸的是,返回的内容不正确,因为我显然没有按照我上面描述的方式在话语中分层。使用我的一个调试打印,这就是我所看到的:

This is the  window when a conversation has been added ['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."]
Neutral
['Neutral']
This is the window when a conversation has been added ["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?']
Neutral
['Neutral', 'Neutral']
This is the window when a conversation has been added ['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."]
Neutral
['Neutral', 'Neutral', 'Neutral']
This is the window when a conversation has been added ['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."]
Neutral
['Neutral', 'Neutral', 'Neutral', 'Neutral']

如您所见,我的逻辑似乎正在评估所有语句。

然后我可以非常简单地将分类转换为整数值,然后为最终的“对话”情感分类得出整个语句分组的平均值。

如有任何想法或帮助,我们将不胜感激。

最佳答案

我是这样解决问题的:


# Define the size of the sliding window
window_size = 3

# Define the conversation as a list of utterances
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_scores = []
for i, utterance in enumerate(conversation):
# Define the window based on the current utterance
if i < window_size:
window = conversation[:i+1]
else:
window = conversation[i-window_size+1:i+1]
print (window)

# Join the utterances in the window into a single string
text = " ".join(window)

# Use the OpenAI completion class to evaluate sentiment for the window
response = openai.Completion.create(
engine="text-davinci-003",
prompt = f"classify the sentiment of this text as Positive, Negative, or Neutral: {text}\nResult:",
temperature=0,
max_tokens=1,
n=1,
stop=None,
frequency_penalty=0,
presence_penalty=0
)

# Extract the sentiment score from the response
sentiment = response.choices[0].text.strip().split('\n')[0]
print(sentiment)

# Add the sentiment score to the list
sentiment_scores.append(sentiment)

关于python - 使用滑动窗口评估文本分组的情绪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75559316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com