test.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # coding: utf8
  2. import logging
  3. import re
  4. from core import OrderAnalyser
  5. logging.basicConfig()
  6. logger = logging.getLogger("jarvis")
  7. logger.setLevel(logging.DEBUG)
  8. # This does not work because of different encoding when using accent
  9. # from core import OrderAnalyser
  10. # order = "jarvis régle le réveil pour sept heures et vingt minutes"
  11. #
  12. # oa = OrderAnalyser(order)
  13. #
  14. # oa.start()
  15. user_said = "jarvis régle le réveil pour sept heures et vingts minutes please"
  16. order = "régle le réveil pour {{ hour }} heures et {{ minute }} minutes"
  17. def _spelt_order_match_brain_order_via_table(order_to_analyse, user_said):
  18. list_word_user_said = user_said.split()
  19. split_order_without_bracket = _get_list_word_without_bracket(order_to_analyse)
  20. number_of_word_in_order = len(split_order_without_bracket)
  21. # if all words in the list of what the user said in in the list of word in the order
  22. return len(set(split_order_without_bracket).intersection(list_word_user_said)) == number_of_word_in_order
  23. def _get_list_word_without_bracket(order):
  24. """
  25. Get an order with bracket inside like: "hello my name is {{ name }}.
  26. return a list of string without bracket like ["hello", "my", "name", "is"]
  27. :param order: sentence to split
  28. :return: list of string without bracket
  29. """
  30. pattern = r"((?:{{\s*)[\w\.]+(?:\s*}}))"
  31. # find everything like {{ word }}
  32. matches = re.findall(pattern, order)
  33. for match in matches:
  34. order = order.replace(match, "")
  35. # then split
  36. split_order = order.split()
  37. return split_order
  38. # main test
  39. if _spelt_order_match_brain_order_via_table(order, user_said):
  40. print "order matched"
  41. else:
  42. print "order does not match"