NeuronParameterLoader.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from kalliope.core.Cortex import Cortex
  2. from kalliope.core.Utils import Utils
  3. import logging
  4. logging.basicConfig()
  5. logger = logging.getLogger("kalliope")
  6. class NeuronParameterLoader(object):
  7. @classmethod
  8. def get_parameters(cls, synapse_order, user_order):
  9. """
  10. Class method to get all params coming from a string order. Returns a dict of key/value.
  11. """
  12. params = dict()
  13. if Utils.is_containing_bracket(synapse_order):
  14. params = cls._associate_order_params_to_values(user_order, synapse_order)
  15. logger.debug("[NeuronParameterLoader.get_parameters]Parameters for order: %s" % params)
  16. # we place the dict of parameters load from order into a cache in Cortex so the user can save it later
  17. Cortex.add_parameters_from_order(params)
  18. return params
  19. @classmethod
  20. def _associate_order_params_to_values(cls, order, order_to_check):
  21. """
  22. Associate the variables from the order to the incoming user order
  23. :param order_to_check: the order to check incoming from the brain
  24. :type order_to_check: str
  25. :param order: the order from user
  26. :type order: str
  27. :return: the dict corresponding to the key / value of the params
  28. """
  29. logger.debug("[NeuronParameterLoader._associate_order_params_to_values] user order: %s, "
  30. "order from synapse: %s" % (order, order_to_check))
  31. list_word_in_order = Utils.remove_spaces_in_brackets(order_to_check).split()
  32. # remove sentence before order which are sentences not matching anyway
  33. truncate_list_word_said = order.split()
  34. # make dict var:value
  35. dict_var = dict()
  36. for idx, ow in enumerate(list_word_in_order):
  37. if not Utils.is_containing_bracket(ow):
  38. while truncate_list_word_said and ow.lower() != truncate_list_word_said[0].lower():
  39. truncate_list_word_said = truncate_list_word_said[1:]
  40. else:
  41. # remove bracket and grab the next value / stop value
  42. var_name = ow.replace("{{", "").replace("}}", "")
  43. stop_value = Utils.get_next_value_list(list_word_in_order[idx:])
  44. if stop_value is None:
  45. dict_var[var_name] = " ".join(truncate_list_word_said)
  46. break
  47. for word_said in truncate_list_word_said:
  48. if word_said.lower() == stop_value.lower(): # Do not consider the case
  49. break
  50. if var_name in dict_var:
  51. dict_var[var_name] += " " + word_said
  52. truncate_list_word_said = truncate_list_word_said[1:]
  53. else:
  54. dict_var[var_name] = word_said
  55. truncate_list_word_said = truncate_list_word_said[1:]
  56. return dict_var