I am trying to create a sliding window to evaluate sentiment on groupings of utterances within a conversation, with the goal of:
- Evaluating the sentiment of a single utterance in a conversational grouping
- Evaluating a grouping of sentiment based on the statement from item 1 above and then adding a new utterance string to the predictor (the next utterance in the conversation) so that the predictor evaluates the previous string in context with the new string. Note that the individual additive statement in this step would also receive a sentiment score
- Repeating item 1 and 2 by adding a new utterance string to data to be evaluated (wherein the new 3rd string utterance gets evaluated but also is evaluated in the context of the previous 2 utterances - such that now there are three utterances to be evaluated in addition to the individual newly added string.
For example:
Statement 1: Neutral
Statement 2: Positive
Statement 1+2: Neutral
Statement 3: Negative
Statement 1+2+3: Neutral
etc...
I could then transform the classifications to integer values rather simply, and then come up with the mean of entire statement grouping for a final "conversation" sentiment classification.
Here is my 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."]
Here is my routine -
#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)
Unfortunately, what is being returned is not correct because I am apparently not layering in the utterances the way I am describing above. Using one of my debug prints, this is what I am seeing:
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']
As you can see, it appears my logic is evaluating all statements.
I could then transform the classifications to integer values rather simply, and then come up with the mean of entire statement grouping for a final "conversation" sentiment classification.
Any thoughts or assistance would be greatly appreciated.