test_nico.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_and_name = list()
  45. index = 0 # index to count element in the list counting the brackets
  46. returned_index = 1 # this is the index where the parameter is placed
  47. for el in list_word_in_order:
  48. if _is_containing_opening_bracket(el):
  49. new_parameter = dict()
  50. new_parameter["name"] = list_word_in_order[index+1]
  51. new_parameter["index"] = returned_index
  52. list_parameter_position_and_name.append(new_parameter)
  53. else:
  54. try:
  55. # if the next element is not a closing bracket
  56. if not _is_containing_closing_bracket(list_word_in_order[index+1]) and not \
  57. _is_containing_closing_bracket(el):
  58. # so we are not facing a parameter. we can add the word to the list
  59. list_word_without_bracket.append(el)
  60. else:
  61. returned_index -= 1
  62. except IndexError:
  63. if not _is_containing_bracket(el):
  64. list_word_without_bracket.append(el)
  65. returned_index += 1
  66. index += 1
  67. print list_word_without_bracket
  68. print list_parameter_position_and_name
  69. returned_dict = dict()
  70. returned_dict["list_word_without_bracket"] = list_word_without_bracket
  71. returned_dict["list_parameter_position_and_name"] = list_parameter_position_and_name
  72. return returned_dict
  73. def _get_usefull_words(list_word_user_said, list_word_without_bracket):
  74. first_valid_word = list_word_without_bracket[0]
  75. last_word_valid_word = list_word_without_bracket[len(list_word_without_bracket)-1]
  76. print first_valid_word
  77. print last_word_valid_word
  78. start_index = list_word_user_said.index(first_valid_word)
  79. stop_index = list_word_user_said.index(last_word_valid_word)
  80. new_list = list()
  81. for x in (range(start_index, stop_index)):
  82. new_list.append(list_word_user_said[int(x)])
  83. print new_list
  84. return new_list
  85. def try_match_order_in_synapse(list_word_user_said, list_word_in_order):
  86. """
  87. Test if what the user said match an order with brackets
  88. :param list_word_user_said: List of words that the user said
  89. :param list_word_in_order: List of word in the order to test, including bracket
  90. :return: True if the order match
  91. """
  92. # first, get a list of word that composed to order without variable
  93. returned_dict = _get_list_word_in_order_without_parameter(list_word_in_order)
  94. list_word_without_bracket = returned_dict["list_word_without_bracket"]
  95. parameters_position_and_name = returned_dict["list_parameter_position_and_name"]
  96. number_of_word_in_order = len(list_word_without_bracket)
  97. # if all words in the list of what the user said in in the list of word in the order
  98. if len(set(list_word_without_bracket).intersection(list_word_user_said)) == number_of_word_in_order:
  99. # we match the order!
  100. print "order matched !" # we can get parameters in the sentence the user said
  101. # remove unused word in the list of word spelt by the user.
  102. usefull_words_in_user_said_list = _get_usefull_words(list_word_user_said, list_word_without_bracket)
  103. val_parameter = dict()
  104. for el in parameters_position_and_name:
  105. print el["name"]
  106. print
  107. val_parameter[el["name"]] = list_word_user_said[int(el["index"])]
  108. print "The dict Var : %s" % val_parameter
  109. # make a list of word
  110. list_word_user_said = user_said.split()
  111. print "user said: %s" % list_word_user_said
  112. # check if the order contain bracket
  113. if _is_containing_bracket(order):
  114. print "The order contain bracket"
  115. # create a list from the order to test
  116. list_word_in_order = order.split()
  117. try_match_order_in_synapse(list_word_user_said, list_word_in_order)