<commit_before>#!/usr/bin/env python
# encoding=utf8
from __future__ import print_function
from flexbe_core import EventState, Logger
import rospy
import re
from wm_nlu.srv import GPSRReceiveAction, GPSRReceiveActionResponse


class SaraNLU(EventState):
    '''
    Use wm_nlu to parse a sentence and return the detected actions in a standard format (ActionForm)

    ># sentence     string      sentence to parse
    
    #> ActionForms     string[]      list of ActionForms
    
    <= understood     Finished job.
    <= not_understood     Finished job but no commands detected.
    <= fail     service unavailable.
    '''

    def __init__(self):
        # See example_state.py for basic explanations.
        super(SaraNLU, self).__init__(outcomes=['understood', 'not_understood', 'fail'], input_keys=['sentence'],
                                      output_keys=['ActionForms'])
        self.RecurentSubject = None
        self.Person = None
        self.serviceName = "/gpsr_receive_action"

    def execute(self, userdata):

        # Special case for STOP
        regex = re.compile('.*(stop).*')
        if regex.match(userdata.sentence.lower()):
            Logger.loginfo("Stop now!")
            userdata.ActionForms = [["Stop"]]
            return "understood"

        # Assuring the availability of the NLU service
        if not rospy.wait_for_service(self.serviceName, 1):
            Logger.logerr(self.name + ": " + self.serviceName + " service not found. Will wait 10s before giving up")
            if not rospy.wait_for_service(self.serviceName, 10):
                Logger.logerr(self.name + ": " + self.serviceName + " waited 9s but service not found.")
                userdata.ActionForms = []
                return "fail"
            Logger.logwarn(self.name + ": " + self.serviceName + ". The service has been found!")

        # Call the NLU service
        serv = rospy.ServiceProxy(self.serviceName, GPSRReceiveAction)
        Resp = serv(userdata.sentence)

        # Checking the validity of the responce
        count = len(Resp.actions.actions)
        Logger.loginfo("Action list - count : " + str(count))
        if count == 0:
            userdata.ActionForms = []
            return "not_understood"

        # Convert the ActionMsgArray into ActionForms
        ActionForms = []
        for Action in Resp.actions.actions:
            # create an ActionForm
            ActionForm = []
            ActionForm.append(Action.Action)
            for arg in Action.args:
                ActionForm.append(arg)

            # Add it to the list
            ActionForms.append(ActionForm)

        userdata.ActionForms = ActionForms

        return "understood"

    def on_enter(self, userdata):
        Logger.loginfo('Enter SaraNLU')<commit_msg>fait marcher la state nlu<commit_after>#!/usr/bin/env python
# encoding=utf8
from __future__ import print_function
from flexbe_core import EventState, Logger
import rospy
import re
from wm_nlu.srv import GPSRReceiveAction, GPSRReceiveActionResponse
from std_msgs.msg import String

class SaraNLU(EventState):
    '''
    Use wm_nlu to parse a sentence and return the detected actions in a standard format (ActionForm)

    ># sentence     string      sentence to parse
    
    #> ActionForms     string[]      list of ActionForms
    
    <= understood     Finished job.
    <= not_understood     Finished job but no commands detected.
    <= fail     service unavailable.
    '''

    def __init__(self):
        # See example_state.py for basic explanations.
        super(SaraNLU, self).__init__(outcomes=['understood', 'not_understood', 'fail'], input_keys=['sentence'],
                                      output_keys=['ActionForms'])
        self.RecurentSubject = None
        self.Person = None
        self.serviceName = "/gpsr_receive_action"

        Logger.loginfo("waiting forservice: " + self.serviceName)
        rospy.wait_for_service(self.serviceName)

    def execute(self, userdata):

        # Special case for STOP
        regex = re.compile('.*(stop).*')
        if regex.match(userdata.sentence.lower()):
            Logger.loginfo("Stop now!")
            userdata.ActionForms = [["Stop"]]
            return "understood"

        # Call the NLU service
        serv = rospy.ServiceProxy(self.serviceName, GPSRReceiveAction)
        Resp = serv(String(userdata.sentence))

        # Checking the validity of the responce
        count = len(Resp.actions.actions)
        Logger.loginfo("Action list - count : " + str(count))
        if count == 0:
            userdata.ActionForms = []
            return "not_understood"

        # Convert the ActionMsgArray into ActionForms
        ActionForms = []
        for Action in Resp.actions.actions:
            # create an ActionForm
            ActionForm = []
            ActionForm.append(Action.Action)
            for arg in Action.args:
                ActionForm.append(arg)

            # Add it to the list
            ActionForms.append(ActionForm)

        userdata.ActionForms = ActionForms

        return "understood"

    def on_enter(self, userdata):
        Logger.loginfo('Enter SaraNLU')
{'fraction_non_alphanumeric': 0.09115540932843043, 'fraction_numerical': 0.0019353590090961873, 'mean_word_length': 2.25645872715816, 'pattern_counts': {'":': 3, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2555412', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1387, 'n_words': 375}