test.py 2.0 KB

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