test_nico.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import re
  4. from core.ConfigurationManager import SettingLoader
  5. from core.ConfigurationManager.BrainLoader import BrainLoader
  6. from core.CrontabManager import CrontabManager
  7. from core.Models import Order
  8. from core.OrderAnalyser import OrderAnalyser
  9. import logging
  10. from core.TriggerLauncher import TriggerLauncher
  11. # user_said = "maman je voudrais ecouter ACDC"
  12. # order = "je voudrais ecouter {{ artist_name }}"
  13. user_said = "jarvis regle le reveil pour sept heures et dix minutes please"
  14. order = "regle le reveil pour {{ hour }} heures et {{ minute }} minutes"
  15. def _is_containing_closing_bracket(sentence):
  16. pattern = r"}}"
  17. bool = re.search(pattern, sentence)
  18. if bool is not None:
  19. return True
  20. return False
  21. def _is_containing_opening_bracket(sentence):
  22. pattern = r"{{"
  23. bool = re.search(pattern, sentence)
  24. if bool is not None:
  25. return True
  26. return False
  27. def _is_containing_bracket(sentence):
  28. pattern = r"{{|}}"
  29. bool = re.search(pattern, sentence)
  30. if bool is not None:
  31. return True
  32. return False
  33. def _get_list_word_in_order_without_parameter(list_word_in_order):
  34. """
  35. Receive a list of word with bracket like
  36. ['regle', 'le', 'reveil', 'pour', '{{', 'hour', '}}', 'heures', 'et', '{{', 'minute', '}}', 'minutes']
  37. Return a dict without parameter and a list of parameter place:
  38. TODO insert example
  39. :param list_word_in_order: List of string that can contain a word or brackets
  40. :return: Dict
  41. """
  42. print list_word_in_order
  43. list_word_without_bracket = list()
  44. list_parameter_position = list()
  45. parameters_name = list()
  46. index = 0 # index to count element in the list counting the brackets
  47. returned_index = 0 # this is the index where the parameter is placed
  48. for el in list_word_in_order:
  49. if _is_containing_opening_bracket(el):
  50. list_parameter_position.append(returned_index)
  51. parameters_name.append(list_word_in_order[index+1])
  52. else:
  53. try:
  54. # if the next element is not a closing bracket
  55. if not _is_containing_closing_bracket(list_word_in_order[index+1]) and not \
  56. _is_containing_closing_bracket(el):
  57. # so we are not facing a parameter. we can add the word to the list
  58. list_word_without_bracket.append(el)
  59. else:
  60. returned_index -= 1
  61. except IndexError:
  62. if not _is_containing_bracket(el):
  63. list_word_without_bracket.append(el)
  64. returned_index += 1
  65. index += 1
  66. print list_word_without_bracket
  67. print list_parameter_position
  68. print parameters_name
  69. returned_dict = dict()
  70. returned_dict["list_word_without_bracket"] = list_word_without_bracket
  71. returned_dict["list_parameter_position"] = list_parameter_position
  72. returned_dict["parameters_name"] = parameters_name
  73. return returned_dict
  74. def try_match_order_in_synapse(list_word_user_said, list_word_in_order):
  75. """
  76. Test if what the user said match an order with brackets
  77. :param list_word_user_said: List of words that the user said
  78. :param list_word_in_order: List of word in the order to test, including bracket
  79. :return: True if the order match
  80. """
  81. # first, get a list of word that composed to order without variable
  82. returned_dict = _get_list_word_in_order_without_parameter(list_word_in_order)
  83. list_word_without_bracket = returned_dict["list_word_without_bracket"]
  84. parameters_position = returned_dict["list_parameter_position"]
  85. number_of_word_in_order = len(list_word_without_bracket)
  86. # if all words in the list of what the user said in in the list of word in the order
  87. if len(set(list_word_without_bracket).intersection(list_word_user_said)) == number_of_word_in_order:
  88. # we match the order!
  89. print "order matched !"
  90. # we can get parameters in the sentence the user said
  91. # TODO get parameters
  92. # # we create a new list from the order with parameter tag inside
  93. # order_list_with_parameter_tag = None
  94. # for position in parameters_position:
  95. # order_list_with_parameter_tag = list_word_without_bracket.insert(int(position), "#PARAMETER#")
  96. # print order_list_with_parameter_tag
  97. # make a list of word
  98. list_word_user_said = user_said.split()
  99. print "user said: %s" % list_word_user_said
  100. # check if the order contain bracket
  101. if _is_containing_bracket(order):
  102. print "The order contain bracket"
  103. # create a list from the order to test
  104. list_word_in_order = order.split()
  105. try_match_order_in_synapse(list_word_user_said, list_word_in_order)