test_nico.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. index = 0 # index to count element in the list counting the brackets
  46. returned_index = 0 # 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. list_parameter_position.append(returned_index)
  50. else:
  51. try:
  52. # if the next element is not a closing bracket
  53. if not _is_containing_closing_bracket(list_word_in_order[index+1]) and not \
  54. _is_containing_closing_bracket(el):
  55. # so we are not facing a parameter. we can add the word to the list
  56. list_word_without_bracket.append(el)
  57. else:
  58. returned_index -= 1
  59. except IndexError:
  60. if not _is_containing_bracket(el):
  61. list_word_without_bracket.append(el)
  62. returned_index += 1
  63. index += 1
  64. print list_word_without_bracket
  65. print list_parameter_position
  66. returned_dict = dict()
  67. returned_dict["list_word_without_bracket"] = list_word_without_bracket
  68. returned_dict["list_parameter_position"] = list_parameter_position
  69. return returned_dict
  70. def try_match_order_in_synapse(list_word_user_said, list_word_in_order):
  71. """
  72. Test if what the user said match an order with brackets
  73. :param list_word_user_said: List of words that the user said
  74. :param list_word_in_order: List of word in the order to test, including bracket
  75. :return: True if the order match
  76. """
  77. # first, get a list of word that composed to order without variable
  78. returned_dict = _get_list_word_in_order_without_parameter(list_word_in_order)
  79. list_word_without_bracket = returned_dict["list_word_without_bracket"]
  80. parameters_position = returned_dict["list_parameter_position"]
  81. number_of_word_in_order = len(list_word_without_bracket)
  82. # if all words in the list of what the user said in in the list of word in the order
  83. if len(set(list_word_without_bracket).intersection(list_word_user_said)) == number_of_word_in_order:
  84. # we match the order!
  85. print "order matched !"
  86. # we can get parameters in the sentence the user said
  87. # TODO get parameters
  88. # # we create a new list from the order with parameter tag inside
  89. # order_list_with_parameter_tag = None
  90. # for position in parameters_position:
  91. # order_list_with_parameter_tag = list_word_without_bracket.insert(int(position), "#PARAMETER#")
  92. # print order_list_with_parameter_tag
  93. # make a list of word
  94. list_word_user_said = user_said.split()
  95. print "user said: %s" % list_word_user_said
  96. # check if the order contain bracket
  97. if _is_containing_bracket(order):
  98. print "The order contain bracket"
  99. # create a list from the order to test
  100. list_word_in_order = order.split()
  101. try_match_order_in_synapse(list_word_user_said, list_word_in_order)