Skip to content
Snippets Groups Projects
Commit 3366fe46 authored by Oscar Linnarsson's avatar Oscar Linnarsson
Browse files

Merge branch 'fix/nlp_emotion_core' into 'master'

fix: nlp_emotion_core.py

See merge request !8
parents 9fb6af0d 522bb736
No related branches found
No related tags found
1 merge request!8fix: nlp_emotion_core.py
Pipeline #57217 passed
# NLP EMOTION CORE - @nlp
**TLDR**
Make sure to build the project.
Start the following nodes:
- text_2_emotion_1 (.py)
- text_2_emotion_2 (.py)
- nlp_emotion_core (.py)
Listen to the topic 'nlp_emotion'
To test the system post text responses to the topic 'dialogflow_response' as the following:
ros2 topic pub /dialogflow_response lhw_interfaces/msg/Response "{recognized_text: 'I am feeling happy today!'}" -1
**A bit more details**
Two nodes perofm text to emotion analysis:
- text_2_emotion_1 (.py)
- text_2_emotion_2 (.py)
Both of these nodes take input from the topic 'dialogflow_resonse' and publish to the topic 'text_emotion'.
The node 'nlp_emotion_core' listens for events on the 'text_emotion' topic. It analyzes the input, weights the different emotions and publishes the best emotion(results) to the 'nlp_emotion' topic.
\ No newline at end of file
......@@ -23,6 +23,7 @@ class NodeNlpEmotionCore(Node):
# Publish data to the topic 'nlp_emotion'
# The data type for the response is '???????'
self.input_dict = {}
self.reset_data()
self.timer = Timer(1, self.fire_emotion_event)
self.broadcast_emotion = self.create_publisher(EventT2E, 'nlp_emotion', 10)
......@@ -34,24 +35,25 @@ class NodeNlpEmotionCore(Node):
}
def event_t2e_init_zero(self, obj):
obj.angry = 0
obj.disgust = 0
obj.fear = 0
obj.happy = 0
obj.neutral = 0
obj.sad = 0
obj.surprised = 0
obj.angry = 0.0
obj.disgust = 0.0
obj.fear = 0.0
obj.happy = 0.0
obj.neutral = 0.0
obj.sad = 0.0
obj.surprised = 0.0
return obj
def event_t2e_normalize(self, obj):
sum = 0
sum += obj.angry
sum += obj.disgust
sum += obj.fear
sum += obj.happy
sum += obj.neutral
sum += obj.sad
sum += obj.surprised
sum += obj.angry if obj.angry > 0 else 0
sum += obj.disgust if obj.disgust > 0 else 0
sum += obj.fear if obj.fear > 0 else 0
sum += obj.happy if obj.happy > 0 else 0
sum += obj.neutral if obj.neutral > 0 else 0
sum += obj.sad if obj.sad > 0 else 0
sum += obj.surprised if obj.surprised > 0 else 0
obj.angry = obj.angry / sum
obj.disgust = obj.disgust / sum
obj.fear = obj.fear / sum
......@@ -59,6 +61,15 @@ class NodeNlpEmotionCore(Node):
obj.neutral = obj.neutral / sum
obj.sad = obj.sad / sum
obj.surprised = obj.surprised / sum
obj.angry = obj.angry if obj.angry > 0 else 0.0
obj.disgust = obj.disgust if obj.disgust > 0 else 0.0
obj.fear = obj.fear if obj.fear > 0 else 0.0
obj.happy = obj.happy if obj.happy > 0 else 0.0
obj.neutral = obj.neutral if obj.neutral > 0 else 0.0
obj.sad = obj.sad if obj.sad > 0 else 0.0
obj.surprised = obj.surprised if obj.surprised > 0 else 0.0
return obj
def get_aggregated_emotion_event(self, key):
......@@ -126,7 +137,7 @@ class NodeNlpEmotionCore(Node):
msg_emotion = self.event_t2e_normalize(msg_emotion)
dominant_emotion = self.CLASS_NAMES.index(max([
temp_lst = [
msg_emotion.angry,
msg_emotion.disgust,
msg_emotion.fear,
......@@ -134,9 +145,12 @@ class NodeNlpEmotionCore(Node):
msg_emotion.neutral,
msg_emotion.sad,
msg_emotion.surprised
]))
]
dominant_emotion = self.CLASS_NAMES[temp_lst.index(max(temp_lst))]
msg_emotion.dominant_emotion = dominant_emotion
print('--------------------THIS IS THE FINAL EMOTION OUTPUT:\n ', msg_emotion)
self.broadcast_emotion.publish(msg_emotion)
self.reset_data()
......@@ -146,23 +160,17 @@ class NodeNlpEmotionCore(Node):
print(emotion_event)
if (emotion_event.source_id == Response.TEXT_2_EMOTION_1):
print('----- TEXT_2_EMOTION 1111111')
self.timer.cancel()
self.input_dict['t2e-1'].append(emotion_event)
self.timer = Timer(timer_wait_time, self.fire_emotion_event)
self.timer.start()
elif (emotion_event.source_id == Response.TEXT_2_EMOTION_2):
print('----- TEXT_2_EMOTION 2222222')
self.timer.cancel()
self.input_dict['t2e-2'].append(emotion_event)
self.timer = Timer(timer_wait_time, self.fire_emotion_event)
self.timer.start()
print(self.input_dict)
print('')
def main(args=None):
rclpy.init(args=args)
nlm_emotion_core = NodeNlpEmotionCore()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment